2015-09-19 09:19:56 +00:00
< ? php
register_module ([
" name " => " Page mover " ,
2020-08-06 14:47:41 +00:00
" version " => " 0.9.5 " ,
2015-09-19 09:19:56 +00:00
" author " => " Starbeamrainbowlabs " ,
" description " => " Adds an action to allow administrators to move pages. " ,
" id " => " page-move " ,
" code " => function () {
2015-12-21 08:45:38 +00:00
global $settings ;
2016-06-15 18:20:03 +00:00
/**
2016-10-13 17:00:20 +00:00
* @ api { get } ? action = move [ & new_name = { newPageName }] Move a page
2016-06-15 18:20:03 +00:00
* @ apiName Move
* @ apiGroup Page
* @ apiPermission Moderator
*
* @ apiParam { string } new_name The new name to move the page to . If not set a page will be returned containing a move page form .
*
* @ apiUse UserNotModeratorError
* @ apiError EditingDisabledError Editing is disabled on this wiki , so pages can ' t be moved .
* @ apiError PageExistsAtDestinationError A page already exists with the specified new name .
* @ apiError NonExistentPageError The page you 're trying to move doesn' t exist in the first place .
* @ apiError PreExistingFileError A pre - existing file on the server ' s file system was detected .
*/
2015-12-26 12:55:19 +00:00
/*
* ███ ███ ██████ ██ ██ ███████
* ████ ████ ██ ██ ██ ██ ██
* ██ ████ ██ ██ ██ ██ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██████ ████ ███████
*/
2015-09-19 09:19:56 +00:00
add_action ( " move " , function () {
2015-11-11 18:49:08 +00:00
global $pageindex , $settings , $env , $paths ;
2015-09-19 09:19:56 +00:00
if ( ! $settings -> editing )
{
2015-09-22 13:34:18 +00:00
exit ( page_renderer :: render_main ( " Moving $env->page - error " , " <p>You tried to move $env->page , but editing is disabled on this wiki.</p>
2015-09-19 09:19:56 +00:00
< p > If you wish to move this page , please re - enable editing on this wiki first .</ p >
2015-09-22 13:34:18 +00:00
< p >< a href = 'index.php?page=$env->page' > Go back to $env -> page </ a >.</ p >
2015-09-19 09:19:56 +00:00
< p > Nothing has been changed .</ p > " ));
}
2015-09-22 13:34:18 +00:00
if ( ! $env -> is_admin )
2015-09-19 09:19:56 +00:00
{
2015-09-22 13:34:18 +00:00
exit ( page_renderer :: render_main ( " Moving $env->page - Error " , " <p>You tried to move $env->page , but you do not have permission to do that.</p>
2015-09-19 09:19:56 +00:00
< p > You should try < a href = 'index.php?action=login' > logging in </ a > as an admin .</ p > " ));
}
if ( ! isset ( $_GET [ " new_name " ]) or strlen ( $_GET [ " new_name " ]) == 0 )
2015-09-22 13:34:18 +00:00
exit ( page_renderer :: render_main ( " Moving $env->page " , " <h2>Moving $env->page </h2>
2015-09-19 09:19:56 +00:00
< form method = 'get' action = 'index.php' >
< input type = 'hidden' name = 'action' value = 'move' />
< label for = 'old_name' > Old Name :</ label >
2015-09-22 13:34:18 +00:00
< input type = 'text' name = 'page' value = '$env->page' readonly />
2015-09-19 09:19:56 +00:00
< br />
< label for = 'new_name' > New Name :</ label >
< input type = 'text' name = 'new_name' />
< br />
< input type = 'submit' value = 'Move Page' />
</ form > " ));
$new_name = makepathsafe ( $_GET [ " new_name " ]);
2015-09-22 13:34:18 +00:00
$page = $env -> page ;
2015-09-19 09:19:56 +00:00
if ( ! isset ( $pageindex -> $page ))
2015-09-22 13:34:18 +00:00
exit ( page_renderer :: render_main ( " Moving $env->page - Error " , " <p>You tried to move $env->page to $new_name , but the page with the name $env->page does not exist in the first place.</p>
2015-09-19 09:19:56 +00:00
< p > Nothing has been changed .</ p > " ));
2015-09-22 13:34:18 +00:00
if ( $env -> page == $new_name )
exit ( page_renderer :: render_main ( " Moving $env->page - Error " , " <p>You tried to move $page , but the new name you gave is the same as it's current name.</p>
2015-09-19 09:19:56 +00:00
< p > It is possible that you tried to use some characters in the new name that are not allowed and were removed .</ p >
2016-11-25 19:34:20 +00:00
< p > Page names may not contain any of these characters : < code > ? %*:| \ " ><()[]</code></p> " ));
2015-09-19 09:19:56 +00:00
2016-04-02 20:21:34 +00:00
if ( isset ( $pageindex -> $page -> uploadedfile ) and
file_exists ( $new_name ))
2020-08-06 14:47:41 +00:00
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>
2016-06-22 08:13:32 +00:00
< 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 > " ));
2020-08-06 14:47:41 +00:00
// 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 );
2016-04-02 20:21:34 +00:00
2016-06-05 13:35:09 +00:00
// Move the page in the page index
2015-09-19 09:19:56 +00:00
$pageindex -> $new_name = new stdClass ();
2020-08-06 14:47:41 +00:00
foreach ( $pageindex -> $page as $key => $value ) {
2015-09-19 09:19:56 +00:00
$pageindex -> $new_name -> $key = $value ;
}
unset ( $pageindex -> $page );
2016-08-19 18:39:53 +00:00
$pageindex -> $new_name -> filename = " $new_name .md " ;
2016-06-05 13:35:09 +00:00
2015-10-25 11:02:20 +00:00
// If this page has an associated file, then we should move that too
2020-08-06 14:47:41 +00:00
if ( ! empty ( $pageindex -> $new_name -> uploadedfile )) {
2015-10-25 11:09:26 +00:00
// Update the filepath to point to the description and not the image
$pageindex -> $new_name -> filename = $pageindex -> $new_name -> filename . " .md " ;
2015-10-25 11:02:20 +00:00
// Move the file in the pageindex
$pageindex -> $new_name -> uploadedfilepath = $new_name ;
// Move the file on disk
2015-11-08 21:15:08 +00:00
rename ( $env -> storage_prefix . $env -> page , $env -> storage_prefix . $new_name );
2015-10-25 11:02:20 +00:00
}
2016-06-05 13:35:09 +00:00
// Come to think about it, we should probably move the history while we're at it
2020-08-06 14:47:41 +00:00
foreach ( $pageindex -> $new_name -> history as & $revisionData ) {
2016-06-05 13:35:09 +00:00
// We're only interested in edits
if ( $revisionData -> type !== " edit " ) continue ;
$newRevisionName = $pageindex -> $new_name -> filename . " .r $revisionData->rid " ;
// Move the revision to it's new name
rename (
$env -> storage_prefix . $revisionData -> filename ,
$env -> storage_prefix . $newRevisionName
);
// Update the pageindex entry
$revisionData -> filename = $newRevisionName ;
}
// Save the updated pageindex
2019-06-01 14:55:48 +00:00
save_pageindex ();
2015-09-19 09:19:56 +00:00
2015-11-11 18:49:08 +00:00
// Move the page on the disk
2015-11-08 21:15:08 +00:00
rename ( " $env->storage_prefix $env->page .md " , " $env->storage_prefix $new_name .md " );
2015-09-19 09:19:56 +00:00
2015-11-11 18:49:08 +00:00
// Move the page in the id index
ids :: movepagename ( $page , $new_name );
2017-11-23 15:16:08 +00:00
// Move the comments file as well, if it exists
if ( file_exists ( " $env->storage_prefix $env->page .comments.json " )) {
rename (
" $env->storage_prefix $env->page .comments.json " ,
" $env->storage_prefix $new_name .comments.json "
);
}
2018-03-18 21:38:58 +00:00
// Add a recent change announcing the move if the recent changes
// module is installed
if ( module_exists ( " feature-recent-changes " ))
{
add_recent_change ([
" type " => " move " ,
" timestamp " => time (),
" oldpage " => $page ,
" page " => $new_name ,
" user " => $env -> user
]);
}
2015-11-11 18:49:08 +00:00
// Exit with a nice message
2017-11-23 15:16:08 +00:00
exit ( page_renderer :: render_main ( " Moving " . htmlentities ( $env -> page ), " <p><a href='index.php?page= " . rawurlencode ( $env -> page ) . " '> " . htmlentities ( $env -> page ) . " </a> has been moved to <a href='index.php?page= " . rawurlencode ( $new_name ) . " '> " . htmlentities ( $new_name ) . " </a> successfully.</p> " ));
2015-09-19 09:19:56 +00:00
});
2015-12-21 08:45:38 +00:00
// Register a help section
2017-11-23 15:16:08 +00:00
add_help_section ( " 60-move " , " Moving Pages " , " <p>If you are logged in as an administrator, then you have the power to move pages. To do this, click "Move" in the "More..." menu when browsing the pge you wish to move. Type in the new name of the page, and then click "Move Page".</p> " );
2015-09-19 09:19:56 +00:00
}
]);
2015-09-22 13:34:18 +00:00
2015-09-19 09:19:56 +00:00
?>