Module: Wp2txt::Extractor

Defined in:
lib/wp2txt/extractor.rb

Overview

Article extraction utilities for WpApp

Constant Summary collapse

EXIT_SUCCESS =

Exit codes

0
EXIT_ERROR =
1
EXIT_PARTIAL =
2

Instance Method Summary collapse

Instance Method Details

#build_extraction_config(opts, format) ⇒ Object

Build config hash for article extraction



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/wp2txt/extractor.rb', line 248

def build_extraction_config(opts, format)
  config = {
    format: format,
    num_procs: 1,  # Single-threaded for article extraction
    file_size: opts[:file_size],
    bz2_gem: opts[:bz2_gem]
  }

  %i[title list heading table pre ref redirect multiline category category_only
     summary_only metadata_only marker extract_citations expand_templates].each do |opt|
    config[opt] = opts[opt]
  end

  # Section extraction options
  %i[sections section_output min_section_length skip_empty
     alias_file no_section_aliases show_matched_sections].each do |opt|
    config[opt] = opts[opt]
  end

  # Parse sections string into array if provided
  if config[:sections].is_a?(String)
    config[:sections] = config[:sections].split(",").map(&:strip).reject(&:empty?)
  end

  config[:markers] = parse_markers_option(opts[:markers])
  config
end

#download_partial_streams(manager, index, stream_offsets, force: false) ⇒ Object

Download only the streams containing the requested articles

Parameters:

  • manager (DumpManager)

    The dump manager

  • index (MultistreamIndex)

    The multistream index

  • stream_offsets (Array<Integer>)

    Byte offsets of streams to download

  • force (Boolean) (defaults to: false)

    Force re-download even if cached



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/wp2txt/extractor.rb', line 157

def download_partial_streams(manager, index, stream_offsets, force: false)
  # Calculate how many streams are needed
  all_offsets = index.stream_offsets
  max_offset_needed = stream_offsets.max

  # Find the index of the highest needed stream
  max_idx = all_offsets.index(max_offset_needed)
  if max_idx.nil? || max_idx >= all_offsets.size - 1
    # Need full file for last stream
    return manager.download_multistream(force: force)
  end

  # Request partial download (DumpManager handles caching logic)
  stream_count = max_idx + 1
  manager.download_multistream(max_streams: stream_count, force: force)
end

#extract_category_articles(opts) ⇒ Object

Extract articles from a Wikipedia category



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/wp2txt/extractor.rb', line 277

