wp2txt 2.4.0 → 2.5.0

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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +16 -1
  3. data/.github/workflows/publish-image.yml +8 -4
  4. data/.gitignore +0 -1
  5. data/CHANGELOG.md +23 -0
  6. data/DEVELOPMENT.md +62 -92
  7. data/DEVELOPMENT_ja.md +60 -92
  8. data/Dockerfile +1 -1
  9. data/README.md +54 -8
  10. data/README_ja.md +54 -8
  11. data/bin/wp2txt +23 -6
  12. data/lib/wp2txt/article.rb +1 -2
  13. data/lib/wp2txt/bz2_stream.rb +63 -0
  14. data/lib/wp2txt/cli.rb +48 -26
  15. data/lib/wp2txt/config.rb +2 -11
  16. data/lib/wp2txt/constants.rb +0 -1
  17. data/lib/wp2txt/corpus.rb +1 -21
  18. data/lib/wp2txt/data/template_aliases.json +3 -0
  19. data/lib/wp2txt/data/template_patterns.json +61643 -0
  20. data/lib/wp2txt/dump_snapshot.rb +99 -0
  21. data/lib/wp2txt/extraction_run.rb +272 -0
  22. data/lib/wp2txt/extractor.rb +476 -325
  23. data/lib/wp2txt/fts_index.rb +1 -1
  24. data/lib/wp2txt/index_cache.rb +144 -55
  25. data/lib/wp2txt/index_commands.rb +22 -19
  26. data/lib/wp2txt/link_counter.rb +1 -1
  27. data/lib/wp2txt/magic_words.rb +44 -28
  28. data/lib/wp2txt/metadata_index.rb +6 -13
  29. data/lib/wp2txt/multistream.rb +890 -824
  30. data/lib/wp2txt/output_writer.rb +3 -1
  31. data/lib/wp2txt/parser_functions.rb +23 -5
  32. data/lib/wp2txt/ractor_worker.rb +6 -4
  33. data/lib/wp2txt/regex.rb +24 -4
  34. data/lib/wp2txt/site_info.rb +123 -0
  35. data/lib/wp2txt/stream_store.rb +91 -0
  36. data/lib/wp2txt/template_args.rb +111 -0
  37. data/lib/wp2txt/template_expander.rb +92 -125
  38. data/lib/wp2txt/template_patterns.rb +182 -0
  39. data/lib/wp2txt/utils.rb +160 -125
  40. data/lib/wp2txt/version.rb +1 -1
  41. data/spec/auto_download_spec.rb +9 -6
  42. data/spec/category_fetcher_spec.rb +135 -463
  43. data/spec/cli_targeted_spec.rb +60 -0
  44. data/spec/config_spec.rb +2 -15
  45. data/spec/constants_spec.rb +0 -4
  46. data/spec/dump_fetch_spec.rb +293 -0
  47. data/spec/extraction_run_spec.rb +520 -0
  48. data/spec/index_build_spec.rb +113 -0
  49. data/spec/index_commands_version_spec.rb +17 -0
  50. data/spec/multistream_reader_spec.rb +140 -0
  51. data/spec/multistream_spec.rb +8 -214
  52. data/spec/output_integrity_spec.rb +45 -2
  53. data/spec/regex_spec.rb +30 -0
  54. data/spec/spec_helper.rb +3 -3
  55. data/spec/support/dump_status.rb +34 -0
  56. data/spec/support/multistream_fixture.rb +26 -8
  57. data/spec/template_args_spec.rb +133 -0
  58. data/spec/template_expander_spec.rb +29 -111
  59. data/spec/template_passthrough_spec.rb +17 -2
  60. data/spec/template_patterns_spec.rb +253 -0
  61. data/spec/tracked_paths_spec.rb +168 -0
  62. data/spec/utils_spec.rb +7 -1
  63. metadata +30 -5
  64. data/lib/wp2txt/category_cache.rb +0 -313
  65. data/spec/category_cache_spec.rb +0 -226
@@ -2,503 +2,175 @@
2
2
 
3
3
  require_relative "spec_helper"
4
4
  require "webmock/rspec"
5
- require "tmpdir"
6
- require "fileutils"
5
+ require "json"
7
6
 
