Implement wanted pages statistics! Yay! I've been looking forward to this mya

It's faster than I thought it would be too :D
This commit is contained in:
Starbeamrainbowlabs 2017-07-14 21:22:37 +01:00
parent 6adb919c2a
commit 12a7d9fd14
4 changed files with 54 additions and 9 deletions

View File

@ -1694,7 +1694,7 @@ if(!isset($pageindex->{$env->page}) and isset($_GET["search-redirect"]))
// Note we use $_GET here because $env->action isn't populated at this point
if($settings->require_login_view === true && // If this site requires a login in order to view pages
!$env->is_logged_in && // And the user isn't logged in
!in_array($_GET["action"], [ "login", "checklogin", "opensearch-description" ])) // And the user isn't trying to login, or get the opensearch description
!in_array($_GET["action"], [ "login", "checklogin", "opensearch-description", "invindex-rebuild", "stats-update" ])) // And the user isn't trying to login, or get the opensearch description, or access actions that apply their own access rules
{
// Redirect the user to the login page
http_response_code(307);
@ -4041,9 +4041,18 @@ register_module([
add_action("stats-update", function() {
global $env, $paths, $settings;
if(!$env->is_admin)
if(!$env->is_admin &&
(
empty($_POST["secret"]) ||
$_POST["secret"] !== $settings->secret
)
)
exit(page_renderer::render_main("Error - Recalculating Statistics - $settings->sitename", "<p>You need to be logged in as a moderator or better to get $settings->sitename to recalculate it's statistics. If you're logged in, try <a href='?action=logout'>logging out</a> and logging in again as a moderator. If you aren't logged in, try <a href='?action=login&returnto=%3Faction%3Dstats-update'>logging in</a>.</p>"));
// Delete the old stats cache
unlink($paths->statsindex);
update_statistics(true);
header("content-type: application/json");
echo(file_get_contents($paths->statsindex) . "\n");
@ -6876,13 +6885,30 @@ register_module([
$result = new stdClass(); // completed, value, state
$pages = [];
foreach($pageindex as $pagename => $pagedata) {
if(!file_exists($env->storage_prefix . $pagedata->filename)) {
continue;
}
$page_content = file_get_contents($env->storage_prefix . $pagedata->filename);
preg_match_all("/\[\[([^\]]+)\]\]/", $page_content, $linked_pages);
if(count($linked_pages) < 2)
if(count($linked_pages[1]) === 0)
continue; // No linked pages here
foreach($linked_pages[1] as $linked_page) {
// Strip everything after the | and the #
if(strpos($linked_page, "|") !== false)
$linked_page = substr($linked_page, 0, strpos($linked_page, "|"));
if(strpos($linked_page, "#") !== false)
$linked_page = substr($linked_page, 0, strpos($linked_page, "#"));
if(strlen($linked_page) === 0)
continue;
// Make sure we try really hard to find this page in the
// pageindex
if(!empty($pageindex->{ucfirst($linked_page)}))
$linked_page = ucfirst($linked_page);
else if(!empty($pageindex->{ucwords($linked_page)}))
$linked_page = ucwords($linked_page);
// We're only interested in pages that don't exist
if(!empty($linked_page)) continue;
if(!empty($pageindex->$linked_page)) continue;
if(empty($pages[$linked_page]))
$pages[$linked_page] = 0;

View File

@ -113,7 +113,7 @@
"author": "Starbeamrainbowlabs",
"description": "An extensible statistics calculation system. Comes with a range of built-in statistics, but can be extended by other modules too.",
"id": "feature-stats",
"lastupdate": 1499980292,
"lastupdate": 1500061821,
"optional": false
},
{
@ -266,7 +266,7 @@
"author": "Emanuil Rusev & Starbeamrainbowlabs",
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
"id": "parser-parsedown",
"lastupdate": 1500060122,
"lastupdate": 1500063269,
"optional": false
}
]

View File

@ -33,9 +33,11 @@ register_module([
$_POST["secret"] !== $settings->secret
)
)
if(!$env->is_admin)
exit(page_renderer::render_main("Error - Recalculating Statistics - $settings->sitename", "<p>You need to be logged in as a moderator or better to get $settings->sitename to recalculate it's statistics. If you're logged in, try <a href='?action=logout'>logging out</a> and logging in again as a moderator. If you aren't logged in, try <a href='?action=login&returnto=%3Faction%3Dstats-update'>logging in</a>.</p>"));
// Delete the old stats cache
unlink($paths->statsindex);
update_statistics(true);
header("content-type: application/json");
echo(file_get_contents($paths->statsindex) . "\n");

View File

@ -31,13 +31,30 @@ register_module([
$result = new stdClass(); // completed, value, state
$pages = [];
foreach($pageindex as $pagename => $pagedata) {
if(!file_exists($env->storage_prefix . $pagedata->filename)) {
continue;
}
$page_content = file_get_contents($env->storage_prefix . $pagedata->filename);
preg_match_all("/\[\[([^\]]+)\]\]/", $page_content, $linked_pages);
if(count($linked_pages) < 2)
if(count($linked_pages[1]) === 0)
continue; // No linked pages here
foreach($linked_pages[1] as $linked_page) {
// Strip everything after the | and the #
if(strpos($linked_page, "|") !== false)
$linked_page = substr($linked_page, 0, strpos($linked_page, "|"));
if(strpos($linked_page, "#") !== false)
$linked_page = substr($linked_page, 0, strpos($linked_page, "#"));
if(strlen($linked_page) === 0)
continue;
// Make sure we try really hard to find this page in the
// pageindex
if(!empty($pageindex->{ucfirst($linked_page)}))
$linked_page = ucfirst($linked_page);
else if(!empty($pageindex->{ucwords($linked_page)}))
$linked_page = ucwords($linked_page);
// We're only interested in pages that don't exist
if(!empty($linked_page)) continue;
if(!empty($pageindex->$linked_page)) continue;
if(empty($pages[$linked_page]))
$pages[$linked_page] = 0;