Another minor issue, not important enough to log as a bug I think - but still something
worth having a workaround for:
When you use this method to create the document, it creates it in cache only.
In order to actually see the document (for example, if it is an article that normally
shows on front page); you have to edit the new document and save it manually.
If you flush cache before doing this, it does not delete the document, but it does lose
all content including objects attached
I was thinking, is this save-required problem perhaps why the attachments raise an error?
As the document is cached, not created directly?
I found a workaround in using a timeout to pass the user into an edit page, although
certainly not a good workaround, I could not think of anything that had less reliance on
the user. I have included the code I used, as I'm sure there is a better way - if
not, I'm sure someone out there will find a use for it too.
To add a "Clone Document" feature to the "More Actions" menu:
Edit View Header (style field or viewheader.vm file) and add the following (suggest it be
just above the Help details):
[notes - the "if" clause should be modified to add the fullName of the clone
document - this makes sure you don't get recursive cloning.]
___________________________________
<div class="xwikimenutitle">Other</div>
#if("Main.cloneDocument" != $doc.fullName)
#xwikiitem($xwiki.getURL("Main.cloneDocument", "view",
"cloneweb=${doc.web}&clonedoc=${doc.name}&cloneparent=${doc.parent}&doclone=true&editaction=${editaction}")
"Clone Content")
#end
___________________________________
Create a new document Main.cloneDocument:
___________________________________
#set($sOldWeb=$request.cloneweb)
#set($sOldDoc=$request.clonedoc)
#set($sOldParent=$request.cloneparent)
#set($sDoClone=$request.doclone)
#set($sCloneEditType=$request.editaction)
1.1 Clone a document.
This will copy a document and all it's related content into a new document.
#if("true" == $sDoClone)
<form action="$doc.getURL("view")" id="newdoc">
<input type="hidden" name="oldparent"
value="${sOldParent}">
<input type="hidden" name="oldwebname"
value="${sOldWeb}" size="8">
<input type="hidden" name="olddocname"
value="${sOldDoc}" size="8">
<input type="hidden" name="edittype"
value="${sCloneEditType}" size="8">
Name: <input type="text" name="name" value="" />
<input type="button" value="Create" onclick='if
(updateName(this.form.name)) {this.form.submit(); }'>
</form>
#elseif($request.getParameter("oldwebname"))
#set($sOldWeb=$request.getParameter("oldwebname"))
#set($sOldDoc=$request.getParameter("olddocname"))
#set($sNewDoc=$request.getParameter("name"))
#set($sEditType=$request.getParameter("edittype"))
#set($sourceDocName="${sOldWeb}.${sOldDoc}")
#set($targetDocName="${sOldWeb}.${sNewDoc}")
$xwiki.copyDocument($sourceDocName, $targetDocName)
* *Clone Prepaired.*
* *You will be taken to the edit facility for this document shortly. Saving the document
is a required step in the Cloning process.*
* *The new document can also be edited here: <a
href="../../${sEditType}/${sOldWeb}/${sNewDoc}">Click Here</a>*
<script language="javascript">
setTimeout(
"document.location.href='../../${sEditType}/${sOldWeb}/${sNewDoc}'",
2000 );
</script>
#else
* *No document information. Please try again, from the "More Actions" menu
while viewing the document you wish to Clone.*
#end
___________________________________
________________________________
From: BOUSQUET Jeremie [mailto:Jeremie.BOUSQUET@gemalto.com]
Sent: 04 September 2006 17:16
To: xwiki-users(a)objectweb.org
Subject: RE: [xwiki-users] "Clone" another documents objects when creating a new
document
I just tested on my side, I have the same problem than you, the attachments copy fails. In
fact it seems that attachments creation in the new page fails. (I only tested once)
I'm certain that I could have it work during my first tests of page copy. I don't
have time to test again, but I think sometimes it works, sometimes it doesn't ...
In my case the attached files are listed in the copied page, but if you click on one you
get an exception. And I can't delete the page at all for the same reason (database
exception, a row cannot be found in the attachments table).
By the way I had some problems with attachments, apart of pages copy, but with the same
exceptions returned, and in a random fashion ...
________________________________
From: Esbach, Brandon [mailto:Esbachb@tycoelectronics.com]
Sent: lundi 4 septembre 2006 10:24
To: xwiki-users(a)objectweb.org
Subject: RE: [xwiki-users] "Clone" another documents objects when creating a
new document
Attachments don't seem to copy with this method - I get a wrapped exception error:
The size of the attachment is around 20k.
Comments are fine, as are other objects (at least the one's I've tried).
________________________________
From: Esbach, Brandon [mailto:Esbachb@tycoelectronics.com]
Sent: 01 September 2006 10:31
To: xwiki-users(a)objectweb.org
Subject: RE: [xwiki-users] "Clone" another documents objects when creating a
new document
That seems to do it perfectly, thanks!
________________________________
From: BOUSQUET Jeremie [mailto:Jeremie.BOUSQUET@gemalto.com]
Sent: 01 September 2006 09:45
To: xwiki-users(a)objectweb.org
Subject: RE: [xwiki-users] "Clone" another documents objects when creating a
new document
Hello,
I believe you can use the copyDocument() method. I use it to rename pages, I copy a
document to a newly named document, then delete the old one.
All objects and attachments are also copied in the process, so that may interest you (as
far as all objects are included in the template document I believe)
$xwiki.copyDocument($sourceDoc, $targetDoc)
Jeremie, Bousquet
GSE Solution CMO
Gemalto
Tel: +33 4 42 36 42 93
Avenue du Jujubier, Z.I Athelia IV
13705 La Ciotat, FRANCE
Jeremie.Bousquet(a)gemalto.com
www.gemalto.com <http://www.gemalto.com/>
________________________________
From: Esbach, Brandon [mailto:Esbachb@tycoelectronics.com]
Sent: vendredi 25 août 2006 12:58
To: xwiki-users(a)objectweb.org
Subject: [xwiki-users] "Clone" another documents objects when creating a new
document
Hi All,
Is there any way to clone all objects in an existing document to a different document
with one server request?
So that a template document can be maintained with default object values; and can be
brought in painlessly to a new document.
Brandon Esbach
Software Engineer
M/A-Com Eurotec Operations
LoughMahon Technology Park,
Skehard Road,
Blackrock,
Cork, Ireland
Tel +353 21 4808305