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
data/lib/wp2txt/output_writer.rb
CHANGED
|
@@ -30,9 +30,10 @@ module Wp2txt
|
|
|
30
30
|
# Write formatted article to output
|
|
31
31
|
# Thread-safe for parallel processing
|
|
32
32
|
# @param content [String, Hash] Content to write (String for text, Hash for JSON)
|
|
33
|
+
# @return [Boolean] false when there was nothing to write
|
|
33
34
|
# @raise [Wp2txt::FileIOError] on disk full or other I/O errors
|
|
34
35
|
def write(content)
|
|
35
|
-
return if content.nil? || (content.is_a?(String) && content.strip.empty?)
|
|
36
|
+
return false if content.nil? || (content.is_a?(String) && content.strip.empty?)
|
|
36
37
|
|
|
37
38
|
@mutex.synchronize do
|
|
38
39
|
ensure_file_open
|
|
@@ -43,6 +44,7 @@ module Wp2txt
|
|
|
43
44
|
|
|
44
45
|
rotate_file_if_needed
|
|
45
46
|
end
|
|
47
|
+
true
|
|
46
48
|
rescue Errno::ENOSPC
|
|
47
49
|
close_on_error
|
|
48
50
|
raise Wp2txt::FileIOError, "Disk full: cannot write to output directory '#{@output_dir}'"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "time"
|
|
4
|
+
require_relative "template_args"
|
|
4
5
|
|
|
5
6
|
module Wp2txt
|
|
6
7
|
# Evaluates MediaWiki parser functions
|
|
@@ -11,9 +12,12 @@ module Wp2txt
|
|
|
11
12
|
July August September October November December
|
|
12
13
|
].freeze
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
# @param literal_equals [String, nil] put in place of "=" in what a
|
|
16
|
+
# function shows (see TemplateArgs.literal_equals)
|
|
17
|
+
def initialize(reference_date: nil, preserve_unknown: false, literal_equals: nil)
|
|
15
18
|
@reference_date = reference_date || Time.now
|
|
16
19
|
@preserve_unknown = preserve_unknown
|
|
20
|
+
@literal_equals = literal_equals
|
|
17
21
|
end
|
|
18
22
|
|
|
19
23
|
# Main evaluation method
|
|
@@ -66,7 +70,7 @@ module Wp2txt
|
|
|
66
70
|
|
|
67
71
|
content = text[(start_idx + 2)...end_idx]
|
|
68
72
|
expanded = evaluate_parser_function(content)
|
|
69
|
-
result << expanded
|
|
73
|
+
result << TemplateArgs.literal_equals(expanded, @literal_equals)
|
|
70
74
|
|
|
71
75
|
pos = end_idx + 2
|
|
72
76
|
end
|
|
@@ -106,6 +110,11 @@ module Wp2txt
|
|
|
106
110
|
function_name = content[1...colon_idx].downcase
|
|
107
111
|
args_str = content[(colon_idx + 1)..]
|
|
108
112
|
args = split_arguments(args_str)
|
|
113
|
+
# The values compared or computed hold their "=" as written; a branch
|
|
114
|
+
# chosen keeps its placeholders, as templates in it still read their own
|
|
115
|
+
# arguments. A case of #switch is split at its own "=" first
|
|
116
|
+
operands = OPERANDS.fetch(function_name, args.size)
|
|
117
|
+
args = args.each_with_index.map { |arg, i| i < operands ? plain(arg) : arg }
|
|
109
118
|
|
|
110
119
|
case function_name
|
|
111
120
|
when "if"
|
|
@@ -151,6 +160,15 @@ module Wp2txt
|
|
|
151
160
|
end
|
|
152
161
|
end
|
|
153
162
|
|
|
163
|
+
# How many leading arguments a function compares or computes, for those
|
|
164
|
+
# that return one of the others as it is (all of them, for the rest)
|
|
165
|
+
OPERANDS = { "if" => 1, "ifeq" => 2, "ifexpr" => 1, "iferror" => 0, "switch" => 0 }.freeze
|
|
166
|
+
|
|
167
|
+
# Text with each "=" held as a placeholder given back as written
|
|
168
|
+
def plain(text)
|
|
169
|
+
@literal_equals ? text.tr(@literal_equals, "=") : text
|
|
170
|
+
end
|
|
171
|
+
|
|
154
172
|
def split_arguments(str)
|
|
155
173
|
args = []
|
|
156
174
|
current = +""
|
|
@@ -218,7 +236,7 @@ module Wp2txt
|
|
|
218
236
|
def evaluate_switch(args)
|
|
219
237
|
return "" if args.empty?
|
|
220
238
|
|
|
221
|
-
value = args[0]
|
|
239
|
+
value = plain(args[0].to_s).strip
|
|
222
240
|
cases = args[1..]
|
|
223
241
|
default = ""
|
|
224
242
|
pending_cases = []
|
|
@@ -226,7 +244,7 @@ module Wp2txt
|
|
|
226
244
|
cases.each do |case_arg|
|
|
227
245
|
if case_arg.include?("=")
|
|
228
246
|
key, result = case_arg.split("=", 2)
|
|
229
|
-
key = key.strip
|
|
247
|
+
key = plain(key).strip
|
|
230
248
|
|
|
231
249
|
if key == "#default"
|
|
232
250
|
default = result
|
|
@@ -236,7 +254,7 @@ module Wp2txt
|
|
|
236
254
|
pending_cases.clear
|
|
237
255
|
else
|
|
238
256
|
# Fall-through case
|
|
239
|
-
trimmed = case_arg.strip
|
|
257
|
+
trimmed = plain(case_arg).strip
|
|
240
258
|
if trimmed == value
|
|
241
259
|
pending_cases << trimmed
|
|
242
260
|
else
|
data/lib/wp2txt/ractor_worker.rb
CHANGED
|
@@ -135,9 +135,9 @@ module Wp2txt
|
|
|
135
135
|
raise "Unknown operation: #{op}"
|
|
136
136
|
end
|
|
137
137
|
rescue StandardError
|
|
138
|
-
nil #
|
|
138
|
+
next [i, nil, true] # Done again outside, where an error is reported as usual
|
|
139
139
|
end
|
|
140
|
-
[i, result] #
|
|
140
|
+
[i, result, false] # Index for ordering, result, and whether it failed
|
|
141
141
|
end
|
|
142
142
|
end
|
|
143
143
|
|
|
@@ -145,8 +145,10 @@ module Wp2txt
|
|
|
145
145
|
collected = Array.new(items.size)
|
|
146
146
|
ractors.each do |r|
|
|
147
147
|
r.join
|
|
148
|
-
idx, result = r.value
|
|
149
|
-
|
|
148
|
+
idx, result, failed = r.value
|
|
149
|
+
# An item that failed inside a Ractor is processed again here, so its
|
|
150
|
+
# error reaches the caller instead of the item going missing
|
|
151
|
+
collected[idx] = failed ? process_single(items[idx], operation, frozen_config) : result
|
|
150
152
|
end
|
|
151
153
|
|
|
152
154
|
collected
|
data/lib/wp2txt/regex.rb
CHANGED
|
@@ -252,10 +252,30 @@ module Wp2txt
|
|
|
252
252
|
# For incomplete File/Image links (opened but not closed)
|
|
253
253
|
CLEANUP_FILE_INCOMPLETE_REGEX = Regexp.new('\[\[(?:' + FILE_NAMESPACES + '):[^\]]*\|?\s*$', Regexp::IGNORECASE | Regexp::MULTILINE)
|
|
254
254
|
|
|
255
|
-
#
|
|
256
|
-
#
|
|
257
|
-
#
|
|
258
|
-
|
|
255
|
+
# A category the page belongs to: [[Category:Name]] or [[Category:Name|sortkey]],
|
|
256
|
+
# capturing "Name". A link that only shows a category ([[:Category:Name]],
|
|
257
|
+
# [[:en:Category:Name|...]]) is not one. Templates written in the name stay
|
|
258
|
+
# in the capture; category_names reads them. A link broken across lines or
|
|
259
|
+
# not closed puts the page in no category
|
|
260
|
+
CATEGORY_TEMPLATE = '\{\{(?:[^\{\}\n]|\{\{[^\{\}\n]*\}\})*\}\}'
|
|
261
|
+
CATEGORY_REGEX = Regexp.new('\[\[[ \t]*(?:' + CATEGORY_NAMESPACES + ')[ \t]*:((?:[^\|\[\]\{\}\n]|' + CATEGORY_TEMPLATE +
|
|
262
|
+
')+)(?:\|[^\[\]\n]*)?\]\]', Regexp::IGNORECASE)
|
|
263
|
+
# Templates in a category name that show nothing: the sort key set for the page
|
|
264
|
+
CATEGORY_SILENT_TEMPLATE = /\{\{\s*(?:DEFAULTSORT|DEFAULTSORTKEY|DEFAULTCATEGORYSORT)\s*:(?:[^{}]|\{\{[^{}]*\}\})*\}\}/i
|
|
265
|
+
|
|
266
|
+
# Names of the categories a text assigns, in order. {{!}} ends the name as
|
|
267
|
+
# "|" does; a template that sets the sort key shows nothing in it, and a name
|
|
268
|
+
# holding any other template (one that shows the page's name, say) is left
|
|
269
|
+
# out rather than read wrong
|
|
270
|
+
def self.category_names(text)
|
|
271
|
+
text.gsub(/\{\{\s*!\s*\}\}/, "|").scan(CATEGORY_REGEX).filter_map do |(name)|
|
|
272
|
+
name = name.gsub(CATEGORY_SILENT_TEMPLATE, "")
|
|
273
|
+
next if name.include?("{{")
|
|
274
|
+
|
|
275
|
+
name = name.strip
|
|
276
|
+
name unless name.empty?
|
|
277
|
+
end
|
|
278
|
+
end
|
|
259
279
|
|
|
260
280
|
ESCAPE_NOWIKI_REGEX = Regexp.new('<nowiki>(.*?)<\/nowiki>', Regexp::MULTILINE)
|
|
261
281
|
UNESCAPE_NOWIKI_REGEX = Regexp.new('<nowiki\-(\d+?)>')
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wp2txt
|
|
4
|
+
# The <siteinfo> of a dump: which wiki it is, and its namespaces with the
|
|
5
|
+
# case rule of each. It sits in the dump's first stream, so it always
|
|
6
|
+
# matches the version of the articles.
|
|
7
|
+
class SiteInfo
|
|
8
|
+
Namespace = Struct.new(:id, :name, :case_rule)
|
|
9
|
+
|
|
10
|
+
# Names MediaWiki accepts for core namespaces on every wiki, whatever the
|
|
11
|
+
# local names are
|
|
12
|
+
CANONICAL_NAMES = {
|
|
13
|
+
-2 => "Media", -1 => "Special", 1 => "Talk", 2 => "User", 3 => "User talk",
|
|
14
|
+
4 => "Project", 5 => "Project talk", 6 => "File", 7 => "File talk",
|
|
15
|
+
8 => "MediaWiki", 9 => "MediaWiki talk", 10 => "Template", 11 => "Template talk",
|
|
16
|
+
12 => "Help", 13 => "Help talk", 14 => "Category", 15 => "Category talk",
|
|
17
|
+
828 => "Module", 829 => "Module talk"
|
|
18
|
+
}.freeze
|
|
19
|
+
CORE_ALIASES = { "Image" => 6, "Image talk" => 7 }.freeze
|
|
20
|
+
|
|
21
|
+
# Namespaces whose pages are not in a dump of articles
|
|
22
|
+
VIRTUAL_NAMESPACES = [-2, -1].freeze
|
|
23
|
+
|
|
24
|
+
attr_reader :dbname, :namespaces
|
|
25
|
+
|
|
26
|
+
# @param xml [String] text holding a <siteinfo> element
|
|
27
|
+
def self.parse(xml)
|
|
28
|
+
siteinfo = xml[%r{<siteinfo\b[^>]*>(.*?)</siteinfo>}m, 1].to_s
|
|
29
|
+
default_case = siteinfo[%r{<case>\s*([\w-]+)\s*</case>}, 1] || "first-letter"
|
|
30
|
+
namespaces = siteinfo.scan(%r{<namespace\b([^>]*?)(?:/>|>(.*?)</namespace>)}m).map do |attrs, name|
|
|
31
|
+
id = attrs[/\bkey="(-?\d+)"/, 1].to_i
|
|
32
|
+
Namespace.new(id, unescape(name.to_s.strip), attrs[/\bcase="([\w-]+)"/, 1] || default_case)
|
|
33
|
+
end
|
|
34
|
+
new(dbname: siteinfo[%r{<dbname>\s*(\S+?)\s*</dbname>}, 1], case_rule: default_case, namespaces: namespaces)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.unescape(text)
|
|
38
|
+
text.gsub("<", "<").gsub(">", ">").gsub(""", '"').gsub("'", "'").gsub("&", "&")
|
|
39
|
+
end
|
|
40
|
+
private_class_method :unescape
|
|
41
|
+
|
|
42
|
+
def initialize(dbname:, case_rule:, namespaces:)
|
|
43
|
+
@dbname = dbname
|
|
44
|
+
@case_rule = case_rule
|
|
45
|
+
@namespaces = namespaces
|
|
46
|
+
@by_id = namespaces.to_h { |ns| [ns.id, ns] }
|
|
47
|
+
@by_prefix = {}
|
|
48
|
+
namespaces.each { |ns| @by_prefix[ns.name.downcase] = ns unless ns.name.empty? }
|
|
49
|
+
CANONICAL_NAMES.each do |id, name|
|
|
50
|
+
@by_prefix[name.downcase] ||= @by_id[id] || Namespace.new(id, name, @case_rule)
|
|
51
|
+
end
|
|
52
|
+
CORE_ALIASES.each do |name, id|
|
|
53
|
+
@by_prefix[name.downcase] ||= @by_prefix[CANONICAL_NAMES.fetch(id).downcase]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Whether the text held a siteinfo with namespaces at all
|
|
58
|
+
def valid?
|
|
59
|
+
!@dbname.nil? && @namespaces.any?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Case rule of a namespace (of the main namespace by default)
|
|
63
|
+
def case_rule(namespace_id = 0)
|
|
64
|
+
@by_id[namespace_id]&.case_rule || @case_rule
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The namespace a title prefix names, if any
|
|
68
|
+
# @param prefix [String] the text before the first colon, normalized
|
|
69
|
+
def namespace_for(prefix)
|
|
70
|
+
@by_prefix[prefix.downcase]
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Normalizes titles the way MediaWiki does before looking them up: spaces
|
|
75
|
+
# for underscores, runs of spaces collapsed, Unicode NFC, a namespace prefix
|
|
76
|
+
# written in its local name, and an upper-case first letter where the
|
|
77
|
+
# namespace's case rule says so. Ruby's upcase may differ from MediaWiki's
|
|
78
|
+
# for a few characters; callers keep the original title as well.
|
|
79
|
+
class TitleNormalizer
|
|
80
|
+
Title = Struct.new(:text, :namespace_id)
|
|
81
|
+
|
|
82
|
+
def initialize(site_info)
|
|
83
|
+
@site = site_info
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @return [String] the normalized title
|
|
87
|
+
def normalize(title)
|
|
88
|
+
parse(title).text
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Whether normalizing the title upper-cases a non-ASCII first letter,
|
|
92
|
+
# where Ruby's mapping may not be MediaWiki's
|
|
93
|
+
def uncertain_case?(title)
|
|
94
|
+
text = title.to_s.unicode_normalize(:nfc).tr("_", " ").strip.delete_prefix(":").strip
|
|
95
|
+
prefix, rest = text.split(":", 2)
|
|
96
|
+
ns = rest && @site.namespace_for(prefix.strip)
|
|
97
|
+
text = rest.strip if ns && !ns.id.zero?
|
|
98
|
+
first = text[0].to_s
|
|
99
|
+
!first.ascii_only? && first.upcase != first
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @return [Title] the normalized title and its namespace
|
|
103
|
+
def parse(title)
|
|
104
|
+
text = title.to_s.unicode_normalize(:nfc).tr("_", " ").gsub(/[[:space:]]+/, " ").strip
|
|
105
|
+
text = text.delete_prefix(":").strip
|
|
106
|
+
prefix, rest = text.split(":", 2)
|
|
107
|
+
if rest && (ns = @site.namespace_for(prefix.strip)) && !ns.id.zero?
|
|
108
|
+
name = ns.name.empty? ? SiteInfo::CANONICAL_NAMES.fetch(ns.id, prefix.strip) : ns.name
|
|
109
|
+
return Title.new("#{name}:#{apply_case(rest.strip, ns.case_rule)}", ns.id)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
Title.new(apply_case(text, @site.case_rule(0)), 0)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def apply_case(text, rule)
|
|
118
|
+
return text if text.empty? || rule == "case-sensitive"
|
|
119
|
+
|
|
120
|
+
text[0].upcase + text[1..]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
require "digest"
|
|
6
|
+
require_relative "bz2_stream"
|
|
7
|
+
|
|
8
|
+
module Wp2txt
|
|
9
|
+
# Raised when a range of the dump is not in the stream store: it could not
|
|
10
|
+
# be fetched. The articles it holds are unavailable, not cut short.
|
|
11
|
+
class StreamUnavailableError < StreamError
|
|
12
|
+
def reason
|
|
13
|
+
:stream_unavailable
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Index ranges of one dump snapshot, fetched one by one and kept as
|
|
18
|
+
# separate files named by their byte range and the SHA-1 of their data. A
|
|
19
|
+
# file is in place only once its data is complete and decompresses, so a
|
|
20
|
+
# reader never sees a partial one; a file whose data no longer matches its
|
|
21
|
+
# name is set aside and fetched again.
|
|
22
|
+
class StreamStore
|
|
23
|
+
# Hex digits of the SHA-1 kept in a stored file's name
|
|
24
|
+
DIGEST_LENGTH = 16
|
|
25
|
+
|
|
26
|
+
attr_reader :dir, :total_size
|
|
27
|
+
|
|
28
|
+
# @param dir [String] directory of this snapshot's ranges
|
|
29
|
+
# @param total_size [Integer] size of the whole dump file, where the last range ends
|
|
30
|
+
def initialize(dir, total_size:)
|
|
31
|
+
@dir = dir
|
|
32
|
+
@total_size = total_size
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Stored file of a range, if any
|
|
36
|
+
def path_for(offset, end_offset)
|
|
37
|
+
Dir.glob(File.join(@dir, "#{offset}-#{end_offset}-*.bz2")).first
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Whether the range is stored intact. A stored file that does not match
|
|
41
|
+
# its digest is set aside, so the range is fetched again.
|
|
42
|
+
def have?(offset, end_offset)
|
|
43
|
+
path = path_for(offset, end_offset)
|
|
44
|
+
return false unless path
|
|
45
|
+
return true if File.size(path) == end_offset - offset && Digest::SHA1.file(path).hexdigest.start_with?(digest_of(path))
|
|
46
|
+
|
|
47
|
+
File.rename(path, "#{path}.bad-#{Time.now.utc.strftime('%Y%m%dT%H%M%SZ')}")
|
|
48
|
+
warn " A cached range at offset #{offset} was damaged; fetching it again"
|
|
49
|
+
false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Compressed data of the range starting at offset
|
|
53
|
+
# @param next_offset [Integer, nil] start of the next range; nil for the last range
|
|
54
|
+
# @raise [StreamUnavailableError] when the range was not fetched
|
|
55
|
+
def read_range(offset, next_offset)
|
|
56
|
+
end_offset = next_offset || @total_size
|
|
57
|
+
path = path_for(offset, end_offset)
|
|
58
|
+
unless path && File.size(path) == end_offset - offset
|
|
59
|
+
raise StreamUnavailableError.new("the range at offset #{offset} could not be fetched", offset: offset)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
File.binread(path)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Keep the data of a range once it decompresses in full
|
|
66
|
+
# @raise [StreamError] when it does not
|
|
67
|
+
def store(offset, end_offset, data)
|
|
68
|
+
raise StreamError.new("range at offset #{offset} has #{data.bytesize} bytes, not #{end_offset - offset}", offset: offset) \
|
|
69
|
+
unless data.bytesize == end_offset - offset
|
|
70
|
+
|
|
71
|
+
Wp2txt.decompress_bz2(data, offset: offset)
|
|
72
|
+
FileUtils.mkdir_p(@dir)
|
|
73
|
+
path = File.join(@dir, "#{offset}-#{end_offset}-#{Digest::SHA1.hexdigest(data)[0, DIGEST_LENGTH]}.bz2")
|
|
74
|
+
tmp = "#{path}.#{Process.pid}.#{SecureRandom.hex(4)}.part"
|
|
75
|
+
File.binwrite(tmp, data)
|
|
76
|
+
File.rename(tmp, path)
|
|
77
|
+
path
|
|
78
|
+
ensure
|
|
79
|
+
FileUtils.rm_f(tmp) if tmp && File.exist?(tmp)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def digest_of(path)
|
|
83
|
+
File.basename(path, ".bz2").split("-").last
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Bytes held, for the cache status
|
|
87
|
+
def size
|
|
88
|
+
Dir.glob(File.join(@dir, "*.bz2")).sum { |f| File.size(f) }
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wp2txt
|
|
4
|
+
# The arguments of a template call, read the way MediaWiki reads them. An
|
|
5
|
+
# argument is named when it holds an "=" outside nested markup; its name is
|
|
6
|
+
# the text before that "=" (any characters, case kept), and both name and
|
|
7
|
+
# value are trimmed. The other arguments are numbered from 1 in order and
|
|
8
|
+
# kept as written. A later argument with the same name or number replaces
|
|
9
|
+
# an earlier one, so in {{x|1=a|b}} the first argument is "b".
|
|
10
|
+
class TemplateArgs
|
|
11
|
+
# Highest number read as a position, so that a short call never makes a
|
|
12
|
+
# long list. An argument numbered above it (only an explicit "100000=x"
|
|
13
|
+
# can be) is read only by its number, args["100000"]
|
|
14
|
+
MAX_POSITION = 1000
|
|
15
|
+
|
|
16
|
+
# @param parts [Array<String>] the call's arguments, split at top-level pipes
|
|
17
|
+
def self.parse(parts)
|
|
18
|
+
values = {}
|
|
19
|
+
number = 0
|
|
20
|
+
parts.each do |part|
|
|
21
|
+
eq = top_level_equals(part)
|
|
22
|
+
if eq
|
|
23
|
+
values[part[0...eq].strip] = part[(eq + 1)..].strip
|
|
24
|
+
else
|
|
25
|
+
number += 1
|
|
26
|
+
values[number.to_s] = part
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
new(values)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Index of the first "=" not inside {{...}} or [[...]]
|
|
33
|
+
def self.top_level_equals(part)
|
|
34
|
+
depth = 0
|
|
35
|
+
i = 0
|
|
36
|
+
while i < part.length
|
|
37
|
+
pair = part[i, 2]
|
|
38
|
+
if pair == "{{" || pair == "[["
|
|
39
|
+
depth += 1
|
|
40
|
+
i += 2
|
|
41
|
+
elsif pair == "}}" || pair == "]]"
|
|
42
|
+
depth -= 1
|
|
43
|
+
i += 2
|
|
44
|
+
else
|
|
45
|
+
return i if part[i] == "=" && depth <= 0
|
|
46
|
+
|
|
47
|
+
i += 1
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
private_class_method :top_level_equals
|
|
53
|
+
|
|
54
|
+
# What a template or function shows, with each "=" held as `literal`, so
|
|
55
|
+
# that a template around it does not read the "=" as naming an argument.
|
|
56
|
+
# Templates in it still to be expanded keep their own "=".
|
|
57
|
+
def self.literal_equals(text, literal)
|
|
58
|
+
return text unless literal && text.include?("=")
|
|
59
|
+
|
|
60
|
+
depth = 0
|
|
61
|
+
out = +""
|
|
62
|
+
i = 0
|
|
63
|
+
while i < text.length
|
|
64
|
+
pair = text[i, 2]
|
|
65
|
+
if pair == "{{" || (pair == "}}" && depth.positive?)
|
|
66
|
+
depth += pair == "{{" ? 1 : -1
|
|
67
|
+
out << pair
|
|
68
|
+
i += 2
|
|
69
|
+
else
|
|
70
|
+
out << (text[i] == "=" && depth.zero? ? literal : text[i])
|
|
71
|
+
i += 1
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
out
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def initialize(values)
|
|
78
|
+
@values = values
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @param key [String, Integer] a name, or the number of an argument
|
|
82
|
+
def [](key)
|
|
83
|
+
@values[key.to_s]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def key?(key)
|
|
87
|
+
@values.key?(key.to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Numbered arguments from 1 up to the highest given (nil where one is missing)
|
|
91
|
+
def positional
|
|
92
|
+
(1..positional_count).map { |n| @values[n.to_s] }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The highest position given
|
|
96
|
+
def positional_count
|
|
97
|
+
@positional_count ||= @values.keys.grep(/\A[1-9]\d{0,3}\z/).map(&:to_i).select { |n| n <= MAX_POSITION }.max || 0
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Arguments with names that are not numbers
|
|
101
|
+
def named
|
|
102
|
+
@values.reject { |key, _| key.match?(/\A[1-9]\d*\z/) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The shape the expander's handlers read: {positional: [...], "name" => value}.
|
|
106
|
+
# A numbered argument skipped in the call reads as empty there.
|
|
107
|
+
def to_params
|
|
108
|
+
{ positional: positional.map { |value| value.to_s.strip } }.merge(named)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|