1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.wiki.workspacesmigrator.internal

File WorkspacesMigration.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

20
61
9
1
250
150
21
0.34
6.78
9
2.33

Classes

Class Line # Actions
WorkspacesMigration 56 61 0% 21 12
0.866666786.7%
 

Contributing tests

This file is covered by 4 tests. .

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 org.xwiki.wiki.workspacesmigrator.internal;
21   
22    import java.util.Iterator;
23    import java.util.LinkedList;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.WikiReference;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36   
37    import com.xpn.xwiki.XWiki;
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.XWikiException;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.BaseObject;
42    import com.xpn.xwiki.store.migration.DataMigrationException;
43    import com.xpn.xwiki.store.migration.XWikiDBVersion;
44    import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
45   
46    /**
47    * Migrator that restores pages previously removed by WorkspaceManager.Install and remove SearchConfigSources added
48    * by that script.
49    *
50    * @since 5.3RC1
51    * @version $Id: 4ca9f37fe1fde43cf38a4bc055076c0c558425b0 $
52    */
53    @Component
54    @Named("R530000WorkspacesMigration")
55    @Singleton
 
56    public class WorkspacesMigration extends AbstractHibernateDataMigration
57    {
58    private static final String WORKSPACE_CLASS_SPACE = "WorkspaceManager";
59   
60    private static final String WORKSPACE_CLASS_PAGE = "WorkspaceClass";
61   
62    @Inject
63    private WikiDescriptorManager wikiDescriptorManager;
64   
65    @Inject
66    private DocumentRestorerFromAttachedXAR documentRestorerFromAttachedXAR;
67   
68    @Inject
69    private SearchSuggestCustomConfigDeleter searchSuggestCustomConfigDeleter;
70   
71    @Inject
72    private Logger logger;
73   
 
74  4 toggle @Override
75    protected void hibernateMigrate() throws DataMigrationException, XWikiException
76    {
77    // Current wiki
78  4 String currentWikiId = wikiDescriptorManager.getCurrentWikiId();
79   
80    // Delete the search suggest config object
81  4 deleteSearchSuggestCustomConfig(currentWikiId);
82   
83    // If the wiki is a workspace
84  4 if (isWorkspace(currentWikiId)) {
85    // Restore the documents removed by WorkspaceManager.Install
86  3 restoreDeletedDocuments(currentWikiId);
87    }
88    }
89   
 
90  4 toggle private boolean isWorkspace(String wikiId) throws DataMigrationException, XWikiException
91    {
92    // The main wiki is not a workspace
93  4 if (wikiId.equals(wikiDescriptorManager.getMainWikiId())) {
94  0 return false;
95    }
96   
97    // Context, XWiki
98  4 XWikiContext context = getXWikiContext();
99  4 XWiki xwiki = context.getWiki();
100   
101    // Get the old wiki descriptor
102  4 DocumentReference oldWikiDescriptorReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
103    XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(wikiId)));
104  4 XWikiDocument oldWikiDescriptor = xwiki.getDocument(oldWikiDescriptorReference, context);
105   
106    // Try to get the old workspace object
107  4 DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
108    WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE);
109  4 BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument);
110   
111  4 return (oldObject != null) || isWorkspaceTemplate(wikiId);
112    }
113   
 
114  2 toggle private boolean isWorkspaceTemplate(String wikiId)
115    {
116    // Context, XWiki
117  2 XWikiContext context = getXWikiContext();
118  2 XWiki xwiki = context.getWiki();
119   
120    // In the first version of the Workspace Application, workspacetemplate did not have the workspace object.
121    // We test for the existence of XWiki.ManageWorkspace just to be sure that the workspacetemplate is a workspace.
122  2 return wikiId.equals("workspacetemplate") && xwiki.exists(new DocumentReference(wikiId,
123    "XWiki", "ManageWorkspace"), context);
124    }
125   
 
126  0 toggle @Override
127    public String getDescription()
128    {
129  0 return "https://jira.xwiki.org/browse/XWIKI-9738";
130    }
131   
 
132  29 toggle @Override
133    public XWikiDBVersion getVersion()
134    {
135    // XWiki 5.3, migration.
136  29 return new XWikiDBVersion(53000);
137    }
138   
139    /**
140    * The WorkspaceManager.Install script has added a new XWiki.SearchSuggestSourceClass object that we need to remove.
141    * If we don't remove it, it will cause a conflict in DW since we have added a new object in the standard
142    * distribution (see https://jira.xwiki.org/browse/XWIKI-9697).
143    *
144    * @param wikiId id of the wiki where the config should be removed
145    */
 
