1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.tag

File TagPluginApi.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

10
52
14
1
304
129
24
0.46
3.71
14
1.71

Classes

Class Line # Actions
TagPluginApi 40 52 0% 24 76
0.00%
 

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.plugin.tag;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27   
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.doc.XWikiDocument;
31    import com.xpn.xwiki.plugin.PluginApi;
32   
33    /**
34    * API for the TagPlugin. TagPlugin is a plugin that allows to manipulate tags easily. It allows to get, rename and
35    * delete tags.
36    *
37    * @see PluginApi
38    * @version $Id: d69cd2aea790418452f94d8a1ea435c9a042df05 $
39    */
 
40    public class TagPluginApi extends PluginApi<TagPlugin>
41    {
42    /** Logging helper object. */
43    private static final Logger LOGGER = LoggerFactory.getLogger(TagPluginApi.class);
44   
45    /** The required access level for modifying document tags. */
46    private static final String TAG_ACCESS_RIGHT = "edit";
47   
48    /**
49    * XWiki Plugin API constructor.
50    *
51    * @param plugin The wrapped plugin.
52    * @param context The current request context.
53    * @see PluginApi#PluginApi(com.xpn.xwiki.plugin.XWikiPluginInterface, XWikiContext)
54    */
 
55  0 toggle public TagPluginApi(TagPlugin plugin, XWikiContext context)
56    {
57  0 super(plugin, context);
58    }
59   
60    /**
61    * Get tags within the wiki.
62    *
63    * @return list of tags.
64    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
65    */
 
66  0 toggle public List<String> getAllTags() throws XWikiException
67    {
68  0 return this.getProtectedPlugin().getAllTags(this.context);
69    }
70   
71    /**
72    * Get cardinality map of tags within the wiki.
73    *
74    * @return map of tags with their occurences counts.
75    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
76    */
 
77  0 toggle public Map<String, Integer> getTagCount() throws XWikiException
78    {
79  0 return this.getProtectedPlugin().getTagCount(this.context);
80    }
81   
82    /**
83    * Get cardinality map of tags for a specific wiki space.
84    *
85    * @param space the space to get tags in
86    * @return map of tags with their occurences counts
87    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
88    * @since 1.2
89    */
 
90  0 toggle public Map<String, Integer> getTagCount(String space) throws XWikiException
91    {
92  0 return this.getProtectedPlugin().getTagCount(space, this.context);
93    }
94   
95    /**
96    * Get cardinality map of tags for list wiki spaces.
97    *
98    * @param spaces the list of space to get tags in, as a comma separated, quoted string
99    * @return map of tags with their occurences counts
100    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
101    * @since 8.1
102    */
 
103  0 toggle public Map<String, Integer> getTagCountForSpaces(String spaces) throws XWikiException
104    {
105  0 return this.getProtectedPlugin().getTagCountForSpaces(spaces, this.context);
106    }
107   
108    /**
109    * Get cardinality map of tags matching an hql query. Examples of usage:
110    * <ul>
111    * <li>
112    * <code>
113    * $xwiki.tag.getTagCountForQuery("","doc.creator='XWiki.JohnDoe'")
114    * </code> will return the cardinality map of tags for documents created by user XWiki.JohnDoe</li>
115    * <li>
116    * <code>
117    * $xwiki.tag.getTagCountForQuery(", BaseObject as obj",
118    * "obj.name=doc.fullName and obj.className='Blog.BlogPostClass'")
119    * </code> will return the cardinality map of tags associated to blog post documents</li>
120    * <li>
121    * <code>
122    * $xwiki.tag.getTagCountForQuery("", "")
123    * </code> will return all tags within the wiki</li>
124    * </ul>
125    *
126    * @param from the from fragment of the query
127    * @param where the where fragment from the query
128    * @return map of tags with their occurrences counts
129    * @throws XWikiException if search query fails (possible failures: DB access problems, incorrect query fragments).
130    * @since 1.2
131    * @see TagPluginApi#getTagCountForQuery(String, String, java.util.List)
132    */
 
133  0 toggle public Map<String, Integer> getTagCountForQuery(String from, String where) throws XWikiException
134    {
135  0 return getTagCountForQuery(from, where, null);
136    }
137   
138    /**
139    * Get cardinality map of tags matching an hql query (parametrized version). Example of usage:
140    * <ul>
141    * <li>
142    * <code>
143    * $xwiki.tag.getTagCountForQuery("", "doc.creator = ?", ["$!{request.creator}"])
144    * </code> will return the cardinality map of tags for documents created by user-provided creator name</li>
145    * </ul>
146    *
147    * @param from the from fragment of the query
148    * @param where the parameterized where fragment from the query
149    * @param parameterValues list of parameter values for the query
150    * @return map of tags with their occurrences counts
151    * @throws XWikiException if search query fails (possible failures: DB access problems, incorrect query fragments).
152    * @since 1.18
153    */
 
154  0 toggle public Map<String, Integer> getTagCountForQuery(String from, String where, List< ? > parameterValues)
155    throws XWikiException
156    {
157  0 return this.getProtectedPlugin().getTagCountForQuery(from, where, parameterValues, this.context);
158    }
159   
160    /**
161    * Get all the documents containing the given tag.
162    *
163    * @param tag tag to match.
164    * @return list of pages.
165    * @throws XWikiException if search query fails (possible failures: DB access problems, etc).
166    */
 
167  0 toggle public List<String> getDocumentsWithTag(String tag) throws XWikiException
168    {
169  0 return this.getProtectedPlugin().getDocumentsWithTag(tag, this.context);
170    }
171   
172    /**
173    * Get tags from a document.
174    *
175    * @param documentName name of the document.
176    * @return list of tags.
177    * @throws XWikiException if document read fails (possible failures: insufficient rights, DB access problems, etc).
178    */
 
179  0 toggle public List<String> getTagsFromDocument(String documentName) throws XWikiException
180    {
181  0 return this.getProtectedPlugin().getTagsFromDocument(documentName, this.context);
182    }
183   
184    /**
185    * Add a tag to a document. The document is saved (minor edit) after this operation.
186    *
187    * @param tag tag to set.
188    * @param documentName name of the document.
189    * @return the {@link TagOperationResult result} of the operation
190    */
 
191  0 toggle public TagOperationResult addTagToDocument(String tag, String documentName)
192    {
193  0 TagOperationResult result;
194  0 try {
195  0 XWikiDocument document = this.context.getWiki().getDocument(documentName, this.context);
196  0 if (this.context.getWiki().checkAccess(TAG_ACCESS_RIGHT, document, this.context)) {
197  0 result = this.getProtectedPlugin().addTagToDocument(tag, document, this.context);
198    } else {
199  0 result = TagOperationResult.NOT_ALLOWED;
200    }
201    } catch (Exception ex) {
202  0 LOGGER.warn("Failed to add tag to document: [{}]", ex.getMessage());
203  0 result = TagOperationResult.FAILED;
204    }
205  0 return result;
206    }
207   
208    /**
209    * Add a list of tags to a document. The document is saved (minor edit) after this operation
210    *
211    * @param tags the comma separated list of tags to set; whitespace around the tags is stripped
212    * @param documentName the name of the target document
213    * @return the {@link TagOperationResult result} of the operation. {@link TagOperationResult#NO_EFFECT} is returned
214    * only if all the tags were already set on the document, {@link TagOperationResult#OK} is returned even if
215    * only some of the tags are new.
216    */
 
217  0 toggle public TagOperationResult addTagsToDocument(String tags, String documentName)
218    {
219  0 TagOperationResult result;
220  0 try {
221  0 XWikiDocument document = this.context.getWiki().getDocument(documentName, this.context);
222  0 if (this.context.getWiki().checkAccess(TAG_ACCESS_RIGHT, document, this.context)) {
223  0 result = this.getProtectedPlugin().addTagsToDocument(tags, document, this.context);
224    } else {
225  0 result = TagOperationResult.NOT_ALLOWED;
226    }
227    } catch (Exception ex) {
228  0 LOGGER.warn("Failed to add tags to document: [{}]", ex.getMessage());
229  0 result = TagOperationResult.FAILED;
230    }
231  0 return result;
232    }
233   
234    /**
235    * Remove a tag from a document. The document is saved (minor edit) after this operation.
236    *
237    * @param tag tag to remove.
238    * @param documentName name of the document.
239    * @return the {@link TagOperationResult result} of the operation
240    */
 
241  0 toggle public TagOperationResult removeTagFromDocument(String tag, String documentName)
242    {
243  0 TagOperationResult result;
244  0 try {
245  0 XWikiDocument document = this.context.getWiki().getDocument(documentName, this.context);
246  0 if (this.context.getWiki().checkAccess(TAG_ACCESS_RIGHT, document, this.context)) {
247  0 result = this.getProtectedPlugin().removeTagFromDocument(tag, documentName, this.context);
248    } else {
249  0 result = TagOperationResult.NOT_ALLOWED;
250    }
251    } catch (Exception ex) {
252  0 LOGGER.warn("Failed to remove tag from document: [{}]", ex.getMessage());
253  0 result = TagOperationResult.FAILED;
254    }
255  0 return result;
256    }
257   
258    /**
259    * Rename a tag in all the documents that contains it. Requires admin rights. Document containing this tag are saved
260    * (minor edit) during this operation.
261    *
262    * @param tag tag to rename.
263    * @param newTag new tag.
264    * @return the {@link TagOperationResult result} of the operation
265    */
 
266  0 toggle public TagOperationResult renameTag(String tag, String newTag)
267    {
268  0 TagOperationResult result;
269  0 try {
270  0 if (hasAdminRights()) {
271  0 result = this.getProtectedPlugin().renameTag(tag, newTag, this.context);
272    } else {
273  0 result = TagOperationResult.NOT_ALLOWED;
274    }
275    } catch (Exception ex) {
276  0 LOGGER.warn("Failed to rename tag: [{}]", ex.getMessage());
277  0 result = TagOperationResult.FAILED;
278    }
279  0 return result;
280    }
281   
282    /**
283    * Delete a tag from all the documents that contains it. Requires admin rights. Document containing this tag are
284    * saved (minor edit) during this operation.
285    *
286    * @param tag tag to delete.
287    * @return the {@link TagOperationResult result} of the operation
288    */
 
289  0 toggle public TagOperationResult deleteTag(String tag)
290    {
291  0 TagOperationResult result;
292  0 try {
293  0 if (hasAdminRights()) {
294  0 result = this.getProtectedPlugin().deleteTag(tag, this.context);
295    } else {
296  0 result = TagOperationResult.NOT_ALLOWED;
297    }
298    } catch (Exception ex) {
299  0 LOGGER.warn("Failed to delete tag: [{}]", ex.getMessage());
300  0 result = TagOperationResult.FAILED;
301    }
302  0 return result;
303    }
304    }