page-move: Ensure that the new subpage actually exists - fixes #201

This commit is contained in:
Starbeamrainbowlabs 2020-08-06 15:47:41 +01:00
parent b25c144f1e
commit 75c15d66b2
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 27 additions and 15 deletions

View File

@ -54,7 +54,8 @@ Make sure you have PHP 7.3+ when you update past this point! It isn't the end of
- Fixed an obscure bug in the search engine when excluding terms that appear both in a page's title and body - Fixed an obscure bug in the search engine when excluding terms that appear both in a page's title and body
- Squashed a warning at the top of search results (more insight is needed though to squash the inconsistencies in the search index that creep in though) - Squashed a warning at the top of search results (more insight is needed though to squash the inconsistencies in the search index that creep in though)
- Removed annoying scrollbars when editing long pages - Removed annoying scrollbars when editing long pages
- Fixed an obscure warning when previewing PDFs - Fixed an obscure warning when previewing PDFs (#202)
- Ensure that the parent page exists when moving a page to be a child of a non-existent parent (#201)
## v0.21.1-hotfix1 ## v0.21.1-hotfix1

View File

@ -210,23 +210,26 @@ function get_subpages($pageindex, $pagename)
/** /**
* Makes sure that a subpage's parents exist. * Makes sure that a subpage's parents exist.
* Note this doesn't check the pagename itself. * Note this doesn't check the pagename itself.
* @package core * @package core
* @param string $pagename The pagename to check. * @param string $pagename The pagename to check.
* @param bool $create_dir Whether to create an associated directory for subpages or not.
*/ */
function check_subpage_parents(string $pagename) function check_subpage_parents(string $pagename, bool $create_dir = true)
{ {
global $pageindex, $paths, $env; global $pageindex, $paths, $env;
// Save the new pageindex and return if there aren't any more parent pages to check // Save the new pageindex and return if there aren't any more parent pages to check
if(strpos($pagename, "/") === false) if(strpos($pagename, "/") === false)
return save_pageindex(); return save_pageindex();
$pagename = makepathsafe($pagename); // Just in case
$parent_pagename = substr($pagename, 0, strrpos($pagename, "/")); $parent_pagename = substr($pagename, 0, strrpos($pagename, "/"));
$parent_page_filename = "$parent_pagename.md"; $parent_page_filename = "$parent_pagename.md";
if(!file_exists($env->storage_prefix . $parent_page_filename)) if(!file_exists($env->storage_prefix . $parent_page_filename))
{ {
// This parent page doesn't exist! Create it and add it to the page index. // This parent page doesn't exist! Create it and add it to the page index.
touch($env->storage_prefix . $parent_page_filename, 0); touch($env->storage_prefix . $parent_page_filename, 0);
$newentry = new stdClass(); $newentry = new stdClass();
$newentry->filename = $parent_page_filename; $newentry->filename = $parent_page_filename;
$newentry->size = 0; $newentry->size = 0;
@ -234,8 +237,13 @@ function check_subpage_parents(string $pagename)
$newentry->lasteditor = "none"; $newentry->lasteditor = "none";
$pageindex->$parent_pagename = $newentry; $pageindex->$parent_pagename = $newentry;
} }
if($create_dir) {
$dirname = $env->storage_prefix . $parent_pagename;
if(!file_exists($dirname))
mkdir($dirname, 0755, true);
}
check_subpage_parents($parent_pagename); check_subpage_parents($parent_pagename, $create_dir);
} }
/** /**

View File

@ -1,7 +1,7 @@
<?php <?php
register_module([ register_module([
"name" => "Page mover", "name" => "Page mover",
"version" => "0.9.4", "version" => "0.9.5",
"author" => "Starbeamrainbowlabs", "author" => "Starbeamrainbowlabs",
"description" => "Adds an action to allow administrators to move pages.", "description" => "Adds an action to allow administrators to move pages.",
"id" => "page-move", "id" => "page-move",
@ -72,21 +72,25 @@ register_module([
if(isset($pageindex->$page->uploadedfile) and if(isset($pageindex->$page->uploadedfile) and
file_exists($new_name)) file_exists($new_name))
exit(page_renderer::render_main("Moving $env->page - Error - $settings->sitename", "<p>Whilst moving the file associated with $env->page, $settings->sitename detected a pre-existing file on the server's file system. Because $settings->sitename can't determine whether the existing file is important to another component of $settings->sitename or it's host web server, the move have been aborted - just in case.</p> exit(page_renderer::render_main("Moving $env->page - Error - $settings->sitename", "<p>Whilst moving the file associated with $env->page, $settings->sitename detected a pre-existing file on the server's file system. Because $settings->sitename can't determine whether the existing file is important to another component of $settings->sitename or it's host web server, the move has been aborted - just in case.</p>
<p>If you know that this move is actually safe, please get your site administrator (" . $settings->admindetails_name . ") to perform the move manually. Their contact address can be found at the bottom of every page (including this one).</p>")); <p>If you know that this move is actually safe, please get your site administrator (" . $settings->admindetails_name . ") to perform the move manually. Their contact address can be found at the bottom of every page (including this one).</p>"));
// Make sure that the parent page exists
$do_create_dir = true;
if(strpos($new_name, "/", $do_create_dir) === false)
$do_create_dir = false;
check_subpage_parents($new_name, $do_create_dir);
// Move the page in the page index // Move the page in the page index
$pageindex->$new_name = new stdClass(); $pageindex->$new_name = new stdClass();
foreach($pageindex->$page as $key => $value) foreach($pageindex->$page as $key => $value) {
{
$pageindex->$new_name->$key = $value; $pageindex->$new_name->$key = $value;
} }
unset($pageindex->$page); unset($pageindex->$page);
$pageindex->$new_name->filename = "$new_name.md"; $pageindex->$new_name->filename = "$new_name.md";
// If this page has an associated file, then we should move that too // If this page has an associated file, then we should move that too
if(!empty($pageindex->$new_name->uploadedfile)) if(!empty($pageindex->$new_name->uploadedfile)) {
{
// Update the filepath to point to the description and not the image // Update the filepath to point to the description and not the image
$pageindex->$new_name->filename = $pageindex->$new_name->filename . ".md"; $pageindex->$new_name->filename = $pageindex->$new_name->filename . ".md";
// Move the file in the pageindex // Move the file in the pageindex
@ -96,8 +100,7 @@ register_module([
} }
// Come to think about it, we should probably move the history while we're at it // Come to think about it, we should probably move the history while we're at it
foreach($pageindex->$new_name->history as &$revisionData) foreach($pageindex->$new_name->history as &$revisionData) {
{
// We're only interested in edits // We're only interested in edits
if($revisionData->type !== "edit") continue; if($revisionData->type !== "edit") continue;
$newRevisionName = $pageindex->$new_name->filename . ".r$revisionData->rid"; $newRevisionName = $pageindex->$new_name->filename . ".r$revisionData->rid";