MwDelDocs

Last modified by Site Administrator on 2015/03/01 02:58

import com.xpn.xwiki.doc.XWikiAttachment;    
import com.xpn.xwiki.doc.XWikiDocument;

/////////////////////////////////
restoreRecycleBin("mwtest");
/////////////////////////////////

def restoreRecycleBin(wik) {
    xcontext.setDatabase(wik);
    def workWiki = xcontext.getXWiki();
   try {
// The query below is originally from
// http://extensions.xwiki.org/xwiki/bin/view/Extension/Deleted+Documents suggesting:
// "select distinct ddoc.fullName from XWikiDeletedDocument as ddoc where ddoc.fullName not in (select doc.fullName from XWikiDocument as doc) order by 1 asc"
       def docsResult = runQuery("select distinct ddoc.XDD_FULLNAME,ddoc.XDD_LANGUAGE from "
 + wik
 //TODO: to be entirely correct, don't restore documents already existing but with some
 //other doc.XWD_LANGUAGE==ddoc.XDD_LANGUAGE ..
 + ".xwikirecyclebin as ddoc where ddoc.XDD_FULLNAME not in (select doc.XWD_FULLNAME from "
 + wik
 + ".xwikidoc as doc) order by 1 asc"
 );

       if (docsResult.size() > 0) {
           for (doc in docsResult) {
                def docName = doc.get("XDD_FULLNAME");
                def docLang = doc.get("XDD_LANGUAGE");
                workWiki.getDeletedDocuments(docName, docLang ? docLang : "", xcontext.context).each{
                   try { // attempt to parallel what happens w/ http://maven.xwiki.org/site/clover/20101017/com/xpn/xwiki/web/UndeleteAction.html
println "RecycleBin: " + it.fullName + " " + it.language + " - " + it.getDate() + " - " + it.id;
                       // http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/doc/XWikiDeletedDocument.html#restoreDocument(com.xpn.xwiki.doc.XWikiDocument, com.xpn.xwiki.XWikiContext)
                       //def restored = null;
                       def restored = it.restoreDocument(null, xcontext.context);
                        println "Restoring:  " + it.fullName + " " + restored;

                        workWiki.saveDocument(restored, "MwDelDocs did restoration from recycle bin", xcontext.context);

                       // see http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core/apidocs/com/xpn/xwiki/store/XWikiHibernateRecycleBinStore.html
                       workWiki.getRecycleBinStore().deleteFromRecycleBin(restored, it.id, xcontext.context, true); //true-->create new Transaction each restore
                       println "restored:   " + it.fullName + " " + restored;

                       // Save attachments    
                       List<XWikiAttachment> attachlist = restored.getAttachmentList();    
                       if (attachlist.size() > 0) {    
                           for (XWikiAttachment attachment : attachlist) {    
println "Document: " + it.fullName + " Saving Attachment: " + attachment + " ...";
                               // Do not increment attachment version    
                               attachment.setMetaDataDirty(false);    
                                attachment.getAttachment_content().setContentDirty(false);    
                                workWiki.getAttachmentStore().saveAttachmentContent(attachment, false, xcontext.context, true);    
println "Document: " + it.fullName + " Saved Attachment: " + attachment;
                           }
                       }
                   } catch (Exception e) {
                        println "Error in Each-Loop: " + e + " " + it.fullName;
                   }
               }
           }
       }
   } catch (Exception e) {
        println "showDocBin() Error: " + e;
   } finally {
        xcontext.setDatabase(xcontext.getOriginalDatabase());
   }
}

def runQuery(query) {
    def context2   = xcontext.getContext(); //document version
//  def context2   = xwiki.getXWikiContext(); //console version
   def store      = xcontext.getXWiki().getStore().getStore();
    store.beginTransaction(context2);
    def session    = store.getSession(context2);
    def connection = session.connection();
    def stmt       = connection.createStatement();
    def list       = new ArrayList()
try {
   def resultset = stmt.executeQuery(query);
   def mdata     = resultset.getMetaData();
   def nbcols    = mdata.getColumnCount();
   while (true) {
resultset.next();
def map = new HashMap()
   for (i=1;i<=nbcols;i++) {
def value = resultset.getObject(i);
if ((value==null)||(value==""))
   value = ""
map.put(mdata.getColumnName(i), value)
}
list.add(map);
if (resultset.isLast())         
   break;
   }
   stmt.close();
   return list;
} catch (Exception e2) {
   return "Error running sql: ${query} " + e2.getMessage();
} finally {
   stmt.close();
}
}
Tags:
Created by Site Administrator on 2012/06/16 10:26
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4.4 - Documentation