7
+ # Listing categories through a simulated MediaWiki API: categories are held
8
+ # by page ID, members are served two per page with continuation, and any
9
+ # request can be made to fail.
8
10
  RSpec.describe Wp2txt::CategoryFetcher do
9
- let(:lang) { "en" }
10
- let(:category) { "Japanese cities" }
11
- let(:fetcher) { described_class.new(lang, category) }
12
-
13
- before do
14
- WebMock.enable!
15
- WebMock.disable_net_connect!
11
+ # title => { id:, members: [[ns, title, pageid], ...] }
12
+ def wiki(categories)
13
+ @categories = categories
14
+ @failures = {}
15
+ @requests = []
16
+ stub_request(:get, /\w+\.wikipedia\.org\/w\/api\.php/).to_return do |request|
17
+ params = URI.decode_www_form(request.uri.query).to_h
18
+ @requests << params
19
+ failure = @failures[[params["cmpageid"]&.to_i, params["cmcontinue"]]]
20
+ next failure if failure
21
+
22
+ { status: 200, body: JSON.generate(answer(params)) }
23
+ end
16
24
  end
17
25
 
18
- after do
19
- WebMock.disable!
26
+ def fail_at(page_id, continue_token = nil, response)
27
+ @failures[[page_id, continue_token]] = response
20
28
  end
21
29
 
22
- describe "#initialize" do
23
- it "accepts language and category name" do
24
- expect(fetcher.lang).to eq "en"
25
- expect(fetcher.category).to eq "Japanese cities"
26
- end
27
-
28
- it "normalizes category name by removing Category: prefix" do
29
- f = described_class.new("en", "Category:Test Category")
30
- expect(f.category).to eq "Test Category"
30
+ def answer(params)
31
+ if params["titles"]
32
+ cat = @categories[params["titles"]]
33
+ page = cat ? { "pageid" => cat[:id], "ns" => 14, "title" => params["titles"] } : { "ns" => 14, "title" => params["titles"], "missing" => true }
34
+ return { "query" => { "pages" => [page] } }
31
35
  end
32
36
 
33
- it "handles lowercase category prefix" do
34
- f = described_class.new("en", "category:Another Test")
35
- expect(f.category).to eq "Another Test"
36
- end
37
-
38
- it "trims whitespace from category name" do
39
- f = described_class.new("en", " Test Category ")
40
- expect(f.category).to eq "Test Category"
41
- end
42
-
43
- it "defaults max_depth to 0" do
44
- expect(fetcher.max_depth).to eq 0
45
- end
46
-
47
- it "accepts custom max_depth" do
48
- f = described_class.new("ja", "Test", max_depth: 3)
49
- expect(f.max_depth).to eq 3
50
- end
37
+ _, cat = @categories.find { |_, c| c[:id] == params["cmpageid"].to_i }
38
+ start = params["cmcontinue"].to_i
39
+ members = cat[:members].map { |ns, title, id| { "pageid" => id, "ns" => ns, "title" => title } }
40
+ body = { "batchcomplete" => true, "query" => { "categorymembers" => members[start, 2] || [] } }
41
+ body["continue"] = { "cmcontinue" => (start + 2).to_s, "continue" => "-||" } if start + 2 < members.size
42
+ body
51
43
  end
52
44
 
53
- describe "#fetch_preview" do
54
- it "returns statistics without full article list" do
55
- stub_request(:get, /en\.wikipedia\.org/)
56
- .to_return(
57
- status: 200,
58
- body: {
59
- query: {
60
- categorymembers: [
61
- { ns: 0, title: "Tokyo" },
62
- { ns: 0, title: "Osaka" },
63
- { ns: 0, title: "Kyoto" }
64
- ]
65
- }
66
- }.to_json
67
- )
68
-
69
- preview = fetcher.fetch_preview
70
-
71
- expect(preview[:category]).to eq "Japanese cities"
72
- expect(preview[:depth]).to eq 0
73
- expect(preview[:total_articles]).to eq 3
74
- expect(preview[:subcategories]).to be_an(Array)
75
- end
76
-
77
- it "includes subcategory statistics when depth > 0" do
78
- f = described_class.new("en", "Japanese cities", max_depth: 1)
79
-
80
- # Parent category
81
- stub_request(:get, /cmtitle=Category:Japanese%20cities/)
82
- .to_return(
83
- status: 200,
84
- body: {
85
- query: {
86
- categorymembers: [
87
- { ns: 0, title: "Tokyo" },
88
- { ns: 14, title: "Category:Cities in Kanto" }
89
- ]
90
- }
91
- }.to_json
92
- )
93
-
94
- # Subcategory
95
- stub_request(:get, /cmtitle=Category:Cities%20in%20Kanto/)
96
- .to_return(
97
- status: 200,
98
- body: {
99
- query: {
100
- categorymembers: [
101
- { ns: 0, title: "Yokohama" },
102
- { ns: 0, title: "Chiba" }
103
- ]
104
- }
105
- }.to_json
106
- )
107
-
108
- preview = f.fetch_preview
109
-
110
- expect(preview[:total_articles]).to eq 3
111
- expect(preview[:total_subcategories]).to eq 1
112
- expect(preview[:subcategories].size).to eq 2
113
- end
45
+ before do
46
+ WebMock.enable!
47
+ WebMock.disable_net_connect!
114
48
  end
115
49
 
116
- describe "#fetch_articles" do
117
- it "fetches articles from single page response" do
118
- stub_request(:get, /en\.wikipedia\.org/)
119
- .to_return(
120
- status: 200,
121
- body: {
122
- query: {
123
- categorymembers: [
124
- { ns: 0, title: "Tokyo" },
125
- { ns: 0, title: "Osaka" },
126
- { ns: 0, title: "Kyoto" }
127
- ]
128
- }
129
- }.to_json
130
- )
131
-
132
- articles = fetcher.fetch_articles
133
- expect(articles).to contain_exactly("Tokyo", "Osaka", "Kyoto")
134
- end
135
-
136
- it "handles pagination with cmcontinue token" do
137
- # First request
138
- stub_request(:get, /en\.wikipedia\.org/)
139
- .with(query: hash_excluding("cmcontinue"))
140
- .to_return(
141
- status: 200,
142
- body: {
143
- query: {
144
- categorymembers: [
145
- { ns: 0, title: "Article1" },
146
- { ns: 0, title: "Article2" }
147
- ]
148
- },
149
- continue: { cmcontinue: "page2token" }
150
- }.to_json
151
- )
152
-
153
- # Second request with continue token
154
- stub_request(:get, /en\.wikipedia\.org/)
155
- .with(query: hash_including("cmcontinue" => "page2token"))
156
- .to_return(
157
- status: 200,
158
- body: {
159
- query: {
160
- categorymembers: [
161
- { ns: 0, title: "Article3" }
162
- ]
163
- }
164
- }.to_json
165
- )
166
-
167
- articles = fetcher.fetch_articles
168
- expect(articles.size).to eq 3
169
- expect(articles).to include("Article1", "Article2", "Article3")
170
- end
171
-
172
- it "returns unique articles when duplicates exist" do
173
- stub_request(:get, /en\.wikipedia\.org/)
174
- .to_return(
175
- status: 200,
176
- body: {
177
- query: {
178
- categorymembers: [
179
- { ns: 0, title: "Tokyo" },
180
- { ns: 0, title: "Tokyo" },
181
- { ns: 0, title: "Osaka" }
182
- ]
183
- }
184
- }.to_json
185
- )
186
-
187
- articles = fetcher.fetch_articles
188
- expect(articles).to contain_exactly("Tokyo", "Osaka")
189
- end
190
-
191
- it "returns empty array for non-existent category" do
192
- stub_request(:get, /en\.wikipedia\.org/)
193
- .to_return(
194
- status: 200,
195
- body: { query: { categorymembers: [] } }.to_json
196
- )
197
-
198
- articles = fetcher.fetch_articles
199
- expect(articles).to be_empty
200
- end
201
-
202
- it "handles API errors gracefully" do
203
- stub_request(:get, /en\.wikipedia\.org/)
204
- .to_return(status: 500)
205
-
206
- articles = fetcher.fetch_articles
207
- expect(articles).to be_empty
208
- end
209
-
210
- it "handles network timeout gracefully" do
211
- stub_request(:get, /en\.wikipedia\.org/)
212
- .to_timeout
213
-
214
- articles = fetcher.fetch_articles
215
- expect(articles).to be_empty
216
- end
217
-
218
- it "handles malformed JSON response gracefully" do
219
- stub_request(:get, /en\.wikipedia\.org/)
220
- .to_return(
221
- status: 200,
222
- body: "not valid json"
223
- )
224
-
225
- articles = fetcher.fetch_articles
226
- expect(articles).to be_empty
227
- end
50
+ after do
51
+ WebMock.reset!
52
+ WebMock.allow_net_connect!
228
53
  end
229
54
 
230
- describe "subcategory recursion" do
231
- it "does not recurse into subcategories when max_depth is 0" do
232
- stub_request(:get, /cmtitle=Category:Japanese%20cities/)
233
- .to_return(
234
- status: 200,
235
- body: {
236
- query: {
237
- categorymembers: [
238
- { ns: 0, title: "Tokyo" },
239
- { ns: 14, title: "Category:Cities in Kanto" }
240
- ]
241
- }
242
- }.to_json
243
- )
244
-
245
- articles = fetcher.fetch_articles
246
- expect(articles).to eq ["Tokyo"]
247
- expect(WebMock).not_to have_requested(:get, /cmtitle=Category:Cities%20in%20Kanto/)
248
- end
249
-
250
- it "recurses into subcategories when max_depth > 0" do
251
- f = described_class.new("en", "Japanese cities", max_depth: 1)
252
-
253
- # Parent category
254
- stub_request(:get, /cmtitle=Category:Japanese%20cities/)
255
- .to_return(
256
- status: 200,
257
- body: {
258
- query: {
259
- categorymembers: [
260
- { ns: 0, title: "Tokyo" },
261
- { ns: 14, title: "Category:Cities in Kanto" }
262
- ]
263
- }
264
- }.to_json
265
- )
266
-
267
- # Subcategory
268
- stub_request(:get, /cmtitle=Category:Cities%20in%20Kanto/)
269
- .to_return(
270
- status: 200,
271
- body: {
272
- query: {
273
- categorymembers: [
274
- { ns: 0, title: "Yokohama" },
275
- { ns: 0, title: "Chiba" }
276
- ]
277
- }
278
- }.to_json
279
- )
280
-
281
- articles = f.fetch_articles
282
- expect(articles).to contain_exactly("Tokyo", "Yokohama", "Chiba")
283
- end
284
-
285
- it "prevents infinite loops with circular category references" do
286
- f = described_class.new("en", "Category A", max_depth: 5)
287
-
288
- stub_request(:get, /cmtitle=Category:Category%20A/)
289
- .to_return(
290
- status: 200,
291
- body: {
292
- query: {
293
- categorymembers: [
294
- { ns: 0, title: "Article1" },
295
- { ns: 14, title: "Category:Category B" }
296
- ]
297
- }
298
- }.to_json
299
- )
300
-
301
- stub_request(:get, /cmtitle=Category:Category%20B/)
302
- .to_return(
303
- status: 200,
304
- body: {
305
- query: {
306
- categorymembers: [
307
- { ns: 0, title: "Article2" },
308
- { ns: 14, title: "Category:Category A" }
309
- ]
310
- }
311
- }.to_json
312
- )
313
-
314
- # Should complete without infinite loop
315
- expect { f.fetch_articles }.not_to raise_error
316
- articles = f.fetch_articles
317
- expect(articles).to include("Article1", "Article2")
318
- end
319
-
320
- it "respects max_depth limit" do
321
- f = described_class.new("en", "Root", max_depth: 1)
322
-
323
- # Root
324
- stub_request(:get, /cmtitle=Category:Root/)
325
- .to_return(
326
- status: 200,
327
- body: {
328
- query: {
329
- categorymembers: [
330
- { ns: 14, title: "Category:Level1" }
331
- ]
332
- }
333
- }.to_json
334
- )
335
-
336
- # Level 1
337
- stub_request(:get, /cmtitle=Category:Level1/)
338
- .to_return(
339
- status: 200,
340
- body: {
341
- query: {
342
- categorymembers: [
343
- { ns: 0, title: "Article1" },
344
- { ns: 14, title: "Category:Level2" }
345
- ]
346
- }
347
- }.to_json
348
- )
349
-
350
- # Level 2 should not be called
351
- stub_request(:get, /cmtitle=Category:Level2/)
352
- .to_return(
353
- status: 200,
354
- body: {
355
- query: { categorymembers: [{ ns: 0, title: "Article2" }] }
356
- }.to_json
357
- )
55
+ def fetcher(category, depth: 0, lang: "en")
56
+ described_class.new(lang, category, max_depth: depth).tap { |f| allow(f).to receive(:sleep) }
57
+ end
358
58
 
