From de61612b18b88b9a2e91d1bc7b85438221d88762 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 28 Apr 2018 10:59:08 +0100
Subject: [PATCH] Add most linked-to pages statistic

---
 Changelog.md                 |  1 +
 build/index.php              | 56 ++++++++++++++++++++++++++++++++++--
 module_index.json            |  2 +-
 modules/parser-parsedown.php | 54 ++++++++++++++++++++++++++++++++--
 4 files changed, 107 insertions(+), 6 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index d1d5851..e6cac50 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -11,6 +11,7 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
  - Added user count statistic
  - Added redirect page count statistic
  - Add orphan pages statistic
+ - Add most linked-to pages statistic
  - [Rest API] Added support for the `mode` parameter to the `random` action
  - [Rest API] Added `format` parameter to `recentchanges` action
  - [Rest API] Added `comments-fetch` action to return a page's comments as JSON
diff --git a/build/index.php b/build/index.php
index e7d9443..4edea4b 100644
--- a/build/index.php
+++ b/build/index.php
@@ -387,7 +387,7 @@ if($settings->css === "auto")
 /////////////////////////////////////////////////////////////////////////////
 /** The version of Pepperminty Wiki currently running. */
 $version = "v0.16-dev";
-$commit = "c6a3ce16df9886c0135bd8ed36efa3df7cc376a1";
+$commit = "75d6fbaa5657495c892ba6c489149013fb57a9ed";
 /// Environment ///
 /** Holds information about the current request environment. */
 $env = new stdClass();
@@ -7981,7 +7981,13 @@ register_module([
 			return $result;
 		});
 		
-		// Wanted pages
+		/*
+ 		 * ███████ ████████  █████  ████████ ██ ███████ ████████ ██  ██████ ███████
+ 		 * ██         ██    ██   ██    ██    ██ ██         ██    ██ ██      ██
+ 		 * ███████    ██    ███████    ██    ██ ███████    ██    ██ ██      ███████
+ 		 *      ██    ██    ██   ██    ██    ██      ██    ██    ██ ██           ██
+ 		 * ███████    ██    ██   ██    ██    ██ ███████    ██    ██  ██████ ███████
+ 		 */
 		statistic_add([
 			"id" => "wanted-pages",
 			"name" => "Wanted Pages",
@@ -8025,7 +8031,6 @@ register_module([
 				return $result;
 			}
 		]);
-		// Wanted pages
 		statistic_add([
 			"id" => "orphan-pages",
 			"name" => "Orphan Pages",
@@ -8075,6 +8080,51 @@ register_module([
 				return $result;
 			}
 		]);
+		statistic_add([
+			"id" => "most-linked-to-pages",
+			"name" => "Most Linked-To Pages",
+			"type" => "page",
+			"update" => function($old_stats) {
+				global $pageindex, $env;
+				
+				$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);
+					
+					$page_links = PeppermintParsedown::extract_page_names($page_content);
+					
+					foreach($page_links as $linked_page) {
+						// We're only interested in pages that exist
+						if(empty($pageindex->$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;
+			},
+			"render" => function($stats_data) {
+				global $pageindex;
+				$result = "<h2>$stats_data->name</h2>\n";
+				$result .= "<table class='most-linked-to-pages'>\n";
+				$result .= "\t<tr><th>Page Name</th><th>Linking Pages</th></tr>\n";
+				foreach($stats_data->value as $pagename => $link_count) {
+					$pagename_display = !empty($pageindex->$pagename->redirect) && $pageindex->$pagename->redirect ? "<em>$pagename</em>" : $pagename;
+					$result .= "\t<tr><td><a href='?page=" . rawurlencode($pagename) . "'>$pagename_display</a></td><td>$link_count</td></tr>\n";
+				}
+				$result .= "</table>\n";
+				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>
diff --git a/module_index.json b/module_index.json
index 632ad6b..e784ded 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": 1524783071,
+        "lastupdate": 1524909375,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/parser-parsedown.php b/modules/parser-parsedown.php
index 844f800..c9244e6 100644
--- a/modules/parser-parsedown.php
+++ b/modules/parser-parsedown.php
@@ -21,7 +21,13 @@ register_module([
 			return $result;
 		});
 		
-		// Wanted pages
+		/*
+ 		 * ███████ ████████  █████  ████████ ██ ███████ ████████ ██  ██████ ███████
+ 		 * ██         ██    ██   ██    ██    ██ ██         ██    ██ ██      ██
+ 		 * ███████    ██    ███████    ██    ██ ███████    ██    ██ ██      ███████
+ 		 *      ██    ██    ██   ██    ██    ██      ██    ██    ██ ██           ██
+ 		 * ███████    ██    ██   ██    ██    ██ ███████    ██    ██  ██████ ███████
+ 		 */
 		statistic_add([
 			"id" => "wanted-pages",
 			"name" => "Wanted Pages",
@@ -65,7 +71,6 @@ register_module([
 				return $result;
 			}
 		]);
-		// Wanted pages
 		statistic_add([
 			"id" => "orphan-pages",
 			"name" => "Orphan Pages",
@@ -115,6 +120,51 @@ register_module([
 				return $result;
 			}
 		]);
+		statistic_add([
+			"id" => "most-linked-to-pages",
+			"name" => "Most Linked-To Pages",
+			"type" => "page",
+			"update" => function($old_stats) {
+				global $pageindex, $env;
+				
+				$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);
+					
+					$page_links = PeppermintParsedown::extract_page_names($page_content);
+					
+					foreach($page_links as $linked_page) {
+						// We're only interested in pages that exist
+						if(empty($pageindex->$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;
+			},
+			"render" => function($stats_data) {
+				global $pageindex;
+				$result = "<h2>$stats_data->name</h2>\n";
+				$result .= "<table class='most-linked-to-pages'>\n";
+				$result .= "\t<tr><th>Page Name</th><th>Linking Pages</th></tr>\n";
+				foreach($stats_data->value as $pagename => $link_count) {
+					$pagename_display = !empty($pageindex->$pagename->redirect) && $pageindex->$pagename->redirect ? "<em>$pagename</em>" : $pagename;
+					$result .= "\t<tr><td><a href='?page=" . rawurlencode($pagename) . "'>$pagename_display</a></td><td>$link_count</td></tr>\n";
+				}
+				$result .= "</table>\n";
+				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>
