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,520 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
require "json"
|
|
6
|
+
require_relative "support/multistream_fixture"
|
|
7
|
+
require "wp2txt/extractor"
|
|
8
|
+
require "wp2txt/output_writer"
|
|
9
|
+
require "wp2txt/formatter"
|
|
10
|
+
require "wp2txt/article"
|
|
11
|
+
require "wp2txt/cli_ui"
|
|
12
|
+
|
|
13
|
+
RSpec.describe Wp2txt::ExtractionRun do
|
|
14
|
+
include MultistreamFixture
|
|
15
|
+
|
|
16
|
+
around do |example|
|
|
17
|
+
Dir.mktmpdir("wp2txt-run-") do |dir|
|
|
18
|
+
@dir = dir
|
|
19
|
+
example.run
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def new_run(titles, base: "jawiki_articles")
|
|
24
|
+
described_class.new(titles, output_dir: File.join(@dir, "out"), base_name: base, source: { lang: "ja" })
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Write one staged output file holding the given text
|
|
28
|
+
def stage(run, name, text)
|
|
29
|
+
FileUtils.mkdir_p(run.staging_dir)
|
|
30
|
+
path = File.join(run.staging_dir, name)
|
|
31
|
+
File.write(path, text)
|
|
32
|
+
path
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def manifest(run)
|
|
36
|
+
JSON.parse(File.read(run.manifest_path))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe "results and exit codes" do
|
|
40
|
+
it "accounts for every requested title exactly once" do
|
|
41
|
+
run = new_run(%w[A B C D])
|
|
42
|
+
run.located("A")
|
|
43
|
+
run.written("A")
|
|
44
|
+
run.located("B")
|
|
45
|
+
run.skipped_empty("B")
|
|
46
|
+
run.failed("C", :not_found)
|
|
47
|
+
run.located("D")
|
|
48
|
+
run.failed("D", :truncated, "bzip2 data is incomplete")
|
|
49
|
+
expect(run.counts).to eq(requested: 4, located: 3, retrieved: 2, written: 1, records: 1, skipped_empty: 1, failed: 2)
|
|
50
|
+
expect { run.written("A") }.to raise_error(ArgumentError, /already ended/)
|
|
51
|
+
expect { run.failed("A", :whatever) }.to raise_error(ArgumentError, /unknown failure reason/)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
{
|
|
55
|
+
"every title written" => [%i[written written], true, 0],
|
|
56
|
+
"every title skipped on request" => [%i[skipped_empty skipped_empty], true, 0],
|
|
57
|
+
"some titles failed, some written" => [%i[written failed], true, 2],
|
|
58
|
+
"every title failed" => [%i[failed failed], true, 1],
|
|
59
|
+
"an incomplete listing with output" => [%i[written written], false, 2],
|
|
60
|
+
"an incomplete listing without output" => [%i[skipped_empty skipped_empty], false, 1]
|
|
61
|
+
}.each do |label, (states, complete, code)|
|
|
62
|
+
it "exits with #{code} for #{label}" do
|
|
63
|
+
run = new_run(%w[A B])
|
|
64
|
+
run.listing!(complete: complete)
|
|
65
|
+
states.zip(%w[A B]).each do |state, title|
|
|
66
|
+
state == :failed ? run.failed(title, :not_found) : run.public_send(state, title)
|
|
67
|
+
end
|
|
68
|
+
expect(run.exit_code).to eq(code)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "neither publishes nor succeeds while a requested title has no result" do
|
|
73
|
+
run = new_run(%w[A B])
|
|
74
|
+
run.written("A")
|
|
75
|
+
expect(run.exit_code).to eq(1)
|
|
76
|
+
expect { run.publish!([]) }.to raise_error(Wp2txt::Error, /1 requested title/)
|
|
77
|
+
expect(File.exist?(run.manifest_path)).to be(false)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "exits with 1 when the output could not be published" do
|
|
81
|
+
run = new_run(%w[A])
|
|
82
|
+
run.written("A")
|
|
83
|
+
expect(run.exit_code(published: false)).to eq(1)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
describe "#publish!" do
|
|
88
|
+
it "publishes the output files and then a manifest listing them" do
|
|
89
|
+
run = new_run(%w[A B])
|
|
90
|
+
run.written("A")
|
|
91
|
+
run.failed("B", :not_found)
|
|
92
|
+
published = run.publish!([stage(run, "jawiki_articles-1.txt", "A\n")])
|
|
93
|
+
|
|
94
|
+
expect(published.map { |p| File.basename(p) }).to eq(["jawiki_articles-1.txt"])
|
|
95
|
+
expect(File.read(published.first)).to eq("A\n")
|
|
96
|
+
expect(File.directory?(run.staging_dir)).to be(false)
|
|
97
|
+
m = manifest(run)
|
|
98
|
+
expect(m).to include("run_id" => run.run_id, "exit_code" => 2, "output_files" => ["jawiki_articles-1.txt"])
|
|
99
|
+
expect(m["counts"]).to include("requested" => 2, "written" => 1, "failed" => 1)
|
|
100
|
+
expect(m["requests"]).to include({ "requested_title" => "B", "status" => "failed", "reason" => "not_found" })
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "replaces the files of the previous run of the same base and leaves other files alone" do
|
|
104
|
+
first = new_run(%w[A])
|
|
105
|
+
first.written("A")
|
|
106
|
+
first.publish!([stage(first, "jawiki_articles-1.txt", "old 1\n"), stage(first, "jawiki_articles-2.txt", "old 2\n")])
|
|
107
|
+
out = File.dirname(first.manifest_path)
|
|
108
|
+
File.write(File.join(out, "jawiki_articles-notes.txt"), "mine\n")
|
|
109
|
+
File.write(File.join(out, "other-1.txt"), "other base\n")
|
|
110
|
+
|
|
111
|
+
second = new_run(%w[A])
|
|
112
|
+
second.written("A")
|
|
113
|
+
second.publish!([stage(second, "jawiki_articles-1.txt", "new 1\n")])
|
|
114
|
+
|
|
115
|
+
expect(File.read(File.join(out, "jawiki_articles-1.txt"))).to eq("new 1\n")
|
|
116
|
+
expect(File.exist?(File.join(out, "jawiki_articles-2.txt"))).to be(false)
|
|
117
|
+
expect(File.read(File.join(out, "jawiki_articles-notes.txt"))).to eq("mine\n")
|
|
118
|
+
expect(File.read(File.join(out, "other-1.txt"))).to eq("other base\n")
|
|
119
|
+
expect(manifest(second)["run_id"]).to eq(second.run_id)
|
|
120
|
+
expect(Dir.children(out)).not_to include(a_string_matching(/previous/))
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "leaves no manifest, only the previous one set aside, when publication stops part way" do
|
|
124
|
+
first = new_run(%w[A])
|
|
125
|
+
first.written("A")
|
|
126
|
+
first.publish!([stage(first, "jawiki_articles-1.txt", "old\n")])
|
|
127
|
+
|
|
128
|
+
second = new_run(%w[A])
|
|
129
|
+
second.written("A")
|
|
130
|
+
staged = stage(second, "jawiki_articles-1.txt", "new\n")
|
|
131
|
+
allow(File).to receive(:rename).and_call_original
|
|
132
|
+
allow(File).to receive(:rename).with(staged, anything).and_raise(Errno::ENOSPC)
|
|
133
|
+
expect { second.publish!([staged]) }.to raise_error(Errno::ENOSPC)
|
|
134
|
+
|
|
135
|
+
expect(File.exist?(second.manifest_path)).to be(false)
|
|
136
|
+
previous = second.manifest_path.sub(".manifest.json", ".manifest.previous.json")
|
|
137
|
+
expect(JSON.parse(File.read(previous))["run_id"]).to eq(first.run_id)
|
|
138
|
+
expect(File.directory?(second.staging_dir)).to be(false)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "lets the next run remove files a stopped run had already put in place" do
|
|
142
|
+
first = new_run(%w[A])
|
|
143
|
+
first.written("A")
|
|
144
|
+
first.publish!([stage(first, "jawiki_articles-1.txt", "1\n")])
|
|
145
|
+
|
|
146
|
+
stopped = new_run(%w[A])
|
|
147
|
+
stopped.written("A")
|
|
148
|
+
staged = [stage(stopped, "jawiki_articles-1.txt", "s1\n"), stage(stopped, "jawiki_articles-2.txt", "s2\n")]
|
|
149
|
+
allow(stopped).to receive(:write_manifest).and_raise(Errno::ENOSPC)
|
|
150
|
+
expect { stopped.publish!(staged) }.to raise_error(Errno::ENOSPC)
|
|
151
|
+
out = File.dirname(first.manifest_path)
|
|
152
|
+
expect(File.exist?(File.join(out, "jawiki_articles-2.txt"))).to be(true)
|
|
153
|
+
|
|
154
|
+
last = new_run(%w[A])
|
|
155
|
+
last.written("A")
|
|
156
|
+
last.publish!([stage(last, "jawiki_articles-1.txt", "new\n")])
|
|
157
|
+
expect(Dir.children(out).sort).to eq(%w[jawiki_articles-1.txt jawiki_articles.manifest.json])
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it "removes only files named as this base's output, whatever the previous manifest lists" do
|
|
161
|
+
out = File.join(@dir, "out")
|
|
162
|
+
FileUtils.mkdir_p(out)
|
|
163
|
+
kept = %w[jawiki_articles_other.manifest.json jawiki_articles-notes.txt jawiki_articles.txt.bak other.txt]
|
|
164
|
+
kept.each { |name| File.write(File.join(out, name), "x") }
|
|
165
|
+
File.write(File.join(out, "jawiki_articles.manifest.json"),
|
|
166
|
+
JSON.generate("output_files" => kept + ["../escape.txt", "jawiki_articles-3.jsonl"]))
|
|
167
|
+
File.write(File.join(out, "jawiki_articles-3.jsonl"), "old")
|
|
168
|
+
|
|
169
|
+
run = new_run(%w[A])
|
|
170
|
+
run.written("A")
|
|
171
|
+
run.publish!([stage(run, "jawiki_articles.txt", "A\n")])
|
|
172
|
+
expect(Dir.children(out)).to include(*kept)
|
|
173
|
+
expect(Dir.children(out)).not_to include("jawiki_articles-3.jsonl")
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it "stops instead of replacing anything when the previous manifest is not one" do
|
|
177
|
+
out = File.join(@dir, "out")
|
|
178
|
+
FileUtils.mkdir_p(out)
|
|
179
|
+
File.write(File.join(out, "jawiki_articles.manifest.json"), "{not json")
|
|
180
|
+
run = new_run(%w[A])
|
|
181
|
+
run.written("A")
|
|
182
|
+
expect { run.publish!([stage(run, "jawiki_articles.txt", "A\n")]) }
|
|
183
|
+
.to raise_error(Wp2txt::FileIOError, /not a manifest/)
|
|
184
|
+
expect(File.read(File.join(out, "jawiki_articles.manifest.json"))).to eq("{not json")
|
|
185
|
+
expect(File.exist?(File.join(out, "jawiki_articles.txt"))).to be(false)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it "accepts only files staged by this run" do
|
|
189
|
+
run = new_run(%w[A])
|
|
190
|
+
run.written("A")
|
|
191
|
+
outside = File.join(@dir, "jawiki_articles.txt")
|
|
192
|
+
File.write(outside, "x")
|
|
193
|
+
expect { run.publish!([outside]) }.to raise_error(ArgumentError, /not an output file/)
|
|
194
|
+
link = File.join(stage(run, "jawiki_articles-1.txt", "x").then { |p| File.dirname(p) }, "jawiki_articles-2.txt")
|
|
195
|
+
File.symlink(outside, link)
|
|
196
|
+
expect { run.publish!([link]) }.to raise_error(ArgumentError, /not an output file/)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
it "leaves alone a manifest another run published after this one started" do
|
|
200
|
+
slow = new_run(%w[A])
|
|
201
|
+
quick = new_run(%w[A])
|
|
202
|
+
quick.written("A")
|
|
203
|
+
quick.publish!([stage(quick, "jawiki_articles.txt", "quick\n")])
|
|
204
|
+
|
|
205
|
+
stage(slow, "jawiki_articles.txt", "partial")
|
|
206
|
+
slow.abandon!
|
|
207
|
+
expect(manifest(quick)["run_id"]).to eq(quick.run_id)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
it "sets the previous manifest aside when a run is abandoned" do
|
|
211
|
+
first = new_run(%w[A])
|
|
212
|
+
first.written("A")
|
|
213
|
+
first.publish!([stage(first, "jawiki_articles.txt", "A\n")])
|
|
214
|
+
|
|
215
|
+
failed = new_run(%w[A])
|
|
216
|
+
stage(failed, "jawiki_articles.txt", "partial")
|
|
217
|
+
failed.abandon!
|
|
218
|
+
expect(File.exist?(failed.manifest_path)).to be(false)
|
|
219
|
+
expect(File.directory?(failed.staging_dir)).to be(false)
|
|
220
|
+
expect(File.read(File.join(File.dirname(failed.manifest_path), "jawiki_articles.txt"))).to eq("A\n")
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
describe "extracting requested articles" do
|
|
225
|
+
let(:extractor) do
|
|
226
|
+
Object.new.extend(Wp2txt::Extractor).tap do |e|
|
|
227
|
+
e.extend(Wp2txt)
|
|
228
|
+
e.extend(Wp2txt::Formatter)
|
|
229
|
+
def e.set_source_fields(article, page_id, revision_id, _raw, _config)
|
|
230
|
+
article.page_id = page_id
|
|
231
|
+
article.revision_id = revision_id
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Progress lines are not under test
|
|
235
|
+
def e.puts(*) = nil
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
let(:normalizer) { Wp2txt::TitleNormalizer.new(Wp2txt::SiteInfo.parse(MultistreamFixture::FIXTURE_HEADER)) }
|
|
239
|
+
|
|
240
|
+
# A dump laid out like the fixture, from [title, text, redirect target] rows
|
|
241
|
+
def dump_of(pages)
|
|
242
|
+
header = bzip2(MultistreamFixture::FIXTURE_HEADER)
|
|
243
|
+
xml = pages.each_with_index.map do |(title, text, redirect), i|
|
|
244
|
+
page_xml(id: i + 1, ns: 0, title: title, text: text, redirect: redirect)
|
|
245
|
+
end
|
|
246
|
+
dump = File.join(@dir, "custom.xml.bz2")
|
|
247
|
+
File.binwrite(dump, header + bzip2(xml.join) + bzip2(MultistreamFixture::FIXTURE_FOOTER))
|
|
248
|
+
index_path = File.join(@dir, "custom-index.txt")
|
|
249
|
+
File.write(index_path, pages.each_with_index.map { |(title, _), i| "#{header.bytesize}:#{i + 1}:#{title}" }.join("\n") + "\n")
|
|
250
|
+
[dump, index_path]
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def extract(dump, index_path, titles, config = { format: :text }, follow: true)
|
|
254
|
+
manager = instance_double(Wp2txt::DumpManager, verified_full_dump: dump)
|
|
255
|
+
requests = extractor.normalize_requests(titles, normalizer)
|
|
256
|
+
index = Wp2txt::MultistreamIndex.new(index_path, use_cache: false, show_progress: false,
|
|
257
|
+
target_titles: extractor.lookup_titles(requests))
|
|
258
|
+
run = new_run(requests.keys)
|
|
259
|
+
writer = extractor.staged_writer(run, { file_size: 0 }, config[:format])
|
|
260
|
+
extractor.extract_requests(manager, index_path, index, requests, run, writer, config,
|
|
261
|
+
normalizer: normalizer, follow_redirects: follow)
|
|
262
|
+
run.publish!(writer.close)
|
|
263
|
+
run
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def output_of(run, ext = "txt")
|
|
267
|
+
File.read(File.join(File.dirname(run.manifest_path), "jawiki_articles.#{ext}"))
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
it "writes what it read and records the titles the index lacks" do
|
|
271
|
+
run = extract(*create_fixture(@dir), ["Film A", "Nowhere"])
|
|
272
|
+
expect(run.counts).to include(requested: 2, written: 1, failed: 1)
|
|
273
|
+
expect(run.exit_code).to eq(2)
|
|
274
|
+
expect(output_of(run)).to include("Story here.")
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
it "looks titles up the way MediaWiki writes them, keeping the title asked for" do
|
|
278
|
+
run = extract(*create_fixture(@dir), ["film_A", "Film A"])
|
|
279
|
+
expect(run.results.keys).to eq(["film_A"])
|
|
280
|
+
expect(run.results["film_A"]).to include(status: :written, title: "Film A")
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it "keeps apart titles whose first letter's case mapping may differ from MediaWiki's" do
|
|
284
|
+
requests = extractor.normalize_requests(%w[ßeta SSeta], normalizer)
|
|
285
|
+
expect(requests.keys).to eq(%w[ßeta SSeta])
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it "keeps apart listed pages with different IDs, even when their titles normalize alike" do
|
|
289
|
+
requests = extractor.normalize_requests(["Film_A", "Film A"], normalizer, ids: { "Film_A" => 1, "Film A" => 2 })
|
|
290
|
+
expect(requests.size).to eq(2)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
it "refuses a page whose ID differs from the one the index gives" do
|
|
294
|
+
dump, index_path = dump_of([["Twin", "text", nil]])
|
|
295
|
+
File.write(index_path, File.read(index_path).sub(":1:Twin", ":99:Twin"))
|
|
296
|
+
run = extract(dump, index_path, ["Twin"])
|
|
297
|
+
expect(run.results["Twin"]).to include(status: :failed, reason: :index_mismatch)
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
it "does not report an article cut short by broken bzip2 data as extracted" do
|
|
301
|
+
dump, index_path = create_fixture(@dir)
|
|
302
|
+
data = File.binread(dump)
|
|
303
|
+
offsets = File.readlines(index_path).map { |l| l.split(":").first.to_i }.uniq
|
|
304
|
+
# Cut the first article stream short while keeping the file's layout
|
|
305
|
+
broken = data[0...offsets[0]] + data[offsets[0]...offsets[1] - 40] + ("\0" * 40) + data[offsets[1]..]
|
|
306
|
+
File.binwrite(dump, broken)
|
|
307
|
+
|
|
308
|
+
run = extract(dump, index_path, ["Film A", "Category:Films"])
|
|
309
|
+
expect(run.results["Film A"]).to include(status: :failed, reason: :truncated)
|
|
310
|
+
expect(run.results["Category:Films"]).to include(status: :written)
|
|
311
|
+
expect(run.exit_code).to eq(2)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it "records an indexed title missing from its range as an index mismatch" do
|
|
315
|
+
dump, index_path = create_fixture(@dir)
|
|
316
|
+
offset = File.readlines(index_path).first.split(":").first
|
|
317
|
+
File.write(index_path, File.read(index_path) + "#{offset}:99:Ghost\n")
|
|
318
|
+
run = extract(dump, index_path, ["Ghost"])
|
|
319
|
+
expect(run.results["Ghost"]).to include(status: :failed, reason: :index_mismatch)
|
|
320
|
+
expect(run.exit_code).to eq(1)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
it "counts an article left out by --skip-empty as skipped, not written and not failed" do
|
|
324
|
+
config = { format: :text, sections: ["Nonexistent section"], skip_empty: true }
|
|
325
|
+
run = extract(*create_fixture(@dir), ["Film A"], config)
|
|
326
|
+
expect(run.counts).to include(written: 0, skipped_empty: 1, failed: 0)
|
|
327
|
+
expect(run.exit_code).to eq(0)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
describe "redirects" do
|
|
331
|
+
let(:pages) do
|
|
332
|
+
[["Target", "The article.", nil], ["Short", "#REDIRECT [[Target]]", "Target"],
|
|
333
|
+
["Chain", "#REDIRECT [[Short]]", "Short"], ["Loop", "#REDIRECT [[Loop]]", "Loop"],
|
|
334
|
+
["Lost", "#REDIRECT [[Nowhere]]", "Nowhere"], ["Odd", "#REDIRECT [[Special:Random]]", "Special:Random"]]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
it "writes the article a redirect points to, under the title asked for" do
|
|
338
|
+
run = extract(*dump_of(pages), ["Short"], { format: :json })
|
|
339
|
+
expect(run.results["Short"]).to include(status: :written, title: "Target", redirect: "Target")
|
|
340
|
+
record = JSON.parse(output_of(run, "jsonl"))
|
|
341
|
+
expect(record).to include("title" => "Target", "requested_title" => "Short")
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
it "writes an article once for all the requests that come to it" do
|
|
345
|
+
run = extract(*dump_of(pages), %w[Target Short])
|
|
346
|
+
expect(run.counts).to include(requested: 2, written: 2, records: 1)
|
|
347
|
+
expect(run.results["Short"]).to include(status: :written, same_as: "Target")
|
|
348
|
+
expect(output_of(run).scan("The article.").size).to eq(1)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
{
|
|
352
|
+
"Chain" => "chain", "Loop" => "cycle", "Lost" => "missing", "Odd" => "foreign"
|
|
353
|
+
}.each do |title, kind|
|
|
354
|
+
it "does not follow a redirect further than one step: #{kind}" do
|
|
355
|
+
run = extract(*dump_of(pages), [title])
|
|
356
|
+
expect(run.results[title]).to include(status: :failed, reason: :redirect_unresolved)
|
|
357
|
+
expect(run.results[title][:detail]).to start_with(kind)
|
|
358
|
+
expect(run.exit_code).to eq(1)
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
it "follows a redirect to a section of an article, keeping the section" do
|
|
363
|
+
run = extract(*dump_of(pages + [["Part", "#REDIRECT [[Target#History]]", "Target#History"]]), ["Part"])
|
|
364
|
+
expect(run.results["Part"]).to include(status: :written, title: "Target", redirect_section: "History")
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
it "does not follow a redirect to a section of its own page" do
|
|
368
|
+
run = extract(*dump_of(pages + [["Self", "#REDIRECT [[#Top]]", "#Top"]]), ["Self"])
|
|
369
|
+
expect(run.results["Self"]).to include(status: :failed, reason: :redirect_unresolved)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
it "writes the redirect page itself, with where it points, when told not to follow" do
|
|
373
|
+
run = extract(*dump_of(pages), ["Short"], { format: :text, redirect: true }, follow: false)
|
|
374
|
+
expect(run.results["Short"]).to include(status: :written, title: "Short", redirect: "Target")
|
|
375
|
+
expect(output_of(run)).to include("[[Short]]").and include("Target")
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
describe "a targeted extraction from the command line" do
|
|
381
|
+
let(:app) do
|
|
382
|
+
Object.new.tap do |a|
|
|
383
|
+
a.extend(Wp2txt, Wp2txt::CliUI, Wp2txt::Formatter, Wp2txt::Extractor)
|
|
384
|
+
a.instance_variable_set(:@quiet, true)
|
|
385
|
+
a.instance_variable_set(:@no_color, true)
|
|
386
|
+
def a.set_source_fields(article, page_id, revision_id, _raw, _config)
|
|
387
|
+
article.page_id = page_id
|
|
388
|
+
article.revision_id = revision_id
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# Defined by the command (bin/wp2txt)
|
|
392
|
+
def a.parse_markers_option(_value)
|
|
393
|
+
[:all]
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
let(:out) { File.join(@dir, "cli-out").tap { |d| FileUtils.mkdir_p(d) } }
|
|
398
|
+
|
|
399
|
+
before do
|
|
400
|
+
dump, index_path = create_fixture(@dir)
|
|
401
|
+
snapshot = instance_double(Wp2txt::DumpSnapshot, to_h: { lang: "ja", date: "20260101", snapshot: "abc" },
|
|
402
|
+
index_file: Wp2txt::DumpSnapshot::FileInfo.new(sha1: "a" * 40),
|
|
403
|
+
time: Time.utc(2026, 1, 1))
|
|
404
|
+
manager = instance_double(Wp2txt::DumpManager, download_index: index_path, snapshot: snapshot, snapshot_origin: :checked,
|
|
405
|
+
verified_full_dump: dump, cached_multistream_path: dump,
|
|
406
|
+
site_info: Wp2txt::SiteInfo.parse(MultistreamFixture::FIXTURE_HEADER))
|
|
407
|
+
allow(Wp2txt::DumpManager).to receive(:new).and_return(manager)
|
|
408
|
+
allow(Wp2txt::CLI).to receive(:config).and_return(double(dump_expiry_days: 30))
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def run_cli(titles)
|
|
412
|
+
result = nil
|
|
413
|
+
expect do
|
|
414
|
+
result = app.extract_specific_articles(lang: "ja", articles: titles, output_dir: out, format: "text",
|
|
415
|
+
file_size: 0, cache_dir: @dir)
|
|
416
|
+
end.to output.to_stdout
|
|
417
|
+
result
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
it "publishes the output with a manifest and exits with 2 when a title is missing" do
|
|
421
|
+
expect { expect(run_cli("Film A,Nowhere")).to eq(2) }.to output.to_stderr
|
|
422
|
+
manifest = JSON.parse(File.read(File.join(out, "jawiki_articles.manifest.json")))
|
|
423
|
+
expect(manifest["source"]).to include("snapshot" => "abc", "chosen" => "checked")
|
|
424
|
+
expect(manifest["output_files"]).to eq(["jawiki_articles.txt"])
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
it "leaves the previous output alone when an empty category is only previewed" do
|
|
428
|
+
run_cli("Film A")
|
|
429
|
+
before = Dir.children(out).sort.to_h { |name| [name, File.read(File.join(out, name))] }
|
|
430
|
+
empty = Wp2txt::CategoryFetcher::Listing.new(articles: {}, categories: [{ name: "Empty", article_count: 0 }],
|
|
431
|
+
complete: true)
|
|
432
|
+
fetcher = instance_double(Wp2txt::CategoryFetcher, fetch_listing: empty,
|
|
433
|
+
preview_of: { category: "Empty", depth: 0, subcategories: [],
|
|
434
|
+
total_subcategories: 0, total_articles: 0 })
|
|
435
|
+
allow(Wp2txt::CategoryFetcher).to receive(:new).and_return(fetcher)
|
|
436
|
+
allow(app).to receive(:create_spinner).and_return(double(auto_spin: nil, success: nil, error: nil))
|
|
437
|
+
code = nil
|
|
438
|
+
expect do
|
|
439
|
+
code = app.extract_category_articles(lang: "ja", from_category: "Empty", depth: 0, output_dir: out,
|
|
440
|
+
dry_run: true, format: "text", file_size: 0, cache_dir: @dir)
|
|
441
|
+
end.to output.to_stdout
|
|
442
|
+
expect(code).to eq(0)
|
|
443
|
+
# The category's own base is a different one; nothing at all changed
|
|
444
|
+
expect(Dir.children(out).sort.to_h { |name| [name, File.read(File.join(out, name))] }).to eq(before)
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
it "exits with 1 and writes nothing when a category run needs a confirmation it cannot ask for" do
|
|
448
|
+
run_cli("Film A")
|
|
449
|
+
before = Dir.children(out).sort.to_h { |name| [name, File.read(File.join(out, name))] }
|
|
450
|
+
empty = Wp2txt::CategoryFetcher::Listing.new(articles: {}, categories: [{ name: "Empty", article_count: 0 }],
|
|
451
|
+
complete: true)
|
|
452
|
+
fetcher = instance_double(Wp2txt::CategoryFetcher, fetch_listing: empty,
|
|
453
|
+
preview_of: { category: "Empty", depth: 0, subcategories: [],
|
|
454
|
+
total_subcategories: 0, total_articles: 0 })
|
|
455
|
+
allow(Wp2txt::CategoryFetcher).to receive(:new).and_return(fetcher)
|
|
456
|
+
allow(app).to receive(:create_spinner).and_return(double(auto_spin: nil, success: nil, error: nil))
|
|
457
|
+
allow($stdin).to receive(:tty?).and_return(false)
|
|
458
|
+
code = nil
|
|
459
|
+
expect do
|
|
460
|
+
code = app.extract_category_articles(lang: "ja", from_category: "Empty", depth: 0, output_dir: out,
|
|
461
|
+
format: "text", file_size: 0, cache_dir: @dir)
|
|
462
|
+
end.to output(/--yes/).to_stdout.and output(/confirmation/).to_stderr
|
|
463
|
+
expect(code).to eq(1)
|
|
464
|
+
expect(Dir.children(out).sort.to_h { |name| [name, File.read(File.join(out, name))] }).to eq(before)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def incomplete_fetcher(&during)
|
|
468
|
+
partial = Wp2txt::CategoryFetcher::Listing.new(articles: { "Film A" => 1 }, categories: [], complete: false,
|
|
469
|
+
error: "Partial: 503")
|
|
470
|
+
fetcher = instance_double(Wp2txt::CategoryFetcher)
|
|
471
|
+
allow(fetcher).to receive(:fetch_listing) do
|
|
472
|
+
during&.call
|
|
473
|
+
raise Wp2txt::CategoryFetcher::IncompleteListing.new("the listing of Partial could not be read in full", partial)
|
|
474
|
+
end
|
|
475
|
+
allow(Wp2txt::CategoryFetcher).to receive(:new).and_return(fetcher)
|
|
476
|
+
allow(app).to receive(:create_spinner).and_return(double(auto_spin: nil, success: nil, error: nil))
|
|
477
|
+
end
|
|
478
|
+
|
|
479
|
+
def run_category(**opts)
|
|
480
|
+
code = nil
|
|
481
|
+
expect do
|
|
482
|
+
code = app.extract_category_articles(lang: "ja", from_category: "Partial", depth: 0, output_dir: out,
|
|
483
|
+
format: "text", file_size: 0, cache_dir: @dir, **opts)
|
|
484
|
+
end.to output.to_stderr
|
|
485
|
+
code
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
def publish_category_run(text)
|
|
489
|
+
run = Wp2txt::ExtractionRun.new(["Film A"], output_dir: out, base_name: "jawiki_Partial")
|
|
490
|
+
run.written("Film A")
|
|
491
|
+
FileUtils.mkdir_p(run.staging_dir)
|
|
492
|
+
File.write(File.join(run.staging_dir, "jawiki_Partial.txt"), text)
|
|
493
|
+
run.publish!([File.join(run.staging_dir, "jawiki_Partial.txt")])
|
|
494
|
+
run
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
it "changes nothing when a dry run cannot list the category in full" do
|
|
498
|
+
publish_category_run("earlier\n")
|
|
499
|
+
incomplete_fetcher
|
|
500
|
+
before = Dir.children(out).sort
|
|
501
|
+
expect(run_category(dry_run: true)).to eq(1)
|
|
502
|
+
expect(Dir.children(out).sort).to eq(before)
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
it "does not set aside a manifest published while its own listing was being read" do
|
|
506
|
+
later = nil
|
|
507
|
+
incomplete_fetcher { later = publish_category_run("later\n") }
|
|
508
|
+
expect(run_category).to eq(1)
|
|
509
|
+
expect(JSON.parse(File.read(later.manifest_path))["run_id"]).to eq(later.run_id)
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
it "exits with 1, keeps no staging directory and sets the previous manifest aside when writing fails" do
|
|
513
|
+
run_cli("Film A")
|
|
514
|
+
allow_any_instance_of(Wp2txt::OutputWriter).to receive(:write).and_raise(Wp2txt::FileIOError, "Disk full")
|
|
515
|
+
expect { expect(run_cli("Film A")).to eq(1) }.to output(/Disk full/).to_stderr
|
|
516
|
+
expect(Dir.children(out)).not_to include(a_string_matching(/\A\.wp2txt-/))
|
|
517
|
+
expect(File.exist?(File.join(out, "jawiki_articles.manifest.json"))).to be(false)
|
|
518
|
+
end
|
|
519
|
+
end
|
|
520
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
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 "building and reading the multistream index" do
|
|
9
|
+
include MultistreamFixture
|
|
10
|
+
|
|
11
|
+
around do |example|
|
|
12
|
+
Dir.mktmpdir("wp2txt-index-") do |dir|
|
|
13
|
+
@dir = dir
|
|
14
|
+
example.run
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The fixture's index, compressed as Wikimedia ships it
|
|
19
|
+
def bz2_index
|
|
20
|
+
_dump, index_path = create_fixture(@dir)
|
|
21
|
+
path = "#{index_path}.bz2"
|
|
22
|
+
File.binwrite(path, bzip2(File.read(index_path)))
|
|
23
|
+
path
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def offsets_of(path)
|
|
27
|
+
Wp2txt.each_index_line(path).map { |offset, _id, _title| offset }.uniq
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "Wp2txt.each_index_line" do
|
|
31
|
+
it "refuses an index whose bzip2 data is cut short" do
|
|
32
|
+
path = bz2_index
|
|
33
|
+
File.binwrite(path, File.binread(path)[0...-20])
|
|
34
|
+
expect { Wp2txt.each_index_line(path).to_a }.to raise_error(Wp2txt::StreamError, /incomplete or corrupt/)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "lets the caller stop early without an error" do
|
|
38
|
+
expect(Wp2txt.each_index_line(bz2_index).first(2).map { |_o, _i, title| title }).to eq(["Film A", "Film B"])
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe Wp2txt::SiteInfo do
|
|
43
|
+
it "is not valid when the text holds no siteinfo" do
|
|
44
|
+
expect(Wp2txt::SiteInfo.parse("<mediawiki>")).not_to be_valid
|
|
45
|
+
expect(Wp2txt::SiteInfo.parse(MultistreamFixture::FIXTURE_HEADER)).to be_valid
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe Wp2txt::IndexCache do
|
|
50
|
+
it "builds a read-only cache that answers lookups and lists every stream" do
|
|
51
|
+
path = bz2_index
|
|
52
|
+
cache = Wp2txt::IndexCache.new(path, cache_dir: @dir)
|
|
53
|
+
expect(cache.build!).to eq(8)
|
|
54
|
+
expect(cache).to be_valid
|
|
55
|
+
lookup = Wp2txt::LazyTitleIndex.new(cache)
|
|
56
|
+
expect(lookup.find_by_titles(["Film B", "Nowhere"]).keys).to eq(["Film B"])
|
|
57
|
+
expect(lookup.find_by_title("Category:Films")).to include(page_id: 7)
|
|
58
|
+
expect(lookup.stream_offsets).to eq(offsets_of(path))
|
|
59
|
+
# Published as a single file, with no write-ahead log beside it
|
|
60
|
+
expect(Dir.children(@dir).grep(/sqlite3/)).to eq([File.basename(cache.cache_path)])
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "leaves no cache behind when the index is cut short" do
|
|
64
|
+
path = bz2_index
|
|
65
|
+
File.binwrite(path, File.binread(path)[0...-20])
|
|
66
|
+
cache = Wp2txt::IndexCache.new(path, cache_dir: @dir)
|
|
67
|
+
expect { cache.build! }.to raise_error(Wp2txt::StreamError)
|
|
68
|
+
expect(cache).not_to be_valid
|
|
69
|
+
expect(Dir.children(@dir).grep(/sqlite3/)).to be_empty
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "is bound to the index it was built from when the index's digest is given" do
|
|
73
|
+
path = bz2_index
|
|
74
|
+
Wp2txt::IndexCache.new(path, cache_dir: @dir, identity: "a" * 40).build!
|
|
75
|
+
expect(Wp2txt::IndexCache.new(path, cache_dir: @dir, identity: "a" * 40)).to be_valid
|
|
76
|
+
expect(Wp2txt::IndexCache.new(path, cache_dir: @dir, identity: "b" * 40)).not_to be_valid
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "is rebuilt when its index changes" do
|
|
80
|
+
path = bz2_index
|
|
81
|
+
cache = Wp2txt::IndexCache.new(path, cache_dir: @dir)
|
|
82
|
+
cache.build!
|
|
83
|
+
File.utime(Time.now + 60, Time.now + 60, path)
|
|
84
|
+
expect(cache).not_to be_valid
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe Wp2txt::MultistreamIndex do
|
|
89
|
+
it "keeps only the targets of a targeted scan and learns where their last stream ends" do
|
|
90
|
+
path = bz2_index
|
|
91
|
+
index = Wp2txt::MultistreamIndex.new(path, use_cache: false, target_titles: ["Film B"], show_progress: false)
|
|
92
|
+
expect(index.entries_by_title.keys).to eq(["Film B"])
|
|
93
|
+
expect(index).to be_early_terminated
|
|
94
|
+
expect(index.stream_end_offset).to eq(offsets_of(path)[1])
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "reads a targeted scan to the end when a title is missing, still keeping only the targets" do
|
|
98
|
+
index = Wp2txt::MultistreamIndex.new(bz2_index, use_cache: false, target_titles: ["Film B", "Nowhere"],
|
|
99
|
+
show_progress: false)
|
|
100
|
+
expect(index.entries_by_title.keys).to eq(["Film B"])
|
|
101
|
+
expect(index).not_to be_early_terminated
|
|
102
|
+
expect(index.stream_offsets.size).to eq(2)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "refuses an index cut short instead of reporting its later titles as missing" do
|
|
106
|
+
path = bz2_index
|
|
107
|
+
File.binwrite(path, File.binread(path)[0...-20])
|
|
108
|
+
expect do
|
|
109
|
+
Wp2txt::MultistreamIndex.new(path, use_cache: false, target_titles: ["Nowhere"], show_progress: false)
|
|
110
|
+
end.to raise_error(Wp2txt::StreamError)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "wp2txt/cli_ui"
|
|
5
|
+
require "wp2txt/index_commands"
|
|
6
|
+
require "wp2txt/cli"
|
|
7
|
+
|
|
8
|
+
RSpec.describe Wp2txt::IndexCommands do
|
|
9
|
+
let(:app) { Object.new.extend(Wp2txt::CliUI, Wp2txt::IndexCommands) }
|
|
10
|
+
|
|
11
|
+
it "chooses the dump version for --lang the way the extraction modes do" do
|
|
12
|
+
allow(Wp2txt::CLI).to receive(:config).and_return(double(dump_expiry_days: 30))
|
|
13
|
+
expect(Wp2txt::DumpManager).to receive(:new)
|
|
14
|
+
.with("ja", cache_dir: "/cache", dump_expiry_days: 30, dump_date: "20260101", update: true)
|
|
15
|
+
app.send(:dump_manager, lang: "ja", cache_dir: "/cache", dump_date: "20260101", update_cache: true)
|
|
16
|
+
end
|
|
17
|
+
end
|