146  4 toggle private void deleteSearchSuggestCustomConfig(String wikiId) throws XWikiException
147    {
148  4 searchSuggestCustomConfigDeleter.deleteSearchSuggestCustomConfig(wikiId);
149    }
150   
151    /**
152    * The WorkspaceManager.Install script has removed some pages, that we need to restore.
153    * - XWiki.AdminRegistrationSheet
154    * - XWiki.RegistrationConfig
155    * - XWiki.RegistrationHelp
156    * - XWiki.AdminUsersSheet
157    *
158    * @param wikiId id of the wiki to upgrade
159    */
 
160  3 toggle private void restoreDeletedDocuments(String wikiId)
161    {
162  3 XWikiContext xcontext = getXWikiContext();
163  3 XWiki xwiki = xcontext.getWiki();
164   
165    // Create the list of documents to restore
166  3 List<DocumentReference> documentsToRestore = new LinkedList<DocumentReference>();
167  3 documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminRegistrationSheet"));
168  3 documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationConfig"));
169  3 documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationHelp"));
170  3 documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminUsersSheet"));
171   
172    // Remove from the list the document that already exists (so we don't need to restore them)
173  3 Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
174  15 while (itDocumentsToRestore.hasNext()) {
175  12 DocumentReference docRef = itDocumentsToRestore.next();
176  12 if (xwiki.exists(docRef, xcontext)) {
177  0 itDocumentsToRestore.remove();
178    }
179    }
180   
181    // If the list is empty, there is nothing to do
182  3 if (documentsToRestore.isEmpty()) {
183  0 return;
184    }
185   
186    // Try to restore from the workspace-template.xar
187  3 restoreDocumentsFromWorkspaceXar(documentsToRestore);
188   
189    // If the list is empty, the job is done
190  3 if (documentsToRestore.isEmpty()) {
191  0 return;
192    }
193   
194    // Try to copy these documents from the main wiki
195  3 restoreDocumentFromMainWiki(documentsToRestore);
196   
197    // If the list is empty, the job is done
198  3 if (!documentsToRestore.isEmpty()) {
199  3 String documentsToRestoreAsString = new String();
200  3 int counter = 0;
201  3 for (DocumentReference d : documentsToRestore) {
202  11 if (counter++ > 0) {
203  8 documentsToRestoreAsString += ", ";
204    }
205  11 documentsToRestoreAsString += d;
206    }
207  3 logger.warn("Failed to restore some documents: [{}]. You should import manually "
208    + "(1) xwiki-platform-administration-ui.xar and then (2) xwiki-platform-wiki-ui-wiki.xar into your"
209    + " wiki, to restore these documents.", documentsToRestoreAsString);
210    }
211    }
212   
 
213  3 toggle private void restoreDocumentsFromWorkspaceXar(List<DocumentReference> documentsToRestore)
214    {
215  3 DocumentReference installDocumentReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
216    WORKSPACE_CLASS_SPACE, "Install");
217  3 try {
218  3 documentRestorerFromAttachedXAR.restoreDocumentFromAttachedXAR(installDocumentReference,
219    "workspace-template.xar", documentsToRestore);
220    } catch (XWikiException e) {
221  1 logger.error("Error while restoring documents from the Workspace XAR", e);
222    }
223    }
224   
 
225  3 toggle private void restoreDocumentFromMainWiki(List<DocumentReference> documentsToRestore)
226    {
227  3 XWikiContext xcontext = getXWikiContext();
228  3 XWiki xwiki = xcontext.getWiki();
229   
230  3 WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId());
231   
232  3 Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
233  15 while (itDocumentsToRestore.hasNext()) {
234  12 DocumentReference docRef = itDocumentsToRestore.next();
235   
236    // Get the corresponding doc in the main wiki
237  12 DocumentReference mainDocRef = docRef.setWikiReference(mainWikiReference);
238   
239    // If the document exists in the main wiki, copy it
240  12 if (xwiki.exists(mainDocRef, xcontext)) {
241  1 try {
242  1 xwiki.copyDocument(mainDocRef, docRef, xcontext);
243  1 itDocumentsToRestore.remove();
244    } catch (XWikiException e) {
245  0 logger.error("Failed to copy [{}] to [{}].", mainDocRef, docRef, e);
246    }
247    }
248    }
249    }
250    }