====Cloning an existing WikiPage==== {{lastedit}} <>==See also:== Documentation: CloneHandlerInfo.>> This is the development page for the clone handler.::c:: I like to re-use and hate to redesign the wheel... Surely I'm not alone ;-) ===Two solutions=== Either an action CloneAction or this CloneHandler. When the action was first published, DarTar explained that a handler would make more sense. It certainly is the right way to do so here is the handler. ===Dependancy=== This relies on the ""ExistsPage()"" function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaDevelopment. ===The code=== Copy the code into a handler named: clone.php - place it in the handlers/page folder. Thanks to DarTar, the original code has been improved: please get the correct code below. ===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 type in ""/clone"" at the end of its URL. Then you must fill the name of the new WikiPage to be created, 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 handler more than welcome. Here are a few ideas I have to improve this handler: - Offer the possibility to clone the ACLs if the user wants to - Offer the possibility to clone the comments if the user wants to All discussions on this page still are at WikiTemplate. We definitely need a PageNameChange handler ;-) ~''See MovePages - not implemented, it has its problems; but the wish/requirement isn't new ;-) --JavaWoman'' ---- ====Improved CloneHandler==== Christian, your code had a number of bugs that I've fixed in a new version of the handler posted below. ~-Empty or invalid page names are now forbidden (//in the future the validation will have to be done by a core function//). ~-I've changed the validation order in a way that I find more sensible. ~-Finally, I've replaced all the javascript dialogs with inline alert messages. --- ~I think this is an issue that should be addressed in general, not only in this case. We should try to produce an accessible code also for users whose browser does not support javascript. A central error handler, displaying alert messages (both //local//, as those produced by handlers and actions, and //global//, as those produced by the database or the engine itself), would be a major improvement of WikkaWiki. I've uploaded this handler to the server so please test it and give your feedback here. -- DarTar %%(php)
tag; $to = $this->tag; $note = 'Cloned from '.$from; #i18n $editoption = ''; $box = 'Please fill in a valid target ""PageName"" and an (optional) edit note.'; #i18n // print header echo $this->Format('==== Clone current page ===='); // 1. check source page existence if (!$this->ExistsPage($from)) { // source page does not exist! $box = ' Sorry, page '.$from.' does not exist.'; #i18n } else { // 2. page exists - now check user's read-access to the source page if (!$this->HasAccess('read', $from)) { // user can't read source page! $box = ' //You aren not allowed to read the source of this page.//'; #i18n } else { // page exists and user has read-access to the source - proceed if ($_POST) { // get parameters $to = ($_POST['to'])? $_POST['to'] : $to; $note = ($_POST['note'])? $_POST['note'] : $note; $editoption = (isset($_POST['editoption']))? 'checked="checked"' : ''; // 3. check target pagename validity if (!preg_match("/^[A-ZÄÖÜ]+[a-zßäöü]+[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to)) { // invalid pagename! $box = '""
You must specify a valid PageName
""'; #i18n } else { // 4. target page name is valid - now check user's write-access if (!$this->HasAccess('write', $to)) { $box = '""
Sorry! You don\'t have write-access to '.$to.'
""'; #i18n } else { // 5. check target page existence if ($this->ExistsPage($to)) { // page already exists! $box = '""
Sorry! The destination page already exists
""'; #i18n } else { // 6. Valid request - proceed to page cloning $thepage=$this->LoadPage($from); # load the source page if ($thepage) $pagecontent = $thepage['body']; # get its content $this->SavePage($to, $pagecontent, $note); #create target page if ($editoption == 'checked="checked"') { // quick edit $this->Redirect($this->href('edit',$to)); } else { // show confirmation message $box = '""'.$this->MiniHref('',$to).'"" was succesfully created!'; #i18n } } } } } // build form $form = $this->FormOpen('clone'); $form .= ''. ''. ''. ''. ''. ''. ''. ''. ''. ''. ''. ''. ''. '
Clone '.$this->Link($this->GetPageTag()).' to:
Edit note:
'. ' Edit after creation '. ''. '
'; $form .= $this->FormClose(); } } // display messages if (isset($box)) echo $this->Format(' --- '.$box.' --- --- '); // print form if (isset($form)) print $form; ?>
%% I've added the feature of letting the user clone the ACLs in this version: (I also fixed what I thought was a bug, it's now possible to have a blank note for the cloned page.) Comments are welcome. /AndreasHeintze %%(php)
tag; $to = $this->tag; $note = sprintf(CLONED_FROM, $from); $editoption = ''; $cloneacls = ''; $box = PLEASE_FILL_VALID_TARGET; // print header echo $this->Format(CLONE_HEADER); // 1. check source page existence if (!$this->ExistsPage($from)) { // source page does not exist! $box = sprintf(ERROR_PAGE_NOT_EXIST, $from); } else { // 2. page exists - now check user's read-access to the source page if (!$this->HasAccess('read', $from)) { // user can't read source page! $box = ERROR_ACL_READ; } else { // page exists and user has read-access to the source - proceed if (isset($_POST) && $_POST) { // get parameters $to = isset($_POST['to']) && $_POST['to'] ? $_POST['to'] : $to; $note = isset($_POST['note']) ? $_POST['note'] : $note; $editoption = (isset($_POST['editoption']))? 'checked="checked"' : ''; $cloneacls = (isset($_POST['cloneacls']))? 'checked="checked"' : ''; // 3. check target pagename validity if (!preg_match(VALID_PAGENAME_PATTERN, $to)) //TODO use central regex library { // invalid pagename! $box = '""'.ERROR_INVALID_PAGENAME.'""'; } else { // 4. target page name is valid - now check user's write-access if (!$this->HasAccess('write', $to)) { $box = '""'.sprintf(ERROR_ACL_WRITE, $to).'""'; } else { // 5. check target page existence if ($this->ExistsPage($to)) { // page already exists! $box = '""'.ERROR_PAGE_ALREADY_EXIST.'""'; } else { // 6. Valid request - proceed to page cloning $thepage=$this->LoadPage($from); # load the source page if ($thepage) $pagecontent = $thepage['body']; # get its content $this->SavePage($to, $pagecontent, $note); #create target page if ($cloneacls == 'checked="checked"') { // Clone ACLs too $acls = $this->LoadAllACLs($from); $this->SaveACL($to, 'read', $acls['read_acl']); $this->SaveACL($to, 'write', $acls['write_acl']); $this->SaveACL($to, 'comment', $acls['comment_acl']); } if ($editoption == 'checked="checked"') { // quick edit $this->Redirect($this->href('edit', $to)); } else { // show confirmation message $box = '""'.sprintf(CLONE_SUCCESSFUL, $to).'""'; } } } } } // build form $form = $this->FormOpen('clone'); $form .= ''."\n". ''."\n". ''."\n". ''."\n". ''."\n". ''."\n". ''. ''."\n". ''."\n". ''."\n". ''."\n". ''."\n". ''."\n". '
'.sprintf(CLONE_X_TO, $this->Link($this->GetPageTag())).'
'.EDIT_NOTE.'
'."\n". '
'."\n". '

'."\n". ''."\n". '
'."\n"; $form .= $this->FormClose(); } } // display messages if (isset($box)) echo $this->Format(' --- '.$box.' --- --- '); // print form if (isset($form)) print $form; ?>
%% ---- CategoryDevelopmentHandlers