From a63d83bc34fb6547d127d53166cce7695eefdcb7 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 14 Jul 2017 20:35:55 +0100
Subject: [PATCH] Make stats-update accept the site secret as a POST param

---
 build/index.php              | 32 ++++++++++++++++++++++++++++++++
 module_index.json            |  2 +-
 modules/feature-stats.php    |  7 +++++++
 modules/parser-parsedown.php | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/build/index.php b/build/index.php
index 06fdccb..b5ee20c 100644
--- a/build/index.php
+++ b/build/index.php
@@ -6866,6 +6866,38 @@ register_module([
 			return $result;
 		});
 		
+		// Wanted pages
+		statistic_add([
+			"id" => "wanted-pages",
+			"name" => "Wanted Pages",
+			"update" => function($old_stats) {
+				global $pageindex, $env;
+				
+				$result = new stdClass(); // completed, value, state
+				$pages = [];
+				foreach($pageindex as $pagename => $pagedata) {
+					$page_content = file_get_contents($env->storage_prefix . $pagedata->filename);
+					preg_match_all("/\[\[([^\]]+)\]\]/", $page_content, $linked_pages);
+					if(count($linked_pages) < 2)
+						continue; // No linked pages here
+					foreach($linked_pages[1] as $linked_page) {
+						// We're only interested in pages that don't exist
+						if(!empty($linked_page)) continue;
+						
+						if(empty($pages[$linked_page]))
+							$pages[$linked_page] = 0;
+						$pages[$linked_page]++;
+					}
+				}
+				
+				arsort($pages);
+				
+				$result->value = $pages;
+				$result->completed = true;
+				return $result;
+			}
+		]);
+		
 		add_help_section("20-parser-default", "Editor Syntax",
 		"<p>$settings->sitename's editor uses an extended version of <a href='http://parsedown.org/'>Parsedown</a> to render pages, which is a fantastic open source Github flavoured markdown parser. You can find a quick reference guide on Github flavoured markdown <a href='https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet'>here</a> by <a href='https://github.com/adam-p/'>adam-p</a>, or if you prefer a book <a href='https://www.gitbook.com/book/roachhd/master-markdown/details'>Mastering Markdown</a> by KB is a good read, and free too!</p>
 		<h3>Tips</h3>
diff --git a/module_index.json b/module_index.json
index 5a549a7..d34974d 100755
--- a/module_index.json
+++ b/module_index.json
@@ -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": 1489056285,
+        "lastupdate": 1500060122,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/feature-stats.php b/modules/feature-stats.php
index 7566ed7..9240650 100644
--- a/modules/feature-stats.php
+++ b/modules/feature-stats.php
@@ -26,6 +26,13 @@ register_module([
 		add_action("stats-update", function() {
 			global $env, $paths, $settings;
 			
+			
+			if(!$env->is_admin &&
+				(
+					empty($_POST["secret"]) ||
+					$_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>"));
 			
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 03a14b7..8ae52c2 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -21,6 +21,38 @@ register_module([
 			return $result;
 		});
 		
+		// Wanted pages
+		statistic_add([
+			"id" => "wanted-pages",
+			"name" => "Wanted Pages",
+			"update" => function($old_stats) {
+				global $pageindex, $env;
+				
+				$result = new stdClass(); // completed, value, state
+				$pages = [];
+				foreach($pageindex as $pagename => $pagedata) {
+					$page_content = file_get_contents($env->storage_prefix . $pagedata->filename);
+					preg_match_all("/\[\[([^\]]+)\]\]/", $page_content, $linked_pages);
+					if(count($linked_pages) < 2)
+						continue; // No linked pages here
+					foreach($linked_pages[1] as $linked_page) {
+						// We're only interested in pages that don't exist
+						if(!empty($linked_page)) continue;
+						
+						if(empty($pages[$linked_page]))
+							$pages[$linked_page] = 0;
+						$pages[$linked_page]++;
+					}
+				}
+				
+				arsort($pages);
+				
+				$result->value = $pages;
+				$result->completed = true;
+				return $result;
+			}
+		]);
+		
 		add_help_section("20-parser-default", "Editor Syntax",
 		"<p>$settings->sitename's editor uses an extended version of <a href='http://parsedown.org/'>Parsedown</a> to render pages, which is a fantastic open source Github flavoured markdown parser. You can find a quick reference guide on Github flavoured markdown <a href='https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet'>here</a> by <a href='https://github.com/adam-p/'>adam-p</a>, or if you prefer a book <a href='https://www.gitbook.com/book/roachhd/master-markdown/details'>Mastering Markdown</a> by KB is a good read, and free too!</p>
 		<h3>Tips</h3>
