Cloning an existing WikiPage

I like to re-use and hate to redesign the wheel... Surely I'm not alone ;-)

My solution

I wanted to build an action {{clone from="OldPage" to="NewPage" editoption="true"}} that would duplicate an existing page to a new page .
It would first check the existence of the template page, the non existence of the page to be created, the user right to read the page to be cloned and the user right to create a page.
The editoption when true will create the new page and then open it for edition. This action code is here below, however DarTar convinced me that a handler makes nore sense so you should rather go to CloneHandler.

Dependancy

This relies of the ExistsPage function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaBugsResolved under the heading "check of user-names against page-names"..

The code

Copy this code into an action named: clone.php - place it in the actions folder.
<?php
/**
 * Duplicate an existing  page to a new named page
 *
 * Usage: {{clone from="OldPage" to="NewPage" editoption="true"}}
 *
 * This action checks the existence of the template page, the non existence of the page to be created, the user
 * right to read the template and the user right to create a page.
 * The editoption when true will create the new page and then open it for edition.
 *
 *
 * @package         Actions
 * @subpackage        
 * @name             clone
 *
 * @author            {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy}
 * @version           0.3
 * @since             Wikka 1.1.5.3
 *
 * @input             string  $from  optional: the template page to be duplicated
 *                            must be an existing page and must be authorized for reading to the current user  
 *                            defaulted to the current WikiPage
 *                              
 * @input             string  $to  mandatory: the page to be created
 *                            must be a non existing page and current user must be authorized to create
 *
 * @input             string  $note  optional: the note to be added to the page when created
 *                            default is "Templated from " followed by the name of the template page
 *
 * @input             boolean $editoption optional: if true, the newly created page will be opened for edition
 *                            default is false
 */


 // ***** CONSTANTS *****
define ('USER_NAME',$this->GetUserName());
define ('BASE_URL',$this->config['base_url']);
// ***** END CONSTANTS *****

 // ***** INPUT FORM *****

echo $this->FormOpen();
?>
<table class="clone" border="1">
    <tr>
        <th align="left">WikiPage to be cloned (empty means this page):</th>
        <td><input type="text" name="from" size="37" value="<?php echo $from ?>" /></td>
    </tr>
    <tr>
        <th align="left">WikiPage to be created:</th>
        <td><input type="text" name="to" size="37" value="<?php echo $to ?>" /></td>
    </tr>
    <tr>
        <th align="left">Added note to your edit:</th>
        <td><input type="text" name="note" size="37" value="<?php echo $note ?>" /></td>
    </tr>
    <tr>
        <th align="left">Do you want to edit the new page when created?</th>
        <td>
            <table border="1">
                <tr>
                    <td><input type="checkbox" name="editoption" value="Y" <?php echo $editoption == "true" ? "checked=\"checked\"" : "" ?> />Edit after creation</td>
                    <td><input type="submit" name="create" value="Clone it now"></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<?php
echo $this->FormClose();
 // ***** END INPUT FORM *****
 
// ***** GET PARAMETERS *****
$from = $_POST["from"];
$to = $_POST["to"];
$note = $_POST["note"];
$editoption = $_POST["editoption"];
// ***** END PARAMETERS *****

