1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File DeleteVersionsAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

18
44
4
1
145
84
17
0.39
11
4
4.25

Classes

Class Line # Actions
DeleteVersionsAction 36 44 0% 17 18
0.7272727572.7%
 

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    import org.xwiki.refactoring.batch.BatchOperationExecutor;
25   
26    import com.xpn.xwiki.XWikiContext;
27    import com.xpn.xwiki.XWikiException;
28    import com.xpn.xwiki.doc.XWikiDocument;
29    import com.xpn.xwiki.doc.XWikiDocumentArchive;
30   
31    /**
32    * Struts action for deleting document versions.
33    *
34    * @version $Id: 8ea2ae7fe01f8895a97ac17aa1bb63701ac4234b $
35    */
 
36    public class DeleteVersionsAction extends XWikiAction
37    {
 
38  3 toggle @Override
39    public boolean action(XWikiContext context) throws XWikiException
40    {
41  3 DeleteVersionsForm form = (DeleteVersionsForm) context.getForm();
42  3 if (!form.isConfirmed() || !csrfTokenCheck(context)) {
43  0 return true;
44    }
45   
46  3 XWikiDocument doc = context.getDoc();
47  3 String language = form.getLanguage();
48  3 XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
49  3 XWikiDocumentArchive archive = tdoc.getDocumentArchive(context);
50   
51    // Get the versions
52  3 Version[] versions = getVersionsFromForm(form, archive);
53  3 Version v1 = versions[0];
54  3 Version v2 = versions[1];
55   
56  3 if (v1 != null && v2 != null) {
57    // Remove the versions
58  3 archive.removeVersions(v1, v2, context);
59  3 context.getWiki().getVersioningStore().saveXWikiDocArchive(archive, true, context);
60  3 tdoc.setDocumentArchive(archive);
61   
62    // Is this the last remaining version? If so, then recycle the document.
63  3 if (archive.getLatestVersion() == null) {
64    // Wrap the work as a batch operation.
65  0 BatchOperationExecutor batchOperationExecutor = Utils.getComponent(BatchOperationExecutor.class);
66  0 batchOperationExecutor.execute(() -> {
67  0 if (StringUtils.isEmpty(language) || language.equals(doc.getDefaultLanguage())) {
68  0 context.getWiki().deleteAllDocuments(doc, context);
69    } else {
70    // Only delete the translation
71  0 context.getWiki().deleteDocument(tdoc, context);
72    }
73    });
74    } else {
75    // There are still some versions left.
76    // If we delete the most recent (current) version, then rollback to latest undeleted version.
77  3 if (!tdoc.getRCSVersion().equals(archive.getLatestVersion())) {
78  3 context.getWiki().rollback(tdoc, archive.getLatestVersion().toString(), false, context);
79    }
80    }
81    }
82  3 sendRedirect(context);
83  3 return false;
84    }
85   
86    /**
87    * @param form the {@link DeleteVersionsForm} which to extract versions from
88    * @param archive the document archive used to resolve pseudoversions, if needed
89    * @return an array of versions to use as interval for deletion, regardless if "rev1" and "rev2" were passed
90    * individually or if just "rev" was used
91    */
 
92  3 toggle private Version[] getVersionsFromForm(DeleteVersionsForm form, XWikiDocumentArchive archive)
93    {
94    // Determine if we used rev or rev1&rev2.
95  3 String[] versions = new String[2];
96  3 if (form.getRev() == null) {
97  2 versions[0] = form.getRev1();
98  2 versions[1] = form.getRev2();
99    } else {
100  1 versions[0] = form.getRev();
101  1 versions[1] = form.getRev();
102    }
103   
104    // Convert to Version objects.
105  3 Version[] result = new Version[2];
106  9 for (int i = 0; i < versions.length; i++) {
107    // Support for the "latest" and "previous" pseudoversions.
108  6 if ("latest".equals(versions[i])) {
109  4 result[i] = archive.getLatestVersion();
110  2 } else if ("previous".equals(versions[i])) {
111  0 Version currentVersion = archive.getLatestVersion();
112  0 result[i] = archive.getPrevVersion(currentVersion);
113    } else {
114    // Just use the given value.
115  2 try {
116  2 result[i] = new Version(versions[i]);
117    } catch (Exception e) {
118    // Protect against invalid versions.
119  0 result[i] = null;
120    }
121    }
122    }
123   
124  3 return result;
125    }
126   
127    /**
128    * redirect back to view history.
129    *
130    * @param context used in redirecting
131    * @throws XWikiException if any error
132    */
 
133  3 toggle private void sendRedirect(XWikiContext context) throws XWikiException
134    {
135    // forward to view
136  3 String redirect = Utils.getRedirect("view", "viewer=history", context);
137  3 sendRedirect(context.getResponse(), redirect);
138    }
139   
 
140  0 toggle @Override
141    public String render(XWikiContext context) throws XWikiException
142    {
143  0 return "deleteversionsconfirm";
144    }
145    }