====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 [[WikkaBugsResolved | 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) GetUserName()); define ('BASE_URL',$this->config['base_url']); // ***** END CONSTANTS ***** // ***** INPUT FORM ***** echo $this->FormOpen(); ?>
WikiPage to be cloned (empty means this page):
WikiPage to be created:
Added note to your edit:
Do you want to edit the new page when created?
/>Edit after creation
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