From e2661a5ebb3bd722adceb1e63ab0930fa840f28c Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 17 Apr 2017 14:01:19 +0100
Subject: [PATCH] Add stacked bar to show where your space has gone

---
 build/index.php       | 65 +++++++++++++++++++++++++++++++++++++++----
 module_index.json     |  2 +-
 modules/page-help.php | 53 +++++++++++++++++++++++++++++++----
 theme_default.css     |  3 ++
 4 files changed, 110 insertions(+), 13 deletions(-)

diff --git a/build/index.php b/build/index.php
index 125a60b..c54d361 100644
--- a/build/index.php
+++ b/build/index.php
@@ -299,6 +299,9 @@ textarea ~ input[type=submit] { margin: 0.5rem 0; padding: 0.5rem; font-weight:
 
 .help-section-header::after { content: "#" attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
 
+.stacked-bar { display: flex; }
+.stacked-bar-part	{ break-inside: avoid; white-space: pre; padding: 0.2em 0.3em; }
+
 .cursor-query { cursor: help; }
 
 summary { cursor: pointer; }
@@ -684,6 +687,15 @@ function mb_stripos_all($haystack, $needle) {
 		return false;
 }
 
+/**
+ * Tests whether a string starts with a specified substring.
+ * @param 	string	$haystack	The string to check against.
+ * @param 	string	$needle		The substring to look for.
+ * @return	bool				Whether the string starts with the specified substring.
+ */
+function startsWith($haystack, $needle) {
+	return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
+}
 /**
  * Tests whether a string ends with a given substring.
  * @param  string $whole The string to test against.
@@ -4944,17 +4956,58 @@ register_module([
 				
 				$content .= "<li>The id index is currently " . human_filesize(filesize($paths->idindex)) . " in size, and took " . $env->perfdata->idindex_decode_time . "ms to decode.</li>";
 				
-				$wikiSize = 0;
+				$content .= "</ul>\n";
+				
+				$content .= "<h3>Data</h3>\n";
+				
+				$wikiSize = new stdClass();
+				$wikiSize->all = 0;
+				$wikiSize->images = 0;
+				$wikiSize->audio = 0;
+				$wikiSize->videos = 0;
+				$wikiSize->pages = 0;
+				$wikiSize->history = 0;
+				$wikiSize->indexes = 0;
+				$wikiSize->other = 0;
 				$wikiFiles = glob_recursive($env->storage_prefix . "*");
 				foreach($wikiFiles as $filename)
 				{
-					if(endsWith($filename, ".php")) continue; // Skip php files
-					$wikiSize += filesize($filename);
+					$extension = strtolower(substr($filename, strrpos($filename, ".") + 1));
+					if($extension === "php") continue; // Skip php files
+					
+					$nextFilesize = filesize($filename);
+					$wikiSize->all += $nextFilesize;
+					if($extension[0] === "r") // It's a revision of a page
+						$wikiSize->history += $nextFilesize;
+					else if($extension == "md") // It's a page
+						$wikiSize->pages += $nextFilesize;
+					else if($extension == "json") // It's an index
+						$wikiSize->indexes += $nextFilesize;
+					else if(in_array($extension, [ // It's an uploaded image
+						"jpg", "jpeg", "png", "gif", "webp", "svg"
+					]))
+						$wikiSize->images += $nextFilesize;
+					else if(in_array($extension, [ "mp3", "ogg", "wav", "aac", "m4a" ])) // It's an audio file
+						$wikiSize->audio += $nextFilesize;
+					else if(in_array($extension, [ "avi", "mp4", "m4v", "webm" ])) // It's a video file
+						$wikiSize->videos += $nextFilesize;
+					else
+						$wikiSize->other += $nextFilesize;
 				}
 				
-				$content .= "<li>$settings->sitename is currently " . human_filesize($wikiSize) . " in size.</li>\n";
-				
-				$content .= "</ul>";
+				$content .= "<p>$settings->sitename is currently " . human_filesize($wikiSize->all) . " in size.</p>\n";
+				$content .= "<div class='stacked-bar'>
+					<div class='stacked-bar-part' style='flex: $wikiSize->indexes; background: hsla(191, 100%, 41%, 0.6)'>Indexes: " . human_filesize($wikiSize->indexes) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->pages; background: hsla(112, 83%, 40%, 0.6)'>Pages: " . human_filesize($wikiSize->pages) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->history; background: hsla(116, 84%, 25%, 0.68)'>Page History: " . human_filesize($wikiSize->history) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->images; background: hsla(266, 88%, 47%, 0.6)'>Images: " . human_filesize($wikiSize->images) . "</div>\n";
+				if($wikiSize->audio > 0)
+					$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->audio; background: hsla(237, 68%, 38%, 0.64)'>Audio: " . human_filesize($wikiSize->audio) . "</div>\n";
+				if($wikiSize->videos > 0)
+					$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->videos; background: hsla(338, 79%, 54%, 0.64)'>Videos: " . human_filesize($wikiSize->videos) . "</div>\n";
+				if($wikiSize->other > 0)
+				$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->other; background: hsla(62, 55%, 90%, 0.6)'>Other: " . human_filesize($wikiSize->other) . "</div>\n";
+				$content .= "</div>";
 			}
 			else
 			{
diff --git a/module_index.json b/module_index.json
index 93493d7..0717bfe 100755
--- a/module_index.json
+++ b/module_index.json
@@ -158,7 +158,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds a rather useful help page. Access through the 'help' action. This module also exposes help content added to Pepperminty Wiki's inbuilt invisible help section system.",
         "id": "page-help",
-        "lastupdate": 1492429520,
+        "lastupdate": 1492433537,
         "optional": false
     },
     {
diff --git a/modules/page-help.php b/modules/page-help.php
index 81ba18e..23c1f5e 100644
--- a/modules/page-help.php
+++ b/modules/page-help.php
@@ -66,17 +66,58 @@ register_module([
 				
 				$content .= "<li>The id index is currently " . human_filesize(filesize($paths->idindex)) . " in size, and took " . $env->perfdata->idindex_decode_time . "ms to decode.</li>";
 				
-				$wikiSize = 0;
+				$content .= "</ul>\n";
+				
+				$content .= "<h3>Data</h3>\n";
+				
+				$wikiSize = new stdClass();
+				$wikiSize->all = 0;
+				$wikiSize->images = 0;
+				$wikiSize->audio = 0;
+				$wikiSize->videos = 0;
+				$wikiSize->pages = 0;
+				$wikiSize->history = 0;
+				$wikiSize->indexes = 0;
+				$wikiSize->other = 0;
 				$wikiFiles = glob_recursive($env->storage_prefix . "*");
 				foreach($wikiFiles as $filename)
 				{
-					if(endsWith($filename, ".php")) continue; // Skip php files
-					$wikiSize += filesize($filename);
+					$extension = strtolower(substr($filename, strrpos($filename, ".") + 1));
+					if($extension === "php") continue; // Skip php files
+					
+					$nextFilesize = filesize($filename);
+					$wikiSize->all += $nextFilesize;
+					if($extension[0] === "r") // It's a revision of a page
+						$wikiSize->history += $nextFilesize;
+					else if($extension == "md") // It's a page
+						$wikiSize->pages += $nextFilesize;
+					else if($extension == "json") // It's an index
+						$wikiSize->indexes += $nextFilesize;
+					else if(in_array($extension, [ // It's an uploaded image
+						"jpg", "jpeg", "png", "gif", "webp", "svg"
+					]))
+						$wikiSize->images += $nextFilesize;
+					else if(in_array($extension, [ "mp3", "ogg", "wav", "aac", "m4a" ])) // It's an audio file
+						$wikiSize->audio += $nextFilesize;
+					else if(in_array($extension, [ "avi", "mp4", "m4v", "webm" ])) // It's a video file
+						$wikiSize->videos += $nextFilesize;
+					else
+						$wikiSize->other += $nextFilesize;
 				}
 				
-				$content .= "<li>$settings->sitename is currently " . human_filesize($wikiSize) . " in size.</li>\n";
-				
-				$content .= "</ul>";
+				$content .= "<p>$settings->sitename is currently " . human_filesize($wikiSize->all) . " in size.</p>\n";
+				$content .= "<div class='stacked-bar'>
+					<div class='stacked-bar-part' style='flex: $wikiSize->indexes; background: hsla(191, 100%, 41%, 0.6)'>Indexes: " . human_filesize($wikiSize->indexes) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->pages; background: hsla(112, 83%, 40%, 0.6)'>Pages: " . human_filesize($wikiSize->pages) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->history; background: hsla(116, 84%, 25%, 0.68)'>Page History: " . human_filesize($wikiSize->history) . "</div>
+					<div class='stacked-bar-part' style='flex: $wikiSize->images; background: hsla(266, 88%, 47%, 0.6)'>Images: " . human_filesize($wikiSize->images) . "</div>\n";
+				if($wikiSize->audio > 0)
+					$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->audio; background: hsla(237, 68%, 38%, 0.64)'>Audio: " . human_filesize($wikiSize->audio) . "</div>\n";
+				if($wikiSize->videos > 0)
+					$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->videos; background: hsla(338, 79%, 54%, 0.64)'>Videos: " . human_filesize($wikiSize->videos) . "</div>\n";
+				if($wikiSize->other > 0)
+				$content .= "<div class='stacked-bar-part' style='flex: $wikiSize->other; background: hsla(62, 55%, 90%, 0.6)'>Other: " . human_filesize($wikiSize->other) . "</div>\n";
+				$content .= "</div>";
 			}
 			else
 			{
diff --git a/theme_default.css b/theme_default.css
index 62761f7..ea7e4a0 100644
--- a/theme_default.css
+++ b/theme_default.css
@@ -93,6 +93,9 @@ textarea ~ input[type=submit] { margin: 0.5rem 0; padding: 0.5rem; font-weight:
 
 .help-section-header::after { content: "#" attr(id); float: right; color: rgba(0, 0, 0, 0.4); font-size: 0.8rem; font-weight: normal; }
 
+.stacked-bar { display: flex; }
+.stacked-bar-part	{ break-inside: avoid; white-space: pre; padding: 0.2em 0.3em; }
+
 .cursor-query { cursor: help; }
 
 summary { cursor: pointer; }
