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
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/.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
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,29 @@ 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
|
+
|
|
11
34
|
## [2.4.0] - 2026-10-02
|
|
12
35
|
|
|
13
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
|
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
|
data/DEVELOPMENT_ja.md
CHANGED
|
@@ -39,10 +39,14 @@ WP2TXTはWikipediaダンプを処理するためにストリーミングアー
|
|
|
39
39
|
| `StreamProcessor` | `lib/wp2txt/stream_processor.rb` | 適応的バッファリングで圧縮ダンプからページをストリーミング |
|
|
40
40
|
| `Article` | `lib/wp2txt/article.rb` | MediaWikiマークアップをパース |
|
|
41
41
|
| `OutputWriter` | `lib/wp2txt.rb` | 出力ファイルローテーションを管理 |
|
|
42
|
-
| `DumpManager` | `lib/wp2txt/multistream.rb` |
|
|
42
|
+
| `DumpManager` | `lib/wp2txt/multistream.rb` | ダンプの版を選び、ファイルをダウンロードして照合 |
|
|
43
|
+
| `DumpSnapshot` | `lib/wp2txt/dump_snapshot.rb` | Wikimediaの`dumpstatus.json`による、完了したダンプの一つの版 |
|
|
44
|
+
| `StreamStore` | `lib/wp2txt/stream_store.rb` | 対象を絞った抽出で取得したダンプの範囲 |
|
|
43
45
|
| `MultistreamIndex` | `lib/wp2txt/multistream.rb` | ランダムアクセス用に記事をインデックス化 |
|
|
44
|
-
| `MultistreamReader` | `lib/wp2txt/multistream.rb` |
|
|
45
|
-
| `
|
|
46
|
+
| `MultistreamReader` | `lib/wp2txt/multistream.rb` | 記事を読み、要求されたすべてのエントリの結果を報告 |
|
|
47
|
+
| `ExtractionRun` | `lib/wp2txt/extraction_run.rb` | 対象を絞った抽出の、題名ごとの結果、終了コード、マニフェスト |
|
|
48
|
+
| `SiteInfo` / `TitleNormalizer` | `lib/wp2txt/site_info.rb` | ウィキの名前空間と題名の規則 |
|
|
49
|
+
| `CategoryFetcher` | `lib/wp2txt/multistream.rb` | Wikipedia APIからカテゴリメンバーを一覧(すべて読めた場合だけ使う) |
|
|
46
50
|
| `MemoryMonitor` | `lib/wp2txt/memory_monitor.rb` | クロスプラットフォームメモリ監視 |
|
|
47
51
|
| `Bz2Validator` | `lib/wp2txt/bz2_validator.rb` | bz2ファイルの整合性を検証 |
|
|
48
52
|
| `CLI` | `lib/wp2txt/cli.rb` | コマンドラインオプションのパース |
|
|
@@ -52,7 +56,6 @@ WP2TXTはWikipediaダンプを処理するためにストリーミングアー
|
|
|
52
56
|
| クラス | ファイル | 目的 |
|
|
53
57
|
|--------|----------|------|
|
|
54
58
|
| `GlobalDataCache` | `lib/wp2txt/global_data_cache.rb` | パース済みJSONデータファイルのSQLiteキャッシュ |
|
|
55
|
-
| `CategoryCache` | `lib/wp2txt/category_cache.rb` | Wikipediaカテゴリ階層のSQLiteキャッシュ |
|
|
56
59
|
| `IndexCache` | `lib/wp2txt/index_cache.rb` | マルチストリームインデックスエントリのSQLiteキャッシュ |
|
|
57
60
|
|
|
58
61
|
### 要素タイプ
|
|
@@ -96,13 +99,22 @@ MARKER_TYPES = %i[math code chem table score timeline graph ipa].freeze
|
|
|
96
99
|
| テンプレートタイプ | 例 | 出力 |
|
|
97
100
|
|--------------------|-----|------|
|
|
98
101
|
| 生年月日/没年月日 | `{{birth date|1990|5|15}}` | "May 15, 1990" |
|
|
99
|
-
| 単位変換 | `{{convert|100|km|mi}}` | "100 km
|
|
102
|
+
| 単位変換 | `{{convert|100|km|mi}}` | "100 km"(書かれたとおり。換算値は計算しない) |
|
|
100
103
|
| 座標 | `{{coord|35|41|N|139|41|E}}` | "35°41′N 139°41′E" |
|
|
101
104
|
| 言語タグ | `{{lang|ja|日本語}}` | "日本語" |
|
|
102
105
|
| 仮名 | `{{nihongo|Tokyo|東京|Tōkyō}}` | "Tokyo (東京, Tōkyō)" |
|
|
103
106
|
|
|
104
107
|
テンプレート展開はデフォルトで有効です。`--no-expand-templates`または`expand_templates: false`で無効化できます。
|
|
105
108
|
|
|
109
|
+
引数は`TemplateArgs`(`lib/wp2txt/template_args.rb`)がMediaWikiと同じように読みます。設定でウィキを指定すると(`lang:`)、その版での表示が分かっているテンプレートは、まず`TemplatePatterns`が`lib/wp2txt/data/template_patterns.json`から表示します。引用と、規則で取り除くテンプレートは取り除いたままです。データは、各版のパーサーに、よく使われるテンプレートの表示を訊く保守用スクリプトで作ります:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
bundle exec ruby scripts/fetch_template_patterns.rb # すべての版
|
|
113
|
+
bundle exec ruby scripts/fetch_template_patterns.rb ja ko # 一部の版(ほかの版は残す)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
型は、表示された文字列に引数の枠を入れたものです(`"ドイツ語: {1}"`)。引数が一つまたは二つのときの変種(`p1`、`p2`)、言語コードの引数があるときのコードごとの型(`code_arg`、`by_code`)、名前のない引数の代わりに表示される名前付き引数(`replaces`)、空のときにテンプレートが別の引数で埋める引数(`fallback`)を持ちます。数を取るテンプレートは、学習した引数の数(`n`)を記録します。それより多い呼び出しや、最初が数でない呼び出しは、型がその表示を示さないので、ほかの規則に任せます。試したどの言語コードでも書かれたとおりに表示される引数は、コードではなく普通の引数です。どのコードでも何も変えない引数は、言語コードの書き方をしている場合(示す文字列の前にあり、ほかの引数がすべて表示される場合)に限ってコードとみなします。このとき型は、試した引数の数(`n`)と、別の表示になったり失敗したりしたコードがあれば型どおりに表示したコード(`codes`)を記録し、その形で、すべての引数があり、それらのコードのどれかを持つ呼び出しにだけ使います。それ以外の場合や、すでに見た呼び出しを型が別の表示にする場合は、ほかの形で試し、どれにも合わなければ未知とします。言語コードは数ではないので、コードの位置に数がある呼び出しにはコードの型を使いません。型が省く引数は表示を変えてはならず(日付として読める二組の数で試します)、変える場合は型を使いません。年だけを表示する日付テンプレートでは、残りが落ちるためです。テンプレート名の数字の並びにない固有の数字(どの文字体系でも)を持つ型(学習した日の日付、画像の大きさ)は使いません。`{{2e|voix}}`のように名前にある数字は、呼び出しに書かれています。読者に隠された文字列(`display:none`)と、編集プレビューでだけ出る警告は、型に含めません。波括弧を含む表示は、枠と読まれるので型にしません。試したどの引数でも、引数を一つも表示しないブロックだけで表示されるテンプレートは、空の型として記録します。ブロックが引数を表示するもの(リスト、中央寄せの行)や、何も表示しなかったものは未知とします。書かれた文字列を持つか、ほかの引数では文字列を表示しうるためです。名前付き引数は名前ごとに学びます。テンプレートのソースが読む名前(`{{{name|...}}}`)とTemplateDataに記された名前に、`label`、`lt`、`text`を加えた最大40個を、型を学んだ呼び出しに、目印、`y`、`no`のそれぞれ単独で与えます。どれでも表示を変えない名前は何も変えません(`ignores`)。目印が表示される名前は表示されます(`shows`)。名前のない引数の代わりに表示されるか(`replaces`)、独自の型を持ちます(`with`。`y`、数を取る引数では数も、同じ位置に表示される場合)。目印と`y`で同じように表示を変え、`no`でも同じように変えるか何も変えない名前も独自の型を持ち、何も変えない値(`no`と、`n`、`NO`、`No`、`false`、`0`、`off`のうち何も変えないもの)はオフと読みます(`off`)。続いて、すべての名前を一度に、名前のない引数ありとなしで与えます(失敗したら、何も変えなかった名前を一つずつ単独で与えます)。そこで表示された名前は、ほかの名前と並べたときだけ表示されるものとして、表示されるとみなします。呼び出しは、名前付き引数が何も変えないか、引数の代わりに立つか(引数ごとに最初に与えられたもの)、オフと読まれるか、未知である場合は型で表示します。ほかに既知の名前を与えず、その名前で学んだ型が呼び出し自身の型の引数を保ち(名前付き引数をその位置に表示する場合を除く)、呼び出しが空にした引数に文字列が接しない場合は、その名前で学んだ型で表示します。それ以外は、呼び出しの型に続けて、テンプレートが表示する名前付き引数のうち語か数を含む値を、呼び出しの順に一度ずつ表示します。ブロックのマーカー(`[INFOBOX]`、`[REFERENCES]`)で表すテンプレートのうち、型が何も表示しないものはマーカーに任せます。値は、本文がすでにそれを表示していれば表示済みとみなします(リンクは表示される文字列として読みます)。n個の引数から学んだコードの型は、未知の名前付き引数を持つ呼び出しには使いません。既知でも規則で扱うものでもないテンプレートはクリーナーに任せ、クリーナーは最初の名前のない引数を残します(別の引数の前にある言語コードは飛ばします)。テンプレートが表示するものの「=」は、format_wikiが終わるまで置き換え文字で保ち、外側のテンプレートが引数名の区切りと読まないようにします。全体の実行には、毎秒1リクエストで数時間かかります。
|
|
117
|
+
|
|
106
118
|
### マジックワード展開
|
|
107
119
|
|
|
108
120
|
`MagicWordExpander`クラス(`lib/wp2txt/magic_words.rb`)はMediaWikiマジックワードを実際の値に展開します:
|
|
@@ -139,28 +151,6 @@ Wp2txt::GlobalDataCache.stats # キャッシュ統計を取得
|
|
|
139
151
|
|
|
140
152
|
キャッシュ検証:ソースファイルの変更時刻とサイズをチェックします。ソースファイルが変更されると自動的にキャッシュは無効化されます。
|
|
141
153
|
|
|
142
|
-
### CategoryCache
|
|
143
|
-
|
|
144
|
-
Wikipedia APIからのカテゴリ階層をキャッシュし、カテゴリベースの記事抽出を高速化:
|
|
145
|
-
|
|
146
|
-
```ruby
|
|
147
|
-
cache = Wp2txt::CategoryCache.new("en", cache_dir: "/path/to/cache")
|
|
148
|
-
|
|
149
|
-
# カテゴリデータを保存
|
|
150
|
-
cache.save("カテゴリ名", ["記事1", "記事2"], ["サブカテゴリ1"])
|
|
151
|
-
|
|
152
|
-
# カテゴリデータを取得
|
|
153
|
-
data = cache.get("カテゴリ名") # { pages: [...], subcats: [...] }
|
|
154
|
-
|
|
155
|
-
# カテゴリツリー内のすべてのページを取得
|
|
156
|
-
pages = cache.get_all_pages("ルートカテゴリ", max_depth: 2)
|
|
157
|
-
|
|
158
|
-
# 統計とメンテナンス
|
|
159
|
-
cache.stats # キャッシュ統計
|
|
160
|
-
cache.cleanup_expired! # 古いエントリを削除
|
|
161
|
-
cache.clear! # すべてクリア
|
|
162
|
-
```
|
|
163
|
-
|
|
164
154
|
### IndexCache
|
|
165
155
|
|
|
166
156
|
パース済みマルチストリームインデックスエントリをキャッシュし、記事の高速検索を実現:
|
|
@@ -171,12 +161,12 @@ cache = Wp2txt::IndexCache.new("/path/to/index.txt", cache_dir: "/path/to/cache"
|
|
|
171
161
|
# キャッシュの有効性を確認
|
|
172
162
|
cache.valid? # キャッシュが存在しソースファイルと一致する場合 true
|
|
173
163
|
|
|
174
|
-
#
|
|
175
|
-
cache.
|
|
176
|
-
data = cache.load # { entries_by_title: {}, entries_by_id: {}, stream_offsets: [] }
|
|
164
|
+
# インデックスファイルから1行ずつ一時ファイルに作り、置き換える
|
|
165
|
+
cache.build!
|
|
177
166
|
|
|
178
167
|
# バッチ検索
|
|
179
168
|
results = cache.find_by_titles(["記事1", "記事2"])
|
|
169
|
+
results = cache.find_by_ids([12345])
|
|
180
170
|
```
|
|
181
171
|
|
|
182
172
|
### キャッシュの場所
|
|
@@ -185,10 +175,13 @@ results = cache.find_by_titles(["記事1", "記事2"])
|
|
|
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 # 使用中の版のファイル(ハードリンク)
|
|
180
|
+
├── enwiki-<date>-multistream-index.txt_*.sqlite3 # IndexCache(インデックスファイルごと)
|
|
181
|
+
└── enwiki-<date>-<snapshot>/ # ダンプの一つの版
|
|
182
|
+
├── dumpstatus.json
|
|
183
|
+
├── <公式のファイル名> # 照合済みのファイル(ハードリンク)
|
|
184
|
+
└── streams/ # StreamStoreの範囲
|
|
192
185
|
```
|
|
193
186
|
|
|
194
187
|
## テストシステム
|
|
@@ -232,81 +225,42 @@ WP2TXTは効率的な記事抽出のためにWikipediaのマルチストリー
|
|
|
232
225
|
1. **インデックスファイル** (`-multistream-index.txt.bz2`): 記事タイトルをバイトオフセットにマッピング
|
|
233
226
|
2. **マルチストリームファイル** (`-multistream.xml.bz2`): 連結されたbz2ストリーム
|
|
234
227
|
|
|
235
|
-
###
|
|
228
|
+
### 記事の読み取り
|
|
236
229
|
|
|
237
|
-
`MultistreamReader
|
|
230
|
+
`MultistreamReader#each_entry_result`は、要求されたすべてのエントリについて結果を報告し、黙って欠けることがないようにします:
|
|
238
231
|
|
|
239
232
|
```ruby
|
|
240
|
-
reader = MultistreamReader.new(multistream_path,
|
|
241
|
-
|
|
242
|
-
#
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
# 並列処理でイテレート
|
|
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: 読んだページ(ns、redirect、text_availableを持つ)。範囲にそのページが無ければnil
|
|
236
|
+
# error: 範囲を読めなかったときの { reason:, message: }
|
|
248
237
|
end
|
|
249
238
|
```
|
|
250
239
|
|
|
251
|
-
|
|
240
|
+
エントリはストリームオフセットでまとめるので、各bz2ストリームは一度だけ解凍します。最後まで解凍できないストリームは、短くなったページを返さず`StreamError`を発生させます。
|
|
252
241
|
|
|
253
|
-
###
|
|
242
|
+
### ダンプの版と部分ダウンロード
|
|
254
243
|
|
|
255
|
-
|
|
244
|
+
`DumpManager`は一つの`DumpSnapshot`を使います。`dump_date:`で指定した日付、なければ完了した最新の版(ネットワークでの確認は`dump_expiry_days`ごとに最大1回)、オフラインならキャッシュにある最新の版です。すべてのファイルを、その版の`dumpstatus.json`にあるサイズとSHA-1で照合し、`<cache>/<lang>wiki-<date>-<snapshot>/`にハードリンクで保存します。
|
|
256
245
|
|
|
257
|
-
|
|
258
|
-
# 最初のNストリームのみダウンロード
|
|
259
|
-
manager.download_multistream(max_streams: 10)
|
|
260
|
-
|
|
261
|
-
# 必要なバイト範囲のみダウンロード
|
|
262
|
-
download_file_range(url, path, start_byte, end_byte)
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
### 差分ダウンロード
|
|
266
|
-
|
|
267
|
-
部分ダンプが存在する場合、`download_multistream_full`はダウンロードを再開できます:
|
|
246
|
+
対象を絞った抽出では、ダンプの必要な範囲だけを取得します:
|
|
268
247
|
|
|
269
248
|
```ruby
|
|
270
249
|
manager = DumpManager.new("ja")
|
|
271
|
-
|
|
272
|
-
# 既存の部分ダンプを確認
|
|
273
|
-
partial = manager.find_any_partial_cache
|
|
274
|
-
# => { path: "...", dump_date: "20260101", stream_count: 100, size: 1000000, mtime: ... }
|
|
275
|
-
|
|
276
|
-
# 差分ダウンロードが可能か確認
|
|
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
|
-
# 差分ダウンロードサポート付きでフルダンプをダウンロード(対話式プロンプト)
|
|
282
|
-
path = manager.download_multistream_full(interactive: true)
|
|
283
|
-
|
|
284
|
-
# 非対話モード(ユーザープロンプトをスキップ、必要に応じて常に新規ダウンロード)
|
|
285
|
-
path = manager.download_multistream_full(interactive: false)
|
|
250
|
+
store = manager.fetch_streams([[offset, end_offset]]) # HTTPのRange要求、同時に最大2つ
|
|
286
251
|
```
|
|
287
252
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
1. **同一日付の部分ダンプが存在:**
|
|
291
|
-
- `[Y]` ダウンロードを再開(残りのデータのみダウンロード)
|
|
292
|
-
- `[n]` 既存の部分ダンプをそのまま使用
|
|
293
|
-
- `[f]` 新規フルダンプをダウンロード
|
|
294
|
-
|
|
295
|
-
2. **古い部分ダンプが存在:**
|
|
296
|
-
- `[D]` 古い部分を削除して最新をダウンロード(推奨)
|
|
297
|
-
- `[k]` 古い部分を保持、最新を別途ダウンロード
|
|
298
|
-
- `[u]` 古い部分をそのまま使用(内容が古い可能性あり)
|
|
253
|
+
各範囲は`StreamStore`に、バイト範囲とSHA-1を名前にした個別のファイルとして、最後まで解凍できた場合だけ保存します。取得できなかった範囲は保存せず、読もうとすると`StreamUnavailableError`を発生させます。
|
|
299
254
|
|
|
300
255
|
### 記事抽出フロー
|
|
301
256
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
```
|
|
257
|
+
1. ダンプの版を選び、そのインデックスをダウンロード
|
|
258
|
+
2. インデックスキャッシュ(SQLite)をインデックスファイルごとに一度だけ作成
|
|
259
|
+
3. 要求された題名をウィキのサイト情報で正規化して検索
|
|
260
|
+
4. 転送を1段階たどる
|
|
261
|
+
5. 記事を含む範囲を取得
|
|
262
|
+
6. すべてのエントリを読み、結果を`ExtractionRun`に記録
|
|
263
|
+
7. 出力ファイルとマニフェストをまとめて公開
|
|
310
264
|
|
|
311
265
|
## メモリ管理
|
|
312
266
|
|
|
@@ -397,6 +351,20 @@ docs/INDEXES.md の MCP ツール表は spec/docs_sync_spec.rb が実際のサ
|
|
|
397
351
|
- RuboCop設定は`.rubocop.yml`
|
|
398
352
|
- 全体でUTF-8エンコーディング
|
|
399
353
|
|
|
354
|
+
## 追跡するファイル
|
|
355
|
+
|
|
356
|
+
追跡するファイルはすべて `scripts/tracked_paths.allow` のどれかの行に合い、各行もどれかの
|
|
357
|
+
追跡ファイルに合う必要がある。gem は `git ls-files` から作られるので、コミットは公開の第一歩になる。
|
|
358
|
+
|
|
359
|
+
```bash
|
|
360
|
+
ruby scripts/check_tracked_paths.rb # index(次のコミットの中身)を検査
|
|
361
|
+
ruby scripts/check_tracked_paths.rb --tree HEAD # コミットを、その中の許可リストで検査
|
|
362
|
+
ln -s ../../scripts/pre-push .git/hooks/pre-push # push する各コミットを送信前に検査
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
CI も最初にこの検査を走らせる。新しいファイルを追跡するときは、その場所と種類を表す
|
|
366
|
+
最も狭い行を足し(Markdown は 1 本ずつ名前で書く)、許可リストも一緒にステージする。
|
|
367
|
+
|
|
400
368
|
## Docker
|
|
401
369
|
|
|
402
370
|
Dockerイメージのビルドとプッシュ:
|
data/Dockerfile
CHANGED