def extract_category_articles(opts)
  lang = opts[:lang]
  category = opts[:from_category]
  max_depth = opts[:depth]
  cache_dir = opts[:cache_dir]
  dry_run = opts[:dry_run]
  skip_confirm = opts[:yes]
  total_steps = 6
  start_time = Time.now

  # Mode banner
  print_mode_banner("Category Extraction", {
    "Language" => lang,
    "Category" => category,
    "Depth" => max_depth,
    "Output" => opts[:output_dir]
  })

  # Get config values
  app_config = Wp2txt::CLI.config

  # Create category fetcher
  fetcher = Wp2txt::CategoryFetcher.new(
    lang, category,
    max_depth: max_depth,
    cache_expiry_days: app_config.category_expiry_days
  )

  # Step 1: Fetch preview
  print_header("Scanning category", step: 1, total_steps: total_steps)
  spinner = create_spinner("Fetching category information...")
  spinner.auto_spin

  begin
    preview = fetcher.fetch_preview
    spinner.success(pastel.green("Done!"))
  rescue Net::OpenTimeout, Net::ReadTimeout, SocketError, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError, JSON::ParserError, IOError => e
    spinner.error(pastel.red("Failed!"))
    print_error("Error fetching category: #{e.message}")
    return EXIT_ERROR
  end

  # Display preview
  print_subheader("Category Preview")
  print_info("Category", preview[:category])
  print_info("Depth", preview[:depth].to_s)
  puts

  if preview[:subcategories] && !preview[:subcategories].empty?
    puts pastel.dim("Categories scanned:")
    preview[:subcategories].each do |subcat|
      print_list_item("#{subcat[:name]} (#{subcat[:article_count]} articles)")
    end
    puts
  end

  puts pastel.dim("Summary:")
  print_info("Subcategories", (preview[:total_subcategories] || 0).to_s, indent: 1)
  print_info("Total articles", pastel.bold(preview[:total_articles].to_s), indent: 1)
  puts

  # Check if there are any articles
  if preview[:total_articles].zero?
    print_warning("No articles found in this category.")
    return EXIT_SUCCESS  # Not an error, just empty category
  end

  # Warn about large extractions
  if preview[:total_articles] > 1000
    print_warning("Large category with #{preview[:total_articles]} articles.")
    puts pastel.yellow("  Extraction may take a long time and require significant disk space.")
    puts
  end

  # Show cache status before confirmation
  temp_manager = Wp2txt::DumpManager.new(
    lang,
    cache_dir: cache_dir,
    dump_expiry_days: app_config.dump_expiry_days
  )
  cache_stale = show_download_estimate(temp_manager, app_config)
  force_update = opts[:update_cache]

  # Dry run mode - exit here
  if dry_run
    print_info_message("Dry run mode - no articles will be extracted.")
    return EXIT_SUCCESS
  end

  # Confirmation prompt
  unless skip_confirm
    unless $stdin.tty?
      print_error("Interactive confirmation required.")
      puts pastel.red("  Use --yes to skip confirmation when running non-interactively.")
      return EXIT_ERROR
    end

    unless confirm?("Proceed with extraction?")
      puts "Extraction cancelled."
      return EXIT_SUCCESS  # User chose to cancel
    end
  end

  # Step 2: Fetch full article list
  print_header("Fetching articles from API", step: 2, total_steps: total_steps)
  spinner = create_spinner("Fetching article list...")
  spinner.auto_spin

  begin
    article_titles = fetcher.fetch_articles
    spinner.success(pastel.green("#{article_titles.size} articles"))
  rescue Net::OpenTimeout, Net::ReadTimeout, SocketError, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError, JSON::ParserError, IOError => e
    spinner.error(pastel.red("Failed!"))
    print_error("Error fetching articles: #{e.message}")
    return EXIT_ERROR
  end

  if article_titles.empty?
    print_warning("No articles to extract.")
    return EXIT_SUCCESS  # Not an error, just empty result
  end

  # Create dump manager
  manager = Wp2txt::DumpManager.new(
    lang,
    cache_dir: cache_dir,
    dump_expiry_days: app_config.dump_expiry_days
  )

  # Step 3: Download index
  print_header("Downloading index", step: 3, total_steps: total_steps)
  index_path = manager.download_index(force: force_update)

  # Step 4: Load index and locate articles
  print_header("Locating articles in dump", step: 4, total_steps: total_steps)

  # Use SQLite cache for fast repeated access
  index = Wp2txt::MultistreamIndex.new(index_path, cache_dir: cache_dir)

  if index.loaded_from_cache?
    puts "  Index loaded from cache (#{index.size} entries)"
  else
    puts "  Index parsed (#{index.size} entries)"
  end

  # Find articles in index
  found_articles = []
  not_found = []

  article_titles.each do |title|
    entry = index.find_by_title(title)
    if entry
      found_articles << entry
    else
      not_found << title
    end
  end

  print_list_item("Found in dump: #{found_articles.size}", status: :success)
  print_list_item("Not in dump: #{not_found.size}", status: not_found.any? ? :warning : :success) if not_found.any?

  if found_articles.empty?
    print_error("No articles found in dump. The dump may be out of date.")
    return EXIT_ERROR
  end

  # Step 5: Download multistream
  streams_needed = found_articles.map { |e| e[:offset] }.uniq.sort
  print_header("Downloading data (#{streams_needed.size} streams)", step: 5, total_steps: total_steps)
  multistream_path = download_partial_streams(manager, index, streams_needed, force: force_update)

  # Create multistream reader, reusing the existing index (avoids double parsing)
  reader = Wp2txt::MultistreamReader.new(multistream_path, index)

  # Build config
  format = opts[:format].to_s.downcase.to_sym
  config = build_extraction_config(opts, format)

  # Create output writer
  base_name = "#{lang}wiki_#{sanitize_filename(category)}"
  writer = OutputWriter.new(
    output_dir: opts[:output_dir],
    base_name: base_name,
    format: format,
    file_size_mb: opts[:file_size]
  )

  # Step 6: Extract and process articles
  print_header("Extracting articles", step: 6, total_steps: total_steps)
  total_count = found_articles.size
  bar = create_progress_bar("  Processing", total_count)

  extracted_count = 0
  extraction_start = Time.now

  # Use parallel extraction for large batches (>50 articles across multiple streams)
  streams_count = found_articles.map { |e| e[:offset] }.uniq.size
  use_parallel = total_count > 50 && streams_count > 1

  if use_parallel
    # Parallel extraction: process streams concurrently
    num_procs = [streams_count, 4].min # Cap at 4 processes
    pages = reader.each_article_parallel(found_articles, num_processes: num_procs).to_a

    pages.each do |page|
      article = Article.new(page[:text], page[:title], !config[:marker])
      result = format_article(article, config)
      writer.write(result)
      extracted_count += 1
      bar.advance
    end
  else
    # Sequential extraction for small batches
    found_articles.each do |entry|
      title = entry[:title]
      page = reader.extract_article(title)

      if page
        article = Article.new(page[:text], page[:title], !config[:marker])
        result = format_article(article, config)
        writer.write(result)
        extracted_count += 1
      end

      bar.advance
    end
  end

  bar.finish
  extraction_time = Time.now - extraction_start

  # Close output
  output_files = writer.close

  # Summary
  total_time = Time.now - start_time
  status = not_found.empty? ? :success : :warning

  print_summary("Extraction Complete", {
    "Articles extracted" => "#{extracted_count}/#{article_titles.size}",
    "Output files" => output_files.size.to_s,
    "Extraction time" => format_duration(extraction_time),
    "Total time" => format_duration(total_time)
  }, status: status)

  if not_found.any?
    puts unless quiet?
    if not_found.size <= 10
      print_warning("Not found in dump (#{not_found.size}):")
      not_found.each { |t| print_list_item(t, status: :warning) }
    else
      print_warning("#{not_found.size} articles not found (may be newer than dump)")
    end
  end

  puts unless quiet?
  puts pastel.dim("Output files:") unless quiet?
  output_files.each { |f| print_list_item(f, status: :success) }

  # Return appropriate exit code
  not_found.empty? ? EXIT_SUCCESS : EXIT_PARTIAL
