Code source wiki de Administration de la recherche Solr
Modifié par Thomas Mortagne le 2022/06/29 13:01
Afficher les derniers auteurs
author | version | line-number | content |
---|---|---|---|
1 | {{velocity output='false'}} | ||
2 | #macro (displaySolrIndexForm) | ||
3 | {{html clean="false"}} | ||
4 | <form class="xform" action="$xwiki.relativeRequestURL" method="post"> | ||
5 | <div class="hidden"> | ||
6 | <input type="hidden" name="form_token" value="$services.csrf.token" /> | ||
7 | </div> | ||
8 | <dl> | ||
9 | <dt> | ||
10 | <label for="solrAdminAction">$services.localization.render('solr.admin.indexing.action.label')</label> | ||
11 | <span class="xHint">$services.localization.render('solr.admin.indexing.action.hint')</span> | ||
12 | </dt> | ||
13 | <dd> | ||
14 | <select id="solrAdminAction" name="action"> | ||
15 | <option value="index">$services.localization.render('solr.admin.indexing.action.select_index')</option> | ||
16 | <option value="delete">$services.localization.render('solr.admin.indexing.action.select_delete')</option> | ||
17 | <option value="reindex">$services.localization.render('solr.admin.indexing.action.select_reindex')</option> | ||
18 | </select> | ||
19 | </dd> | ||
20 | ## Indexing on the entire farm or on various wikis is done only on the main wiki. | ||
21 | #if ($xcontext.isMainWiki()) | ||
22 | <dt> | ||
23 | <label for="solrAdminWiki">$services.localization.render('solr.admin.indexing.wiki.label')</label> | ||
24 | <span class="xHint">$services.localization.render('solr.admin.indexing.wiki.hint')</span> | ||
25 | </dt> | ||
26 | <dd> | ||
27 | <select id="solrAdminWiki" name="wiki"> | ||
28 | <option value="">$services.localization.render('solr.admin.indexing.wiki.select_farm')</option> | ||
29 | #foreach ($wiki in $allWikis) | ||
30 | #set ($wikiPrettyName = $services.wiki.getById($wiki).prettyName) | ||
31 | #if ("$!wikiPrettyName.trim()" == '') | ||
32 | #set ($wikiPrettyName = $wiki) | ||
33 | #end | ||
34 | <option value="$wiki">$wikiPrettyName</option> | ||
35 | #end | ||
36 | </select> | ||
37 | </dd> | ||
38 | #end | ||
39 | ## We allow only users with programming rights to execute custom queries in order to prevent administrators that | ||
40 | ## don't have programming rights from executing full statement queries when this page is saved with programming | ||
41 | ## rights. | ||
42 | #if ($hasProgramming) | ||
43 | <dt> | ||
44 | <label for="solrAdminCustomQuery">$services.localization.render('solr.admin.indexing.query.label')</label> | ||
45 | <span class="xHint">$services.localization.render('solr.admin.indexing.query.hint')</span> | ||
46 | </dt> | ||
47 | <dd> | ||
48 | <select name="customQueryLanguage"> | ||
49 | <option value="xwql">XWiki Query Language (XWQL)</option> | ||
50 | <option value="hql">Hibernate Query Language (HQL)</option> | ||
51 | </select> | ||
52 | <textarea id="solrAdminCustomQuery" name="customQuery" rows="7" cols="45"></textarea> | ||
53 | </dd> | ||
54 | #end | ||
55 | </dl> | ||
56 | <p class="buttons"> | ||
57 | <span class="buttonwrapper"> | ||
58 | <input type="submit" class="button" value="$services.localization.render('solr.admin.submit')" /> | ||
59 | </span> | ||
60 | </p> | ||
61 | </form> | ||
62 | {{/html}} | ||
63 | #end | ||
64 | |||
65 | #macro (displaySolrAdminSection) | ||
66 | #set ($discard = $xwiki.ssx.use('XWiki.SolrSearchAdmin')) | ||
67 | #set ($discard = $xwiki.jsx.use('XWiki.SolrSearchAdmin')) | ||
68 | == $services.localization.render('solr.admin.status.title') == | ||
69 | |||
70 | (% class="xform" %)((( | ||
71 | ; {{html clean="false"}}<label>$services.localization.render('solr.admin.status.queueSize.label')</label>{{/html}}## | ||
72 | (% class="xHint" %)$services.localization.render('solr.admin.status.queueSize.hint') | ||
73 | : (% class="solrQueueSize" %)$services.solr.queueSize | ||
74 | ))) | ||
75 | |||
76 | == $services.localization.render('solr.admin.indexing.title') == | ||
77 | |||
78 | #displaySolrIndexForm() | ||
79 | #end | ||
80 | |||
81 | #macro (handleSolrIndexAction) | ||
82 | ## | ||
83 | ## Build the targets. | ||
84 | ## | ||
85 | #set ($targetReferences = []) | ||
86 | #set ($wikis = $request.getParameterValues('wiki')) | ||
87 | #if ($xcontext.isMainWiki()) | ||
88 | #if (!$wikis || ($wikis.size() == 1 && $wikis[0] == '')) | ||
89 | ## No wiki specified => target the entire farm. | ||
90 | #set ($wikis = $allWikis) | ||
91 | #end | ||
92 | #else | ||
93 | ## Target only the current (sub)wiki. | ||
94 | #set ($wikis = [$xcontext.database]) | ||
95 | #end | ||
96 | ## On each wiki, execute the custom query or add the entire wiki. | ||
97 | #foreach ($wiki in $wikis) | ||
98 | #set ($wikiReference = $services.model.createWikiReference($wiki)) | ||
99 | ## Only administrators with programmings rights are allowed to execute a custom query. | ||
100 | #if ($hasProgramming && !$stringtool.isBlank($request.customQuery)) | ||
101 | #set ($query = $services.query.createQuery($request.customQuery, $request.customQueryLanguage)) | ||
102 | ## Always use the database specified by the wiki parameter. | ||
103 | #set ($discard = $query.setWiki($wiki).addFilter('unique')) | ||
104 | ## Run the custom query and get the document references. | ||
105 | #foreach ($documentName in $query.execute()) | ||
106 | #set ($documentReference = $services.model.resolveDocument($documentName, $wikiReference)) | ||
107 | #set ($discard = $targetReferences.add($documentReference)) | ||
108 | #end | ||
109 | #else | ||
110 | #set ($discard = $targetReferences.add($wikiReference)) | ||
111 | #end | ||
112 | #end | ||
113 | ## | ||
114 | ## Perform the action on the computed targets. | ||
115 | ## | ||
116 | #if ($request.action == 'index') | ||
117 | #set ($discard = $services.solr.index($targetReferences)) | ||
118 | #elseif ($request.action == 'delete') | ||
119 | #set ($discard = $services.solr.delete($targetReferences)) | ||
120 | #elseif ($request.action == 'reindex') | ||
121 | #set ($discard = $services.solr.delete($targetReferences)) | ||
122 | ## Proceed to the index step only if the delete step was successful. | ||
123 | #if ("$!xcontext.lastexception" == '') | ||
124 | #set ($discard = $services.solr.index($targetReferences)) | ||
125 | #end | ||
126 | #end | ||
127 | ## | ||
128 | ## Display the action result. | ||
129 | ## | ||
130 | #set ($exception = $xcontext.lastexception) | ||
131 | #if ("$!exception" != '') | ||
132 | #set ($message = "$services.localization.render('solr.admin.error.action', $stringtool.capitalize($request.action)) $exception.message ($exception.class)") | ||
133 | #if ($isAjaxRequest) | ||
134 | $response.sendError(500, $message) | ||
135 | #else | ||
136 | {{error}}$message{{/error}} | ||
137 | #end | ||
138 | #elseif (!$isAjaxRequest) | ||
139 | {{success}}$services.localization.render('solr.admin.success.action', $escapetool.xml($stringtool.capitalize($request.action))){{/success}} | ||
140 | ## Only administrators with programmings rights are allowed to execute a custom query. | ||
141 | #if ($hasProgramming && !$stringtool.isBlank($request.customQuery)) | ||
142 | |||
143 | {{info}}$services.localization.render('solr.admin.info.affectedDocuments') $targetReferences{{/info}} | ||
144 | #end | ||
145 | |||
146 | #displaySolrAdminSection() | ||
147 | #end | ||
148 | #end | ||
149 | |||
150 | #macro(handleSolrAction $withValidToken) | ||
151 | #set ($indexActions = ['index', 'reindex', 'delete']) | ||
152 | #if ($withValidToken && $indexActions.contains($request.action)) | ||
153 | #set ($allWikis = $collectiontool.sort($xwiki.wikiNames)) | ||
154 | #handleSolrIndexAction() | ||
155 | #elseif ($request.action == 'getQueueSize') | ||
156 | $response.setContentType('application/json') | ||
157 | $jsontool.serialize({ | ||
158 | 'queueSize': $services.solr.queueSize, | ||
159 | 'timestamp': $datetool.date.time | ||
160 | }) | ||
161 | #else | ||
162 | #set ($allWikis = $collectiontool.sort($xwiki.wikiNames)) | ||
163 | #displaySolrAdminSection() | ||
164 | #end | ||
165 | #end | ||
166 | |||
167 | #macro (handleSolrRequest) | ||
168 | #if (!$hasGlobalAdmin) | ||
169 | #if ("$!request.action" != '') | ||
170 | {{error}}$services.localization.render('solr.admin.error.notallowed'){{/error}} | ||
171 | #else | ||
172 | {{error}}$services.localization.render('solr.admin.error.norights'){{/error}} | ||
173 | #end | ||
174 | #elseif ($request.form_token) | ||
175 | #if ($services.csrf.isTokenValid($request.form_token)) | ||
176 | #handleSolrAction(true) | ||
177 | #elseif ($isAjaxRequest) | ||
178 | $response.sendError(401, 'CSRF token verification failed!') | ||
179 | #else | ||
180 | $response.sendRedirect($services.csrf.getResubmissionURL()) | ||
181 | #end | ||
182 | #else | ||
183 | #handleSolrAction(false) | ||
184 | #end | ||
185 | #end | ||
186 | {{/velocity}} | ||
187 | |||
188 | {{velocity}} | ||
189 | #set ($isAjaxRequest = $request.getHeader('X-Requested-With') == 'XMLHttpRequest') | ||
190 | #handleSolrRequest() | ||
191 | {{/velocity}} |