359
- articles = f.fetch_articles
360
- expect(articles).to eq ["Article1"]
361
- expect(WebMock).not_to have_requested(:get, /cmtitle=Category:Level2/)
59
+ describe "#initialize" do
60
+ it "keeps the category name without a Category: prefix" do
61
+ expect(described_class.new("en", " category:Japanese cities ").category).to eq("Japanese cities")
62
+ expect(described_class.new("en", "Test").max_depth).to eq(0)
362
63
  end
363
64
  end
364
65
 
365
- describe "caching" do
366
- let(:cache_dir) { Dir.mktmpdir("wp2txt_test_") }
367
-
368
- after do
369
- FileUtils.rm_rf(cache_dir)
66
+ describe "listing a category" do
67
+ it "lists every article through all continuation, with page IDs" do
68
+ wiki("Category:Cities" => { id: 1, members: [[0, "Tokyo", 10], [0, "Osaka", 11], [0, "Kyoto", 12],
69
+ [0, "Nara", 13], [0, "Kobe", 14]] })
70
+ listing = fetcher("Cities").fetch_listing
71
+ expect(listing.articles).to eq("Tokyo" => 10, "Osaka" => 11, "Kyoto" => 12, "Nara" => 13, "Kobe" => 14)
72
+ expect(listing.complete).to be(true)
73
+ expect(@requests.map { |r| r["continue"] }).to eq([nil, nil, "-||", "-||"])
370
74
  end
371
75
 
372
- it "caches category members to SQLite database" do
373
- f = described_class.new("en", "Test Category")
374
- f.enable_cache(cache_dir)
375
-
376
- stub_request(:get, /en\.wikipedia\.org/)
377
- .to_return(
378
- status: 200,
379
- body: {
380
- query: {
381
- categorymembers: [{ ns: 0, title: "Cached Article" }]
382
- }
383
- }.to_json
384
- )
385
-
386
- f.fetch_articles
387
-
388
- # SQLite cache file should exist
389
- cache_files = Dir.glob(File.join(cache_dir, "categories_*.sqlite3"))
390
- expect(cache_files.size).to eq 1
76
+ it "reads the listing once for the preview and the articles" do
77
+ wiki("Category:Cities" => { id: 1, members: [[0, "Tokyo", 10]] })
78
+ f = fetcher("Cities")
79
+ expect(f.fetch_preview).to include(total_articles: 1, total_subcategories: 0)
80
+ expect(f.fetch_articles).to eq(["Tokyo"])
81
+ expect(@requests.size).to eq(2)
391
82
  end
392
83
 
393
- it "uses cached data on subsequent calls" do
394
- f = described_class.new("en", "Test Category")
395
- f.enable_cache(cache_dir)
396
-
397
- # Pre-populate cache using CategoryCache
398
- cache = Wp2txt::CategoryCache.new("en", cache_dir: cache_dir)
399
- cache.save("Test Category", ["Cached Article"], [])
400
- cache.close
401
-
402
- # Should not make API request
403
- articles = f.fetch_articles
404
- expect(articles).to eq ["Cached Article"]
405
- expect(WebMock).not_to have_requested(:get, /wikipedia\.org/)
84
+ it "follows subcategories by page ID, whatever the wiki calls the namespace" do
85
+ wiki("Category:Wurzel" => { id: 1, members: [[0, "A", 10], [14, "Kategorie:Kind", 2]] },
86
+ "Kategorie:Kind" => { id: 2, members: [[0, "B", 11]] })
87
+ listing = fetcher("Wurzel", depth: 1, lang: "de").fetch_listing
88
+ expect(listing.articles.keys).to eq(%w[A B])
89
+ expect(listing.categories.map { |c| c[:name] }).to eq(%w[Wurzel Kind])
90
+ expect(@requests.map { |r| r["cmpageid"] }.compact).to eq(%w[1 2])
406
91
  end
