1ca76a2e6SAndreas Gohr<?php 2ca76a2e6SAndreas Gohr 3ca76a2e6SAndreas Gohruse dokuwiki\plugin\totext\Extractor\ExtractorFactory; 4ca76a2e6SAndreas Gohruse splitbrain\phpcli\Options; 5ca76a2e6SAndreas Gohr 6ca76a2e6SAndreas Gohr/** 7ca76a2e6SAndreas Gohr * DokuWiki Plugin totext (CLI Component) 8ca76a2e6SAndreas Gohr * 9ca76a2e6SAndreas Gohr * Prints the plain text extracted from a document to STDOUT. 10ca76a2e6SAndreas Gohr * 11ca76a2e6SAndreas Gohr * @license GPL-2.0-only 12ca76a2e6SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 13ca76a2e6SAndreas Gohr */ 14ca76a2e6SAndreas Gohrclass cli_plugin_totext extends \dokuwiki\Extension\CLIPlugin 15ca76a2e6SAndreas Gohr{ 16ca76a2e6SAndreas Gohr /** @inheritDoc */ 17ca76a2e6SAndreas Gohr protected function setup(Options $options) 18ca76a2e6SAndreas Gohr { 19ca76a2e6SAndreas Gohr $options->setHelp( 20*0fbee189SAndreas Gohr 'Extract a document to STDOUT. By default prints the body text ' . 21*0fbee189SAndreas Gohr 'followed by the metadata as "Key: value" lines. ' . 22ca76a2e6SAndreas Gohr 'Supported formats: ' . implode(', ', ExtractorFactory::supportedExtensions()) 23ca76a2e6SAndreas Gohr ); 24*0fbee189SAndreas Gohr $options->registerOption( 25*0fbee189SAndreas Gohr 'text', 26*0fbee189SAndreas Gohr 'Print only the body text (omit metadata).', 27*0fbee189SAndreas Gohr 't' 28*0fbee189SAndreas Gohr ); 29*0fbee189SAndreas Gohr $options->registerOption( 30*0fbee189SAndreas Gohr 'meta', 31*0fbee189SAndreas Gohr 'Print only the metadata as "Key: value" lines (omit body text).', 32*0fbee189SAndreas Gohr 'm' 33*0fbee189SAndreas Gohr ); 34*0fbee189SAndreas Gohr $options->registerArgument('file', 'The file to extract from', true); 35ca76a2e6SAndreas Gohr } 36ca76a2e6SAndreas Gohr 37ca76a2e6SAndreas Gohr /** @inheritDoc */ 38ca76a2e6SAndreas Gohr protected function main(Options $options) 39ca76a2e6SAndreas Gohr { 40ca76a2e6SAndreas Gohr [$file] = $options->getArgs(); 41ca76a2e6SAndreas Gohr // Any ExtractionException bubbles up; the phpcli CLI base catches it, 42ca76a2e6SAndreas Gohr // prints the message to STDERR and exits non-zero. 43*0fbee189SAndreas Gohr $result = ExtractorFactory::extract($file); 44*0fbee189SAndreas Gohr 45*0fbee189SAndreas Gohr // --text and --meta are mutually exclusive "only this" switches; with 46*0fbee189SAndreas Gohr // neither (the default) both are shown, metadata after the body text. 47*0fbee189SAndreas Gohr $showText = !$options->getOpt('meta'); 48*0fbee189SAndreas Gohr $showMeta = !$options->getOpt('text'); 49*0fbee189SAndreas Gohr if (!$showText && !$showMeta) { 50*0fbee189SAndreas Gohr $showText = $showMeta = true; 51*0fbee189SAndreas Gohr } 52*0fbee189SAndreas Gohr 53*0fbee189SAndreas Gohr // A requested half may have failed while the other was salvaged. 54*0fbee189SAndreas Gohr $failures = []; 55*0fbee189SAndreas Gohr if ($showText && $result->textError !== null) { 56*0fbee189SAndreas Gohr $failures['text'] = $result->textError; 57*0fbee189SAndreas Gohr } 58*0fbee189SAndreas Gohr if ($showMeta && $result->metadataError !== null) { 59*0fbee189SAndreas Gohr $failures['metadata'] = $result->metadataError; 60*0fbee189SAndreas Gohr } 61*0fbee189SAndreas Gohr 62*0fbee189SAndreas Gohr $blocks = []; 63*0fbee189SAndreas Gohr if ($showText && $result->text !== '') { 64*0fbee189SAndreas Gohr $blocks[] = $result->text; 65*0fbee189SAndreas Gohr } 66*0fbee189SAndreas Gohr if ($showMeta && $result->metadata !== []) { 67*0fbee189SAndreas Gohr $lines = []; 68*0fbee189SAndreas Gohr foreach ($result->metadata as $key => $value) { 69*0fbee189SAndreas Gohr $lines[] = "$key: $value"; 70*0fbee189SAndreas Gohr } 71*0fbee189SAndreas Gohr $blocks[] = implode("\n", $lines); 72*0fbee189SAndreas Gohr } 73*0fbee189SAndreas Gohr 74*0fbee189SAndreas Gohr // Nothing usable came through for what was requested: a hard failure. 75*0fbee189SAndreas Gohr // Re-throwing lets the CLI's exception handler report it and exit 76*0fbee189SAndreas Gohr // non-zero (the same path a total failure already takes). 77*0fbee189SAndreas Gohr if ($blocks === [] && $failures !== []) { 78*0fbee189SAndreas Gohr throw reset($failures); 79*0fbee189SAndreas Gohr } 80*0fbee189SAndreas Gohr 81*0fbee189SAndreas Gohr // Partial success: warn on STDERR about the failed half but still emit 82*0fbee189SAndreas Gohr // the salvaged half on STDOUT. Body text first, then the metadata block, 83*0fbee189SAndreas Gohr // separated by a blank line. 84*0fbee189SAndreas Gohr foreach ($failures as $what => $error) { 85*0fbee189SAndreas Gohr $this->warning("$what extraction failed: " . $error->getMessage()); 86*0fbee189SAndreas Gohr } 87*0fbee189SAndreas Gohr echo implode("\n\n", $blocks) . "\n"; 88ca76a2e6SAndreas Gohr return 0; 89ca76a2e6SAndreas Gohr } 90ca76a2e6SAndreas Gohr} 91