Revision history for CloneHandler


Revision [19423]

Last edited on 2008-01-28 00:15:45 by JavaWoman [Modified links pointing to docs server]

No Differences

Revision [17804]

Edited on 2007-12-12 10:09:32 by JavaWoman [prevent function references looking as page links]
Additions:
This relies on the ""ExistsPage()"" function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaDevelopment.
Deletions:
This relies of the ExistsPage function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaDevelopment.


Revision [16870]

Edited on 2007-05-31 23:26:44 by AndreasHeintze [Reverted]
Additions:
if (!preg_match("/^[A-ZÄÖÜ]+[a-zßäöü]+[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to))
if(!defined('VALID_PAGENAME_PATTERN')) define ('VALID_PAGENAME_PATTERN', '/^[A-Za-zÄÖÜßäöü]+[A-Za-z0-9ÄÖÜßäöü]*$/s');
if (isset($_POST) && $_POST)
$to = isset($_POST['to']) && $_POST['to'] ? $_POST['to'] : $to;
$note = isset($_POST['note']) ? $_POST['note'] : $note;
$cloneacls = (isset($_POST['cloneacls']))? 'checked="checked"' : '';
if (!preg_match(VALID_PAGENAME_PATTERN, $to)) //TODO use central regex library
$box = '""<em class="error">'.ERROR_INVALID_PAGENAME.'</em>""';
$box = '""<em class="error">'.sprintf(ERROR_ACL_WRITE, $to).'</em>""';
$box = '""<em class="error">'.ERROR_PAGE_ALREADY_EXIST.'</em>""';
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']);
$this->Redirect($this->href('edit', $to));
$box = '""<em class="success">'.sprintf(CLONE_SUCCESSFUL, $to).'</em>""';
$form .= '<table class="clone">'."\n".
'<tr>'."\n".
'<td>'.sprintf(CLONE_X_TO, $this->Link($this->GetPageTag())).'</td>'."\n".
'<td><input type="text" name="to" value="'.$to.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td>'.EDIT_NOTE.'</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td></td>'."\n".
'<td>'."\n".
'<input type="checkbox" name="editoption" '.$editoption.' id="editoption" /><label for="editoption">'.LABEL_EDIT_OPTION.'</label><br />'."\n".
'<input type="checkbox" name="cloneacls" '.$cloneacls.' id="cloneacls" checked /><label for="cloneacls">'.LABEL_CLONEACLS_OPTION.'</label><br /><br />'."\n".
'<input type="submit" name="create" value="'.LABEL_CLONE.'" />'."\n".
'</td>'."\n".
'</tr>'."\n".
'</table>'."\n";
CategoryDevelopmentHandlers
Deletions:
if (!preg_match("/^[A-ZÄÖÜ] [a-zßäöü] [A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to))
if(!defined('VALID_PAGENAME_PATTERN')) define ('VALID_PAGENAME_PATTERN', '/^[A-Za-zÄÖÜßäöü] [A-Za-z0-9ÄÖÜßäöü]*$/s');
if (isset($_POST)


Revision [16668]

Edited on 2007-05-31 10:34:39 by Dj3Nur [Reverted]
Additions:
if (!preg_match("/^[A-ZÄÖÜ] [a-zßäöü] [A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to))
if(!defined('VALID_PAGENAME_PATTERN')) define ('VALID_PAGENAME_PATTERN', '/^[A-Za-zÄÖÜßäöü] [A-Za-z0-9ÄÖÜßäöü]*$/s');
if (isset($_POST)
Deletions:
if (!preg_match("/^[A-ZÄÖÜ]+[a-zßäöü]+[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to))
if(!defined('VALID_PAGENAME_PATTERN')) define ('VALID_PAGENAME_PATTERN', '/^[A-Za-zÄÖÜßäöü]+[A-Za-z0-9ÄÖÜßäöü]*$/s');
if (isset($_POST) && $_POST)
$to = isset($_POST['to']) && $_POST['to'] ? $_POST['to'] : $to;
$note = isset($_POST['note']) ? $_POST['note'] : $note;
$cloneacls = (isset($_POST['cloneacls']))? 'checked="checked"' : '';
if (!preg_match(VALID_PAGENAME_PATTERN, $to)) //TODO use central regex library
$box = '""<em class="error">'.ERROR_INVALID_PAGENAME.'</em>""';
$box = '""<em class="error">'.sprintf(ERROR_ACL_WRITE, $to).'</em>""';
$box = '""<em class="error">'.ERROR_PAGE_ALREADY_EXIST.'</em>""';
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']);
$this->Redirect($this->href('edit', $to));
$box = '""<em class="success">'.sprintf(CLONE_SUCCESSFUL, $to).'</em>""';
$form .= '<table class="clone">'."\n".
'<tr>'."\n".
'<td>'.sprintf(CLONE_X_TO, $this->Link($this->GetPageTag())).'</td>'."\n".
'<td><input type="text" name="to" value="'.$to.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td>'.EDIT_NOTE.'</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td></td>'."\n".
'<td>'."\n".
'<input type="checkbox" name="editoption" '.$editoption.' id="editoption" /><label for="editoption">'.LABEL_EDIT_OPTION.'</label><br />'."\n".
'<input type="checkbox" name="cloneacls" '.$cloneacls.' id="cloneacls" checked /><label for="cloneacls">'.LABEL_CLONEACLS_OPTION.'</label><br /><br />'."\n".
'<input type="submit" name="create" value="'.LABEL_CLONE.'" />'."\n".
'</td>'."\n".
'</tr>'."\n".
'</table>'."\n";
CategoryDevelopmentHandlers


Revision [15849]

Edited on 2007-01-05 03:46:50 by AndreasHeintze [Reverted]
Additions:
(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.
Deletions:
(I also fixed what I thought was a bug, it's now possible to not a blank note for the cloned page.)


Revision [15848]

Edited on 2007-01-05 03:45:01 by AndreasHeintze [Added ACL cloning]
Additions:
$box = '""<div class="error">Sorry! The destination page already exists</div>""'; #i18n
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 not a blank note for the cloned page.)
/AndreasHeintze
* @author {@link http://wikkawiki.org/ChristianBarthelemy Christian Barthelemy} - original idea and code.
* @author {@link http://wikkawiki.org/DarTar Dario Taraborelli} - bugs fixed, code improved, removed popup alerts.
* @since Wikka 1.1.6.0
// defaults
if(!defined('VALID_PAGENAME_PATTERN')) define ('VALID_PAGENAME_PATTERN', '/^[A-Za-zÄÖÜßäöü]+[A-Za-z0-9ÄÖÜßäöü]*$/s');
// i18n
define('CLONE_HEADER', '==== Clone current page ====');
define('CLONE_SUCCESSFUL', '%s was succesfully created!');
define('CLONE_X_TO', 'Clone %s to:');
define('CLONED_FROM', 'Cloned from %s');
define('EDIT_NOTE', 'Edit note:');
define('ERROR_ACL_READ', 'You are not allowed to read the source of this page.');
define('ERROR_ACL_WRITE', 'Sorry! You don\'t have write-access to %s');
define('ERROR_INVALID_PAGENAME', 'This page name is invalid. Valid page names must start with a letter and contain only letters and numbers.');
define('ERROR_PAGE_ALREADY_EXIST', 'Sorry, the destination page already exists');
define('ERROR_PAGE_NOT_EXIST', ' Sorry, page %s does not exist.');
define('LABEL_CLONE', 'Clone');
define('LABEL_EDIT_OPTION', ' Edit after creation ');
define('LABEL_CLONEACLS_OPTION', ' Clone ACLs ');
define('PLEASE_FILL_VALID_TARGET', 'Please fill in a valid target ""PageName"" and an (optional) edit note.');
// initialization
$note = sprintf(CLONED_FROM, $from);
$cloneacls = '';
$box = PLEASE_FILL_VALID_TARGET;
echo $this->Format(CLONE_HEADER);
$box = sprintf(ERROR_PAGE_NOT_EXIST, $from);
$box = ERROR_ACL_READ;
if (isset($_POST) && $_POST)
$to = isset($_POST['to']) && $_POST['to'] ? $_POST['to'] : $to;
$note = isset($_POST['note']) ? $_POST['note'] : $note;
$cloneacls = (isset($_POST['cloneacls']))? 'checked="checked"' : '';
if (!preg_match(VALID_PAGENAME_PATTERN, $to)) //TODO use central regex library
$box = '""<em class="error">'.ERROR_INVALID_PAGENAME.'</em>""';
$box = '""<em class="error">'.sprintf(ERROR_ACL_WRITE, $to).'</em>""';
$box = '""<em class="error">'.ERROR_PAGE_ALREADY_EXIST.'</em>""';
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']);
$this->Redirect($this->href('edit', $to));
$box = '""<em class="success">'.sprintf(CLONE_SUCCESSFUL, $to).'</em>""';
$form .= '<table class="clone">'."\n".
'<tr>'."\n".
'<td>'.sprintf(CLONE_X_TO, $this->Link($this->GetPageTag())).'</td>'."\n".
'<td><input type="text" name="to" value="'.$to.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td>'.EDIT_NOTE.'</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" maxlength="75" /></td>'."\n".
'</tr>'."\n".
'<tr>'."\n".
'<td></td>'."\n".
'<td>'."\n".
'<input type="checkbox" name="editoption" '.$editoption.' id="editoption" /><label for="editoption">'.LABEL_EDIT_OPTION.'</label><br />'."\n".
'<input type="checkbox" name="cloneacls" '.$cloneacls.' id="cloneacls" checked /><label for="cloneacls">'.LABEL_CLONEACLS_OPTION.'</label><br /><br />'."\n".
'<input type="submit" name="create" value="'.LABEL_CLONE.'" />'."\n".
'</td>'."\n".
'</tr>'."\n".
'</table>'."\n";
Deletions:
$box = '""<div class="error">Sorry! The destination page already exists</div>""'; #i18n


Revision [8573]

Edited on 2005-05-28 11:12:31 by JavaWoman [move to subcategory]
Additions:
====Cloning an existing WikiPage====
{{lastedit}}

<<This handler is included in WikkaWiki since version 1.1.6.0<<>>==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 of 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)
<div class="page">
<?php
/**
* Clone the current page and save a copy of it as a new page.
*
* Usage: append /clone to the URL of the page you want to clone
*
* This handler checks the existence of the source page, the validity of the
* name of the target page to be created, the user's read-access to the source
* page and write-access to the target page.
* If the edit option is selected, the user is redirected to the target page for
* edition immediately after its creation.
*
* @package Handlers
* @subpackage
* @name clone
*
* @author {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy} - original idea and code.
* @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - bugs fixed, code improved, removed popup alerts.
* @version 0.4
* @since Wikka 1.1.6.X
*
* @input string $to required: the page to be created
* must be a non existing page and current user must be authorized to create it
* default is source page name
*
* @input string $note optional: the note to be added to the page when created
* default is "Cloned from " followed by the name of the source page
*
* @input boolean $editoption optional: if true, the new page will be opened for edition on creation
* default is false (to allow multiple cloning of the same source)
*
* @todo Use central library for valid pagenames.
*
*/

// set defaults
$from = $this->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 = '""<div class="error">You must specify a valid PageName</div>""'; #i18n
} else
{
// 4. target page name is valid - now check user's write-access
if (!$this->HasAccess('write', $to))
{
$box = '""<div class="error">Sorry! You don\'t have write-access to '.$to.'</div>""'; #i18n
} else
{
// 5. check target page existence
if ($this->ExistsPage($to))
{
// page already exists!
$box = '""<div class="error">Sorry! The destination page already exists</div>""'; #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 .= '<table class="clone">'.
'<tr>'.
'<td><strong>Clone '.$this->Link($this->GetPageTag()).' to:</strong></td>'.
'<td><input type="text" name="to" value="'.$to.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td><strong>Edit note:</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td></td>'.
'<td>'.
'<input type="checkbox" name="editoption" '.$editoption.' /> Edit after creation '.
'<input type="submit" name="create" value="Clone" />'.
'</td>'.
'</tr>'.
'</table>';
$form .= $this->FormClose();
}
}

// display messages
if (isset($box)) echo $this->Format(' --- '.$box.' --- --- ');
// print form
if (isset($form)) print $form;
?>
</div>


%%



----
CategoryDevelopmentHandlers
Deletions:
====Cloning an existing WikiPage====
{{lastedit}}

<<This handler is included in WikkaWiki since version 1.1.6.0<<>>==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 of 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)
<div class="page">
<?php
/**
* Clone the current page and save a copy of it as a new page.
*
* Usage: append /clone to the URL of the page you want to clone
*
* This handler checks the existence of the source page, the validity of the
* name of the target page to be created, the user's read-access to the source
* page and write-access to the target page.
* If the edit option is selected, the user is redirected to the target page for
* edition immediately after its creation.
*
* @package Handlers
* @subpackage
* @name clone
*
* @author {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy} - original idea and code.
* @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - bugs fixed, code improved, removed popup alerts.
* @version 0.4
* @since Wikka 1.1.6.X
*
* @input string $to required: the page to be created
* must be a non existing page and current user must be authorized to create it
* default is source page name
*
* @input string $note optional: the note to be added to the page when created
* default is "Cloned from " followed by the name of the source page
*
* @input boolean $editoption optional: if true, the new page will be opened for edition on creation
* default is false (to allow multiple cloning of the same source)
*
* @todo Use central library for valid pagenames.
*
*/

// set defaults
$from = $this->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 = '""<div class="error">You must specify a valid PageName</div>""'; #i18n
} else
{
// 4. target page name is valid - now check user's write-access
if (!$this->HasAccess('write', $to))
{
$box = '""<div class="error">Sorry! You don\'t have write-access to '.$to.'</div>""'; #i18n
} else
{
// 5. check target page existence
if ($this->ExistsPage($to))
{
// page already exists!
$box = '""<div class="error">Sorry! The destination page already exists</div>""'; #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 .= '<table class="clone">'.
'<tr>'.
'<td><strong>Clone '.$this->Link($this->GetPageTag()).' to:</strong></td>'.
'<td><input type="text" name="to" value="'.$to.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td><strong>Edit note:</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td></td>'.
'<td>'.
'<input type="checkbox" name="editoption" '.$editoption.' /> Edit after creation '.
'<input type="submit" name="create" value="Clone" />'.
'</td>'.
'</tr>'.
'</table>';
$form .= $this->FormClose();
}
}

// display messages
if (isset($box)) echo $this->Format(' --- '.$box.' --- --- ');
// print form
if (isset($form)) print $form;
?>
</div>


%%



----
CategoryDevelopment


Revision [4408]

Edited on 2005-01-11 09:09:26 by DarTar [Adding link]
Additions:
<<This handler is included in WikkaWiki since version 1.1.6.0<<>>==See also:==
Documentation: CloneHandlerInfo.>>
This is the development page for the clone handler.::c::


Revision [4085]

Edited on 2005-01-06 19:06:48 by ChristianBarthelemy [Adding link]
Additions:
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
Deletions:
Any ideas around this handler more than welcome.


Revision [3919]

Edited on 2005-01-03 13:22:53 by ChristianBarthelemy [Removed original code - only leave the one improved by DarTar]
Additions:
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.
Deletions:
Copy this code into a handler named: clone.php - place it in the handlers/page folder.
* Clone a "template" page to a new named page
* Usage: type in /clone at the end of the URL you want to clone
* This handler checks 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.
* @author {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy}
* @version 0.2
* @since Wikka 1.1.6.x
* @input string $to mandatory: the page to be created
* must be a non existing page and current user must be authorized to create
* 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 *****
if ($_POST)
// ***** GET PARAMETERS *****
$from = $this->GetPageTag();
$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.";
$this->SetMessage($errormessage);
$this->Redirect($this->Href());
}
// 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)
// the page to be duplicated 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.";
$this->SetMessage($errormessage);
$this->Redirect($this->Href());
else
// this is what happens if the user tries to duplicate a non existing page...
$errormessage = "The template page does not exist.";
$this->SetMessage($errormessage);
$this->Redirect($this->Href());
// 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...
$message=$to." has been created from ".$from." and note was: ".$note;
if ($editoption)
$this->redirect(BASE_URL.$to."/edit"); // drops the user to the new page for edition if he choosed so...
else
$this->SetMessage($message);
$this->Redirect($this->Href()); // ...or back where he comes from.
// ***** 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.";
$this->SetMessage($errormessage);
$this->Redirect($this->Href());
}
// ***** END TEMPLATING PROCESS *****
else
// ***** INPUT FORM *****
print $this->FormOpen("clone");
echo '<table class="clone" border="1">';
echo ' <tr>';
echo ' <th align="left">WikiPage to be created:</th>';
echo ' <td><input type="text" name="to" size="37">'.$to.'</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th align="left">Added note to your edit:</th>';
echo ' <td><input type="text" name="note" size="37">'.$note.'</td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th align="left">Do you want to edit the new page when created?</th>';
echo ' <td>';
echo ' <table border="1">';
echo ' <tr>';
echo ' <td><input type="checkbox" name="editoption" value="false">Edit after creation'.$editoption.'</td>';
echo ' <td><input type="submit" name="create" value="Clone it now"></td>';
echo ' </tr>';
echo ' </table>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
print $this->FormClose();
// ***** END INPUT FORM *****


Revision [3867]

Edited on 2004-12-31 17:52:15 by DarTar [posting clone handler v0.4]
Additions:
* Clone the current page and save a copy of it as a new page.
* Usage: append /clone to the URL of the page you want to clone
* @version 0.4
* @since Wikka 1.1.6.X
* must be a non existing page and current user must be authorized to create it
* default is source page name
* @input boolean $editoption optional: if true, the new page will be opened for edition on creation
* default is false (to allow multiple cloning of the same source)
* @todo Use central library for valid pagenames.
$editoption = '';
echo $this->Format('==== Clone current page ====');
if (!$this->HasAccess('read', $from))
// user can't read source page!
$to = ($_POST['to'])? $_POST['to'] : $to;
$note = ($_POST['note'])? $_POST['note'] : $note;
$editoption = (isset($_POST['editoption']))? 'checked="checked"' : '';
if (!preg_match("/^[A-ZÄÖÜ]+[a-zßäöü]+[A-Z0-9ÄÖÜ][A-Za-z0-9ÄÖÜßäöü]*$/s", $to))
if (!$this->HasAccess('write', $to))
if ($thepage) $pagecontent = $thepage['body']; # get its content
if ($editoption == 'checked="checked"')
// quick edit
// show confirmation message
$box = '""'.$this->MiniHref('',$to).'"" was succesfully created!'; #i18n
$form = $this->FormOpen('clone');
'<input type="checkbox" name="editoption" '.$editoption.' /> Edit after creation '.
'</table>';
if (isset($box)) echo $this->Format(' --- '.$box.' --- --- ');
Deletions:
* Clones the current page and saves a copy of it as a new page.
* Usage: append /clone to the URL of the page you want to clone.
* @version 0.3
* default is true
* @todo Use central function for valid pagenames.
$editoption = 'checked';
echo $this->Format('==== Clone current page ===='); #i18n
if (!$this->HasAccess("read", $from))
// user can't read page!
$to = ($_POST["to"])? $_POST["to"] : $to;
$note = ($_POST["note"])? $_POST["note"] : $note;
$editoption = (isset($_POST['checkbox[0]']))? "checked" : "";
if (!preg_match("/^[A-Z,a-z,ƒ÷‹,?‰ˆ¸]+[A-Z,a-z,0-9,ƒ÷‹,?‰ˆ¸]*$/s", $to))
if (!$this->HasAccess("write", $to))
if ($thepage) $pagecontent = $thepage["body"]; # get its content
if ($editoption == 'checked')
$box = 'Page '.$to.' was succesfully created!'; #i18n
$form = $this->FormOpen("clone");
'<input type="checkbox" name="editoption" checked="'.$editoption.'" /> Edit after creation '.
'</table>'; #i18n
if (isset($box)) echo $this->Format(" --- ".$box." --- --- ");


Revision [3665]

Edited on 2004-12-25 19:13:15 by DarTar [Improved CloneHandler with bugfixes/changes]
Additions:
Christian, your code had a number of bugs that I've fixed in a new version of the handler posted below.
Deletions:
Christian, your code had a number of bugs that I've fixed in a new version of the handler that I've posted below.


Revision [3664]

Edited on 2004-12-25 19:10:28 by DarTar [Improved CloneHandler with bugfixes/changes]
Additions:
$editoption = (isset($_POST['checkbox[0]']))? "checked" : "";
Deletions:
$editoption = ($_POST["editoption"])? $_POST["editoption"] : $editoption;


Revision [3663]

Edited on 2004-12-25 19:01:03 by DarTar [Improved CloneHandler with bugfixes/changes]
Additions:
$editoption = 'checked';
if ($editoption == 'checked')
'<input type="checkbox" name="editoption" checked="'.$editoption.'" /> Edit after creation '.
Deletions:
$editoption = 'true';
if ($editoption == 'true')
'<input type="checkbox" name="editoption" value="'.$editoption.'" /> Edit after creation '.


Revision [3662]

Edited on 2004-12-25 18:55:08 by DarTar [Improved CloneHandler with bugfixes/changes]
Additions:
echo $this->Format('==== Clone current page ===='); #i18n
'</table>'; #i18n
Deletions:
echo $this->Format('==== Clone current page ====');
'</table>';


Revision [3661]

Edited on 2004-12-25 18:54:16 by DarTar [Improved CloneHandler with bugfixes/changes]
Additions:
// set defaults
Deletions:
// ***** SET DEFAULTS *****


Revision [3656]

Edited on 2004-12-25 18:40:54 by DarTar [New CloneHandler with bugfixes/changes]
Additions:
* Usage: append /clone to the URL of the page you want to clone.
Deletions:
* Usage: append /clone to the URL of the page you want to clone


Revision [3655]

Edited on 2004-12-25 18:38:42 by DarTar [New CloneHandler with bugfixes/changes]
Additions:
{{lastedit}}
====Improved CloneHandler====
Christian, your code had a number of bugs that I've fixed in a new version of the handler that I've 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
* Clones the current page and saves a copy of it as a new page.
* Usage: append /clone to the URL of the page you want to clone
* This handler checks the existence of the source page, the validity of the
* name of the target page to be created, the user's read-access to the source
* page and write-access to the target page.
* If the edit option is selected, the user is redirected to the target page for
* edition immediately after its creation.
* @author {@link http://wikka.jsnx.com/ChristianBarthelemy Christian Barthelemy} - original idea and code.
* @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - bugs fixed, code improved, removed popup alerts.
* @version 0.3
* @input string $to required: the page to be created
* default is "Cloned from " followed by the name of the source page
* default is true
* @todo Use central function for valid pagenames.
*
// ***** SET DEFAULTS *****
$from = $this->tag;
$to = $this->tag;
$note = 'Cloned from '.$from; #i18n
$editoption = 'true';
$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 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 = ($_POST["editoption"])? $_POST["editoption"] : $editoption;
// 3. check target pagename validity
if (!preg_match("/^[A-Z,a-z,ƒ÷‹,?‰ˆ¸]+[A-Z,a-z,0-9,ƒ÷‹,?‰ˆ¸]*$/s", $to))
// invalid pagename!
$box = '""<div class="error">You must specify a valid PageName</div>""'; #i18n
} else
// 4. target page name is valid - now check user's write-access
if (!$this->HasAccess("write", $to))
$box = '""<div class="error">Sorry! You don\'t have write-access to '.$to.'</div>""'; #i18n
} else
// 5. check target page existence
if ($this->ExistsPage($to))
{
// page already exists!
$box = '""<div class="error">Sorry! The destination page already exists</div>""'; #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 == 'true')
{
$this->Redirect($this->href('edit',$to));
} else
{
$box = 'Page '.$to.' was succesfully created!'; #i18n
}
}
// build form
$form = $this->FormOpen("clone");
$form .= '<table class="clone">'.
'<tr>'.
'<td><strong>Clone '.$this->Link($this->GetPageTag()).' to:</strong></td>'.
'<td><input type="text" name="to" value="'.$to.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td><strong>Edit note:</strong></td>'.
'<td><input type="text" name="note" value="'.$note.'" size="37" /></td>'.
'</tr>'.
'<tr>'.
'<td></td>'.
'<td>'.
'<input type="checkbox" name="editoption" value="'.$editoption.'" /> Edit after creation '.
'<input type="submit" name="create" value="Clone" />'.
'</td>'.
'</tr>'.
'</table>';
$form .= $this->FormClose();
// display messages
if (isset($box)) echo $this->Format(" --- ".$box." --- --- ");
// print form
if (isset($form)) print $form;


Revision [3641]

Edited on 2004-12-24 18:25:04 by ChristianBarthelemy [CloneHandler needs ExistsPage function from JavaWoman]
Additions:
===Dependancy===
This relies of the ExistsPage function developed by JavaWoman and part of release 1.1.6.0. You can find this version at WikkaDevelopment.


Revision [3502]

Edited on 2004-12-19 22:56:17 by JavaWoman [added remark about MovePages; changed @since]
Additions:
* @since Wikka 1.1.6.x
~''See MovePages - not implemented, it has its problems; but the wish/requirement isn't new ;-) --JavaWoman''
Deletions:
* @since Wikka 1.1.5.3


Revision [3493]

The oldest known version of this page was created on 2004-12-19 22:05:30 by ChristianBarthelemy [added remark about MovePages; changed @since]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki