rbmediawiki 0.2 → 0.2.1

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