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
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
require_relative "support/multistream_fixture"
|
|
6
|
+
require "wp2txt/multistream"
|
|
7
|
+
|
|
8
|
+
RSpec.describe Wp2txt::MultistreamReader do
|
|
9
|
+
include MultistreamFixture
|
|
10
|
+
|
|
11
|
+
around do |example|
|
|
12
|
+
Dir.mktmpdir("wp2txt-reader-") do |dir|
|
|
13
|
+
@dir = dir
|
|
14
|
+
example.run
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Build a dump from a list of streams, each a string of XML, and an index
|
|
19
|
+
# listing the given titles at the offset of the stream holding them.
|
|
20
|
+
def write_dump(streams, titles_by_stream)
|
|
21
|
+
compressed = streams.map { |xml| xml.is_a?(Hash) ? xml[:raw] : bzip2(xml) }
|
|
22
|
+
dump = File.join(@dir, "dump.xml.bz2")
|
|
23
|
+
File.binwrite(dump, compressed.join)
|
|
24
|
+
offsets = compressed.each_with_index.map { |_, i| compressed[0...i].sum(&:bytesize) }
|
|
25
|
+
lines = titles_by_stream.flat_map do |i, titles|
|
|
26
|
+
titles.each_with_index.map { |t, j| "#{offsets[i]}:#{i * 10 + j + 1}:#{t}" }
|
|
27
|
+
end
|
|
28
|
+
index_path = File.join(@dir, "index.txt")
|
|
29
|
+
File.write(index_path, lines.join("\n") + "\n")
|
|
30
|
+
index = Wp2txt::MultistreamIndex.new(index_path, use_cache: false, show_progress: false)
|
|
31
|
+
described_class.new(dump, index)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def page(title, text, id: 1, **opts)
|
|
35
|
+
page_xml(id: id, ns: 0, title: title, text: text, **opts)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "reads the pages of the first and the last range of a dump laid out by Wikimedia" do
|
|
39
|
+
dump, index_path = create_fixture(@dir)
|
|
40
|
+
reader = described_class.new(dump, Wp2txt::MultistreamIndex.new(index_path, use_cache: false,
|
|
41
|
+
show_progress: false))
|
|
42
|
+
expect(reader.extract_article("Film A")).to include(title: "Film A", id: 1, ns: 0, text_available: true)
|
|
43
|
+
# The last range runs to the end of the file and so also holds </mediawiki>
|
|
44
|
+
expect(reader.extract_article("Category:Films")).to include(title: "Category:Films", text: "Top category.\n")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "treats a closing tag before the last range as a malformed range" do
|
|
48
|
+
reader = write_dump([page("Early", "x", id: 1) + "</mediawiki>", page("Late", "y", id: 2)],
|
|
49
|
+
{ 0 => ["Early"], 1 => ["Late"] })
|
|
50
|
+
expect { reader.extract_article("Early") }.to raise_error(Wp2txt::StreamError, /malformed/)
|
|
51
|
+
expect(reader.extract_article("Late")).to include(text: "y")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "reads pages that share a stream with the dump header" do
|
|
55
|
+
reader = write_dump([MultistreamFixture::FIXTURE_HEADER + page("Première", "Texte."), "</mediawiki>"],
|
|
56
|
+
{ 0 => ["Première"] })
|
|
57
|
+
expect(reader.extract_article("Première")).to include(text: "Texte.")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "keeps reading the dump file it was given when the path gets another file" do
|
|
61
|
+
reader = write_dump([page("Same", "old text")], { 0 => ["Same"] })
|
|
62
|
+
other = File.join(@dir, "other.xml.bz2")
|
|
63
|
+
File.binwrite(other, bzip2(page("Same", "new text")))
|
|
64
|
+
File.rename(other, reader.multistream_path)
|
|
65
|
+
expect(reader.extract_article("Same")).to include(text: "old text")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "reports the redirect target the dump records" do
|
|
69
|
+
reader = write_dump([page("Old", "#REDIRECT [[New]]", redirect: "New")], { 0 => ["Old"] })
|
|
70
|
+
expect(reader.extract_article("Old")).to include(redirect: "New")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "tells a suppressed text from an empty one" do
|
|
74
|
+
suppressed = "<page><title>Gone</title><ns>0</ns><id>2</id><revision><id>3</id>" \
|
|
75
|
+
"<text deleted=\"deleted\" /></revision></page>"
|
|
76
|
+
empty = "<page><title>Empty</title><ns>0</ns><id>4</id><revision><id>5</id><text /></revision></page>"
|
|
77
|
+
reader = write_dump([suppressed + empty], { 0 => %w[Gone Empty] })
|
|
78
|
+
expect(reader.extract_article("Gone")).to include(text: "", text_available: false)
|
|
79
|
+
expect(reader.extract_article("Empty")).to include(text: "", text_available: true)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it "keeps multibyte text and replaces bytes that are not UTF-8" do
|
|
83
|
+
xml = page("日本語", "本文@@です。").b.sub("@@".b, "\xFF".b)
|
|
84
|
+
reader = write_dump([xml], { 0 => ["日本語"] })
|
|
85
|
+
expect(reader.extract_article("日本語")[:text]).to eq("本文�です。")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "refuses a range whose bzip2 data is cut short" do
|
|
89
|
+
whole = bzip2(page("Cut", "a" * 50_000))
|
|
90
|
+
reader = write_dump([{ raw: whole[0...-30] }], { 0 => ["Cut"] })
|
|
91
|
+
expect { reader.extract_article("Cut") }.to raise_error(Wp2txt::StreamError, /incomplete or corrupt/)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "refuses a range whose pages are not well-formed" do
|
|
95
|
+
reader = write_dump(["<page><title>Open</title><ns>0</ns><id>1</id><revision><text>unclosed"],
|
|
96
|
+
{ 0 => ["Open"] })
|
|
97
|
+
expect { reader.extract_article("Open") }.to raise_error(Wp2txt::StreamError, /malformed/)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe "#each_entry_result" do
|
|
101
|
+
it "hands over each range's results before reading the next range" do
|
|
102
|
+
reader = write_dump([page("One", "1", id: 1), page("Two", "2", id: 2)], { 0 => ["One"], 1 => ["Two"] })
|
|
103
|
+
events = []
|
|
104
|
+
allow(reader).to receive(:pages_at).and_wrap_original do |original, offset|
|
|
105
|
+
events << [:read, offset]
|
|
106
|
+
original.call(offset)
|
|
107
|
+
end
|
|
108
|
+
entries = %w[One Two].map { |t| reader.index.find_by_title(t) }
|
|
109
|
+
reader.each_entry_result(entries) { |entry, _page, _error| events << [:yield, entry[:title]] }
|
|
110
|
+
expect(events.map(&:first)).to eq(%i[read yield read yield])
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "calls before_fork before each parallel batch" do
|
|
114
|
+
reader = write_dump([page("One", "1", id: 1), page("Two", "2", id: 2)], { 0 => ["One"], 1 => ["Two"] })
|
|
115
|
+
entries = %w[One Two].map { |t| reader.index.find_by_title(t) }
|
|
116
|
+
forks = 0
|
|
117
|
+
results = reader.each_entry_result(entries, num_processes: 2, before_fork: -> { forks += 1 }).to_a
|
|
118
|
+
expect(results.map { |e, page, _| [e[:title], page[:text]] }).to eq([%w[One 1], %w[Two 2]])
|
|
119
|
+
expect(forks).to eq(1)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
let(:reader) do
|
|
123
|
+
write_dump([page("Good", "ok", id: 1) + page("Also good", "", id: 2),
|
|
124
|
+
{ raw: bzip2(page("Bad", "b" * 50_000, id: 3))[0...-30] }],
|
|
125
|
+
{ 0 => ["Good", "Also good", "Not in range"], 1 => ["Bad"] })
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
[1, 2].each do |procs|
|
|
129
|
+
it "reports a page, a missing page, or the failure of its range for every entry (#{procs} process)" do
|
|
130
|
+
entries = %w[Good Also\ good Not\ in\ range Bad].map { |t| reader.index.find_by_title(t) }
|
|
131
|
+
results = reader.each_entry_result(entries, num_processes: procs)
|
|
132
|
+
.to_h { |entry, page, error| [entry[:title], [page&.dig(:title), error && [error[:reason], error[:message][/incomplete/]]]] }
|
|
133
|
+
expect(results).to eq(
|
|
134
|
+
"Good" => ["Good", nil], "Also good" => ["Also good", nil],
|
|
135
|
+
"Not in range" => [nil, nil], "Bad" => [nil, [:truncated, "incomplete"]]
|
|
136
|
+
)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
data/spec/multistream_spec.rb
CHANGED
|
@@ -5,8 +5,11 @@ require "tempfile"
|
|
|
5
5
|
require "fileutils"
|
|
6
6
|
require "zlib"
|
|
7
7
|
require "webmock/rspec"
|
|
8
|
+
require_relative "support/dump_status"
|
|
8
9
|
|
|
9
10
|
RSpec.describe "Wp2txt Multistream" do
|
|
11
|
+
include DumpStatusFixture
|
|
12
|
+
|
|
10
13
|
before do
|
|
11
14
|
# Ensure WebMock is enabled (may be disabled by other specs)
|
|
12
15
|
WebMock.enable!
|
|
@@ -16,6 +19,7 @@ RSpec.describe "Wp2txt Multistream" do
|
|
|
16
19
|
# Stub Wikipedia dump listing page
|
|
17
20
|
stub_request(:get, %r{dumps\.wikimedia\.org})
|
|
18
21
|
.to_return(status: 200, body: '<a href="20260101/">20260101/</a>')
|
|
22
|
+
stub_dump_status
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
after do
|
|
@@ -153,14 +157,6 @@ RSpec.describe "Wp2txt Multistream" do
|
|
|
153
157
|
end
|
|
154
158
|
end
|
|
155
159
|
|
|
156
|
-
describe "#find_suitable_partial_cache" do
|
|
157
|
-
context "when no partial cache exists" do
|
|
158
|
-
it "returns nil" do
|
|
159
|
-
expect(manager.find_suitable_partial_cache(100)).to be_nil
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
160
|
describe "resumable download support" do
|
|
165
161
|
let(:test_file_path) { File.join(temp_dir, "test_download.bz2") }
|
|
166
162
|
let(:test_url) { "https://example.com/test.bz2" }
|
|
@@ -373,60 +369,8 @@ RSpec.describe "Wp2txt Multistream" do
|
|
|
373
369
|
expect(fetcher.instance_variable_get(:@lang)).to eq("ja")
|
|
374
370
|
end
|
|
375
371
|
|
|
376
|
-
it "accepts custom cache_expiry_days" do
|
|
377
|
-
fetcher = described_class.new("en", "Test", cache_expiry_days: 14)
|
|
378
|
-
expect(fetcher.instance_variable_get(:@cache_expiry_days)).to eq(14)
|
|
379
|
-
end
|
|
380
|
-
end
|
|
381
|
-
|
|
382
|
-
describe "#enable_cache" do
|
|
383
|
-
it "sets cache directory" do
|
|
384
|
-
fetcher.enable_cache("/tmp/test_cache")
|
|
385
|
-
expect(fetcher.instance_variable_get(:@cache_dir)).to eq("/tmp/test_cache")
|
|
386
|
-
end
|
|
387
372
|
end
|
|
388
373
|
|
|
389
|
-
describe "cache operations" do
|
|
390
|
-
let(:temp_cache) { Dir.mktmpdir }
|
|
391
|
-
let(:fetcher_with_cache) do
|
|
392
|
-
f = described_class.new("en", "Test Category")
|
|
393
|
-
f.enable_cache(temp_cache)
|
|
394
|
-
f
|
|
395
|
-
end
|
|
396
|
-
|
|
397
|
-
after { FileUtils.rm_rf(temp_cache) if File.exist?(temp_cache) }
|
|
398
|
-
|
|
399
|
-
it "creates CategoryCache when cache enabled" do
|
|
400
|
-
expect(fetcher_with_cache.cache).to be_a(Wp2txt::CategoryCache)
|
|
401
|
-
end
|
|
402
|
-
|
|
403
|
-
it "returns nil for cache when cache disabled" do
|
|
404
|
-
fetcher_no_cache = described_class.new("en", "Test")
|
|
405
|
-
expect(fetcher_no_cache.cache).to be_nil
|
|
406
|
-
end
|
|
407
|
-
|
|
408
|
-
it "uses SQLite-based cache file" do
|
|
409
|
-
fetcher_with_cache.cache
|
|
410
|
-
cache_files = Dir.glob(File.join(temp_cache, "categories_*.sqlite3"))
|
|
411
|
-
expect(cache_files.size).to eq 1
|
|
412
|
-
end
|
|
413
|
-
|
|
414
|
-
it "saves and loads from cache" do
|
|
415
|
-
category = "Cache_Test"
|
|
416
|
-
members = { pages: ["Article1", "Article2"], subcats: ["SubCat1"] }
|
|
417
|
-
|
|
418
|
-
fetcher_with_cache.send(:save_to_cache, category, members)
|
|
419
|
-
loaded = fetcher_with_cache.send(:load_from_cache, category)
|
|
420
|
-
|
|
421
|
-
expect(loaded[:pages]).to contain_exactly("Article1", "Article2")
|
|
422
|
-
expect(loaded[:subcats]).to contain_exactly("SubCat1")
|
|
423
|
-
end
|
|
424
|
-
|
|
425
|
-
it "returns nil for non-existent cache" do
|
|
426
|
-
result = fetcher_with_cache.send(:load_from_cache, "NonExistent")
|
|
427
|
-
expect(result).to be_nil
|
|
428
|
-
end
|
|
429
|
-
end
|
|
430
374
|
end
|
|
431
375
|
|
|
432
376
|
describe Wp2txt::MultistreamReader do
|
|
@@ -476,18 +420,15 @@ RSpec.describe "Wp2txt Multistream" do
|
|
|
476
420
|
end
|
|
477
421
|
end
|
|
478
422
|
|
|
479
|
-
describe "#
|
|
423
|
+
describe "#each_entry_result" do
|
|
480
424
|
it "returns an enumerator when no block given" do
|
|
481
425
|
reader = described_class.new(multistream_path, index_path, cache_dir: temp_dir)
|
|
482
|
-
|
|
483
|
-
expect(result).to be_an(Enumerator)
|
|
426
|
+
expect(reader.each_entry_result([], num_processes: 2)).to be_an(Enumerator)
|
|
484
427
|
end
|
|
485
428
|
|
|
486
|
-
it "
|
|
429
|
+
it "yields nothing for no entries" do
|
|
487
430
|
reader = described_class.new(multistream_path, index_path, cache_dir: temp_dir)
|
|
488
|
-
|
|
489
|
-
reader.each_article_parallel([], num_processes: 2) { |page| pages << page }
|
|
490
|
-
expect(pages).to eq([])
|
|
431
|
+
expect(reader.each_entry_result([], num_processes: 2).to_a).to eq([])
|
|
491
432
|
end
|
|
492
433
|
end
|
|
493
434
|
end
|
|
@@ -539,152 +480,5 @@ RSpec.describe "Wp2txt Multistream" do
|
|
|
539
480
|
end
|
|
540
481
|
end
|
|
541
482
|
|
|
542
|
-
describe "#cached_partial_multistream_path" do
|
|
543
|
-
let(:temp_dir) { Dir.mktmpdir }
|
|
544
|
-
let(:manager) { described_class.new("en", cache_dir: temp_dir) }
|
|
545
|
-
|
|
546
|
-
after { FileUtils.remove_entry(temp_dir) }
|
|
547
|
-
|
|
548
|
-
it "includes stream count in filename" do
|
|
549
|
-
path = manager.cached_partial_multistream_path(1000)
|
|
550
|
-
expect(path).to include("1000streams")
|
|
551
|
-
expect(path).to end_with(".xml.bz2")
|
|
552
|
-
end
|
|
553
|
-
end
|
|
554
|
-
|
|
555
|
-
describe "#find_any_partial_cache" do
|
|
556
|
-
let(:temp_dir) { Dir.mktmpdir }
|
|
557
|
-
let(:manager) { described_class.new("en", cache_dir: temp_dir) }
|
|
558
|
-
|
|
559
|
-
after { FileUtils.remove_entry(temp_dir) }
|
|
560
|
-
|
|
561
|
-
context "when no partial exists" do
|
|
562
|
-
it "returns nil" do
|
|
563
|
-
expect(manager.find_any_partial_cache).to be_nil
|
|
564
|
-
end
|
|
565
|
-
end
|
|
566
|
-
|
|
567
|
-
context "when partial dumps exist" do
|
|
568
|
-
before do
|
|
569
|
-
# Create fake partial dump files
|
|
570
|
-
File.write(File.join(temp_dir, "enwiki-20260101-multistream-100streams.xml.bz2"), "BZh9" + "x" * 100)
|
|
571
|
-
File.write(File.join(temp_dir, "enwiki-20260101-multistream-500streams.xml.bz2"), "BZh9" + "x" * 500)
|
|
572
|
-
end
|
|
573
|
-
|
|
574
|
-
it "returns the largest partial by stream count" do
|
|
575
|
-
result = manager.find_any_partial_cache
|
|
576
|
-
expect(result).not_to be_nil
|
|
577
|
-
expect(result[:stream_count]).to eq(500)
|
|
578
|
-
expect(result[:dump_date]).to eq("20260101")
|
|
579
|
-
end
|
|
580
|
-
|
|
581
|
-
it "includes file size and mtime" do
|
|
582
|
-
result = manager.find_any_partial_cache
|
|
583
|
-
expect(result[:size]).to be > 0
|
|
584
|
-
expect(result[:mtime]).to be_a(Time)
|
|
585
|
-
end
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
context "with partials from different dates" do
|
|
589
|
-
before do
|
|
590
|
-
File.write(File.join(temp_dir, "enwiki-20260101-multistream-100streams.xml.bz2"), "BZh9" + "x" * 100)
|
|
591
|
-
File.write(File.join(temp_dir, "enwiki-20260201-multistream-50streams.xml.bz2"), "BZh9" + "x" * 50)
|
|
592
|
-
end
|
|
593
|
-
|
|
594
|
-
it "returns the largest regardless of date" do
|
|
595
|
-
result = manager.find_any_partial_cache
|
|
596
|
-
expect(result[:stream_count]).to eq(100)
|
|
597
|
-
expect(result[:dump_date]).to eq("20260101")
|
|
598
|
-
end
|
|
599
|
-
end
|
|
600
|
-
end
|
|
601
|
-
|
|
602
|
-
describe "#can_resume_from_partial?" do
|
|
603
|
-
let(:temp_dir) { Dir.mktmpdir }
|
|
604
|
-
let(:manager) { described_class.new("en", cache_dir: temp_dir) }
|
|
605
|
-
|
|
606
|
-
after { FileUtils.remove_entry(temp_dir) }
|
|
607
|
-
|
|
608
|
-
context "when partial_info is nil" do
|
|
609
|
-
it "returns not possible with :no_partial reason" do
|
|
610
|
-
result = manager.can_resume_from_partial?(nil)
|
|
611
|
-
expect(result[:possible]).to be false
|
|
612
|
-
expect(result[:reason]).to eq(:no_partial)
|
|
613
|
-
end
|
|
614
|
-
end
|
|
615
|
-
|
|
616
|
-
context "when dump dates don't match" do
|
|
617
|
-
let(:partial_info) do
|
|
618
|
-
{
|
|
619
|
-
path: File.join(temp_dir, "enwiki-20250101-multistream-100streams.xml.bz2"),
|
|
620
|
-
dump_date: "20250101",
|
|
621
|
-
stream_count: 100,
|
|
622
|
-
size: 1000
|
|
623
|
-
}
|
|
624
|
-
end
|
|
625
|
-
|
|
626
|
-
before do
|
|
627
|
-
# Create the file
|
|
628
|
-
File.write(partial_info[:path], "BZh9" + "x" * 100)
|
|
629
|
-
# Stub the latest_dump_date to return a different date
|
|
630
|
-
allow(manager).to receive(:latest_dump_date).and_return("20260101")
|
|
631
|
-
end
|
|
632
|
-
|
|
633
|
-
it "returns not possible with :date_mismatch reason" do
|
|
634
|
-
result = manager.can_resume_from_partial?(partial_info)
|
|
635
|
-
expect(result[:possible]).to be false
|
|
636
|
-
expect(result[:reason]).to eq(:date_mismatch)
|
|
637
|
-
expect(result[:partial_date]).to eq("20250101")
|
|
638
|
-
expect(result[:latest_date]).to eq("20260101")
|
|
639
|
-
end
|
|
640
|
-
end
|
|
641
|
-
|
|
642
|
-
context "when partial file is invalid" do
|
|
643
|
-
let(:partial_info) do
|
|
644
|
-
{
|
|
645
|
-
path: File.join(temp_dir, "enwiki-20260101-multistream-100streams.xml.bz2"),
|
|
646
|
-
dump_date: "20260101",
|
|
647
|
-
stream_count: 100,
|
|
648
|
-
size: 1000
|
|
649
|
-
}
|
|
650
|
-
end
|
|
651
|
-
|
|
652
|
-
before do
|
|
653
|
-
# Create an invalid bz2 file (wrong magic bytes)
|
|
654
|
-
File.write(partial_info[:path], "XXXX" + "x" * 100)
|
|
655
|
-
allow(manager).to receive(:latest_dump_date).and_return("20260101")
|
|
656
|
-
end
|
|
657
|
-
|
|
658
|
-
it "returns not possible with :invalid_partial reason" do
|
|
659
|
-
result = manager.can_resume_from_partial?(partial_info)
|
|
660
|
-
expect(result[:possible]).to be false
|
|
661
|
-
expect(result[:reason]).to eq(:invalid_partial)
|
|
662
|
-
end
|
|
663
|
-
end
|
|
664
|
-
end
|
|
665
|
-
|
|
666
|
-
describe "#get_remote_file_size" do
|
|
667
|
-
let(:temp_dir) { Dir.mktmpdir }
|
|
668
|
-
let(:manager) { described_class.new("en", cache_dir: temp_dir) }
|
|
669
|
-
|
|
670
|
-
after { FileUtils.remove_entry(temp_dir) }
|
|
671
|
-
|
|
672
|
-
it "returns file size from Content-Length header" do
|
|
673
|
-
stub_request(:head, %r{dumps\.wikimedia\.org})
|
|
674
|
-
.to_return(status: 200, headers: { "Content-Length" => "12345678" })
|
|
675
|
-
|
|
676
|
-
allow(manager).to receive(:latest_dump_date).and_return("20260101")
|
|
677
|
-
size = manager.send(:get_remote_file_size, "https://dumps.wikimedia.org/enwiki/20260101/test.xml.bz2")
|
|
678
|
-
expect(size).to eq(12_345_678)
|
|
679
|
-
end
|
|
680
|
-
|
|
681
|
-
it "returns 0 when Content-Length is missing" do
|
|
682
|
-
stub_request(:head, %r{dumps\.wikimedia\.org})
|
|
683
|
-
.to_return(status: 200, headers: {})
|
|
684
|
-
|
|
685
|
-
size = manager.send(:get_remote_file_size, "https://dumps.wikimedia.org/test.xml.bz2")
|
|
686
|
-
expect(size).to eq(0)
|
|
687
|
-
end
|
|
688
|
-
end
|
|
689
483
|
end
|
|
690
484
|
end
|
|
@@ -7,6 +7,7 @@ require "tmpdir"
|
|
|
7
7
|
require "parallel"
|
|
8
8
|
require_relative "support/multistream_fixture"
|
|
9
9
|
require "wp2txt/multistream"
|
|
10
|
+
require "wp2txt/extractor"
|
|
10
11
|
require "wp2txt/output_writer"
|
|
11
12
|
require "wp2txt/stream_processor"
|
|
12
13
|
|
|
@@ -119,8 +120,8 @@ RSpec.describe "output integrity" do
|
|
|
119
120
|
index = Wp2txt::MultistreamIndex.new(index_path, use_cache: false, target_titles: ["Film A"],
|
|
120
121
|
show_progress: false)
|
|
121
122
|
expect(index.early_terminated?).to be(true)
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
first_stream, second_stream = File.readlines(index_path).map { |line| line.split(":").first.to_i }.uniq
|
|
124
|
+
expect(index.stream_offsets).to eq([first_stream])
|
|
124
125
|
expect(index.stream_end_offset).to eq(second_stream)
|
|
125
126
|
end
|
|
126
127
|
|
|
@@ -142,5 +143,47 @@ RSpec.describe "output integrity" do
|
|
|
142
143
|
stub_const("Wp2txt::MultistreamReader::MAX_TAIL_STREAM_BYTES", 0)
|
|
143
144
|
expect { reader.extract_article("Film A") }.to raise_error(Wp2txt::Error, /cannot locate the end/)
|
|
144
145
|
end
|
|
146
|
+
|
|
147
|
+
describe "choosing the ranges to fetch" do
|
|
148
|
+
let(:extractor) { Object.new.extend(Wp2txt::Extractor) }
|
|
149
|
+
let(:snapshot) { instance_double(Wp2txt::DumpSnapshot, dump_file: Wp2txt::DumpSnapshot::FileInfo.new(size: 10_000)) }
|
|
150
|
+
let(:manager) { instance_double(Wp2txt::DumpManager, verified_full_dump: nil, snapshot: snapshot) }
|
|
151
|
+
|
|
152
|
+
def stream_starts(index_path)
|
|
153
|
+
File.readlines(index_path).map { |line| line.split(":").first.to_i }.uniq
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def source_for(index_path, title, **index_opts)
|
|
157
|
+
index = Wp2txt::MultistreamIndex.new(index_path, use_cache: false, show_progress: false, **index_opts)
|
|
158
|
+
extractor.stream_source(manager, index, [index.find_by_title(title)[:offset]])
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
it "fetches only the range holding the article after an early index stop" do
|
|
162
|
+
_dump, index_path = create_fixture(@dir)
|
|
163
|
+
first, second = stream_starts(index_path)
|
|
164
|
+
expect(manager).to receive(:fetch_streams).with([[first, second]])
|
|
165
|
+
source_for(index_path, "Film A", target_titles: ["Film A"])
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it "fetches the same range when the whole index was read" do
|
|
169
|
+
_dump, index_path = create_fixture(@dir)
|
|
170
|
+
first, second = stream_starts(index_path)
|
|
171
|
+
expect(manager).to receive(:fetch_streams).with([[first, second]])
|
|
172
|
+
source_for(index_path, "Film A")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
it "fetches the last range up to the end of the dump file" do
|
|
176
|
+
_dump, index_path = create_fixture(@dir)
|
|
177
|
+
expect(manager).to receive(:fetch_streams).with([[stream_starts(index_path)[1], 10_000]])
|
|
178
|
+
source_for(index_path, "Category:Films", target_titles: ["Category:Films"])
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
it "reads the cached whole dump instead when it is known to be complete" do
|
|
182
|
+
dump, index_path = create_fixture(@dir)
|
|
183
|
+
allow(manager).to receive(:verified_full_dump).and_return(dump)
|
|
184
|
+
expect(manager).not_to receive(:fetch_streams)
|
|
185
|
+
expect(source_for(index_path, "Film A")).to eq(dump)
|
|
186
|
+
end
|
|
187
|
+
end
|
|
145
188
|
end
|
|
146
189
|
end
|
data/spec/regex_spec.rb
CHANGED
|
@@ -190,6 +190,36 @@ RSpec.describe "Wp2txt Regex Patterns" do
|
|
|
190
190
|
end
|
|
191
191
|
end
|
|
192
192
|
|
|
193
|
+
describe ".category_names" do
|
|
194
|
+
it "reads the name without the sort key" do
|
|
195
|
+
expect(Wp2txt.category_names("[[Category:Science|Physics]] [[ Kategorie : Wissenschaft ]]"))
|
|
196
|
+
.to eq ["Science", "Wissenschaft"]
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
it "does not take a link that only shows a category" do
|
|
200
|
+
text = "[[:Category:Science]] [[:en:Category:Polish painters|Category:Polish painters{{Fontsize|75%|(en)}}]]"
|
|
201
|
+
expect(Wp2txt.category_names(text)).to eq []
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "does not take a namespace name written outside a link" do
|
|
205
|
+
expect(Wp2txt.category_names("{{unbulleted list|CT:{{Start date|1979}}}} |Category:Science]]")).to eq []
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it "leaves out a template that sets the sort key, and a name holding any other template" do
|
|
209
|
+
expect(Wp2txt.category_names("[[Category:Actors{{DEFAULTSORT:}}]] [[Category:A{{DEFAULTSORT:{{PAGENAME}}}}]]"))
|
|
210
|
+
.to eq ["Actors", "A"]
|
|
211
|
+
expect(Wp2txt.category_names("[[Category:Alpha_{{PAGENAME}}]] [[Category:{{PAGENAME}}]]")).to eq []
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
it "reads {{!}} as the bar before the sort key" do
|
|
215
|
+
expect(Wp2txt.category_names("[[Category:Before{{!}}After]]")).to eq ["Before"]
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
it "does not take a link broken across lines or not closed" do
|
|
219
|
+
expect(Wp2txt.category_names("[[\nCategory:Split]] [[Category:\nBeta]] [[Category:Open|sort")).to eq []
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
193
223
|
describe "IN_LINK_REGEX" do
|
|
194
224
|
it "matches wikilinks on their own line" do
|
|
195
225
|
expect("[[Article]]").to match(in_link_regex)
|
data/spec/spec_helper.rb
CHANGED
|
@@ -61,9 +61,9 @@ RSpec.configure do |config|
|
|
|
61
61
|
|
|
62
62
|
# Per-example wall-clock guard. The suite deliberately exercises runaway
|
|
63
63
|
# queries and forked workers; an interrupted or wedged example must not turn
|
|
64
|
-
# into a process spinning at
|
|
65
|
-
#
|
|
66
|
-
#
|
|
64
|
+
# into a process spinning at full CPU indefinitely. The limit is far above
|
|
65
|
+
# what any example needs. Override with WP2TXT_SPEC_TIMEOUT=0 to disable,
|
|
66
|
+
# or tag an example :no_timeout.
|
|
67
67
|
spec_timeout = (ENV["WP2TXT_SPEC_TIMEOUT"] || 120).to_i
|
|
68
68
|
if spec_timeout.positive?
|
|
69
69
|
require "timeout"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "digest"
|
|
5
|
+
|
|
6
|
+
# A dumpstatus.json as Wikimedia publishes it for one dump date, describing
|
|
7
|
+
# the multistream index and dump file (size and SHA-1).
|
|
8
|
+
module DumpStatusFixture
|
|
9
|
+
def dump_status_json(lang, date, index: "index", dump: "dump", status: "done")
|
|
10
|
+
prefix = "#{lang}wiki-#{date}-pages-articles-multistream"
|
|
11
|
+
file = lambda do |name, content|
|
|
12
|
+
{ name => { "size" => content.bytesize, "url" => "/#{lang}wiki/#{date}/#{name}",
|
|
13
|
+
"md5" => Digest::MD5.hexdigest(content), "sha1" => Digest::SHA1.hexdigest(content) } }
|
|
14
|
+
end
|
|
15
|
+
JSON.generate(
|
|
16
|
+
"jobs" => {
|
|
17
|
+
"articlesmultistreamdumprecombine" => {
|
|
18
|
+
"status" => status,
|
|
19
|
+
"files" => file.("#{prefix}-index.txt.bz2", index).merge(file.("#{prefix}.xml.bz2", dump))
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"version" => "0.8"
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Answer every dumpstatus.json request with a finished status for the
|
|
27
|
+
# language and date in its URL
|
|
28
|
+
def stub_dump_status(index: "index", dump: "dump")
|
|
29
|
+
stub_request(:get, %r{dumps\.wikimedia\.org/\w+wiki/\d{8}/dumpstatus\.json}).to_return do |request|
|
|
30
|
+
lang, date = request.uri.path.match(%r{/(\w+)wiki/(\d{8})/})&.captures
|
|
31
|
+
{ status: 200, body: dump_status_json(lang, date, index: index, dump: dump) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
require "open3"
|
|
4
4
|
|
|
5
|
-
# Synthetic
|
|
6
|
-
#
|
|
5
|
+
# Synthetic multistream dump used by metadata index / corpus specs, laid out
|
|
6
|
+
# like a Wikimedia dump: a header stream (siteinfo), stream 1 with articles
|
|
7
|
+
# (ns 0), stream 2 with category pages (ns 14), and a footer stream.
|
|
7
8
|
module MultistreamFixture
|
|
8
|
-
def page_xml(id:, ns:, title:, text:)
|
|
9
|
+
def page_xml(id:, ns:, title:, text:, redirect: nil)
|
|
9
10
|
escaped = text.gsub("&", "&").gsub("<", "<").gsub(">", ">")
|
|
11
|
+
redirect_line = redirect ? %(\n <redirect title="#{redirect}" />) : ""
|
|
10
12
|
<<~XML
|
|
11
13
|
<page>
|
|
12
14
|
<title>#{title}</title>
|
|
13
15
|
<ns>#{ns}</ns>
|
|
14
|
-
<id>#{id}</id
|
|
16
|
+
<id>#{id}</id>#{redirect_line}
|
|
15
17
|
<revision>
|
|
16
18
|
<id>#{id * 100}</id>
|
|
17
19
|
<text bytes="#{text.bytesize}">#{escaped}</text>
|
|
@@ -20,6 +22,20 @@ module MultistreamFixture
|
|
|
20
22
|
XML
|
|
21
23
|
end
|
|
22
24
|
|
|
25
|
+
FIXTURE_HEADER = <<~XML
|
|
26
|
+
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.11/" version="0.11" xml:lang="en">
|
|
27
|
+
<siteinfo>
|
|
28
|
+
<sitename>Wikipedia</sitename>
|
|
29
|
+
<dbname>testwiki</dbname>
|
|
30
|
+
<case>first-letter</case>
|
|
31
|
+
<namespaces>
|
|
32
|
+
<namespace key="0" case="first-letter" />
|
|
33
|
+
<namespace key="14" case="first-letter">Category</namespace>
|
|
34
|
+
</namespaces>
|
|
35
|
+
</siteinfo>
|
|
36
|
+
XML
|
|
37
|
+
FIXTURE_FOOTER = "</mediawiki>\n"
|
|
38
|
+
|
|
23
39
|
def bzip2(data)
|
|
24
40
|
out, status = Open3.capture2("bzip2", "-c", stdin_data: data)
|
|
25
41
|
raise "bzip2 failed" unless status.success?
|
|
@@ -37,7 +53,7 @@ module MultistreamFixture
|
|
|
37
53
|
page_xml(id: 3, ns: 0, title: "Person X",
|
|
38
54
|
text: "Bio.\n== Career == <!-- legacy anchor -->\nActing.\n== '''Style''' ==\nDistinct prose.\n<!-- [[Category:Hidden]] -->\n[[Category:Japanese actors]]\n"),
|
|
39
55
|
page_xml(id: 4, ns: 0, title: "Old Film",
|
|
40
|
-
text: "#REDIRECT [[Film A]]\n[[Category:Japanese films]]\n")
|
|
56
|
+
text: "#REDIRECT [[Film A]]\n[[Category:Japanese films]]\n", redirect: "Film A")
|
|
41
57
|
]
|
|
42
58
|
stream2_pages = [
|
|
43
59
|
page_xml(id: 5, ns: 14, title: "Category:Japanese films", text: "[[Category:Films]]\n"),
|
|
@@ -46,15 +62,17 @@ module MultistreamFixture
|
|
|
46
62
|
page_xml(id: 8, ns: 14, title: "Category:Japanese actors", text: "[[Category:People]]\n")
|
|
47
63
|
]
|
|
48
64
|
|
|
65
|
+
header = bzip2(FIXTURE_HEADER)
|
|
49
66
|
stream1 = bzip2(stream1_pages.join)
|
|
50
67
|
stream2 = bzip2(stream2_pages.join)
|
|
51
68
|
|
|
52
69
|
multistream_path = File.join(dir, "testwiki-20260101-pages-articles-multistream.xml.bz2")
|
|
53
|
-
File.binwrite(multistream_path, stream1 + stream2)
|
|
70
|
+
File.binwrite(multistream_path, header + stream1 + stream2 + bzip2(FIXTURE_FOOTER))
|
|
54
71
|
|
|
55
|
-
|
|
72
|
+
offset1 = header.bytesize
|
|
73
|
+
offset2 = offset1 + stream1.bytesize
|
|
56
74
|
index_lines = [
|
|
57
|
-
"
|
|
75
|
+
"#{offset1}:1:Film A", "#{offset1}:2:Film B", "#{offset1}:3:Person X", "#{offset1}:4:Old Film",
|
|
58
76
|
"#{offset2}:5:Category:Japanese films", "#{offset2}:6:Category:French films",
|
|
59
77
|
"#{offset2}:7:Category:Films", "#{offset2}:8:Category:Japanese actors"
|
|
60
78
|
]
|