00864: Made *actual* renamer function to rename wiki pages

Summary: Made *actual* renamer function to rename wiki pages
Created: 2007-01-14 14:55
Status: Open
Category: Cookbook
From: Azbok
Assigned:
Priority: 3
Version: 2.2.0 beta 16
OS:

Description: I got the original code from: http://pmwiki.org/wiki/Cookbook/RenamePage

I commented out the original HandlePostRename() function in rename.php and made my own. Ideally the features from the original one should be combined with this one, but I didn't figure out all the regex stuff in the original one so I didn't include it in here.

Note: I have hardcoded the wiki link syntax and this page rename ONLY works with group links such as "Group/Page". Ideally this should be updated to work with all wiki links. Similar to my other update (http://pmwiki.org/wiki/PITS/00863) of removing deleted file entries in the pageindex file, I don't know if this is the proper way to do it, but it works for me.

If you have the update I just mentioned above, this works nicely and it's as if you created the page with the new name to begin with (as far as I can figure anyway).

I hope you guys can make use of this
azbok

function HandlePostRename($pagename) {

  global $WikiDir;

  $newpagename = MakePageName($pagename, stripmagic($_POST['group'].'.'.$_POST['renametext']));

  if (PageExists($newpagename)) {
    Abort("'$newpagename' already exists");
  }

  # Get list backlinks to given page plus page index file
  $backlinks = PageIndexGrep($pagename, false);

  # Make sure backlinks don't include the new name
  $backlinks = array_diff($backlinks, (array)$newpagename);

  # Create search regex for old link
  $linkname_regex = preg_replace('/\./', '\/', $pagename);
  $pagename_regex = preg_replace('/\./', '\.', $pagename);

  # Create replacement text for new link
  $newlinkname = preg_replace('/\./', '/', $newpagename);

  # Search and replace all occurances of the oldlinkname with the newlinkname
  foreach ($backlinks as $backlink) {
    $page = RetrieveAuthPage($backlink, 'edit');
    $page['text'] = preg_replace("/$linkname_regex/", $newlinkname, $page['text']);
    $page['targets'] = preg_replace("/$pagename_regex/", $newpagename, $page['targets']);
    WritePage($backlink,$page);
  }

  # Do the actual file renaming
  $oldqualifiedfile = $WikiDir->pagefile($pagename);
  $newqualifiedfile = $WikiDir->pagefile($newpagename);
  rename($oldqualifiedfile, $newqualifiedfile);

  # Change the internal name property to the new name by just writing the page
  $page = ReadPage($newpagename);
  WritePage($newpagename,$page);

  # Update the index for all pages that have changed
  # Keep the old page name in the backlinks to remove it from the pageindex file
  $backlinks[] = $newpagename;
  PageIndexUpdate($backlinks);

  Redirect($newpagename);

}

Array
(
    [post_max_size] => 64M
    [$_POST keys] => 
    [$_REQUEST keys] => n
    [$_SERVER] => Array
        (
            [CONTEXT_DOCUMENT_ROOT] => /home/pmwiki/public_html
            [CONTEXT_PREFIX] => 
            [DOCUMENT_ROOT] => /home/pmwiki/public_html
            [GATEWAY_INTERFACE] => CGI/1.1
            [HTTPS] => on
            [HTTP_ACCEPT] => */*
            [HTTP_ACCEPT_ENCODING] => gzip, br, zstd, deflate
            [HTTP_HOST] => www.pmwiki.org
            [HTTP_REFERER] => https://pmwiki.org/wiki/PITS/00864
            [HTTP_USER_AGENT] => Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
            [HTTP_X_HTTPS] => 1
            [PATH] => /bin:/usr/bin
            [PHP_INI_SCAN_DIR] => /opt/cpanel/ea-php70/root/etc:/opt/cpanel/ea-php70/root/etc/php.d:.
            [QUERY_STRING] => n=PITS%2f00864
            [REDIRECT_HTTPS] => on
            [REDIRECT_QUERY_STRING] => n=PITS%2f00864
            [REDIRECT_SCRIPT_URI] => https://www.pmwiki.org/wiki/PITS/00864
            [REDIRECT_SCRIPT_URL] => /wiki/PITS/00864
            [REDIRECT_SSL_TLS_SNI] => www.pmwiki.org
            [REDIRECT_STATUS] => 200
            [REDIRECT_UNIQUE_ID] => afKKZGj9mlwEvEUaNpir7AAAAUA
            [REDIRECT_URL] => /wiki/PITS/00864
            [REMOTE_ADDR] => 216.73.216.31
            [REMOTE_PORT] => 60213
            [REQUEST_METHOD] => GET
            [REQUEST_SCHEME] => https
            [REQUEST_URI] => /wiki/PITS/00864
            [SCRIPT_FILENAME] => /home/pmwiki/public_html/index.php
            [SCRIPT_NAME] => /index.php
            [SCRIPT_URI] => https://www.pmwiki.org/wiki/PITS/00864
            [SCRIPT_URL] => /wiki/PITS/00864
            [SERVER_ADDR] => 23.254.203.248
            [SERVER_ADMIN] => webmaster@pmwiki.org
            [SERVER_NAME] => www.pmwiki.org
            [SERVER_PORT] => 443
            [SERVER_PROTOCOL] => HTTP/1.1
            [SERVER_SIGNATURE] => 
            [SERVER_SOFTWARE] => Apache
            [SSL_TLS_SNI] => www.pmwiki.org
            [TZ] => America/Los_Angeles
            [UNIQUE_ID] => afKKZGj9mlwEvEUaNpir7AAAAUA
            [PHP_SELF] => /index.php
            [REQUEST_TIME_FLOAT] => 1777502820.6401
            [REQUEST_TIME] => 1777502820
            [argv] => Array
                (
                    [0] => n=PITS%2f00864
                )

            [argc] => 1
        )

)