1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.wikimacro.internal; |
21 |
|
|
22 |
|
import java.util.HashSet; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Set; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.context.Execution; |
33 |
|
import org.xwiki.model.reference.DocumentReference; |
34 |
|
import org.xwiki.model.reference.SpaceReference; |
35 |
|
import org.xwiki.model.reference.SpaceReferenceResolver; |
36 |
|
import org.xwiki.model.reference.WikiReference; |
37 |
|
import org.xwiki.query.Query; |
38 |
|
import org.xwiki.query.QueryManager; |
39 |
|
import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; |
40 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacro; |
41 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroException; |
42 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; |
43 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroInitializer; |
44 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWikiContext; |
47 |
|
import com.xpn.xwiki.XWikiException; |
48 |
|
import com.xpn.xwiki.doc.MandatoryDocumentInitializer; |
49 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
50 |
|
|
51 |
|
|
52 |
|
@link |
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Singleton |
|
|
| 66.1% |
Uncovered Elements: 21 (62) |
Complexity: 16 |
Complexity Density: 0.36 |
|
59 |
|
public class DefaultWikiMacroInitializer implements WikiMacroInitializer, WikiMacroConstants |
60 |
|
{ |
61 |
|
|
62 |
|
@link |
63 |
|
|
64 |
|
@Inject |
65 |
|
private WikiMacroFactory wikiMacroFactory; |
66 |
|
|
67 |
|
|
68 |
|
@link |
69 |
|
|
70 |
|
@Inject |
71 |
|
private WikiMacroManager wikiMacroManager; |
72 |
|
|
73 |
|
|
74 |
|
@link |
75 |
|
|
76 |
|
@Inject |
77 |
|
private Execution execution; |
78 |
|
|
79 |
|
@Inject |
80 |
|
@Named(WIKI_MACRO_CLASS) |
81 |
|
private MandatoryDocumentInitializer wikiMacroInitializer; |
82 |
|
|
83 |
|
@Inject |
84 |
|
@Named(WIKI_MACRO_PARAMETER_CLASS) |
85 |
|
private MandatoryDocumentInitializer wikiMacroParameterInitializer; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
private Logger logger; |
92 |
|
|
93 |
|
@Inject |
94 |
|
private SpaceReferenceResolver<String> spaceReferenceResolver; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@return |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
76 |
private XWikiContext getContext()... |
102 |
|
{ |
103 |
76 |
return (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0 |
@Override... |
107 |
|
public void registerExistingWikiMacros() throws Exception |
108 |
|
{ |
109 |
0 |
registerExistingWikiMacros(false, null); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
38 |
@Override... |
113 |
|
public void registerExistingWikiMacros(String wiki) throws Exception |
114 |
|
{ |
115 |
38 |
registerExistingWikiMacros(true, wiki); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@throws |
128 |
|
|
129 |
|
|
|
|
| 58.3% |
Uncovered Elements: 5 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
130 |
38 |
private void registerExistingWikiMacros(boolean local, String wiki) throws Exception... |
131 |
|
{ |
132 |
38 |
XWikiContext xcontext = getContext(); |
133 |
|
|
134 |
|
|
135 |
38 |
String originalWiki = xcontext.getWikiId(); |
136 |
38 |
try { |
137 |
38 |
if (!local) { |
138 |
0 |
Set<String> wikiNames = new HashSet<String>(); |
139 |
|
|
140 |
0 |
wikiNames.addAll(xcontext.getWiki().getVirtualWikisDatabaseNames(xcontext)); |
141 |
|
|
142 |
0 |
for (String wikiName : wikiNames) { |
143 |
0 |
registerMacrosForWiki(wikiName, xcontext); |
144 |
|
} |
145 |
|
} else { |
146 |
38 |
registerMacrosForWiki(wiki, xcontext); |
147 |
|
} |
148 |
|
} finally { |
149 |
38 |
xcontext.setWikiId(originalWiki); |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@param |
158 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
159 |
38 |
private void registerMacrosForWiki(String wikiName, XWikiContext xcontext)... |
160 |
|
{ |
161 |
38 |
try { |
162 |
38 |
this.logger.debug("Registering all wiki macros found in wiki [{}]", wikiName); |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
38 |
xcontext.setWikiId(wikiName); |
169 |
|
|
170 |
|
|
171 |
38 |
installOrUpgradeWikiMacroClasses(); |
172 |
|
|
173 |
|
|
174 |
38 |
for (Object[] wikiMacroDocumentData : getWikiMacroDocumentData(xcontext)) { |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
211 |
SpaceReference spaceReference = |
180 |
|
this.spaceReferenceResolver.resolve((String) wikiMacroDocumentData[0], new WikiReference(wikiName)); |
181 |
211 |
DocumentReference wikiMacroDocumentReference = |
182 |
|
new DocumentReference((String) wikiMacroDocumentData[1], spaceReference); |
183 |
|
|
184 |
211 |
registerMacro(wikiMacroDocumentReference, (String) wikiMacroDocumentData[2], xcontext); |
185 |
|
} |
186 |
|
} catch (Exception ex) { |
187 |
0 |
this.logger.warn("Failed to register macros for wiki [{}]: {}", wikiName, ex.getMessage()); |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
@param |
195 |
|
@return |
196 |
|
|
197 |
|
@throws |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
199 |
38 |
private List<Object[]> getWikiMacroDocumentData(XWikiContext xcontext) throws Exception... |
200 |
|
{ |
201 |
38 |
final QueryManager qm = xcontext.getWiki().getStore().getQueryManager(); |
202 |
38 |
final Query q = qm.getNamedQuery("getWikiMacroDocuments"); |
203 |
38 |
return (List<Object[]>) (List) q.execute(); |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
@param |
210 |
|
@param |
211 |
|
@param |
212 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
|
213 |
211 |
private void registerMacro(DocumentReference wikiMacroDocumentReference, String wikiMacroDocumentAuthor,... |
214 |
|
XWikiContext xcontext) |
215 |
|
{ |
216 |
211 |
this.logger.debug("Registering macro in document [{}]...", wikiMacroDocumentReference); |
217 |
|
|
218 |
211 |
DocumentReference originalAuthor = xcontext.getUserReference(); |
219 |
211 |
try { |
220 |
211 |
WikiMacro macro = this.wikiMacroFactory.createWikiMacro(wikiMacroDocumentReference); |
221 |
|
|
222 |
211 |
this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, macro); |
223 |
|
|
224 |
211 |
this.logger.debug("Macro [{}] from document [{}] is now registered.", |
225 |
|
macro.getDescriptor().getId().getId(), wikiMacroDocumentReference); |
226 |
|
} catch (InsufficientPrivilegesException ex) { |
227 |
|
|
228 |
|
|
229 |
0 |
this.logger.debug(ex.getMessage(), ex); |
230 |
|
} catch (WikiMacroException ex) { |
231 |
|
|
232 |
0 |
this.logger.error(ex.getMessage(), ex); |
233 |
|
} finally { |
234 |
211 |
xcontext.setUserReference(originalAuthor); |
235 |
|
} |
236 |
|
} |
237 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
238 |
38 |
@Override... |
239 |
|
public void installOrUpgradeWikiMacroClasses() throws Exception |
240 |
|
{ |
241 |
38 |
XWikiContext xcontext = getContext(); |
242 |
|
|
243 |
|
|
244 |
38 |
XWikiDocument doc = xcontext.getWiki().getDocument(this.wikiMacroInitializer.getDocumentReference(), xcontext); |
245 |
|
|
246 |
38 |
if (this.wikiMacroInitializer.updateDocument(doc)) { |
247 |
0 |
update(doc); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
38 |
doc = xcontext.getWiki().getDocument(WIKI_MACRO_PARAMETER_CLASS, xcontext); |
252 |
|
|
253 |
38 |
if (this.wikiMacroParameterInitializer.updateDocument(doc)) { |
254 |
0 |
update(doc); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
@throws |
263 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
264 |
0 |
private void update(XWikiDocument doc) throws Exception... |
265 |
|
{ |
266 |
0 |
XWikiContext xcontext = getContext(); |
267 |
|
|
268 |
0 |
if (doc.isNew()) { |
269 |
0 |
doc.setParent("XWiki.WebHome"); |
270 |
|
} |
271 |
|
|
272 |
0 |
xcontext.getWiki().saveDocument(doc, xcontext); |
273 |
|
} |
274 |
|
} |