DokuWiki

It's better when it's simple

User Tools

Site Tools


faq:howto-rename-pages

This is an old revision of the document!


HOWTO: Rename Pages

:?: Is there a way to rename a page DokuWiki?

The easy way

Renaming would possibly break a lot of links linking to the old page. Since DokuWiki doesn't know which pages link where this cannot be handled by DokuWiki. An easy way to “rename” a page is to simply create the new one and then change the content of the old page to something like this:

Moved to [[newpage]].

Then visit the pages linking to the old page, and update them to refer to the new page. This can be done gradually. Alternatively, use one of the page-redirect plugins listed at: How to make a redirect page?.

Note that the old revisions will not be moved with that page. If you like to move them too, you should either write a script or use a plugin (see below).

On the file system

You can also rename/move the txt files in the file system, and the gz files in the attic. This will break links and metadata.

Using a Plugin

The move plugin allows a simpler way for an administrator to move/rename pages. It moves history and fixes backlinks.

Old plugins

  • The pagemove plugin. But some report that it does not work currently and produces a lot of unwanted changes!
  • The editx plugin moves/renames pages with their history metadata, but does not update any other pages for you. You still have to check/fix backlinks manually.

Using UNIX commands

Note: This tip assumes one is using a Unix-like OS with text file utilities installed, or is using Cygwin under Windows.

Note that following this tip prevents DokuWiki from keeping a coherent page history (old revisions). Remember to back up all raw text files and data.

How-To Rename a set of pages? Use sed regular expressions to match a filename pattern and replace it with a new filename pattern.

This is example is bad. See why you should never parse LS for details.

Example: Rename all pages starting with <project_name> to another prefix:

for f in `ls *.txt`; do sed -e 's:\[\[project_name\(.*\)\]\]:[[prj_code.\1]]:g' $f > $f.new; done
for f in `ls *.new | sed -e 's:\(.*\)\.new:\1:g'`; do mv $f.new $f; done
for f in `ls *.txt | sed -e 's:project_name\(.*\)\.txt:\1.txt:g'`; do mv project_name-$f prj_code-$f; done

Better:

find . -name '*.txt' -exec sed -e 's:\[\[project_name\(.*\)\]\]:[[prj_code.\1]]:g' '{}' > '{}'.new \;
find . -name '*.new' -exec sed -e 's:\(.*\)\.new:\1:g'`; do mv '{}'.new '{}' \;
find . -name '*.txt' -exec sed -e 's:project_name\(.*\)\.txt:\1.txt:g'`; do mv project_name-'{}' prj_code-'{}' \;

Explanation:

  • The first command uses sed to find all occurrences of a DokuWiki FreeLink that starts with project_name and replaces them with a new FreeLink that is like the old one except that instead of prefix project_name it has prefix prj_code.
  • The second command is used to replace the old raw text files (with old FreeLink) with the new raw text files (with new FreeLink).
  • Finally, the third command renames the files that match those FreeLinks, by replacing the prefix project_name with prefix prj_code in their filename.

Noob questions - I believe using the above script code is a replacement for the “in the filesystem” method, plus it handles all the backlinks, right? The meta data would still be broken? Is that important? (I personally don't care about history, but for completeness would be good to address that). This example seems to be for renaming a namespace, would it also work for renaming the .txt files themselves? If these questions are inappropriate here, my apologies - feel free to delete them, but if you're feeling magnanimous please address the issues in a reply here: http://forum.dokuwiki.org/thread/7677 Thanks.


Here's a simple script to rename any page/namespace and updated links in all files. Simply change the path to your data (aka base) directory. In this script you'd change /www/dokuwiki to your base directory.

Using a Script

FIXME This script is very dangerous! There no checks on valid input and the perl subsitute commands can change page content throughout the whole wiki! What if we change a file named dangerous to safe then also all the words dangerous on all wiki pages will be subsituted!

# vi rename_page
#! /bin/ksh

cd /www/dokuwiki || exit $?

O=$1
N=$2


#-- Moving a name space
if echo "$O" | grep -q :
then
        OPATH="$( echo "$O" | sed 's/:/\//g' )"
        NPATH="$( echo "$N" | sed 's/:/\//g' )"

        find * -type d | grep -w "${OPATH}" | while read ODIR
        do
                NDIR="$( echo "$ODIR" | sed "s:${OPATH}:${NPATH}:" )"
                mkdir -p ${NDIR}
                [[ -d ${NDIR} ]] && mv ${ODIR}/* ${NDIR}/
        done

#-- Moving a file
else
        find * -type f -name "*${O}*" | while read F
        do
                NFILE="$( echo "$F" | sed "s/${O}/${N}/" )"
                mv "$F" "${NFILE}"
        done
fi

find * -type f -exec grep -wl "$O" {} \; | while read F
do
        perl -i -pe "s/$O/$N/g" "$F";
done

Renaming a page

Now let's rename ALL pages and links with the pattern dog_lover in their name to k9_lover.

# chmod 700 rename_page
# rename_page dog_lover k9_lover

If the file name has a space or other special charater

# rename_page "dog lover" "k9 lover"

Only got-cha is the ALL references in ANY file to dog_lover will now read k9_lover. So links in all namespaces will be renamed.

Renaming a namespace

To move a name space, assuming /www/dokuwiki as the base directory. The following command moves namespace n1:n2 to n3:n4. This also tries to update all pages with the new namespace.

# rename_page ns1:ns2 n3:n4
faq/howto-rename-pages.1396382134.txt.gz · Last modified: 2014-04-01 21:55 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki