wp2txt 2.3.4 → 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/.dockerignore +0 -4
- data/.github/workflows/ci.yml +16 -1
- data/.github/workflows/publish-image.yml +8 -4
- data/.gitignore +3 -6
- data/CHANGELOG.md +32 -0
- data/DEVELOPMENT.md +63 -93
- data/DEVELOPMENT_ja.md +61 -93
- data/Dockerfile +1 -1
- data/README.md +93 -8
- data/README_ja.md +84 -8
- data/Rakefile +10 -21
- data/bin/wp2txt +84 -15
- data/bin/wp2txt-mcp +1 -1
- data/docs/INDEXES.md +61 -1
- data/lib/wp2txt/article.rb +2 -3
- data/lib/wp2txt/bz2_stream.rb +63 -0
- data/lib/wp2txt/cli.rb +90 -26
- data/lib/wp2txt/config.rb +2 -11
- data/lib/wp2txt/constants.rb +13 -1
- data/lib/wp2txt/corpus.rb +10 -23
- data/lib/wp2txt/data/template_aliases.json +4 -1
- 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 +481 -327
- data/lib/wp2txt/formatter.rb +14 -12
- data/lib/wp2txt/fts_index.rb +1 -1
- data/lib/wp2txt/index_cache.rb +144 -55
- data/lib/wp2txt/index_commands.rb +109 -18
- data/lib/wp2txt/langlinks_importer.rb +19 -35
- data/lib/wp2txt/lead_terms.rb +228 -0
- data/lib/wp2txt/link_counter.rb +171 -0
- data/lib/wp2txt/magic_words.rb +44 -28
- data/lib/wp2txt/metadata_index.rb +82 -13
- data/lib/wp2txt/multistream.rb +911 -824
- data/lib/wp2txt/output_writer.rb +3 -1
- data/lib/wp2txt/page_props_importer.rb +170 -0
- 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/sql_dump_reader.rb +57 -0
- data/lib/wp2txt/stream_processor.rb +9 -2
- data/lib/wp2txt/stream_store.rb +91 -0
- data/lib/wp2txt/template_args.rb +111 -0
- data/lib/wp2txt/template_expander.rb +110 -124
- data/lib/wp2txt/template_patterns.rb +182 -0
- data/lib/wp2txt/utils.rb +164 -127
- data/lib/wp2txt/version.rb +1 -1
- data/lib/wp2txt/wikitext_regions.rb +66 -0
- 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/docs_sync_spec.rb +3 -3
- 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/langlinks_importer_spec.rb +27 -0
- data/spec/lead_terms_edge_cases_spec.rb +194 -0
- data/spec/lead_terms_links_qids_spec.rb +204 -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/p1_correctness_spec.rb +12 -9
- data/spec/page_properties_spec.rb +161 -0
- data/spec/regex_spec.rb +30 -0
- data/spec/region_semantics_spec.rb +71 -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 +58 -0
- data/spec/template_patterns_spec.rb +253 -0
- data/spec/tracked_paths_spec.rb +168 -0
- data/spec/utils_spec.rb +7 -1
- metadata +45 -5
- data/lib/wp2txt/category_cache.rb +0 -313
- data/spec/category_cache_spec.rb +0 -226
|
@@ -8,6 +8,8 @@ require "parallel"
|
|
|
8
8
|
require "time"
|
|
9
9
|
require "json"
|
|
10
10
|
require_relative "regex"
|
|
11
|
+
require_relative "bz2_stream"
|
|
12
|
+
require_relative "site_info"
|
|
11
13
|
require_relative "section_extractor"
|
|
12
14
|
require_relative "version"
|
|
13
15
|
|
|
@@ -39,9 +41,10 @@ module Wp2txt
|
|
|
39
41
|
|
|
40
42
|
# Normalize a page title the way MediaWiki treats titles:
|
|
41
43
|
# underscores to spaces, trimmed, first letter capitalized
|
|
42
|
-
def self.normalize_title(name)
|
|
44
|
+
def self.normalize_title(name, case_rule: "first-letter")
|
|
43
45
|
n = name.to_s.tr("_", " ").strip.squeeze(" ")
|
|
44
46
|
return n if n.empty?
|
|
47
|
+
return n if case_rule == "case-sensitive"
|
|
45
48
|
|
|
46
49
|
n[0].upcase + n[1..].to_s
|
|
47
50
|
end
|
|
@@ -161,6 +164,69 @@ module Wp2txt
|
|
|
161
164
|
skipped_invalid: meta[:langlinks_skipped_invalid].to_i }
|
|
162
165
|
end
|
|
163
166
|
|
|
167
|
+
# Provenance and counts of imported page properties, or nil
|
|
168
|
+
def page_props_provenance
|
|
169
|
+
return nil unless File.exist?(@db_path)
|
|
170
|
+
|
|
171
|
+
meta = read_metadata
|
|
172
|
+
return nil unless meta && meta[:page_props_imported_at] && page_properties_imported?
|
|
173
|
+
|
|
174
|
+
{ source: meta[:page_props_source],
|
|
175
|
+
source_size: meta[:page_props_source_size].to_i,
|
|
176
|
+
source_sha256: meta[:page_props_source_sha256],
|
|
177
|
+
imported_at: meta[:page_props_imported_at],
|
|
178
|
+
imported_with: meta[:page_props_wp2txt_version],
|
|
179
|
+
page_count: meta[:page_props_page_count].to_i,
|
|
180
|
+
qid_count: meta[:page_props_qid_count].to_i,
|
|
181
|
+
disambiguation_count: meta[:page_props_disambiguation_count].to_i,
|
|
182
|
+
sort_key_count: meta[:page_props_sort_key_count].to_i,
|
|
183
|
+
skipped_invalid_sort_keys: meta[:page_props_skipped_invalid_sort_keys].to_i }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# How incoming links were counted (wp2txt --count-links), or nil
|
|
187
|
+
def links_provenance
|
|
188
|
+
return nil unless File.exist?(@db_path)
|
|
189
|
+
|
|
190
|
+
meta = read_metadata
|
|
191
|
+
return nil unless meta && meta[:links_counted_at]
|
|
192
|
+
|
|
193
|
+
{ counted_at: meta[:links_counted_at],
|
|
194
|
+
rule_version: meta[:links_rule_version],
|
|
195
|
+
counted_with: meta[:links_wp2txt_version],
|
|
196
|
+
article_count: meta[:links_article_count].to_i,
|
|
197
|
+
with_inlinks: meta[:links_with_inlinks].to_i }
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# An incomplete import must not be reported as an absent property.
|
|
201
|
+
def self.page_properties_imported?(db)
|
|
202
|
+
!!db.get_first_value(<<~SQL)
|
|
203
|
+
SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'page_properties'
|
|
204
|
+
AND EXISTS (SELECT 1 FROM metadata WHERE key = 'page_props_imported_at')
|
|
205
|
+
SQL
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def page_properties_imported?
|
|
209
|
+
File.exist?(@db_path) && self.class.page_properties_imported?(open_db)
|
|
210
|
+
rescue SQLite3::Exception
|
|
211
|
+
false
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def self.property_values(row = nil)
|
|
215
|
+
qid, sort_key, disambiguation = row
|
|
216
|
+
{ qid: qid, sort_key: sort_key, disambiguation: disambiguation == 1 }
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# nil means not imported; null/false values mean imported but absent.
|
|
220
|
+
def properties_for(page_id)
|
|
221
|
+
return nil unless page_properties_imported?
|
|
222
|
+
|
|
223
|
+
self.class.property_values(open_db.get_first_row(
|
|
224
|
+
"SELECT qid, sort_key, disambiguation FROM page_properties WHERE page_id = ?", [page_id]
|
|
225
|
+
))
|
|
226
|
+
rescue SQLite3::Exception
|
|
227
|
+
nil
|
|
228
|
+
end
|
|
229
|
+
|
|
164
230
|
def close
|
|
165
231
|
@db&.close
|
|
166
232
|
@db = nil
|
|
@@ -230,7 +296,7 @@ module Wp2txt
|
|
|
230
296
|
end
|
|
231
297
|
end
|
|
232
298
|
|
|
233
|
-
def finalize_build!(source_path)
|
|
299
|
+
def finalize_build!(source_path, case_rule: "first-letter")
|
|
234
300
|
db = open_db
|
|
235
301
|
db.execute("CREATE INDEX IF NOT EXISTS idx_pages_title ON pages(title)")
|
|
236
302
|
db.execute("CREATE INDEX IF NOT EXISTS idx_pc_category ON page_categories(category)")
|
|
@@ -248,6 +314,7 @@ module Wp2txt
|
|
|
248
314
|
source_size: stat.size,
|
|
249
315
|
source_mtime: stat.mtime.to_i,
|
|
250
316
|
dump_name: dump_name,
|
|
317
|
+
case_rule: case_rule,
|
|
251
318
|
built_at: Time.now.utc.iso8601
|
|
252
319
|
)
|
|
253
320
|
db.execute("ANALYZE")
|
|
@@ -629,6 +696,7 @@ module Wp2txt
|
|
|
629
696
|
# Build the index. Yields (batches_done, batches_total) after each batch.
|
|
630
697
|
# @return [MetadataIndex] the built index
|
|
631
698
|
def build(&progress)
|
|
699
|
+
case_rule = case_rule_from_header
|
|
632
700
|
index = MetadataIndex.new(@db_path)
|
|
633
701
|
index.prepare_build!
|
|
634
702
|
# Close before Parallel forks workers so children do not inherit a
|
|
@@ -652,10 +720,18 @@ module Wp2txt
|
|
|
652
720
|
self.class.scan_batch(@multistream_path, batch)
|
|
653
721
|
end
|
|
654
722
|
|
|
655
|
-
index.finalize_build!(@multistream_path)
|
|
723
|
+
index.finalize_build!(@multistream_path, case_rule: case_rule)
|
|
656
724
|
index
|
|
657
725
|
end
|
|
658
726
|
|
|
727
|
+
# The siteinfo may occupy its own stream before the first page offset.
|
|
728
|
+
def case_rule_from_header
|
|
729
|
+
return "first-letter" if @stream_offsets.empty?
|
|
730
|
+
|
|
731
|
+
header_end = @stream_offsets.first.positive? ? @stream_offsets.first : @stream_offsets[1]
|
|
732
|
+
SiteInfo.parse(Wp2txt.decompress_bz2(File.binread(@multistream_path, header_end))).case_rule
|
|
733
|
+
end
|
|
734
|
+
|
|
659
735
|
# Scan a batch of [offset, next_offset] stream pairs.
|
|
660
736
|
# Runs inside worker processes: must not touch SQLite.
|
|
661
737
|
def self.scan_batch(multistream_path, offset_pairs)
|
|
@@ -664,20 +740,13 @@ module Wp2txt
|
|
|
664
740
|
offset_pairs.each do |offset, next_offset|
|
|
665
741
|
f.seek(offset)
|
|
666
742
|
data = next_offset ? f.read(next_offset - offset) : f.read
|
|
667
|
-
xml = decompress_bz2(data)
|
|
743
|
+
xml = Wp2txt.decompress_bz2(data, offset: offset)
|
|
668
744
|
xml.scan(PAGE_BLOCK_REGEX) { scan_page(::Regexp.last_match(1), out) }
|
|
669
745
|
end
|
|
670
746
|
end
|
|
671
747
|
out
|
|
672
748
|
end
|
|
673
749
|
|
|
674
|
-
def self.decompress_bz2(data)
|
|
675
|
-
stdout, status = Open3.capture2("bzcat", stdin_data: data)
|
|
676
|
-
raise "bzcat failed (exit #{status.exitstatus})" unless status.success?
|
|
677
|
-
|
|
678
|
-
stdout.force_encoding(Encoding::UTF_8)
|
|
679
|
-
end
|
|
680
|
-
|
|
681
750
|
def self.scan_page(block, out)
|
|
682
751
|
title = block[TITLE_REGEX, 1]
|
|
683
752
|
return unless title && !title.empty?
|
|
@@ -703,8 +772,8 @@ module Wp2txt
|
|
|
703
772
|
|
|
704
773
|
out[:pages] << [page_id, title, ns, redirect_to, text.length]
|
|
705
774
|
|
|
706
|
-
categories =
|
|
707
|
-
.map { |c| MetadataIndex.normalize_category(c
|
|
775
|
+
categories = Wp2txt.category_names(text)
|
|
776
|
+
.map { |c| MetadataIndex.normalize_category(c) }
|
|
708
777
|
.reject(&:empty?).uniq
|
|
709
778
|
if ns == MetadataIndex::NS_CATEGORY
|
|
710
779
|
child = MetadataIndex.normalize_category(title.sub(/\A[^:]+:/, ""))
|