// ***** CLONING PROCESS *****
If ($to<>"") {
    // The user must provide a WikiPage name to be created...
    if ($this->ExistsPage($to))
    {
        // and it should be a new one...
        $errormessage = "The destination page must not exist.";
        echo $errormessage;
        return;
    }
    // also the user has to be allowed to create this page...
    elseif ($this->HasAccess("write", $to)) {
        // The user should provide a WikiPage name to be duplicated...
        if (!$from)
        {
            // by default the system will duplicate the current page...
            $from = $this->GetPageTag();
        }
        else
        {
            // otherwise, the page to be dublicated must of course exit...
            if ($this->ExistsPage($from))
            {
                // and the user has to be allowed to read it...
                if (!$this->HasAccess("read", $from))
                {
                    $errormessage = "You are not authorized to use the template page.";
                    echo $errormessage;
                    return;
                }
            }
            else
            {
                // this is what happens if the user tries to duplicate a non existing page...
                $errormessage = "The template page does not exist.";
                echo $errormessage;
                return;
            }
        }
        // the user may provide a creation note otherwise...
        if (!$note)
        {
            // the system will create one by default...
            $note = "Templated from ".$from;
        }
       
        // ***** DERIVED VARIABLES *****
        $thepage=$this->LoadPage($from); // load the page that has to be duplicated...
        if ($thepage) $pagecontent = $thepage["body"]; // and get its content.
        // ***** END DERIVED VARIABLES *****
 
        // ***** OUTPUT SECTION *****
        $this->SavePage($to, $pagecontent, $note); // creates the page...
        echo "[[ | ".$to." has been created from ".$from." and note was: ".$note." ]]"; // confirms what has been done
        if ($editoption) $this->redirect(BASE_URL.$to."/edit"); // drops the user to the new page for edition if he choosed so
        // ***** END OUTPUT SECTION *****

    } else
    {  
        // this is what happens if you try to create a page when you are not authorized.
        $errormessage = "You are not authorized to create the destination page.";
        echo $errormessage;
        return;
    }
}
// ***** END CLONING PROCESS *****
?>


How to use it?

Once you have (re)designed a page that fits to be replicated many times (Task Lists, User Pages, Bug Description, Developement Description...) you just have to use the {{clone}} action from any page: you may want to have a dedicated TemplatePage with the action and the instructions on it (I will propose one if this action can be installed on the Wikka server).
Then you must fill the name of the new WikiPage to be created, if you do not fill the name of the page that has to be duplicated the system will duplicate the current page, you may want to add a note, finally you decide if you just want to create the page or if you want to jump and edit it as soon as it has been copied.

To Do

My code needs probably to be reviewed by expert coder as I am not at all a developper.
Any ideas around this action more than welcome.

All discussions on this page still are at WikiTemplate. We definitely need a PageNameChange handler ;-)


CategoryUserContributions
Comments
Comment by DarTar
2004-12-19 15:09:30
Christian, I think it's better to keep all the discussion on the original page (I hadn't seen this page when I added my comments). So if you don't mind, I'll delete this page and replace the name and headers in the original one. We can always ask JsnX to rename the original page as CloneAction.
Comment by DavidKeltie
2005-01-10 21:49:47
When adding the code I get
"Fatal error: Call to undefined function: existspage() in ....../wikka/actions/clone.php on line 82"

Anybody else? Any fix?
Comment by JavaWoman
2005-01-10 22:19:11
David,
It depends on the ExistsPage function I wrote which will be 1.1.6.0 (imminent now). As stated above under Dependancy: "You can find this version at WikkaDevelopment."
Comment by DavidKeltie
2005-01-11 16:07:48
Ooops was so excited to find this action that I didn't read carefully. But I can't "....find this version at WikkaDevelopment." Where is that? There's no download link on the WikkaDevelopment page.

When is imminent?

Great software BTW - I love the way it can integrate Freemind maps.
Comment by ChristianBarthelemy
2005-01-11 16:25:10
I am afraid that the code for the ExistsPage function has been removed from WikkaDevelopment. You have to get it from the revision dated from 2004-12-22 21:53:52. The next one dated from 2004-12-30 23:42:12 by NilsLindenberg removed the code. Any reason to do that until 1.1.6.0 is released?
Comment by JavaWoman
2005-01-11 16:33:09
You can find the code for ExistsPage now on WikkaBugsResolved under the heading "check of user-names against page-names". ;)

"Imminent" is "we hope before the end of the week". Have a look at WikkaReleaseNotes and you'll see there will be a LOT in this release, so it takes a bit to ensure all those things play nicely together.
Comment by DarTar
2005-01-11 16:34:45
(BTW 1.1.6.0 will include a clone handler: see http://wikka.jsnx.com/CloneHandlerInfo)
Comment by NilsLindenberg
2005-01-11 17:50:47
"There's no download link on the WikkaDevelopment page."

My mistake, I forgot to change the link on this page. Even if it was on the developmentpage, it was a bug instead (or a feature ;-)
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki