comparison doc/jp/pubsub.rst @ 3041:72583524cfd3

doc (jp): jp commands are now fully documented: rel 232
author Goffi <goffi@goffi.org>
date Tue, 01 Oct 2019 22:49:06 +0200
parents
children cea52c9ddfd9
comparison
equal deleted inserted replaced
3040:fee60f17ebac 3041:72583524cfd3
1 .. _jp-pubsub:
2
3 =========================
4 pubsub: PubSub management
5 =========================
6
7 PubSub commands are low level command to handle a PubSub Service.
8 They are using the generic pubsub arguments
9
10 For most of those commands, :ref:`pubsub_common` commands are used to specify the
11 destination item.
12
13 set
14 ===
15
16 Publish a pubsub item.
17
18 ``stdin`` is used to get the raw XML of the payload of the item to publish.
19
20 example
21 -------
22
23 Create an item with a custom note XML::
24
25 $ echo '<note xmlns="http://example.net/mynotes">this is a note</note>' | jp pubsub set -n "notes"
26
27 get
28 ===
29
30 Retrieve items from specified node. Default output is prettified and highlighted XML.
31
32 example
33 -------
34
35 Retrieve the last 5 notes from our custom notes node::
36
37 $ jp pubsub get -n notes -m 5
38
39 delete
40 ======
41
42 Delete an item from a node. If ``-N, --notify`` is specified, subscribers will be notified
43 of the item retraction.
44
45 By default a confirmation is requested before deletion is requested to the PubSub service,
46 but you can override this behaviour by using ``-f, --force`` option.
47
48 example
49 -------
50
51 Delete item with id ``123456`` from a node::
52
53 $ jp pubsub delete -n test_node -i 123456
54
55 edit
56 ====
57
58 Edit the raw XML of an item payload using your local editor (the one set in ``$EDITOR``).
59
60 If you don't change anything or publish an empty blog post, the edition will be cancelled.
61
62 :ref:`draft_common` commands can be used.
63
64 example
65 -------
66
67 Edit the last note in our custom node::
68
69 $ jp pubsub edit -n notes -L
70
71 subscribe
72 =========
73
74 Subscribe to a node.
75
76 Subscription is used to get notifications from the node in case of new/updated item or
77 deletion.
78
79 example
80 -------
81
82 Subscribe to an information blog::
83
84 $ jp pubsub subscribe -n informations -s pubsub.example.net
85
86 unsubscribe
87 ===========
88
89 Unsubscribe from a node.
90
91 example
92 -------
93
94 Unsubscribe from an information blog::
95
96 $ jp pubsub unsubscribe -n informations -s pubsub.example.net
97
98 subscriptions
99 =============
100
101 Retrieve subscriptions for all nodes on a service.
102
103 ``-n NODE, --node NODE`` can be used to request subscriptions for a specific node (e.g. if
104 it has subscription with multiple subIDs).
105
106 example
107 -------
108
109 Retrieve all subscriptions on a pubsub service::
110
111 $ jp pubsub subscriptions -s pubsub@example.net
112
113 affiliations
114 =============
115
116 Retrieve affiliations for all nodes at a service.
117
118 ``-n NODE, --node NODE`` can be used to request affiliation for a specific node.
119
120 examples
121 --------
122
123 Retrieve all affiliations at a pubsub service::
124
125 $ jp pubsub affiliations -s pubsub@example.net
126
127 Retrieve affiliation for the ``notes`` node::
128
129 $ jp pubsub affiliations -s pubsub@example.net -n notes
130
131 search
132 ======
133
134 Search items corresponding to one or more filter(s).
135
136 ``search`` will check all items (or some of them according to options used) from one or
137 several nodes (several nodes can be checked if recursion is used, see below). For each
138 item the given filters will be checked, and all corresponding items will be returned.
139
140 This is a resource intensive method (both for server and client), use with caution, and
141 use MAM to do searching when suitable.
142
143 filters
144 -------
145
146 To do a search you one or more filters. Filters are checked in the order in which they are
147 specified. You can use 4 kinds of filters:
148
149 ``-t TEXT, --text TEXT``
150 do a full-text search. If *TEXT* is appearing anywhere in the item (including in XML
151 tags or arguments), the item is selected
152
153 ``-r EXPRESSION, --regex EXPRESSION``
154 do a regular expression search. `Python standard re module`_ is used internally, so you
155 can use its syntax.
156
157 ``-x XPATH, --xpath XPATH``
158 use an `XPath version 1.0`_ expression to filter the query. You can have a look at
159 `Wikipedia XPath page`_ for a user friendly introduction.
160
161 ``-P PYTHON_CODE, --python PYTHON_CODE``
162 use a Python expression to do a test. The expression must return a boolean (``True`` to
163 keep item, ``False`` otherwise). From within the Python expression 3 variables are
164 defined: ``item`` which contain the raw item as a string, and ``item_xml`` which is the
165 parsed XML as an lxml ``etree.Element`` and ``etree`` which is the ``lxml.etree`` module.
166
167 .. _Python standard re module: https://docs.python.org/3.7/library/re.html
168 .. _XPath version 1.0: https://www.w3.org/TR/1999/REC-xpath-19991116/
169 .. _Wikipedia XPath page: https://en.wikipedia.org/wiki/XPath
170
171 filter modifiers
172 ----------------
173
174 Before each filter you can specify one or more filter modifiers. A modifier will change
175 filter behaviour, it's a flag which can be used either without argument (then it will
176 activate the flag), or with an explicit boolean value (i.e. ``true`` or ``false``).
177
178 The available filters are:
179
180 ``-C [BOOLEAN], --ignore-case [BOOLEAN]``
181 (don't) ignore case. Filters are normally case sensitive, this modifier change this
182 behaviour.
183
184 ``-I [BOOLEAN], --invert [BOOLEAN]``
185 (don't) invert effect of following filters. This is applying a logical ``NOT`` to the
186 filter. This means that instead of keeping item matching the filter, it will keep the
187 items which are **not** matching the filter.
188
189 ``-A [BOOLEAN], --dot-all [BOOLEAN]``
190 (don't) use `DOTALL`_ option for regex. This filter only makes sense before a
191 ``--regex`` expression.
192
193 ``-k [BOOLEAN], --only-matching [BOOLEAN]``
194 (don't) keep only the matching part of the item. Normally the whole item is returned,
195 with this flag, only the part matching the filters are kept.
196
197 .. _DOTALL: https://docs.python.org/3.7/library/re.html#re.DOTALL
198
199 actions
200 -------
201
202 Once filters are set, you may indicate what do to with the found items. By default they
203 are printed, but you can also use an other jp command, or even an external tool.
204
205 The following actions are available:
206
207 ``print`` (default)
208 pretty print the found items.
209
210 ``exec``
211 use the given jp command on each found item. Everything after the ``exec`` is used to
212 indicate the command and arguments to use (you must not specify ``jp``, use the command
213 directly). The service, node and item will be set to match the found item.
214
215 ``external``
216 pipe the raw XML of each item to the given command. Everything after the ``external``
217 action is used to indicate the command and arguments to use.
218
219 recursive search
220 ----------------
221
222 By default, only items in the given node will be filtered, but if you specify a recursion
223 depth > 0 (using ``-D MAX_DEPTH, --max-depth MAX_DEPTH``), every node linked in item will
224 be checked too, then node linked in linked item and so on until depth level is reached.
225
226 For instance, if you want to find all comments of a blog node containing an http(s) link,
227 you can do that::
228
229 $ jp pubsub search -n urn:xmpp:microblog:0 -s user@example.net -D 1 -r 'https?://'
230
231 examples
232 --------
233
234 Finding all items containing the text "something interesting" in personal blog::
235
236 $ jp pubsub search -n urn:xmpp:microblog:0 -M -1 -t "something interesting"
237
238 Find which blog items in the last 20 have a body with less than 200 characters (note that
239 body can be either ``<title>`` or ``<content>``, see `XEP-0277`_ for details). Here we use
240 a python expression on the text of the body to count the number of characters::
241
242 $ jp pubsub search -n urn:xmpp:microblog:0 -M 20 --python "len((item_xml.find('.//{http://www.w3.org/2005/Atom}content[@type=\"text\"]') or item_xml.find('.//{http://www.w3.org/2005/Atom}title[@type=\"text\"]')).text) < 200"
243
244 Find items published by ``toto@example.net`` among last 30 on a blog node, and use
245 ``pubsub blog`` command to retrieve id and title. We use ``-N`` to specify the ``pubsub``
246 namespace which is used in the XPath expression, then we use ``exec`` to run ``blog get -k
247 title -k id`` on found items::
248
249 $ jp pubsub search -n some_blog_node -s pubsub.example.net -M 30 -N pubsub http://jabber.org/protocol/pubsub -x '/pubsub:item[starts-with(@publisher, "toto@example.net")]' exec blog get -k title -k id
250
251 Find items which have **NOT** a title among last 30 items in our personal blog. As
252 explained in `XEP-0277`_ Atom's ``<title>`` is always used (even if there is only a body
253 and no title), so we actually look for items without ``<content>``. We do that with an
254 XPath looking for this ``atom:content`` element, then we use the ``-I [BOOLEAN], --invert
255 [BOOLEAN]`` to filter out elements which match.::
256
257 $ jp pubsub search -n urn:xmpp:microblog:0 -M 30 -I -x //atom:content -N atom http://www.w3.org/2005/Atom
258
259 Display authors names from last 10 items and their comments, using the ``-k [BOOLEAN],
260 --only-matching [BOOLEAN]`` modifier to only display the data we need. We use ``-D 1`` to
261 do a recursive search of level 1, which will also look into comments nodes (using last 10
262 items there too)::
263
264 $ jp pubsub search -n urn:xmpp:microblog:0 -M 10 --only-matching -x //atom:author/atom:name -N atom http://www.w3.org/2005/Atom -D 1
265
266 .. _XEP-0277: https://xmpp.org/extensions/xep-0277.html
267
268 transform
269 =========
270
271 Modify items using an external command.
272
273 ``transform`` will retrieve requested items, and will send each of them to the standard
274 input (stdin) of the specified command. The output of the command will be used, it can be
275 3 things:
276
277 - a raw XML of the modified item, in which case the item will be republished
278 - the string ``SKIP``, in which case the item will be ignored
279 - the string ``DELETE``, in which case the item will be retracted
280
281 By default a dry run is done, which means that no item is modified or deleted. To actually
282 do the transformation, you have to use ``--apply`` argument.
283
284 If you have to modify the ``publisher`` of an item, you need specific privileges. The
285 ``--admin`` allows you do to that, but it must be supported by your PubSub service
286 (currently only ``SàT PubSub`` supports this non standard feature).
287
288 To modify all items of a node, use the ``-A, --all`` option. This will use `RSM`_
289 repetitively until all items are treated. Of course that means that your PubSub service
290 must support RSM. The items being republished, they will reappear on top of your node,
291 that's why it is recommended to use ``--order-by creation`` option when supported by the
292 service, to keep consistent order and avoid transforming the same items several times.
293
294 If the command you're using exit with a non zero code, the process will stop. Use ``-I,
295 --ignore_errors`` if you want to continue transformation even if an non zero code is
296 returned.
297
298 .. _RSM: https://xmpp.org/extensions/xep-0059.html
299
300 example
301 -------
302
303 Imagine that you want to replace all occurrences of "sàt" by "Salut à Toi" in your personal blog. You first create a Python script like this:
304
305 .. sourcecode:: python
306
307 #!/usr/bin/env python3
308
309 import sys
310 item_raw = sys.stdin.read()
311 if not "sàt" in item_raw:
312 print("SKIP")
313 else:
314 print(item_raw.replace("sàt", "Salut à Toi"))
315
316 And save it a some location, e.g. ``~/expand_sat.py`` (don't forget to make is executable
317 with ``chmod +x ~/expand_sat.py``).
318
319 To be sure it's safe, you can first do a dry-run and check the result::
320
321 $ jp pubsub transform -n urn:xmpp:microblog:0 -A -o creation ~/expand_sat.py
322
323 Once you have checked that you have the expected behaviour, you can apply the
324 transformations::
325
326 $ jp pubsub transform -n urn:xmpp:microblog:0 -A -o creation --apply ~/expand_sat.py
327
328 And that's it. You can use the same technique for more complex transformations, including
329 modifying the XML (with Python, you can easily do that with standard
330 ``xml.etree.ElementTree`` module or with ``lxml.etree``).
331
332 uri
333 ===
334
335 Build an XMPP URI linking to a PubSub node or item.
336
337 example
338 -------
339
340 Build a link to personal blog::
341
342 $ jp pubsub uri -n urn:xmpp:microblog:0
343
344 node
345 ====
346
347 Subcommands for node management. Please check :ref:`jp-pubsub_node`.
348
349 hook
350 ====
351
352 Subcommands for hooks management. Please check :ref:`jp-pubsub_hook`.