407
92
 
408
- it "ignores stale cache" do
409
- f = described_class.new("en", "Test Category")
410
- f.enable_cache(cache_dir)
411
-
412
- # Pre-populate cache using CategoryCache
413
- cache = Wp2txt::CategoryCache.new("en", cache_dir: cache_dir, expiry_days: 7)
414
- cache.save("Test Category", ["Old Article"], [])
415
-
416
- # Manually update cached_at to make it old (8 days ago)
417
- cache.instance_variable_get(:@db).execute(
418
- "UPDATE categories SET cached_at = ? WHERE name = ?",
419
- [Time.now.to_i - (8 * 24 * 3600), "Test Category"]
420
- )
421
- cache.close
422
-
423
- stub_request(:get, /en\.wikipedia\.org/)
424
- .to_return(
425
- status: 200,
426
- body: {
427
- query: {
428
- categorymembers: [{ ns: 0, title: "Fresh Article" }]
429
- }
430
- }.to_json
431
- )
432
-
433
- articles = f.fetch_articles
434
- expect(articles).to eq ["Fresh Article"]
93
+ it "takes a category named with a local namespace prefix" do
94
+ wiki("Kategorie:Wurzel" => { id: 1, members: [[0, "A", 10]] })
95
+ expect(fetcher("Kategorie:Wurzel", lang: "de").fetch_articles).to eq(["A"])
435
96
  end
436
- end
437
-
438
- describe "special characters in category names" do
439
- it "handles spaces in category names" do
440
- f = described_class.new("en", "Japanese cities")
441
97
 
442
- stub_request(:get, /cmtitle=Category:Japanese%20cities/)
443
- .to_return(
444
- status: 200,
445
- body: { query: { categorymembers: [{ ns: 0, title: "Tokyo" }] } }.to_json
446
- )
447
-
448
- articles = f.fetch_articles
449
- expect(articles).to eq ["Tokyo"]
98
+ [["A", "B"], ["B", "A"]].each do |order|
99
+ it "reaches what lies within the depth, whichever path the API lists first (#{order.join(', ')})" do
100
+ # Root -> A -> B -> C and Root -> B -> C: C is two steps from Root
101
+ members = order.map { |name| [14, "Category:#{name}", name == "A" ? 2 : 3] }
102
+ wiki("Category:Root" => { id: 1, members: members },
103
+ "Category:A" => { id: 2, members: [[14, "Category:B", 3]] },
104
+ "Category:B" => { id: 3, members: [[14, "Category:C", 4]] },
105
+ "Category:C" => { id: 4, members: [[0, "Film A", 10]] })
106
+ expect(fetcher("Root", depth: 2).fetch_articles).to eq(["Film A"])
107
+ end
450
108
  end
451
109
 
452
- it "handles Unicode category names" do
453
- f = described_class.new("ja", "日本の都市")
454
-
455
- stub_request(:get, /ja\.wikipedia\.org/)
456
- .to_return(
457
- status: 200,
458
- body: { query: { categorymembers: [{ ns: 0, title: "東京" }] } }.to_json
459
- )
460
-
461
- articles = f.fetch_articles
462
- expect(articles).to eq ["東京"]
110
+ it "stops at the depth asked for and visits each category once" do
111
+ wiki("Category:Root" => { id: 1, members: [[0, "R", 10], [14, "Category:Sub", 2]] },
112
+ "Category:Sub" => { id: 2, members: [[0, "S", 11], [14, "Category:Root", 1], [14, "Category:Deep", 3]] },
113
+ "Category:Deep" => { id: 3, members: [[0, "D", 12]] })
114
+ expect(fetcher("Root", depth: 1).fetch_articles).to eq(%w[R S])
115
+ expect(@requests.count { |r| r["cmpageid"] == "1" }).to eq(1)
463
116
  end
464
117
 
