1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.internal.descriptor.migrator; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Provider; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang.StringUtils; |
30 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
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.DocumentReferenceResolver; |
35 |
|
import org.xwiki.query.Query; |
36 |
|
import org.xwiki.query.QueryException; |
37 |
|
import org.xwiki.query.QueryManager; |
38 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
39 |
|
import org.xwiki.wiki.internal.descriptor.document.XWikiServerClassDocumentInitializer; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWiki; |
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.XWikiException; |
44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
45 |
|
import com.xpn.xwiki.objects.BaseObject; |
46 |
|
import com.xpn.xwiki.store.migration.DataMigrationException; |
47 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
48 |
|
import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@since |
54 |
|
@version |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("R54300WikiDescriptorMigration") |
58 |
|
@Singleton |
|
|
| 91.7% |
Uncovered Elements: 3 (36) |
Complexity: 9 |
Complexity Density: 0.33 |
|
59 |
|
public class WikiDescriptorMigrator extends AbstractHibernateDataMigration |
60 |
|
{ |
61 |
|
@Inject |
62 |
|
private QueryManager queryManager; |
63 |
|
|
64 |
|
@Inject |
65 |
|
private WikiDescriptorManager wikiDescriptorManager; |
66 |
|
|
67 |
|
@Inject |
68 |
|
private Provider<XWikiContext> xcontextProvider; |
69 |
|
|
70 |
|
@Inject |
71 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
72 |
|
|
73 |
|
@Inject |
74 |
|
private Logger logger; |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
0 |
@Override... |
77 |
|
public String getDescription() |
78 |
|
{ |
79 |
0 |
return "http://jira.xwiki.org/browse/XWIKI-10091"; |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
152 |
@Override... |
83 |
|
public XWikiDBVersion getVersion() |
84 |
|
{ |
85 |
152 |
return new XWikiDBVersion(54300); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
2 |
@Override... |
89 |
|
public boolean shouldExecute(XWikiDBVersion startupVersion) |
90 |
|
{ |
91 |
|
|
92 |
2 |
return wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId()); |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
95 |
3 |
@Override... |
96 |
|
protected void hibernateMigrate() throws DataMigrationException, XWikiException |
97 |
|
{ |
98 |
3 |
String hql = "SELECT DISTINCT doc.fullName FROM XWikiDocument doc, BaseObject obj WHERE doc.fullName = obj.name" |
99 |
|
+ " AND obj.className = :className AND doc.fullName <> :template AND doc.fullName NOT IN " |
100 |
|
+ "(SELECT DISTINCT doc2.fullName FROM XWikiDocument doc2, BaseObject obj2, StringProperty propPrettyName" |
101 |
|
+ " WHERE doc2.fullName = obj2.name AND obj2.className = :className" |
102 |
|
+ " AND propPrettyName.id = obj2.id AND propPrettyName.name = :propertyName)"; |
103 |
|
|
104 |
3 |
try { |
105 |
3 |
Query query = queryManager.createQuery(hql, Query.HQL); |
106 |
2 |
query.bindValue("className", String.format("%s.%s", XWiki.SYSTEM_SPACE, |
107 |
|
XWikiServerClassDocumentInitializer.DOCUMENT_NAME)); |
108 |
2 |
query.bindValue("propertyName", XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME); |
109 |
2 |
query.bindValue("template", "XWiki.XWikiServerClassTemplate"); |
110 |
2 |
List<String> results = query.execute(); |
111 |
2 |
for (String result : results) { |
112 |
2 |
fixDocument(result); |
113 |
|
} |
114 |
|
} catch (QueryException e) { |
115 |
1 |
logger.error("Failed to perform a query on the main wiki.", e); |
116 |
|
} |
117 |
|
} |
118 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
119 |
2 |
private void fixDocument(String documentName)... |
120 |
|
{ |
121 |
2 |
XWikiContext context = xcontextProvider.get(); |
122 |
2 |
XWiki xwiki = context.getWiki(); |
123 |
|
|
124 |
2 |
DocumentReference documentReference = documentReferenceResolver.resolve(documentName); |
125 |
2 |
try { |
126 |
2 |
XWikiDocument document = xwiki.getDocument(documentReference, context); |
127 |
1 |
List<BaseObject> objects = document.getXObjects(XWikiServerClassDocumentInitializer.SERVER_CLASS); |
128 |
1 |
for (BaseObject obj : objects) { |
129 |
2 |
if (obj == null) { |
130 |
1 |
continue; |
131 |
|
} |
132 |
1 |
String value = obj.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME); |
133 |
1 |
if (StringUtils.isBlank(value)) { |
134 |
1 |
obj.setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, |
135 |
|
StringUtils.capitalize(StringUtils.removeStart(documentReference.getName(), "XWikiServer"))); |
136 |
|
} |
137 |
|
} |
138 |
1 |
xwiki.saveDocument(document, "[UPGRADE] Set a default pretty name.", context); |
139 |
|
} catch (XWikiException e) { |
140 |
1 |
logger.warn("Failed to get or save the wiki descriptor document [{}]. You will not see the" |
141 |
|
+ " corresponding wiki in the Wiki Index unless you give it a Pretty Name manually. {}", |
142 |
|
documentName, ExceptionUtils.getRootCauseMessage(e)); |
143 |
|
} |
144 |
|
} |
145 |
|
} |