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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 44e433c039db5f633736354139193eed87e44ff9d61ff330d1e8a63135c02629
|
|
4
|
+
data.tar.gz: 66344664508344c3fc779c54a5281eb0dd09b3132682b81a3099c61666e1ceca
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1c61c461e05e40c73cdaf622fbf569c606426f354a9fb955fa8bd8e5b29d5fdb96eb8751c26b4a07355012ef4a65aff392ce1160bd4b8a8c7ec5084667a19c6b
|
|
7
|
+
data.tar.gz: c59c051b38e2920eeafb8d7adb822c183e7f450822295f4dbdb40b8bd2f4608371613741a2192aa9d34c1bab191f034b6fc1a935e1eba067ae6b4587fca2edc0
|
data/.dockerignore
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
.git
|
|
2
2
|
.github
|
|
3
|
-
.claude
|
|
4
3
|
image
|
|
5
4
|
pkg
|
|
6
5
|
spec
|
|
7
6
|
coverage
|
|
8
7
|
tmp
|
|
9
8
|
.bundle
|
|
10
|
-
research-notes
|
|
11
9
|
benchmark_results
|
|
12
10
|
data/output_samples
|
|
13
11
|
scripts
|
|
@@ -15,10 +13,8 @@ scripts
|
|
|
15
13
|
.solargraph.yml
|
|
16
14
|
.rubocop.yml
|
|
17
15
|
Gemfile.lock
|
|
18
|
-
.private-doc-tokens
|
|
19
16
|
**/.DS_Store
|
|
20
17
|
.ruby-version
|
|
21
|
-
CLAUDE.md
|
|
22
18
|
DEVELOPMENT.md
|
|
23
19
|
DEVELOPMENT_ja.md
|
|
24
20
|
*.gem
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -8,16 +8,31 @@ on:
|
|
|
8
8
|
- cron: '42 5 * * *'
|
|
9
9
|
|
|
10
10
|
jobs:
|
|
11
|
+
tracked-paths:
|
|
12
|
+
name: Tracked files match the allow list
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: ruby/setup-ruby@v1
|
|
17
|
+
with:
|
|
18
|
+
ruby-version: '3.3'
|
|
19
|
+
- run: ruby scripts/check_tracked_paths.rb
|
|
20
|
+
|
|
11
21
|
test:
|
|
22
|
+
needs: tracked-paths
|
|
12
23
|
strategy:
|
|
13
24
|
fail-fast: false
|
|
14
25
|
matrix:
|
|
15
26
|
ruby: ['3.1', '3.2', '3.3']
|
|
16
27
|
include:
|
|
28
|
+
# ruby-builder ships no 3.1 for ubuntu-26.04 (3.1 is past end of
|
|
29
|
+
# life), so the oldest supported version stays on the older image
|
|
30
|
+
- ruby: '3.1'
|
|
31
|
+
os: ubuntu-24.04
|
|
17
32
|
- ruby: 'head'
|
|
18
33
|
experimental: true
|
|
19
34
|
|
|
20
|
-
runs-on: ubuntu-latest
|
|
35
|
+
runs-on: ${{ matrix.os || 'ubuntu-latest' }}
|
|
21
36
|
name: Ruby ${{ matrix.ruby }}
|
|
22
37
|
continue-on-error: ${{ matrix.experimental || false }}
|
|
23
38
|
|
|
@@ -34,6 +34,14 @@ jobs:
|
|
|
34
34
|
steps:
|
|
35
35
|
- uses: actions/checkout@v4
|
|
36
36
|
|
|
37
|
+
- uses: ruby/setup-ruby@v1
|
|
38
|
+
with:
|
|
39
|
+
ruby-version: '3.3'
|
|
40
|
+
|
|
41
|
+
# Nothing is built from a commit that tracks files it should not
|
|
42
|
+
- name: Tracked files match the allow list
|
|
43
|
+
run: ruby scripts/check_tracked_paths.rb
|
|
44
|
+
|
|
37
45
|
- name: Work out the version and whether to publish
|
|
38
46
|
id: plan
|
|
39
47
|
run: |
|
|
@@ -60,10 +68,6 @@ jobs:
|
|
|
60
68
|
echo "publish=$publish" >> "$GITHUB_OUTPUT"
|
|
61
69
|
echo "Version $file_version; publish=$publish"
|
|
62
70
|
|
|
63
|
-
- uses: ruby/setup-ruby@v1
|
|
64
|
-
with:
|
|
65
|
-
ruby-version: '3.3'
|
|
66
|
-
|
|
67
71
|
- uses: docker/setup-qemu-action@v3
|
|
68
72
|
|
|
69
73
|
- uses: docker/setup-buildx-action@v3
|
data/.gitignore
CHANGED
|
@@ -20,20 +20,17 @@ tmp
|
|
|
20
20
|
*.~
|
|
21
21
|
tags
|
|
22
22
|
|
|
23
|
-
# Claude Code development files
|
|
24
|
-
CLAUDE.md
|
|
25
23
|
|
|
26
|
-
# Private design/research notes (not for publication)
|
|
27
|
-
research-notes/
|
|
28
24
|
|
|
29
25
|
# Error logs
|
|
30
26
|
error_log.txt
|
|
31
27
|
spec/examples.txt
|
|
32
|
-
IMPROVEMENTS.md
|
|
33
28
|
|
|
34
29
|
# Generated data
|
|
35
30
|
benchmark_results/
|
|
36
31
|
|
|
37
32
|
# Developer-specific Ruby version
|
|
38
33
|
.ruby-version
|
|
39
|
-
|
|
34
|
+
|
|
35
|
+
# Machine-local files (never committed)
|
|
36
|
+
.local/
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
8
8
|
Entries were rewritten in August 2026 to describe what changed for people using
|
|
9
9
|
wp2txt, rather than how it was implemented. The changes themselves are unaltered.
|
|
10
10
|
|
|
11
|
+
## [2.5.0] - 2026-11-27
|
|
12
|
+
|
|
13
|
+
- **`--articles` and `--from-category` fetch only the parts of the dump that hold the requested articles**: a few megabytes wherever the articles sit, kept in the cache for later runs. They used to fetch the dump from its start up to the last requested article, or all of it (about 25 GB for English)
|
|
14
|
+
- **Targeted extraction no longer reports a missing article as extracted**: an article cut short by a broken download, a page the index named but the dump did not hold, and a redirect written in place of its article all counted as success. Every requested title now ends written, skipped by `--skip-empty`, or failed with a reason, and the exit code says which: 0 when nothing is missing, 2 when some titles failed, 1 when nothing could be written. If you scripted around the old exit codes, check them
|
|
15
|
+
- **A manifest records each targeted extraction**: `<output>.manifest.json`, written next to the output files, gives the dump version used, the result and reason for every requested title, the counts, and the files written. A rerun to the same output replaces the files the previous manifest lists; files of a run that stopped part way are replaced by the next run. A run that fails leaves no manifest
|
|
16
|
+
- **The dump version is chosen and checked**: wp2txt uses the newest dump that Wikimedia marks finished, checks cached files against the sizes and SHA-1 digests Wikimedia publishes, and sets aside a cached file that does not match (`<name>.bad-<time>`) instead of using it. `--dump-date YYYYMMDD` uses a given version, and a version already in the cache works offline. `--update-cache` now checks for a newer version at once; it is no longer needed to replace damaged files. The cache directory must allow hard links
|
|
17
|
+
- **Titles are matched the way Wikipedia writes them**: underscores, a lower-case first letter, decomposed Unicode, and namespace prefixes in English or in the wiki's language are accepted. A redirect is followed one step and its article is written under the title you asked for (`requested_title` in JSON); `--no-follow-redirects` extracts the redirect page itself. An article reached by several requested titles is written once. `--articles-file` takes titles one per line, for titles with commas
|
|
18
|
+
- **Category listings are complete or not used**: subcategories are followed by page ID, breadth first, so every article within `--depth` is found whatever order the API returns them in, and wikis whose category namespace has another name now list subcategories too. A listing that cannot be read in full stops the extraction (exit code 1) unless you pass `--allow-incomplete-category`, and a category that does not exist is an error rather than an empty result. The listing is read once, so the preview and the extraction agree
|
|
19
|
+
- **The category listing cache is gone**: categories are listed from the API on each run. `category_expiry_days` in the configuration file is ignored
|
|
20
|
+
- **Options that work on a whole dump are rejected with `--articles`, `--articles-file`, and `--from-category`**: `--section-stats`, `--build-index` and the other index commands, `--ractor`, and `--no-turbo` used to be accepted and silently ignored
|
|
21
|
+
- **The index cache is rebuilt once**: the first targeted extraction after upgrading rebuilds the title index of each cached dump; it is now built without holding the whole index in memory
|
|
22
|
+
- **Damaged dump data is reported, not returned**: `get_article` and the other MCP tools raise an error for an article in a damaged part of the dump instead of returning a shortened text; `extract_corpus` stops on it
|
|
23
|
+
- **Templates are shown the way each Wikipedia shows them**: on the Japanese, English, German, French, Spanish, Italian, Russian, Chinese, Portuguese, Dutch, Polish, Arabic, and Korean editions, the most used inline templates (language labels, interlanguage links, readings, dates written as text, and similar) are rendered from patterns learned from that edition's own output, in its own language. Text they carry that used to be dropped is now kept, values given by name that they show (a simplified and a traditional spelling, a title) included. Templates those editions show only as blocks outside the sentence (hatnotes, maintenance boxes) are removed there. Templates that compute their output are not reproduced. The manifest records the patterns used (`template_patterns`)
|
|
24
|
+
- **A template wp2txt does not know keeps the text it was given**: its first unnamed argument (passing over a language code before another argument), where it used to be removed with everything in it. Named arguments, which are settings, are still left out
|
|
25
|
+
- **Template arguments are read as MediaWiki reads them**: named arguments in any script and case, explicit numbers such as `2=`, and a later argument replacing an earlier one. `{{!}}` and `{{=}}` come out as `|` and `=` instead of being dropped
|
|
26
|
+
- **Short citations and notes such as `{{Harvnp}}`, `{{Sfnm}}`, `{{Harvard citation}}`, and `{{ref+}}` are removed** like `{{Harv}}`, `{{Sfn}}`, and `{{Efn}}`; their first argument used to be left in the text
|
|
27
|
+
- **Categories are only those the page is in**: a link that shows a category without adding the page to it (`[[:Category:…]]`), and text such as `CT:` inside a template, were listed as categories, and a template written in a category name was kept in it. This applies to the text output and to metadata indexes built with `--build-index`; rebuild an index to update it
|
|
28
|
+
- **Dates written in words are kept as written**: `{{date|5|mars|1986}}` and dates given whole in one argument lost their month or all but the day
|
|
29
|
+
- **Square brackets that are not links are kept as written**: `[sic]`, `[англ.]`, and similar used to lose their brackets as if they were external links
|
|
30
|
+
- **`{{convert}}` keeps what the article wrote**: the values and unit as written, ranges included, without computed conversions
|
|
31
|
+
- **Ages count from the dump's date**, not from the day of the run, so the same dump gives the same text
|
|
32
|
+
- **For Ruby code**: `MultistreamReader#each_article_parallel` is replaced by `#each_entry_result`, which reports every entry (page, missing page, or the error of its part of the dump); pages carry `ns`, `redirect`, and `text_available`. `CategoryFetcher` no longer takes cache options
|
|
33
|
+
|
|
34
|
+
## [2.4.0] - 2026-10-02
|
|
35
|
+
|
|
36
|
+
- **Readings and link text are no longer dropped from Japanese articles**: template expansion, on by default, removed `{{読み仮名}}`, ruby templates, and `{{仮リンク}}` before they could be rendered, so a lead like "'''{{読み仮名|言語|げんご}}'''は…" came out as "は…" and names linked through 仮リンク vanished from running text (one in six Japanese articles uses 仮リンク). Re-extract if you rely on these articles
|
|
37
|
+
- **Interlanguage links import from current dumps again**: dumps now write one row per line, which the importer read as nothing while still reporting success. If you imported langlinks from a dump dated September 2026 or later, re-import with `-U`. An import that reads no rows now fails instead of succeeding quietly
|
|
38
|
+
- **`--lead-terms` (JSON output)**: lists the terms an article introduces in its lead — the bold terms of the first lead paragraph that has one (up to five), the parenthesized text written right after each (split at top-level commas and semicolons, and unsplit), and reading templates as text/reading pairs. Each term carries character offsets into the article's wikitext as stored in the dump, so its source can be cut out and checked. Nothing is interpreted: whether a note is a reading, a native spelling, or a date is left to you. Not available with `--ractor`
|
|
39
|
+
- **`--count-links`**: adds, for every article in an existing metadata index, how many articles link to it — each linking article counted once, through one redirect hop, using the wiki's own capitalization rule, and ignoring links that the wiki does not render (comments, `nowiki`, `pre`, `math`, and similar). About 12 minutes for Japanese Wikipedia
|
|
40
|
+
- **`--import-page-props`**: adds each article's Wikidata ID, MediaWiki's disambiguation mark, and its sort key from the `page_props` dump of the same date as the index. Once imported, JSON output, `get_article`, and `extract_corpus` carry `qid`, `sort_key`, and `disambiguation` (null or false when a page has none). The disambiguation mark finds many more disambiguation pages than their titles do. On Japanese Wikipedia the sort key is a reading normalized for sorting (voicing marks dropped), useful for checking reading candidates
|
|
41
|
+
- **`dump_info` reports where the new data came from**, including the `page_props` file's SHA-256 and the rule used to count links
|
|
42
|
+
|
|
11
43
|
## [2.3.4] - 2026-09-30
|
|
12
44
|
|
|
13
45
|
- **Records are no longer duplicated in `--no-turbo` output or in large `extract_corpus` runs**: the last record written before each batch could appear once more for every worker process — up to eight identical lines in a row. The default mode for `.bz2` dumps was not affected. If you extracted more than 200 articles with `extract_corpus`, or used `--no-turbo`, check earlier output for repeated lines; the repeats are byte-for-byte identical, so removing exact duplicate lines restores it
|
data/DEVELOPMENT.md
CHANGED
|
@@ -39,10 +39,14 @@ Input (bz2/xml) → StreamProcessor → Article Parser → OutputWriter → Outp
|
|
|
39
39
|
| `StreamProcessor` | `lib/wp2txt/stream_processor.rb` | Streams pages from compressed dumps with adaptive buffering |
|
|
40
40
|
| `Article` | `lib/wp2txt/article.rb` | Parses MediaWiki markup |
|
|
41
41
|
| `OutputWriter` | `lib/wp2txt.rb` | Manages output file rotation |
|
|
42
|
-
| `DumpManager` | `lib/wp2txt/multistream.rb` |
|
|
42
|
+
| `DumpManager` | `lib/wp2txt/multistream.rb` | Chooses a dump version, downloads and checks its files |
|
|
43
|
+
| `DumpSnapshot` | `lib/wp2txt/dump_snapshot.rb` | One finished dump version, from Wikimedia's `dumpstatus.json` |
|
|
44
|
+
| `StreamStore` | `lib/wp2txt/stream_store.rb` | Ranges of a dump fetched for targeted extraction |
|
|
43
45
|
| `MultistreamIndex` | `lib/wp2txt/multistream.rb` | Indexes articles for random access |
|
|
44
|
-
| `MultistreamReader` | `lib/wp2txt/multistream.rb` |
|
|
45
|
-
| `
|
|
46
|
+
| `MultistreamReader` | `lib/wp2txt/multistream.rb` | Reads articles and reports on every requested entry |
|
|
47
|
+
| `ExtractionRun` | `lib/wp2txt/extraction_run.rb` | Per-title results, exit code, and manifest of a targeted extraction |
|
|
48
|
+
| `SiteInfo` / `TitleNormalizer` | `lib/wp2txt/site_info.rb` | A wiki's namespaces and title rules |
|
|
49
|
+
| `CategoryFetcher` | `lib/wp2txt/multistream.rb` | Lists category members from the Wikipedia API, complete or not at all |
|
|
46
50
|
| `MemoryMonitor` | `lib/wp2txt/memory_monitor.rb` | Cross-platform memory monitoring |
|
|
47
51
|
| `Bz2Validator` | `lib/wp2txt/bz2_validator.rb` | Validates bz2 file integrity |
|
|
48
52
|
| `CLI` | `lib/wp2txt/cli.rb` | Command-line option parsing |
|
|
@@ -52,7 +56,6 @@ Input (bz2/xml) → StreamProcessor → Article Parser → OutputWriter → Outp
|
|
|
52
56
|
| Class | File | Purpose |
|
|
53
57
|
|-------|------|---------|
|
|
54
58
|
| `GlobalDataCache` | `lib/wp2txt/global_data_cache.rb` | SQLite cache for parsed JSON data files |
|
|
55
|
-
| `CategoryCache` | `lib/wp2txt/category_cache.rb` | SQLite cache for Wikipedia category hierarchy |
|
|
56
59
|
| `IndexCache` | `lib/wp2txt/index_cache.rb` | SQLite cache for multistream index entries |
|
|
57
60
|
|
|
58
61
|
### Element Types
|
|
@@ -96,13 +99,22 @@ The `TemplateExpander` class (`lib/wp2txt/template_expander.rb`) expands common
|
|
|
96
99
|
| Template Type | Example | Output |
|
|
97
100
|
|---------------|---------|--------|
|
|
98
101
|
| Birth/death dates | `{{birth date|1990|5|15}}` | "May 15, 1990" |
|
|
99
|
-
| Unit conversion | `{{convert|100|km|mi}}` | "100 km (
|
|
102
|
+
| Unit conversion | `{{convert|100|km|mi}}` | "100 km" (as written, no computed conversion) |
|
|
100
103
|
| Coordinates | `{{coord|35|41|N|139|41|E}}` | "35°41′N 139°41′E" |
|
|
101
104
|
| Language tags | `{{lang|ja|日本語}}` | "日本語" |
|
|
102
105
|
| Nihongo | `{{nihongo|Tokyo|東京|Tōkyō}}` | "Tokyo (東京, Tōkyō)" |
|
|
103
106
|
|
|
104
107
|
Template expansion is enabled by default. Disable with `--no-expand-templates` or `expand_templates: false`.
|
|
105
108
|
|
|
109
|
+
Arguments are read by `TemplateArgs` (`lib/wp2txt/template_args.rb`) as MediaWiki reads them. When the config gives the wiki (`lang:`), templates that edition is known to show are rendered first from `lib/wp2txt/data/template_patterns.json` by `TemplatePatterns`; citations and templates removed by rule stay removed. The data is generated by a maintainer script that asks each edition's parser how it displays its most used templates:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
bundle exec ruby scripts/fetch_template_patterns.rb # all editions
|
|
113
|
+
bundle exec ruby scripts/fetch_template_patterns.rb ja ko # some editions, keeping the others
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
A pattern holds the displayed text with argument slots (`"ドイツ語: {1}"`), with variants for one or two arguments (`p1`, `p2`), for each language code in a code argument (`code_arg`, `by_code`), for a named argument shown in place of a positional one (`replaces`), and for an argument left empty that the template fills from another (`fallback`). A template that takes a number records how many arguments it was learned with (`n`); a call with more, or with other text than a number first, is left to the other rules, as the pattern does not tell how it shows them. An argument that the template shows as written for every language code tried is an ordinary argument, not a code. One that changes nothing for any code is taken for a code only as a language code is written, before the text it marks, with every other argument shown; the pattern then records the number of arguments tried (`n`) and, where some codes showed otherwise or failed, the codes that showed so (`codes`), and is used only for calls of that shape, with every argument given and one of those codes. Otherwise, or where the pattern shows otherwise a call already seen, the template is tried in the other shapes, and left unknown if none fits. No language code is a number, so a code pattern is not used for a call with a number in the code's place. An argument that a pattern leaves out must not change the text (tried with two sets of numbers that read as a date), or the pattern is not used, as a date template showing only the year would drop the rest. A pattern with digits of its own (in any script) that are not runs of digits in the template's name (a date of the day it was learned, an image size) is not used; those of the name, as in `{{2e|voix}}`, are written in the call. Text hidden from readers (`display:none`) and warnings shown only in an edit preview are not part of a pattern. A rendering with braces of its own is not a pattern, as they would read as slots. A template shown only as blocks that show none of its arguments, for every set of arguments tried, is recorded with an empty pattern. One whose blocks show an argument (a list, a centred line) or that showed nothing at all is left unknown, as it holds written text or may show text for other arguments. Named arguments are learned by name: those the template's source reads (`{{{name|...}}}`) and its TemplateData documents, with `label`, `lt` and `text`, up to 40, are each given alone with a marker, `y` and `no` to the call the pattern was learned from. One that changes the text for none of them changes nothing (`ignores`); one whose marker is shown is shown (`shows`), in place of a positional argument (`replaces`) or with a pattern of its own (`with`, where `y`, or a number for an argument that takes one, shows in its place alike); one that changes the text alike for the marker and `y`, and for `no` alike or not at all, has a pattern of its own too, and the values that change nothing (`no`, and of `n`, `NO`, `No`, `false`, `0`, `off` those that do not) are read as off (`off`). All the names are then given at once, with and without the positional arguments (where that fails, those that changed nothing are given one by one, alone): one shown there is shown after all, as one shown only beside another. A call is shown by the pattern where its named arguments change nothing, stand in for an argument (the first given for each), are read as off, or are not known; by the pattern learned with a named argument where the call gives no other known and that pattern keeps the arguments of the call's own (unless it shows the named argument in their place) without joining text to one the call leaves empty; and otherwise by its pattern, followed by the values of the named arguments the template shows that hold a word or a number, each once, in the call's order. Templates shown as a block marker (`[INFOBOX]`, `[REFERENCES]`) whose pattern shows no text are left to the markers. A value is taken as shown already where the text shows it, links read as the text they show. A code pattern learned from n arguments is not used for a call with a named argument not known. Templates neither known nor handled by rule are left to the cleaner, which keeps their first unnamed argument, passing over a language code before another argument. Whatever a template shows has its "=" held as a placeholder until format_wiki ends, so a template around it does not read it as naming an argument. A full run takes several hours, at one request per second.
|
|
117
|
+
|
|
106
118
|
### Magic Word Expansion
|
|
107
119
|
|
|
108
120
|
The `MagicWordExpander` class (`lib/wp2txt/magic_words.rb`) expands MediaWiki magic words to their actual values:
|
|
@@ -139,28 +151,6 @@ Wp2txt::GlobalDataCache.stats # Get cache statistics
|
|
|
139
151
|
|
|
140
152
|
Cache validation: Checks source file modification time and size. Cache is automatically invalidated when source files change.
|
|
141
153
|
|
|
142
|
-
### CategoryCache
|
|
143
|
-
|
|
144
|
-
Caches Wikipedia category hierarchy from API for faster category-based article extraction:
|
|
145
|
-
|
|
146
|
-
```ruby
|
|
147
|
-
cache = Wp2txt::CategoryCache.new("en", cache_dir: "/path/to/cache")
|
|
148
|
-
|
|
149
|
-
# Save category data
|
|
150
|
-
cache.save("Category Name", ["Article1", "Article2"], ["Subcategory1"])
|
|
151
|
-
|
|
152
|
-
# Retrieve category data
|
|
153
|
-
data = cache.get("Category Name") # { pages: [...], subcats: [...] }
|
|
154
|
-
|
|
155
|
-
# Get all pages in category tree
|
|
156
|
-
pages = cache.get_all_pages("Root Category", max_depth: 2)
|
|
157
|
-
|
|
158
|
-
# Statistics and maintenance
|
|
159
|
-
cache.stats # Cache statistics
|
|
160
|
-
cache.cleanup_expired! # Remove stale entries
|
|
161
|
-
cache.clear! # Clear all data
|
|
162
|
-
```
|
|
163
|
-
|
|
164
154
|
### IndexCache
|
|
165
155
|
|
|
166
156
|
Caches parsed multistream index entries for fast article lookup:
|
|
@@ -171,12 +161,12 @@ cache = Wp2txt::IndexCache.new("/path/to/index.txt", cache_dir: "/path/to/cache"
|
|
|
171
161
|
# Check cache validity
|
|
172
162
|
cache.valid? # true if cache exists and matches source file
|
|
173
163
|
|
|
174
|
-
#
|
|
175
|
-
cache.
|
|
176
|
-
data = cache.load # { entries_by_title: {}, entries_by_id: {}, stream_offsets: [] }
|
|
164
|
+
# Build from the index file, line by line, into a temporary file renamed into place
|
|
165
|
+
cache.build!
|
|
177
166
|
|
|
178
167
|
# Batch lookup
|
|
179
168
|
results = cache.find_by_titles(["Article1", "Article2"])
|
|
169
|
+
results = cache.find_by_ids([12345])
|
|
180
170
|
```
|
|
181
171
|
|
|
182
172
|
### Cache Location
|
|
@@ -185,10 +175,13 @@ All caches are stored in `~/.wp2txt/cache/`:
|
|
|
185
175
|
|
|
186
176
|
```
|
|
187
177
|
~/.wp2txt/cache/
|
|
188
|
-
├── global_data.sqlite3
|
|
189
|
-
├──
|
|
190
|
-
├──
|
|
191
|
-
└──
|
|
178
|
+
├── global_data.sqlite3 # GlobalDataCache
|
|
179
|
+
├── enwiki-<date>-multistream-index.txt.bz2 # files of the version in use (hard links)
|
|
180
|
+
├── enwiki-<date>-multistream-index.txt_*.sqlite3 # IndexCache (per index file)
|
|
181
|
+
└── enwiki-<date>-<snapshot>/ # one dump version
|
|
182
|
+
├── dumpstatus.json
|
|
183
|
+
├── <official file names> # checked files (hard links)
|
|
184
|
+
└── streams/ # StreamStore ranges
|
|
192
185
|
```
|
|
193
186
|
|
|
194
187
|
## Test System
|
|
@@ -232,81 +225,42 @@ WP2TXT supports Wikipedia's multistream format for efficient article extraction.
|
|
|
232
225
|
1. **Index file** (`-multistream-index.txt.bz2`): Maps article titles to byte offsets
|
|
233
226
|
2. **Multistream file** (`-multistream.xml.bz2`): Concatenated bz2 streams
|
|
234
227
|
|
|
235
|
-
###
|
|
228
|
+
### Reading Articles
|
|
236
229
|
|
|
237
|
-
`MultistreamReader`
|
|
230
|
+
`MultistreamReader#each_entry_result` reports on every requested entry, so that nothing goes missing silently:
|
|
238
231
|
|
|
239
232
|
```ruby
|
|
240
|
-
reader = MultistreamReader.new(multistream_path,
|
|
241
|
-
|
|
242
|
-
#
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
# Iterate with parallel processing
|
|
246
|
-
reader.each_article_parallel(entries, num_processes: 4) do |page|
|
|
247
|
-
process(page)
|
|
233
|
+
reader = MultistreamReader.new(multistream_path, index)
|
|
234
|
+
reader.each_entry_result(entries, num_processes: 4) do |entry, page, error|
|
|
235
|
+
# page: the page read (with ns, redirect, text_available), or nil when its range holds no such page
|
|
236
|
+
# error: { reason:, message: } when the range could not be read
|
|
248
237
|
end
|
|
249
238
|
```
|
|
250
239
|
|
|
251
|
-
|
|
240
|
+
Entries are grouped by stream offset, so each bz2 stream is decompressed once. A stream that does not decompress in full raises `StreamError` instead of returning a shortened page.
|
|
252
241
|
|
|
253
|
-
### Partial Downloads
|
|
242
|
+
### Dump Versions and Partial Downloads
|
|
254
243
|
|
|
255
|
-
|
|
244
|
+
`DumpManager` works with one `DumpSnapshot`: the date given with `dump_date:`, else the newest finished version (checked over the network at most once every `dump_expiry_days`), else, offline, the newest one cached. Every file is checked against the size and SHA-1 that version's `dumpstatus.json` records, and kept under `<cache>/<lang>wiki-<date>-<snapshot>/` by hard link.
|
|
256
245
|
|
|
257
|
-
|
|
258
|
-
# Only download first N streams
|
|
259
|
-
manager.download_multistream(max_streams: 10)
|
|
260
|
-
|
|
261
|
-
# Download only needed byte range
|
|
262
|
-
download_file_range(url, path, start_byte, end_byte)
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
### Incremental Downloads
|
|
266
|
-
|
|
267
|
-
When a partial dump exists, `download_multistream_full` can resume the download:
|
|
246
|
+
For targeted extraction only the needed ranges of the dump are fetched:
|
|
268
247
|
|
|
269
248
|
```ruby
|
|
270
249
|
manager = DumpManager.new("ja")
|
|
271
|
-
|
|
272
|
-
# Check for existing partial dump
|
|
273
|
-
partial = manager.find_any_partial_cache
|
|
274
|
-
# => { path: "...", dump_date: "20260101", stream_count: 100, size: 1000000, mtime: ... }
|
|
275
|
-
|
|
276
|
-
# Check if incremental download is possible
|
|
277
|
-
resume_info = manager.can_resume_from_partial?(partial)
|
|
278
|
-
# => { possible: true, current_streams: 100, total_streams: 5000, current_size: 1000000 }
|
|
279
|
-
# => { possible: false, reason: :date_mismatch, partial_date: "20250101", latest_date: "20260101" }
|
|
280
|
-
|
|
281
|
-
# Download full dump with incremental support (interactive prompts)
|
|
282
|
-
path = manager.download_multistream_full(interactive: true)
|
|
283
|
-
|
|
284
|
-
# Non-interactive mode (skips user prompts, always downloads fresh if needed)
|
|
285
|
-
path = manager.download_multistream_full(interactive: false)
|
|
250
|
+
store = manager.fetch_streams([[offset, end_offset]]) # HTTP Range requests, at most two at a time
|
|
286
251
|
```
|
|
287
252
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
1. **Same date partial exists:**
|
|
291
|
-
- `[Y]` Resume download (download only remaining data)
|
|
292
|
-
- `[n]` Use existing partial as-is
|
|
293
|
-
- `[f]` Download fresh full dump
|
|
294
|
-
|
|
295
|
-
2. **Outdated partial exists:**
|
|
296
|
-
- `[D]` Delete old partial and download latest (recommended)
|
|
297
|
-
- `[k]` Keep old partial, download latest separately
|
|
298
|
-
- `[u]` Use old partial as-is (may have outdated content)
|
|
253
|
+
Each range is kept in the `StreamStore` as its own file, named by its byte range and SHA-1, and only once it decompresses in full. A range that cannot be fetched is left out, and reading it raises `StreamUnavailableError`.
|
|
299
254
|
|
|
300
255
|
### Article Extraction Flow
|
|
301
256
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
```
|
|
257
|
+
1. Choose the dump version and download its index
|
|
258
|
+
2. Build the index cache (SQLite), once per index file
|
|
259
|
+
3. Normalize the requested titles with the wiki's site information and look them up
|
|
260
|
+
4. Follow redirects one step
|
|
261
|
+
5. Fetch the ranges holding the articles
|
|
262
|
+
6. Read every entry and record its result in the `ExtractionRun`
|
|
263
|
+
7. Publish the output files and the manifest together
|
|
310
264
|
|
|
311
265
|
## Memory Management
|
|
312
266
|
|
|
@@ -398,6 +352,22 @@ docs/INDEXES.md is checked against the actual server surface by spec/docs_sync_s
|
|
|
398
352
|
- RuboCop configuration in `.rubocop.yml`
|
|
399
353
|
- UTF-8 encoding throughout
|
|
400
354
|
|
|
355
|
+
## Tracked Files
|
|
356
|
+
|
|
357
|
+
Every tracked file must match a line of `scripts/tracked_paths.allow`, and every
|
|
358
|
+
line there must match a tracked file. The gem is built from `git ls-files`, so
|
|
359
|
+
committing a file is the first step of publishing it.
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
ruby scripts/check_tracked_paths.rb # the index (what the next commit holds)
|
|
363
|
+
ruby scripts/check_tracked_paths.rb --tree HEAD # a commit, by the list inside it
|
|
364
|
+
ln -s ../../scripts/pre-push .git/hooks/pre-push # check each pushed commit before it leaves
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
CI runs the same check first. When a new file belongs in the repository, add the
|
|
368
|
+
narrowest line that describes its place and kind (name Markdown files one by one)
|
|
369
|
+
and stage the list with it.
|
|
370
|
+
|
|
401
371
|
## Docker
|
|
402
372
|
|
|
403
373
|
Images are published by GitHub Actions when a `v*` tag is pushed
|
|
@@ -406,7 +376,7 @@ local build sends the working tree as its context, so untracked files ride
|
|
|
406
376
|
along, while a runner starts from a clean checkout.
|
|
407
377
|
|
|
408
378
|
```bash
|
|
409
|
-
rake check_image # Build
|
|
379
|
+
rake check_image # Build from a clean copy of the last commit and run the workflow's gate
|
|
410
380
|
```
|
|
411
381
|
|
|
412
382
|
The gate (`scripts/verify_image.rb`) lists what the image holds under `/wp2txt`
|