diff --git a/tools/README.md b/tools/README.md index 73d5e3e..1cf543c 100644 --- a/tools/README.md +++ b/tools/README.md @@ -1,43 +1,69 @@ # boz-mw command line tools This is a directory for command line tools that uses the boz-mw framework. Actually there are not too much tools. ## Configuration Copy the `config-example.php` to `config.php` and fill it with your bot credentials. -## replace.php +## Available scripts + +### Replace script - `replace.php` This is a script to sobstitute text in the wikitext from an API query. e.g. to transform in *farfallese* the whole Italian Wikipedia: ```bash ./replace.php --wiki=itwiki --generator=allpages \ a afa \ e efe \ i ifi \ o ofo \ u ufu ``` e.g. to replace a simple template parameter in top of the page, e.g. from `{{Sito web|commerciale = Sì}}` to `{{Sito web|lucro = Sì}}`: ```bash ./replace.php \ --wiki=itwiki \ --generator=transcludedin \ --titles=Template:Sito_web \ --rvsection=0 \ --regex \ '/\|commerciale(.*=.*)(Sì|No|sì|no)(.*)/' \ '|lucro$1$2$3' ``` Other options: ```bash ./replace.php --help ``` You can see some [examples](./examples). + +### Mega export - `mega-export.php` + +This is a script that acts similar to the `[[Special:Export]]` page, but exporting the full page history. + +Note that you have to provide your user credentials in the `config.php` script in order to download more than `50` revisions at time. + +``` +Usage: + ./mega-export.php --wiki=WIKI [OPTIONS] Page_title > filename.xml +Allowed OPTIONS: + --wiki=VALUE Available wikis: itwiki, wikidatawiki, commonswiki, metawiki, landscapeforwiki + --limit=VALUE Number of revisions for each request + --file=VALUE Output filename + --help|-h Show this help and quit +``` + +E.g. to download the full history of the [Software libero](https://it.wikipedia.org/wiki/Software_libero) page: + +``` +./mega-export.php --wiki=itwiki "Alessandro Manzoni" > manzoni.xml +``` + +Note that actually the official MediaWiki/XML format is actually mistreated at least for the heading section: you will not obtain the namespace list, the wiki name, and other unuseful things. Just revisions. Much revisions. diff --git a/tools/mega-export.php b/tools/mega-export.php index 0bf6b29..3256f2e 100755 --- a/tools/mega-export.php +++ b/tools/mega-export.php @@ -1,148 +1,156 @@ #!/usr/bin/php . // exit if not CLI $argv or exit( 1 ); // load boz-mw require __DIR__ . '/../autoload.php'; // load configuration include 'config.php'; use \cli\Log; use \cli\Input; use \cli\Opts; use \cli\Param; use \cli\ParamFlag; use \cli\ParamFlagLong; use \cli\ParamValued; use \cli\ParamValuedLong; use \web\MediaWikis; use \mw\API\PageMatcher; // all the available wiki UIDs $mediawiki_uids = []; foreach( MediaWikis::all() as $site ) { $mediawiki_uids[] = $site::UID; } $mediawiki_uids = implode( ', ', $mediawiki_uids ); // register all CLI parameters $opts = new Opts( [ new ParamValuedLong( 'wiki', "Available wikis: $mediawiki_uids" ), new ParamValuedLong( 'limit', "Number of revisions for each request" ), new ParamValuedLong( 'file', "Output filename" ), new ParamFlag( 'help', 'h', "Show this help and quit" ), ] ); -$help = ''; +$messages = []; // choosen wiki $wiki_uid = $opts->getArg( 'wiki' ); if( !$wiki_uid ) { - $help .= "Please specify --wiki=WIKI"; + $messages[] = "Please specify --wiki=WIKI"; } // page titles $page_titles = Opts::unnamedArguments(); if( !$page_titles ) { - $help .= "Please specify some page titles"; + $messages[] = "Please specify some page titles"; } $limit = $opts->getArg( 'limit', 500 ); // show the help -if( $help ) { - echo "Usage:\n {$argv[ 0 ]} [OPTIONS] Page_title > filename.xml\n"; +$show_help = $opts->getArg( 'help' ); +if( $show_help ) { + $messages = []; +} else { + $show_help = $messages; +} + +if( $show_help ) { + echo "Usage:\n {$argv[ 0 ]} --wiki=WIKI [OPTIONS] Page_title > filename.xml\n"; echo "Allowed OPTIONS:\n"; foreach( $opts->getParams() as $param ) { $commands = []; if( $param->hasLongName() ) { $commands[] = '--' . $param->getLongName(); } if( $param->hasShortName() ) { $commands[] = '-' . $param->getShortName(); } $command = implode( '|', $commands ); if( $command && ! $param->isFlag() ) { $command .= $param->isValueOptional() ? '=[VALUE]' : '=VALUE'; } printf( ' % -20s ', $command ); if( $param->hasDescription() ) { echo ' ' . $param->getDescription(); } echo "\n"; } - if( is_string( $help ) ) { - echo "\nError: $help\n"; + foreach( $messages as $msg ) { + echo "\nError: $msg"; } + echo "\n"; exit( $opts->getArg( 'help' ) ? 0 : 1 ); } $wiki = MediaWikis::findFromUID( $wiki_uid ); $wiki->login(); $requests = $wiki->createQuery( [ 'action' => 'query', 'titles' => $page_titles, 'prop' => 'revisions', 'rvprop' => [ 'ids', 'flags', 'timestamp', 'user', 'userid', 'size', 'slotsize', 'sha1', 'comment', 'content', ], 'rvslots' => 'main', 'rvlimit' => $limit, ] ); ?> query->pages as $page ): ?> revisions as $revision ): ?> comment ) ) continue ?> slots as $slot ): ?> contentmodel ) ) continue ?> revid ?> parentid ?> timestamp ?> user ) ?> userid ) ?> comment ) ?> contentmodel ) ?> contentformat ) ?> {'*'} ) ?> sha1 ) ?>