Refactor maintenance script.

- Improvement: Refactored maintenance script, improving user interaction.
This commit is contained in:
Daniel Kraus
2016-12-14 21:32:49 +01:00
parent 979b423e0a
commit e8e49eeab2

View File

@ -1,6 +1,6 @@
<?php <?php
/* /*
* Copyright 2012-2016 Daniel Kraus <bovender@bovender.de> ('bovender') * Copyright 2012-2016 Daniel Kraus <bovender@bovender.de> @bovender
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -65,21 +65,21 @@ class Cli extends \Maintenance {
true, // requires argument true, // requires argument
"s" "s"
); );
$this->addOption( $this->addOption(
"page", "page",
"page to process", "page name to process",
false, // not required false, // not required
true, // requires argument true, // requires argument
"p" "p"
); );
$this->addOption( $this->addOption(
"log", "log",
"enables logging to console", "enables logging to console",
false, // not required false, // not required
false, // requires no argument false, // requires no argument
"l" "l"
); );
$this->addOption( $this->addOption(
"debug", "debug",
"enables debug logging to console", "enables debug logging to console",
false, // not required false, // not required
@ -92,46 +92,54 @@ class Cli extends \Maintenance {
/// if the `--start` option is given) and call LinkTitles::processPage() for /// if the `--start` option is given) and call LinkTitles::processPage() for
/// each page. /// each page.
public function execute() { public function execute() {
global $wgLinkTitlesNamespaces; if ($this->hasOption('log'))
{
Extension::$ltConsoleOutput = true;
}
if ($this->hasOption('debug'))
{
Extension::$ltConsoleOutputDebug = true;
}
if ( $this->hasOption('page') ) {
if ( !$this->hasOption( 'start' ) ) {
$this->singlePage();
}
else {
$this->error( 'FATAL: Must not use --start option with --page option.', 2 );
}
}
else {
$startIndex = intval( $this->getOption( 'start', 0 ) );
if ( $startIndex < 0 ) {
$this->error( 'FATAL: Start index must be 0 or greater.', 1 );
};
$this->allPages( $startIndex);
}
}
$index = intval($this->getOption('start', 0)); private function singlePage() {
if ( $index < 0 ) { $pageName = strval( $this->getOption( 'page' ) );
$this->error('FATAL: Start index must be 0 or greater.', 1); $this->output( "Processing single page: '$pageName'\n" );
}; $title = \Title::newFromText( $pageName );
$success = Extension::processPage( $title, \RequestContext::getMain() );
if ( $success ) {
$this->output( "Finished.\n" );
}
else {
$this->error( 'FATAL: There is no such page.', 3 );
}
return $success;
}
if ($this->hasOption('log')) private function allPages( $index = 0 ) {
{ global $wgLinkTitlesNamespaces;
Extension::$ltConsoleOutput = true;
}
if ($this->hasOption('debug'))
{
Extension::$ltConsoleOutputDebug = true;
}
$pagename = strval($this->getOption('page'));
if ($pagename != null)
{
$this->output( 'Processing single page: ' . $pagename);
$title = \Title::newFromText( $pagename );
$success = Extension::processPage( $title, \RequestContext::getMain() );
if ( $success ) {
$this->output( "\nFinished parsing.\n" );
}
else {
$this->output( "\nError: There is no such page.\n" );
}
return $success;
}
// get our Namespaces
$namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' );
// Connect to the database
$dbr = $this->getDB( DB_SLAVE );
// Retrieve page names from the database. // Retrieve page names from the database.
$dbr = $this->getDB( DB_SLAVE );
$namespacesClause = str_replace( '_', ' ','(' . implode( ', ', $wgLinkTitlesNamespaces ) . ')' );
$res = $dbr->select( $res = $dbr->select(
'page', 'page',
array('page_title', 'page_namespace'), array( 'page_title', 'page_namespace' ),
array( array(
'page_namespace IN ' . $namespacesClause, 'page_namespace IN ' . $namespacesClause,
), ),
@ -143,19 +151,16 @@ class Cli extends \Maintenance {
); );
$numPages = $res->numRows(); $numPages = $res->numRows();
$context = \RequestContext::getMain(); $context = \RequestContext::getMain();
$this->output("Processing ${numPages} pages, starting at index ${index}...\n"); $this->output( "Processing ${numPages} pages, starting at index ${index}...\n" );
// Iterate through the pages; break if a time limit is exceeded.
foreach ( $res as $row ) { foreach ( $res as $row ) {
$index += 1; $index += 1; // at this point, $index is only needed for reporting to user
$curTitle = \Title::makeTitleSafe( $row->page_namespace, $row->page_title); $title = \Title::makeTitleSafe( $row->page_namespace, $row->page_title );
$this->output( $this->output( sprintf( "\rPage #%d (%02.0f%%) ", $index, $index / $numPages * 100 ) );
sprintf("\rPage #%d (%02.0f%%) ", $index, $index / $numPages * 100) Extension::processPage( $title, $context );
);
Extension::processPage($curTitle, $context);
} }
$this->output("\rFinished parsing. \n"); $this->output( "\rFinished. \n" );
} }
} }