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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +16 -1
- data/.github/workflows/publish-image.yml +8 -4
- data/.gitignore +0 -1
- data/CHANGELOG.md +23 -0
- data/DEVELOPMENT.md +62 -92
- data/DEVELOPMENT_ja.md +60 -92
- data/Dockerfile +1 -1
- data/README.md +54 -8
- data/README_ja.md +54 -8
- data/bin/wp2txt +23 -6
- data/lib/wp2txt/article.rb +1 -2
- data/lib/wp2txt/bz2_stream.rb +63 -0
- data/lib/wp2txt/cli.rb +48 -26
- data/lib/wp2txt/config.rb +2 -11
- data/lib/wp2txt/constants.rb +0 -1
- data/lib/wp2txt/corpus.rb +1 -21
- data/lib/wp2txt/data/template_aliases.json +3 -0
- data/lib/wp2txt/data/template_patterns.json +61643 -0
- data/lib/wp2txt/dump_snapshot.rb +99 -0
- data/lib/wp2txt/extraction_run.rb +272 -0
- data/lib/wp2txt/extractor.rb +476 -325
- data/lib/wp2txt/fts_index.rb +1 -1
- data/lib/wp2txt/index_cache.rb +144 -55
- data/lib/wp2txt/index_commands.rb +22 -19
- data/lib/wp2txt/link_counter.rb +1 -1
- data/lib/wp2txt/magic_words.rb +44 -28
- data/lib/wp2txt/metadata_index.rb +6 -13
- data/lib/wp2txt/multistream.rb +890 -824
- data/lib/wp2txt/output_writer.rb +3 -1
- data/lib/wp2txt/parser_functions.rb +23 -5
- data/lib/wp2txt/ractor_worker.rb +6 -4
- data/lib/wp2txt/regex.rb +24 -4
- data/lib/wp2txt/site_info.rb +123 -0
- data/lib/wp2txt/stream_store.rb +91 -0
- data/lib/wp2txt/template_args.rb +111 -0
- data/lib/wp2txt/template_expander.rb +92 -125
- data/lib/wp2txt/template_patterns.rb +182 -0
- data/lib/wp2txt/utils.rb +160 -125
- data/lib/wp2txt/version.rb +1 -1
- data/spec/auto_download_spec.rb +9 -6
- data/spec/category_fetcher_spec.rb +135 -463
- data/spec/cli_targeted_spec.rb +60 -0
- data/spec/config_spec.rb +2 -15
- data/spec/constants_spec.rb +0 -4
- data/spec/dump_fetch_spec.rb +293 -0
- data/spec/extraction_run_spec.rb +520 -0
- data/spec/index_build_spec.rb +113 -0
- data/spec/index_commands_version_spec.rb +17 -0
- data/spec/multistream_reader_spec.rb +140 -0
- data/spec/multistream_spec.rb +8 -214
- data/spec/output_integrity_spec.rb +45 -2
- data/spec/regex_spec.rb +30 -0
- data/spec/spec_helper.rb +3 -3
- data/spec/support/dump_status.rb +34 -0
- data/spec/support/multistream_fixture.rb +26 -8
- data/spec/template_args_spec.rb +133 -0
- data/spec/template_expander_spec.rb +29 -111
- data/spec/template_passthrough_spec.rb +17 -2
- data/spec/template_patterns_spec.rb +253 -0
- data/spec/tracked_paths_spec.rb +168 -0
- data/spec/utils_spec.rb +7 -1
- metadata +30 -5
- data/lib/wp2txt/category_cache.rb +0 -313
- 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 "
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
19
|
-
|
|
26
|
+
def fail_at(page_id, continue_token = nil, response)
|
|
27
|
+
@failures[[page_id, continue_token]] = response
|
|
20
28
|
end
|
|
21
29
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
360
|
-
|
|
361
|
-
expect(
|
|
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 "
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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 "
|
|
373
|
-
|
|
374
|
-
f
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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 "
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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 "
|
|
409
|
-
|
|
410
|
-
|
|
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
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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 "
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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 "
|
|
466
|
-
|
|
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
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
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
|