From 12a7d9fd140730ce874ce3d32210da567218f437 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 14 Jul 2017 21:22:37 +0100
Subject: [PATCH] Implement wanted pages statistics! Yay! I've been looking
 forward to this mya

It's faster than I thought it would be too :D
---
 build/index.php              | 34 ++++++++++++++++++++++++++++++----
 module_index.json            |  4 ++--
 modules/feature-stats.php    |  4 +++-
 modules/parser-parsedown.php | 21 +++++++++++++++++++--
 4 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/build/index.php b/build/index.php
index b5ee20c..4045b18 100644
--- a/build/index.php
+++ b/build/index.php
@@ -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;
diff --git a/module_index.json b/module_index.json
index d34974d..607fb5e 100755
--- a/module_index.json
+++ b/module_index.json
@@ -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
     }
 ]
\ No newline at end of file
diff --git a/modules/feature-stats.php b/modules/feature-stats.php
index 9240650..ec38d78 100644
--- a/modules/feature-stats.php
+++ b/modules/feature-stats.php
@@ -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");
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 8ae52c2..ede1dcf 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -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;
