diff --git a/module_index.json b/module_index.json index 5f80c86..d1fe0d2 100755 --- a/module_index.json +++ b/module_index.json @@ -75,7 +75,7 @@ "version": "0.1", "author": "Starbeamrainbowlabs", "description": "Displays a special page to aid in setting up a new wiki for the first time.", - "lastupdate": 1574596190, + "lastupdate": 1577025060, "optional": false, "extra_data": [] }, @@ -195,7 +195,7 @@ "version": "0.1", "author": "Starbeamrainbowlabs", "description": "Adds per-user watchlists. When a page on a user's watchlist is edited, a notification email is sent.", - "lastupdate": 1577024516, + "lastupdate": 1577116869, "optional": false, "extra_data": [] }, diff --git a/modules/feature-watchlist.php b/modules/feature-watchlist.php index 287b1f8..602e8fc 100644 --- a/modules/feature-watchlist.php +++ b/modules/feature-watchlist.php @@ -111,7 +111,9 @@ register_module([ * @apiError NotLoggedIn You aren't logged in, so you can't edit your watchlist (only logged in users have a watchlist). * @apiError NoEmailAddress The currently logged in user doesn't have an email address specified in their account. * @apiError DoVerbNotRecognised The specified do verb was not recognised. Supported verbs: add, remove, clear (a canonical list is returned with this error). - * @apiError PageNotFound The page name was not found in your watchlist. + * + * @apiError PageNotFoundOnWiki The page name specified was not found on the wiki, so it can't be watched. + * @apiError PageNotFoundOnWatchlist The page name was not found in your watchlist. */ /* @@ -122,29 +124,33 @@ register_module([ * ███████ ██████ ██ ██ */ add_action("watchlist-edit", function () { - global $settings, $env; + global $settings, $env, $pageindex; + + // The thing we should do. + $do = $_GET["do"] ?? "null"; + // The location we should redirect to after doing it successfully, if anywhere + $returnto = empty($_GET["returnto"]) ? null : $_GET["returnto"]; if(!$settings->watchlists_enable) { http_response_code(403); + header("x-status: failed"); header("x-problem: watchlists-disabled"); exit(page_renderer::render_main("Watchlists disabled - $settings->sitename", "

Sorry, but watchlists are currently disabled on $settings->sitename. Contact your moderators to learn - their details are at the bottom of every page.

")); } if(!$env->is_logged_in) { http_response_code(401); + header("x-status: failed"); header("x-problem: not-logged-in"); - exit(page_renderer::render_main("Not logged in - $settings->sitename", "

Only logged in users can have watchlists. Try logging in.")); + exit(page_renderer::render_main("Not logged in - $settings->sitename", "

Only logged in users can have watchlists. Try logging in.

")); } if(empty($env->user_data->emailAddress)) { http_response_code(422); + header("x-status: failed"); header("x-problem: no-email-address-in-user-preferences"); exit(page_renderer::render_main("No email address specified -$settings->sitename", "

You are logged in, but have not specified an email address to send notifications to. Try specifying one in your user preferences and then coming back here.

")); } - // The thing we should do. - $do = $_GET["do"] ?? "null"; - // The location we should redirect to after doing it successfully, if anywhere - $returnto = $_GET["returnto"] ?? null; // If the watchlist doesn't exist, create it // Note that saving this isn't essential - so we don't bother unless we perform some other action too. @@ -153,10 +159,22 @@ register_module([ switch($do) { case "add": + if(empty($pageindex->{$env->page})) { + http_response_code(404); + header("x-status: failed"); + header("x-problem: page-not-found-on-wiki"); + exit(page_renderer::render_main("Page not found - Error - $settings->sitename", "

Oops! The page name ".htmlentities($env->page)." couldn't be found on $settings->sitename. Try creating it and trying to add it to your watchlist again!

")); + } + if(in_array($env->page, $env->user_data->watchlist)) { + http_response_code(422); + header("x-status: failed"); + header("x-problem: watchlist-page-already-present"); + exit(page_renderer::render_main("Already on watchlist - Error - $settings->sitename", "

The page with the name ".htmlentities($env->page)." is already on your watchlist, so it can't be added again.

")); + } // Add the new page to the watchlist $env->user_data->watchlist[] = $env->page; // Sort the list - $collator = new Collator(); + $collator = new Collator(""); $collator->sort($env->user_data->watchlist, SORT_NATURAL | SORT_FLAG_CASE); // Save back to disk save_settings(); @@ -165,6 +183,8 @@ register_module([ $index = array_search($env->page, $env->user_data->watchlist); if($index === false) { http_response_code(400); + header("x-status: failed"); + header("x-problem: watchlist-item-not-found"); exit(page_renderer::render_main("Watchlist item not found - Error - $settings->sitename", "

Oops! The page with the name ".htmlentities($env->page)." isn't currently on your watchlist, so it couldn't be removed. Perhaps you already removed it?

Try going back to your watchlist.

")); } @@ -176,15 +196,20 @@ register_module([ save_settings(); default: http_response_code(400); + header("x-status: failed"); + header("x-problem: watchlist-do-verb-not-recognised"); header("content-type: text/plain"); exit("Error: The do verb '$do' wasn't recognised. Current verbs supported: add, remove, clear"); } - if($redirect) { + $message = "Your watchlist was updated successfully."; + if(!empty($returnto)) { http_response_code(302); + header("x-status: success"); header("location: $returnto"); + $message .= " Click here to return to your previous page."; } - exit(page_renderer::render_main("Watchlist update successful", "

Your watchlist was updated successful. Click here to return to your previous page.

")); + exit(page_renderer::render_main("Watchlist update successful", "

$message

")); }); } ]);