1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File DeleteVersionsAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

18
49
4
1
158
87
17
0.35
12.25
4
4.25

Classes

Class Line # Actions
DeleteVersionsAction 35 49 0% 17 24
0.661971866.2%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.suigeneris.jrcs.rcs.Version;
24   
25    import com.xpn.xwiki.XWikiContext;
26    import com.xpn.xwiki.XWikiException;
27    import com.xpn.xwiki.doc.XWikiDocument;
28    import com.xpn.xwiki.doc.XWikiDocumentArchive;
29   
30    /**
31    * Struts action for deleting document versions.
32    *
33    * @version $Id: 6fbdb4863db11c5f77a06fc29053c733ab094655 $
34    */
 
35    public class DeleteVersionsAction extends XWikiAction
36    {
 
37  2 toggle @Override
38    public boolean action(XWikiContext context) throws XWikiException
39    {
40  2 DeleteVersionsForm form = (DeleteVersionsForm) context.getForm();
41  2 if (!form.isConfirmed() || !csrfTokenCheck(context)) {
42  0 return true;
43    }
44   
45  2 XWikiDocument doc = context.getDoc();
46  2 String language = form.getLanguage();
47  2 XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
48  2 XWikiDocumentArchive archive = tdoc.getDocumentArchive(context);
49   
50    // Get the versions
51  2 Version[] versions = getVersionsFromForm(form, archive);
52  2 Version v1 = versions[0];
53  2 Version v2 = versions[1];
54   
55  2 if (v1 != null && v2 != null) {
56    // Remove the versions
57  2 archive.removeVersions(v1, v2, context);
58  2 context.getWiki().getVersioningStore().saveXWikiDocArchive(archive, true, context);
59  2 tdoc.setDocumentArchive(archive);
60   
61    // Is this the last remaining version? If so, then recycle the document.
62  2 if (archive.getLatestVersion() == null) {
63  0 if (StringUtils.isEmpty(language) || language.equals(doc.getDefaultLanguage())) {
64  0 context.getWiki().deleteAllDocuments(doc, context);
65    } else {
66    // Only delete the translation
67  0 context.getWiki().deleteDocument(tdoc, context);
68    }
69    } else {
70    // There are still some versions left.
71    // If we delete the most recent (current) version, then rollback to latest undeleted version.
72  2 if (!tdoc.getRCSVersion().equals(archive.getLatestVersion())) {
73  2 XWikiDocument newdoc = archive.loadDocument(archive.getLatestVersion(), context);
74    // Reset the document reference, since the one taken from the archive might be wrong (old name from
75    // before a rename)
76  2 newdoc.setDocumentReference(tdoc.getDocumentReference());
77   
78    // Get rid of objects that don't exist in new version
79  2 newdoc.addXObjectsToRemoveFromVersion(tdoc);
80   
81    // Make sure we don't create a new rev!
82  2 newdoc.setMetaDataDirty(false);
83  2 newdoc.setContentDirty(false);
84   
85    // Make sure the previous current document is seen as original document of
86    // the new current document for comparisons
87  2 newdoc.setOriginalDocument(tdoc.getOriginalDocument());
88   
89    // Update the database with what is now the current document
90  2 context.getWiki().saveDocument(newdoc, newdoc.getComment(), context);
91  2 context.setDoc(newdoc);
92    }
93    }
94    }
95  2 sendRedirect(context);
96  2 return false;
97    }
98   
99    /**
100    * @param form the {@link DeleteVersionsForm} which to extract versions from
101    * @param archive the document archive used to resolve pseudoversions, if needed
102    * @return an array of versions to use as interval for deletion, regardless if "rev1" and "rev2" were passed
103    * individually or if just "rev" was used
104    */
 
105  2 toggle private Version[] getVersionsFromForm(DeleteVersionsForm form, XWikiDocumentArchive archive)
106    {
107    // Determine if we used rev or rev1&rev2.
108  2 String[] versions = new String[2];
109  2 if (form.getRev() == null) {
110  2 versions[0] = form.getRev1();
111  2 versions[1] = form.getRev2();
112    } else {
113  0 versions[0] = form.getRev();
114  0 versions[1] = form.getRev();
115    }
116   
117    // Convert to Version objects.
118  2 Version[] result = new Version[2];
119  6 for (int i = 0; i < versions.length; i++) {
120    // Support for the "latest" and "previous" pseudoversions.
121  4 if ("latest".equals(versions[i])) {
122  4 result[i] = archive.getLatestVersion();
123  0 } else if ("previous".equals(versions[i])) {
124  0 Version currentVersion = archive.getLatestVersion();
125  0 result[i] = archive.getPrevVersion(currentVersion);
126    } else {
127    // Just use the given value.
128  0 try {
129  0 result[i] = new Version(versions[i]);
130    } catch (Exception e) {
131    // Protect against invalid versions.
132  0 result[i] = null;
133    }
134    }
135    }
136   
137  2 return result;
138    }
139   
140    /**
141    * redirect back to view history.
142    *
143    * @param context used in redirecting
144    * @throws XWikiException if any error
145    */
 
146  2 toggle private void sendRedirect(XWikiContext context) throws XWikiException
147    {
148    // forward to view
149  2 String redirect = Utils.getRedirect("view", "viewer=history", context);
150  2 sendRedirect(context.getResponse(), redirect);
151    }
152   
 
153  0 toggle @Override
154    public String render(XWikiContext context) throws XWikiException
155    {
156  0 return "deleteversionsconfirm";
157    }
158    }