465
- it "handles special characters in category names" do
466
- f = described_class.new("en", "Rock & Roll")
467
-
468
- stub_request(:get, /cmtitle=Category:Rock%20%26%20Roll/)
469
- .to_return(
470
- status: 200,
471
- body: { query: { categorymembers: [{ ns: 0, title: "Elvis" }] } }.to_json
472
- )
473
-
474
- articles = f.fetch_articles
475
- expect(articles).to eq ["Elvis"]
118
+ it "keeps titles in other scripts and with punctuation as the API gives them" do
119
+ wiki("Category:日本の都市" => { id: 1, members: [[0, "東京都", 10], [0, "Washington, D.C.", 11]] })
120
+ expect(fetcher("日本の都市", lang: "ja").fetch_articles).to eq(["東京都", "Washington, D.C."])
476
121
  end
477
- end
478
122
 
479
- describe "multilingual support" do
480
- it "works with Japanese Wikipedia" do
481
- f = described_class.new("ja", "日本の都市")
482
-
483
- stub_request(:get, /ja\.wikipedia\.org/)
484
- .to_return(
485
- status: 200,
486
- body: { query: { categorymembers: [{ ns: 0, title: "東京" }] } }.to_json
487
- )
488
-
489
- expect(f.fetch_articles).to eq ["東京"]
123
+ it "reports a category that does not exist instead of an empty one" do
124
+ wiki({})
125
+ expect { fetcher("Nowhere").fetch_listing }.to raise_error(described_class::CategoryNotFound, /Nowhere/)
490
126
  end
127
+ end
491
128
 
492
- it "works with German Wikipedia" do
493
- f = described_class.new("de", "Stadt in Deutschland")
494
-
495
- stub_request(:get, /de\.wikipedia\.org/)
496
- .to_return(
497
- status: 200,
498
- body: { query: { categorymembers: [{ ns: 0, title: "Berlin" }] } }.to_json
499
- )
500
-
501
- expect(f.fetch_articles).to eq ["Berlin"]
129
+ describe "a listing that cannot be read in full" do
130
+ def expect_incomplete(f)
131
+ error = nil
132
+ expect { f.fetch_listing }.to raise_error(described_class::IncompleteListing) { |e| error = e }
133
+ expect(error.listing.complete).to be(false)
134
+ error.listing
135
+ end
136
+
137
+ let(:cities) { { "Category:Cities" => { id: 1, members: [[0, "Tokyo", 10], [0, "Osaka", 11], [0, "Kyoto", 12]] } } }
138
+
139
+ it "carries what was read before a failing page" do
140
+ wiki(cities)
141
+ fail_at(1, "2", { status: 503 })
142
+ listing = nil
143
+ expect { listing = expect_incomplete(fetcher("Cities")) }.to output(/attempt/).to_stderr
144
+ expect(listing.articles.keys).to eq(%w[Tokyo Osaka])
145
+ expect(listing.error).to include("Cities")
146
+ end
147
+
148
+ {
149
+ "an API error" => { status: 200, body: JSON.generate("error" => { "code" => "x", "info" => "Bad request" }) },
150
+ "a response that is not JSON" => { status: 200, body: "<html>" },
151
+ "a response without the member list" => { status: 200, body: JSON.generate("query" => {}) },
152
+ "a client error" => { status: 403 }
153
+ }.each do |label, response|
154
+ it "after #{label}" do
155
+ wiki(cities)
156
+ fail_at(1, nil, response)
157
+ expect(expect_incomplete(fetcher("Cities")).articles).to be_empty
158
+ end
159
+ end
160
+
161
+ it "keeps reporting the failure when asked again" do
162
+ wiki(cities)
163
+ fail_at(1, "2", { status: 200, body: JSON.generate("query" => "broken") })
164
+ f = fetcher("Cities")
165
+ expect_incomplete(f)
166
+ expect { f.fetch_articles }.to raise_error(described_class::IncompleteListing)
167
+ end
168
+
169
+ it "when the continuation does not advance" do
170
+ wiki(cities)
171
+ fail_at(1, "2", { status: 200, body: JSON.generate("query" => { "categorymembers" => [] },
172
+ "continue" => { "cmcontinue" => "2", "continue" => "-||" }) })
173
+ expect_incomplete(fetcher("Cities"))
502
174
  end
503
175
  end
504
176
  end