end

#extract_specific_articles(opts) ⇒ Object

Extract specific articles by title



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/wp2txt/extractor.rb', line 15

def extract_specific_articles(opts)
  lang = opts[:lang]
  cache_dir = opts[:cache_dir]
  article_titles = Wp2txt::CLI.parse_article_list(opts[:articles])
  app_config = Wp2txt::CLI.config
  force_update = opts[:update_cache]
  total_steps = 4
  start_time = Time.now

  # Mode banner
  articles_display = article_titles.size > 3 ? "#{article_titles.first(3).join(', ')}... (#{article_titles.size} total)" : article_titles.join(", ")
  print_mode_banner("Article Extraction", {
    "Language" => lang,
    "Articles" => articles_display,
    "Output" => opts[:output_dir]
  })

  # Create dump manager
  manager = Wp2txt::DumpManager.new(
    lang,
    cache_dir: cache_dir,
    dump_expiry_days: app_config.dump_expiry_days
  )

  # Step 1: Download index
  print_header("Downloading index", step: 1, total_steps: total_steps)
  index_path = manager.download_index(force: force_update)

  # Step 2: Load index and find articles
  print_header("Locating articles", step: 2, total_steps: total_steps)

  # Use SQLite cache for fast repeated access
  # Early termination: stop parsing when all target articles are found (if not using cache)
  index = Wp2txt::MultistreamIndex.new(
    index_path,
    cache_dir: cache_dir,
    target_titles: article_titles
  )

  if index.loaded_from_cache?
    puts "  Index loaded from cache (#{index.size} entries)"
  elsif index.early_terminated?
    puts "  Early termination: found all #{article_titles.size} articles"
  end
  puts

  # Find requested articles
  found_articles = []
  not_found = []

  article_titles.each do |title|
    entry = index.find_by_title(title)
    if entry
      found_articles << entry
      print_list_item("#{title}", status: :success)
    else
      not_found << title
      print_list_item("#{title} (not found)", status: :error)
    end
  end

  if found_articles.empty?
    puts unless quiet?
    print_error("No articles found. Please check the titles.")
    return EXIT_ERROR
  end

  # Step 3: Download streams
  streams_needed = found_articles.map { |e| e[:offset] }.uniq.sort
  print_header("Downloading data (#{streams_needed.size} streams)", step: 3, total_steps: total_steps)
  multistream_path = download_partial_streams(manager, index, streams_needed, force: force_update)

  # Create multistream reader, reusing the existing index (avoids double parsing)
  reader = Wp2txt::MultistreamReader.new(multistream_path, index)

  # Build config for processing
  format = opts[:format].to_s.downcase.to_sym
  config = build_extraction_config(opts, format)

  # Create output writer
  base_name = "#{lang}wiki_articles"
  writer = OutputWriter.new(
    output_dir: opts[:output_dir],
    base_name: base_name,
    format: format,
    file_size_mb: opts[:file_size]
  )

  # Step 4: Extract articles
  print_header("Extracting articles", step: 4, total_steps: total_steps)
  extracted_count = 0
  extraction_failures = []

  found_articles.each do |entry|
    title = entry[:title]
    page = reader.extract_article(title)

    if page
      article = Article.new(page[:text], page[:title], !config[:marker])
      result = format_article(article, config)
      writer.write(result)
      extracted_count += 1
      print_list_item("#{title}", status: :success)
    else
      extraction_failures << title
      print_list_item("#{title} (extraction failed)", status: :warning)
    end
  end

  # Close output
  output_files = writer.close
  total_time = Time.now - start_time

  # Summary
  has_issues = not_found.any? || extraction_failures.any?
  status = has_issues ? :warning : :success

  print_summary("Extraction Complete", {
    "Extracted" => "#{extracted_count}/#{article_titles.size}",
    "Output files" => output_files.size.to_s,
    "Total time" => format_duration(total_time)
  }, status: status)

  if not_found.any?
    puts unless quiet?
    print_warning("Not found in index (#{not_found.size}):")
    not_found.each { |t| print_list_item(t, status: :error) }
  end

  puts unless quiet?
  puts pastel.dim("Output files:") unless quiet?
  output_files.each { |f| print_list_item(f, status: :success) }

  # Return appropriate exit code
  has_issues ? EXIT_PARTIAL : EXIT_SUCCESS
