From 19c5679461b125f08032634b55fdbb3c57a7ab3f Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 17 Apr 2018 18:59:23 +0100
Subject: [PATCH] Add format param support to recentchanges

---
 Changelog.md                       |  3 ++-
 build/index.php                    | 43 +++++++++++++++++++-----------
 module_index.json                  |  2 +-
 modules/feature-recent-changes.php | 43 +++++++++++++++++++-----------
 4 files changed, 57 insertions(+), 34 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 1f694dd..d7bcd79 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -4,10 +4,11 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
 ## v0.16-dev
 
 ### Added
- - [Rest API] Added support for the `mode` parameter to the `random` action.
  - Add json support to the search action :D
  - Added page moves to the recent changes page (#151)
  - Hyperlinked image preview on file pages to the original image (#153)
+ - [Rest API] Added support for the `mode` parameter to the `random` action
+ - [Rest API] Added `format` parameter to `recentchanges` action
 
 ### Fixed
  - Fixed various issues with both the module api & the rest api docs.
diff --git a/build/index.php b/build/index.php
index dda5803..c6793d0 100644
--- a/build/index.php
+++ b/build/index.php
@@ -3279,12 +3279,6 @@ register_module([
 	"id" => "feature-recent-changes",
 	"code" => function() {
 		global $settings, $env, $paths;
-		/**
-		 * @api {get} ?action=recentchanges Get a list of recent changes
-		 * @apiName RecentChanges
-		 * @apiGroup Stats
-		 * @apiPermission Anonymous
-		 */
 		
 		// Add the recent changes json file to $paths for convenience.
 		$paths->recentchanges = $env->storage_prefix . "recent-changes.json";
@@ -3292,6 +3286,14 @@ register_module([
 		if(!file_exists($paths->recentchanges))
 			file_put_contents($paths->recentchanges, "[]");
 		
+		/**
+		 * @api {get} ?action=recentchanges[&format={code}] Get a list of recent changes
+		 * @apiName RecentChanges
+		 * @apiGroup Stats
+		 * @apiPermission Anonymous
+		 *
+		 * @apiParam	{string}	format	The format to return the recent changes in. Values: html, json. Default: html.
+		 */
 		/*
 		 * ██████  ███████  ██████ ███████ ███    ██ ████████         
 		 * ██   ██ ██      ██      ██      ████   ██    ██            
@@ -3308,21 +3310,30 @@ register_module([
 		add_action("recent-changes", function() {
 			global $settings, $paths, $pageindex;
 			
-			$content = "\t\t<h1>Recent Changes</h1>\n";
+			$format = $_GET["format"] ?? "html";
 			
 			$recent_changes = json_decode(file_get_contents($paths->recentchanges));
 			
-			if(count($recent_changes) > 0)
-			{
-				$content .= render_recent_changes($recent_changes);
-			}
-			else
-			{
-				// No changes yet :(
-				$content .= "<p><em>None yet! Try making a few changes and then check back here.</em></p>\n";
+			switch($format) {
+				case "html":
+					$content = "\t\t<h1>Recent Changes</h1>\n";
+					
+					if(count($recent_changes) > 0)
+						$content .= render_recent_changes($recent_changes);
+					else // No changes yet :(
+						$content .= "<p><em>None yet! Try making a few changes and then check back here.</em></p>\n";
+						
+					exit(page_renderer::render("Recent Changes - $settings->sitename", $content));
+					break;
+				case "json":
+					$result = json_encode($recent_changes);
+					header("content-type: application/json");
+					header("content-length: $result");
+					exit($result);
+					break;
 			}
 			
-			exit(page_renderer::render("Recent Changes - $settings->sitename", $content));
+			
 		});
 		
 		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
diff --git a/module_index.json b/module_index.json
index 29d5f01..b73503f 100755
--- a/module_index.json
+++ b/module_index.json
@@ -86,7 +86,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds recent changes. Access through the 'recent-changes' action.",
         "id": "feature-recent-changes",
-        "lastupdate": 1521408644,
+        "lastupdate": 1523987868,
         "optional": false
     },
     {
diff --git a/modules/feature-recent-changes.php b/modules/feature-recent-changes.php
index f70029b..276d5b3 100644
--- a/modules/feature-recent-changes.php
+++ b/modules/feature-recent-changes.php
@@ -7,12 +7,6 @@ register_module([
 	"id" => "feature-recent-changes",
 	"code" => function() {
 		global $settings, $env, $paths;
-		/**
-		 * @api {get} ?action=recentchanges Get a list of recent changes
-		 * @apiName RecentChanges
-		 * @apiGroup Stats
-		 * @apiPermission Anonymous
-		 */
 		
 		// Add the recent changes json file to $paths for convenience.
 		$paths->recentchanges = $env->storage_prefix . "recent-changes.json";
@@ -20,6 +14,14 @@ register_module([
 		if(!file_exists($paths->recentchanges))
 			file_put_contents($paths->recentchanges, "[]");
 		
+		/**
+		 * @api {get} ?action=recentchanges[&format={code}] Get a list of recent changes
+		 * @apiName RecentChanges
+		 * @apiGroup Stats
+		 * @apiPermission Anonymous
+		 *
+		 * @apiParam	{string}	format	The format to return the recent changes in. Values: html, json. Default: html.
+		 */
 		/*
 		 * ██████  ███████  ██████ ███████ ███    ██ ████████         
 		 * ██   ██ ██      ██      ██      ████   ██    ██            
@@ -36,21 +38,30 @@ register_module([
 		add_action("recent-changes", function() {
 			global $settings, $paths, $pageindex;
 			
-			$content = "\t\t<h1>Recent Changes</h1>\n";
+			$format = $_GET["format"] ?? "html";
 			
 			$recent_changes = json_decode(file_get_contents($paths->recentchanges));
 			
-			if(count($recent_changes) > 0)
-			{
-				$content .= render_recent_changes($recent_changes);
-			}
-			else
-			{
-				// No changes yet :(
-				$content .= "<p><em>None yet! Try making a few changes and then check back here.</em></p>\n";
+			switch($format) {
+				case "html":
+					$content = "\t\t<h1>Recent Changes</h1>\n";
+					
+					if(count($recent_changes) > 0)
+						$content .= render_recent_changes($recent_changes);
+					else // No changes yet :(
+						$content .= "<p><em>None yet! Try making a few changes and then check back here.</em></p>\n";
+						
+					exit(page_renderer::render("Recent Changes - $settings->sitename", $content));
+					break;
+				case "json":
+					$result = json_encode($recent_changes);
+					header("content-type: application/json");
+					header("content-length: $result");
+					exit($result);
+					break;
 			}
 			
-			exit(page_renderer::render("Recent Changes - $settings->sitename", $content));
+			
 		});
 		
 		register_save_preprocessor(function(&$pageinfo, &$newsource, &$oldsource) {
