rbmediawiki 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ #This class represents a category and performs actions dealing with categories.
2
+ #
3
+ class Category
4
+ attr_reader :title
5
+ attr_reader :id
6
+ def initialize(title = nil, site = nil, id = nil)
7
+ @site = site ? site : Api.new()
8
+ @title = title.gsub(" ","_")
9
+ @id = id
10
+ end
11
+
12
+ #get pages in the category as an array of Page elements
13
+ #returns false if there aren't any, and raises NoPage if page doesn't exist
14
+ def get_members(cmlimit = 500)
15
+ cmcontinue = nil
16
+ cms = Hash.new
17
+ loop {
18
+ result = @site.query_list_categorymembers(@title, @title, nil, nil, cmcontinue, cmlimit)
19
+ if result.key?('query-continue')
20
+ cmcontinue = result['query-continue']['categorymembers']['cmcontinue']
21
+ cms.deep_merge!(result['query'])
22
+ else
23
+ cms.deep_merge!(result['query'])
24
+ break
25
+ end
26
+ }
27
+ if cms['pages']['page'].key?('missing')
28
+ raise NoPage.new(), "Page [[#{title}]] does not exist"
29
+ elsif cms.key?('categorymembers')
30
+ members = Array.new
31
+ cms['categorymembers']['cm'].each{|el| members.push(Page.new(el['title']))}
32
+ return members
33
+ else return false
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,4 @@
1
+ default_lang: es
2
+ default_family: wikipedia
3
+ default_user: Chabacano
4
+ user-agent: rbywikipedia
@@ -0,0 +1,51 @@
1
+ #coding: utf-8
2
+
3
+ class Misc_generator
4
+ def initialize(site)
5
+ @site = site
6
+ end
7
+
8
+ #returns revisions of a page
9
+ # * titles: the page
10
+ # * rvlimit: how many revisions retrieve by query (default: 50, max: 50)
11
+ # * rvprop: fields to retrieve (default: all)
12
+ # * rvstartid: id of the revision to start
13
+ # * rvendid: id of the revision to end
14
+ # * rvdir: older or newer. Defaulr: older.
15
+ # * rvuser: only get revisions by user.
16
+ # * diffto: Revision ID to diff each revision to. Use "prev", "next" and "cur" for the previous, next and current revision respectively.
17
+
18
+
19
+ def history( titles, rvlimit = 50, rvprop = "ids|timestamp|flags|comment|user|size|content", rvstartid = nil, rvendid = nil, rvdir = "older", rvuser = nil, diffto = "prev")
20
+ pages = Hash.new
21
+ finish = false
22
+ while !finish
23
+ result = @site.query_prop_revisions(titles, rvprop, rvlimit, rvstartid, rvendid, nil, nil, rvdir, rvuser, nil, nil, nil, nil, nil, nil, diffto )
24
+ result['query']['pages']['page']['revisions']['rev'].each {|rv|
25
+ puts rv
26
+ yield rv
27
+ }
28
+ if result.key?('query-continue')
29
+ rvstartid = result['query-continue']['revisions']['rvstartid']
30
+ else
31
+ finish = true
32
+ end
33
+ end
34
+ end
35
+
36
+ def history_diff( titles, rvlimit = 30, rvprop = "timestamp|comment|user|size", rvstartid = nil, rvendid = nil, rvdir = "older", rvuser= nil, diffto = "prev")
37
+ pages = Hash.new
38
+ finish = false
39
+ while !finish
40
+ result = @site.query_prop_revisions("japonés", rvprop, rvlimit, rvstartid, rvendid, nil, nil, rvdir, rvuser, nil, nil, nil, nil, nil, nil, "prev")
41
+ result['query']['pages']['page']['revisions'].each {|rv|
42
+ yield rv
43
+ }
44
+ if result.key?('query-continue')
45
+ rvstartid = result['query-continue']['revisions']['rvstartid']
46
+ else
47
+ finish = true
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,465 @@
1
+ #TODO: rollback
2
+ #TODO: patrol
3
+
4
+ #This class represents a page. It gives methods for dealing with single
5
+ #pages: obtainig the content, putting it, appending content, deleting, etc.
6
+ class Page
7
+ attr_reader :title
8
+ def initialize(title = nil, site = nil)
9
+ @site = site ? site : Api.new()
10
+ @title = title
11
+ @normtitle = title.gsub(" ","_")
12
+ end
13
+
14
+ #retrieves the content of the page
15
+ def get()
16
+ result = @site.query_prop_revisions(@normtitle, 'content')
17
+ if result.key?('error')
18
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
19
+ else
20
+ return result['query']['pages']['page']['revisions']['rev']
21
+ end
22
+ end
23
+
24
+ #returns false if it is not a redirect, the redirected title if it is
25
+ def redirect?()
26
+ txt = this.get
27
+ if (txt =~ /#REDIRECT\s+\[\[(.*)\]\]/)
28
+ return $1
29
+ else
30
+ return false
31
+ end
32
+ end
33
+
34
+ #puts the text of a page.
35
+ # * text: the new content of the page
36
+ # * summary: editting summary
37
+ # * minor: is a minor edit? default->true
38
+ # * bot: is a bot flagged edit?
39
+ def put(text, summary = nil, minor = true, bot = true, password = nil)
40
+ #require login
41
+ @site.login(password)
42
+ result = @site.query_prop_info(@normtitle, nil, 'edit')
43
+ token = result['query']['pages']['page']['edittoken']
44
+ result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot)
45
+ if result.key?('error')
46
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
47
+ else
48
+ return true
49
+ end
50
+ puts "content put"
51
+ end
52
+
53
+ #appends texto to a page
54
+ #same as #put, but the text is appended and the previous content preserved
55
+ def append(text, summary = nil, minor = true, bot = true)
56
+ #require login
57
+ @site.login
58
+ puts text
59
+ result = @site.query_prop_info(@normtitle, nil, 'edit')
60
+ token = result['query']['pages']['page']['edittoken']
61
+ result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, text)
62
+ if result.key?('error')
63
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
64
+ else
65
+ return true
66
+ end
67
+ end
68
+
69
+ #prepends text to a page
70
+ #same as #put, but the text is prepended and the previous content preserved
71
+ def prepend(text, summary = nil, minor = true, bot = true)
72
+ #require login
73
+ @site.login
74
+ result = @site.query_prop_info(@normtitle, nil, 'edit')
75
+ token = result['query']['pages']['page']['edittoken']
76
+ result = @site.edit(@normtitle, nil, text, token, summary, minor, nil, bot, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, text)
77
+ if result.key?('error')
78
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
79
+ else
80
+ return true
81
+ end
82
+ end
83
+
84
+ #adds a section to a page
85
+ #same as #append, but is a section what is appended.
86
+ #title is the title of the new section
87
+ def addsection(text, title, minor = false, bot = true)
88
+ #require login
89
+ @site.login
90
+ result = @site.query_prop_info(@normtitle, nil, 'edit')
91
+ token = result['query']['pages']['page']['edittoken']
92
+ result = @site.edit(@normtitle, section, text, token, title, minor, nil, bot)
93
+ if result.key?('error')
94
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
95
+ else
96
+ return true
97
+ end
98
+ end
99
+
100
+ #moves a page
101
+ # * reason: reason or summary
102
+ # * movetalk: move talk pages too (default->true)
103
+ # * noredirect: don't leave a redirect (default->nil)
104
+ def move(to, reason = nil, movetalk = true, noredirect = nil)
105
+ #require login
106
+ @site.login
107
+ result = @site.query_prop_info(@normtitle, nil, 'move')
108
+ token = result['query']['pages']['page']['movetoken']
109
+ result = @site.move(@normtitle, nil, to, token, reason, movetalk, nil, noredirect)
110
+ if result.key?('error')
111
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
112
+ else
113
+ return true
114
+ end
115
+ end
116
+
117
+ #protects a page.
118
+ #reason is the reason for the protection
119
+ #expiry is a timescamp (default is infinite).
120
+ #protections is the action and group that can perform that action, separated
121
+ #by pipes. Exapmple "edit=sysop|move=autoconfirmed".Default is edit=sysop|move=sysop
122
+ def protect(reason = nil, expiry = 'infinite', protections = 'edit=sysop|move=sysop')
123
+ #require login
124
+ @site.login
125
+ result = @site.query_prop_info(@normtitle, nil, 'protect')
126
+ token = result['query']['pages']['page']['protecttoken']
127
+ result = @site.protect(@normtitle, token, protections, expiry, reason)
128
+ if result.key?('error')
129
+ raise RbmediawikiError, "#{title}: "+result['error']['code']
130
+ else
131
+ return true
132
+ end
133
+ end
134
+
135
+ #semipotects a page.
136
+ #is the same as protect, but default for protections is "edit=autoconfirmed|move=autoconfirmed"
137
+ def semiprotect(reason = nil, expiry = 'infinite')
138
+ protect(reason, expiry, 'edit=autoconfirmed|move=autoconfirmed')
139
+ #possible errors: user doesn't have privileges
140
+ end
141
+
142
+ #delete the page.
143
+ #reason : reason for deleting
144
+ #returns true if success, raises NoPage if page doesn't exist
145
+ def delete(reason="")
146
+ @site.login
147
+ result = @site.query_prop_info(@normtitle, nil, 'delete')
148
+ token = result['query']['pages']['page']['deletetoken']
149
+ result = @site.delete(@normtitle, nil, token, reason)
150
+ if result.key?('error')
151
+ raise RbmediawikiError, "#{@title}: "+result['error']['code']
152
+ else
153
+ return true
154
+ end
155
+ end
156
+
157
+ #undeletes a page.
158
+ #reason: reason for deleting
159
+ #returns true if success, false if there aren't deleted revisions
160
+
161
+ def undelete(reason="")
162
+ @site.login
163
+ result = @site.query_list_deletedrevs(@normtitle, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 'token')
164
+ if result.key?('error')
165
+ raise RbmediawikiError, "#{@title}: "+result['error']['code']
166
+ end
167
+ if !result.has_key?('deletedRevs')
168
+ return false
169
+ end
170
+ token = result['query']['deletedrevs']['page']['token']
171
+ result = @site.undelete(@normtitle, token, reason)
172
+ return true
173
+ end
174
+
175
+ #rollback (revert) editr by user. Summary can be given
176
+ def rollback(user = nil, summary = nil, markbot = nil)
177
+ @site.login
178
+ result = @site.query_prop_revisions(@normtitle, nil, nil, nil, nil, nil, nil, nil, user, nil, nil, nil, nil, 'rollback')
179
+ #Page exists?
180
+ if result['query']['pages']['page'].key?('missing')
181
+ raise NoPage, "Page [[#{@title}]] does not exist"
182
+ end
183
+ #Has user edited this?
184
+ if !result['query']['pages']['page'].key?('revisions')
185
+ raise RbmediawikiError, "#{@title}: No edits by user #{user}"
186
+ end
187
+
188
+ #If the user made more than one contribs, this is an array
189
+ #but the token is the same. We only want the token
190
+ if result['query']['pages']['page']['revisions']['rev'].is_a? Array
191
+ token = result['query']['pages']['page']['revisions']['rev'][0]['rollbacktoken']
192
+ else
193
+ token = result['query']['pages']['page']['revisions']['rev']['rollbacktoken']
194
+ end
195
+ result = @site.rollback(@normtitle, user, token, summary, markbot)
196
+ if result.key?('error')
197
+ raise RbmediawikiError, "#{@title}: "+result['error']['code']
198
+ else
199
+ return true
200
+ end
201
+ end
202
+
203
+ #gets info about the protections of a page. Returns an array as for instance
204
+ #{level => sysop,type => edit,expiry => infinity}
205
+ #{level => sysop,type => move,expiry => infinity}
206
+ def protected?()
207
+ result = @site.query_prop_info(@normtitle, 'protection')
208
+ if result.key?('error')
209
+ raise RbmediawikiError, "#{@title}: "+result['error']['code']
210
+ end
211
+ if result['query']['pages']['page'].key?('missing')
212
+ raise NoPage, "Page [[#{@title}]] does not exist"
213
+
214
+ else
215
+ return result['query']['pages']['page']['protection']['pr']
216
+ end
217
+ end
218
+
219
+
220
+ #not working in r1.9
221
+ # #get interwiki links
222
+ # #min is the minimum number of elements to return, lllimit is the number of
223
+ # #elements to request from the API in each iteration. The method will
224
+ # #request elements until it has at least min elements.
225
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
226
+ # def get_interwikis(min = nil, lllimit = 500)
227
+ # llcontinue = nil
228
+ # iws = Hash.new
229
+ # count = 0
230
+ # loop {
231
+ # result = @site.query_prop_langlinks(@normtitle, lllimit, llcontinue)
232
+ # iws.deep_merge!(result['query'])
233
+ # if result.key?('query-continue') && min && count < min
234
+ # count += lllimit
235
+ # llcontinue = result['query-continue']['langlinks']['llcontinue']
236
+ # else
237
+ # break
238
+ # end
239
+ # }
240
+ # if iws['pages']['page'].key?('missing')
241
+ # raise NoPage.new(), "Page [[#{title}]] does not exist"
242
+ # elsif iws['pages']['page'].key?('langlinks')
243
+ # return iws['pages']['page']['langlinks']['ll']
244
+ # else return false
245
+ # end
246
+ # end
247
+ #
248
+ # #gets image links of a page
249
+ # #min is the minimum number of elements to return, imlimit is the number of
250
+ # #elements to request from the API in each iteration. The method will
251
+ # #request elements until it has at least min elements.
252
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
253
+ # def get_images(min = nil, imlimit = 500)
254
+ # imcontinue = nil
255
+ # ims = Hash.new
256
+ # count = 0
257
+ # loop {
258
+ # result = @site.query_prop_images(@normtitle, imlimit, imcontinue)
259
+ # ims.deep_merge!(result['query'])
260
+ # if result.key?('query-continue') && min && count < min
261
+ # count += lllimit
262
+ # imcontinue = result['query-continue']['images']['imcontinue']
263
+ # else
264
+ # break
265
+ # end
266
+ # }
267
+ # if ims['pages']['page'].key?('missing')
268
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
269
+ # elsif ims['pages']['page'].key?('images')
270
+ # return ims['pages']['page']['images']['im']
271
+ # else return false
272
+ # end
273
+ # end
274
+ #
275
+ # #gets templates used in a page
276
+ # #min is the minimum number of elements to return, tllimit is the number of
277
+ # #elements to request from the API in each iteration. The method will
278
+ # #request elements until it has at least min elements.
279
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
280
+ # def get_templates(min = nil, tllimit = 500)
281
+ # tlcontinue = nil
282
+ # tls = Hash.new
283
+ # count = 0
284
+ # loop {
285
+ # result = @site.query_prop_templates(@normtitle, nil, tllimit, tlcontinue)
286
+ # tls.deep_merge!(result['query'])
287
+ # if result.key?('query-continue')&& min && count < min
288
+ # count += lllimit
289
+ # tlcontinue = result['query-continue']['templates']['tlcontinue']
290
+ # else
291
+ # break
292
+ # end
293
+ # }
294
+ # if tls['pages']['page'].key?('missing')
295
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
296
+ # elsif tls['pages']['page'].key?('templates')
297
+ # return tls['pages']['page']['templates']['tl']
298
+ # else return false
299
+ # end
300
+ # end
301
+ #
302
+ # #gets templates used in a page
303
+ # #min is the minimum number of elements to return, cllimit is the number of
304
+ # #elements to request from the API in each iteration. The method will
305
+ # #request elements until it has at least min elements.
306
+ # #clshow can be "hidden" or "!hidden". Default shows both
307
+ # #if sortkey is true will return the sortkey. Default is true
308
+ # def get_categories(min = nil, cllimit = 500, clshow = nil, sortkey = true)
309
+ # clcontinue = nil
310
+ # cls = Hash.new
311
+ # count = 0
312
+ #
313
+ # if sortkey
314
+ # clprop = "sortkey"
315
+ # end
316
+ #
317
+ # loop {
318
+ # result = @site.query_prop_categories(@normtitle, clprop, clshow, cllimit, clcontinue)
319
+ # cls.deep_merge!(result['query'])
320
+ # if result.key?('query-continue')&& min && count < min
321
+ # count += lllimit
322
+ # clcontinue = result['query-continue']['categories']['clcontinue']
323
+ # else
324
+ # break
325
+ # end
326
+ # }
327
+ # if cls['pages']['page'].key?('missing')
328
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
329
+ # elsif cls['pages']['page'].key?('categories')
330
+ # return cls['pages']['page']['categories']['cl']
331
+ # else return false
332
+ # end
333
+ # end
334
+ #
335
+ # #gets external links used in a page
336
+ # #min is the minimum number of elements to return, ellimit is the number of
337
+ # #elements to request from the API in each iteration. The method will
338
+ # #request elements until it has at least min elements.
339
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
340
+ # def get_external_links(min = nil, ellimit = 500)
341
+ # eloffset = nil
342
+ # els = Hash.new
343
+ # count = 0
344
+ # loop {
345
+ # result = @site.query_prop_extlinks(@normtitle, ellimit, eloffset)
346
+ # els.deep_merge!(result['query'])
347
+ # if result.key?('query-continue')&& min && count < min
348
+ # count += lllimit
349
+ # eloffset = result['query-continue']['extlinks']['elcontinue']
350
+ # else
351
+ # break
352
+ # end
353
+ # }
354
+ # if els['pages']['page'].key?('missing')
355
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
356
+ # elsif els['pages']['page'].key?('extlinks')
357
+ # return els['pages']['page']['extlinks']['el']
358
+ # else return false
359
+ # end
360
+ # end
361
+ #
362
+ # #gets backlinks (what links here) used in a page
363
+ # #min is the minimum number of elements to return, bllimit is the number of
364
+ # #elements to request from the API in each iteration. The method will
365
+ # #request elements until it has at least min elements.
366
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
367
+ # def get_backlinks(min = nil, bllimit = 500, blnamespace = nil, blredirect = true)
368
+ # blcontinue = nil
369
+ # bls = Hash.new
370
+ # count = 0
371
+ # loop {
372
+ # result = @site.query_list_backlinks(@normtitle, @normtitle, blcontinue, blnamespace, nil, bllimit, blredirect)
373
+ # bls.deep_merge!(result['query'])
374
+ # if result.key?('query-continue')&& min && count < min
375
+ # count += lllimit
376
+ # blcontinue = result['query-continue']['backlinks']['blcontinue']
377
+ # else
378
+ # break
379
+ # end
380
+ # }
381
+ # if bls['pages']['page'].key?('missing')
382
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
383
+ # elsif bls['backlinks'].key?('bl')
384
+ # return bls['backlinks']['bl']
385
+ # else return false
386
+ # end
387
+ # end
388
+ #
389
+ # #gets deleted revisions of a page
390
+ # #min is the minimum number of elements to return, drlimit is the number of
391
+ # #elements to request from the API in each iteration. The method will
392
+ # #request elements until it has at least min elements.
393
+ # #returns false if there aren't any
394
+ # def get_deletedrevs(min = nil, drlimit = 500)
395
+ # @site.login
396
+ # drcontinue = nil
397
+ # drs = Hash.new
398
+ # count = 0
399
+ # loop {
400
+ # result = @site.query_list_deletedrevs(@normtitle, nil, nil, nil, nil, nil, nil, drcontinue, nil, nil, nil, nil, drlimit)
401
+ # drs.deep_merge!(result['query'])
402
+ # if result.key?('query-continue')&& min && count < min
403
+ # count += lllimit
404
+ # drcontinue = result['query-continue']['deletedrevs']['drstart']
405
+ # else
406
+ # break
407
+ # end
408
+ # }
409
+ # if drs['deletedrevs'].key?('page')
410
+ # return drs['deletedrevs']['page']['revisions']['rev']
411
+ # else return false
412
+ # end
413
+ # end
414
+ #
415
+ # #gets pages in which this page is embedded (or transcluded). Returns a list
416
+ # #of Page elements
417
+ # #min is the minimum number of elements to return, eilimit is the number of
418
+ # #elements to request from the API in each iteration. The method will
419
+ # #request elements until it has at least min elements.
420
+ # #returns false if there aren't any, and raises NoPage if page doesn't exist
421
+ # def get_embeddedin(min = nil, eilimit = 500)
422
+ # eicontinue = nil
423
+ # eis = Hash.new
424
+ # count = 0
425
+ # loop {
426
+ # result = @site.query_list_embeddedin(@normtitle, @normtitle, eicontinue, nil, nil, eilimit)
427
+ # eis.deep_merge!(result['query'])
428
+ # if result.key?('query-continue')&& min && count < min
429
+ # count += lllimit
430
+ # eicontinue = result['query-continue']['embeddedin']['eicontinue']
431
+ # else
432
+ # break
433
+ # end
434
+ # }
435
+ # if eis['pages']['page'].key?('missing')
436
+ # raise NoPage.new(), "Page [[#{@title}]] does not exist"
437
+ # elsif eis['embeddedin'].key?('ei')
438
+ # members = Array.new
439
+ # eis['embeddedin']['ei'].each{|el| members.push(Page.new(el['title']))}
440
+ # return members
441
+ # else return false
442
+ # end
443
+ # end
444
+ #
445
+ # #returns the size of the page content in bytes
446
+ # #Raises NoPage if the page doesn't exist
447
+ # def get_size
448
+ # result = @site.query_prop_info(@normtitle)
449
+ # if result['query']['pages']['page'].key?('missing')
450
+ # raise NoPage.new(), "Page [[#{@normtitle}]] does not exist"
451
+ # else
452
+ # return result['query']['pages']['page']['length']
453
+ # end
454
+ # end
455
+
456
+ end
457
+
458
+ class NoPage < RuntimeError
459
+ end
460
+
461
+ class PageExists < RuntimeError
462
+ end
463
+
464
+ class RbmediawikiError < RuntimeError
465
+ end