end

#sanitize_filename(name) ⇒ Object

Sanitize category name for use in filename



541
542
543
# File 'lib/wp2txt/extractor.rb', line 541

def sanitize_filename(name)
  name.gsub(%r{[/\\:*?"<>|]}, "_").gsub(/\s+/, "_").slice(0, 50)
end

#show_download_estimate(manager, app_config = nil) ⇒ Boolean

Show download estimate before confirmation

Parameters:

  • manager (DumpManager)

    The dump manager

  • app_config (Config) (defaults to: nil)

    Application configuration

Returns:

  • (Boolean)

    True if cache is stale and user should be warned



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/wp2txt/extractor.rb', line 178

def show_download_estimate(manager, app_config = nil)
  puts pastel.dim("Download status:")

  # Check index cache
  index_path = manager.cached_index_path
  index_cached = File.exist?(index_path)
  cache_is_stale = false

  if index_cached
    index_size = format_size(File.size(index_path))
    age_days = manager.cache_age_days
    mtime = manager.cache_mtime
    expiry_days = app_config&.dump_expiry_days || manager.dump_expiry_days

    # Format cache date
    cache_date_str = mtime ? mtime.strftime("%Y-%m-%d") : "unknown"

    # Check if stale
    cache_is_stale = age_days && age_days > expiry_days

    if cache_is_stale
      age_str = age_days >= 1 ? "#{age_days.round(0)} days ago" : "today"
      print_list_item("Index: #{pastel.yellow('cached')} (#{index_size}, #{cache_date_str} - #{age_str})", status: :warning)
      print_list_item("  Cache is older than #{expiry_days} days (recommended refresh)", status: :warning, indent: 2)
    else
      age_str = age_days && age_days >= 1 ? "#{age_days.round(0)} days ago" : "today"
      print_list_item("Index: #{pastel.green('cached')} (#{index_size}, #{cache_date_str} - #{age_str})", status: :success)
    end
  else
    print_list_item("Index: #{pastel.yellow('download required')}", status: :warning)
  end

  # Check multistream cache
  full_path = manager.cached_multistream_path
  full_cached = File.exist?(full_path)

  if full_cached
    dump_size = format_size(File.size(full_path))
    dump_date_str = File.mtime(full_path).strftime("%Y-%m-%d")
    dump_age = ((Time.now - File.mtime(full_path)) / 86400).round(0)
    dump_age_str = dump_age >= 1 ? "#{dump_age} days ago" : "today"

    if cache_is_stale
      print_list_item("Dump: #{pastel.yellow('cached')} (#{dump_size}, #{dump_date_str} - #{dump_age_str})", status: :warning)
    else
      print_list_item("Dump: #{pastel.green('cached')} (#{dump_size}, #{dump_date_str} - #{dump_age_str})", status: :success)
    end
  else
    # Check for partial downloads
    partial = manager.find_suitable_partial_cache(1)
    if partial
      partial_size = format_size(File.size(partial))
      partial_date = File.mtime(partial).strftime("%Y-%m-%d")
      print_list_item("Dump: #{pastel.cyan('partial cached')} (#{partial_size}, #{partial_date})", status: :success)
      print_list_item("  Additional download may be required depending on article locations", status: :pending)
    else
      print_list_item("Dump: #{pastel.yellow('download required')} (several GB)", status: :warning)
    end
  end

  if cache_is_stale
    puts
    print_warning("Cache is stale. Use --update-cache to force refresh.")
  end

  puts
  cache_is_stale
end