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
There are 8 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki