From ae166425229f7188d9c7d55874142903dff7f949 Mon Sep 17 00:00:00 2001 From: Siebrand Mazeland Date: Wed, 23 Apr 2014 13:45:13 +0200 Subject: [PATCH] Pass phpcs-strict on maintenance/ (8/8) Woo! Change-Id: I6c591492db363dfab81e5ddfa4a03213347ff5cb --- maintenance/Maintenance.php | 27 ++++++++-- maintenance/attachLatest.php | 4 +- maintenance/backup.inc | 67 ++++++++++++++---------- maintenance/backupPrefetch.inc | 23 +++++---- maintenance/backupTextPass.inc | 93 ++++++++++++++++++++-------------- 5 files changed, 132 insertions(+), 82 deletions(-) diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 5c05b1efb8..48d8a15bb6 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -49,7 +49,6 @@ $maintClass = false; * @ingroup Maintenance */ abstract class Maintenance { - /** * Constants for DB access type * @see Maintenance::getDbType() @@ -169,8 +168,16 @@ abstract class Maintenance { * @param bool $withArg Is an argument required with this option? * @param string $shortName Character to use as short name */ - protected function addOption( $name, $description, $required = false, $withArg = false, $shortName = false ) { - $this->mParams[$name] = array( 'desc' => $description, 'require' => $required, 'withArg' => $withArg, 'shortName' => $shortName ); + protected function addOption( $name, $description, $required = false, + $withArg = false, $shortName = false + ) { + $this->mParams[$name] = array( + 'desc' => $description, + 'require' => $required, + 'withArg' => $withArg, + 'shortName' => $shortName + ); + if ( $shortName !== false ) { $this->mShortParamsMap[$shortName] = $name; } @@ -413,7 +420,11 @@ abstract class Maintenance { $this->addOption( 'conf', 'Location of LocalSettings.php, if not default', false, true ); $this->addOption( 'wiki', 'For specifying the wiki ID', false, true ); $this->addOption( 'globals', 'Output globals at the end of processing for debugging' ); - $this->addOption( 'memory-limit', 'Set a specific memory limit for the script, "max" for no limit or "default" to avoid changing it' ); + $this->addOption( + 'memory-limit', + 'Set a specific memory limit for the script, ' + . '"max" for no limit or "default" to avoid changing it' + ); $this->addOption( 'server', "The protocol and server name to use in URLs, e.g. " . "http://en.wikipedia.org. This is sometimes necessary because " . "server name detection may fail in command line scripts.", false, true ); @@ -630,7 +641,8 @@ abstract class Maintenance { } } elseif ( substr( $arg, 0, 1 ) == '-' ) { # Short options - for ( $p = 1; $p < strlen( $arg ); $p++ ) { + $argLength = strlen( $arg ); + for ( $p = 1; $p < $argLength; $p++ ) { $option = $arg { $p }; if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) { $option = $this->mShortParamsMap[$option]; @@ -871,7 +883,12 @@ abstract class Maintenance { $this->afterFinalSetup(); $wgShowSQLErrors = true; + + // @codingStandardsIgnoreStart Allow error supppression. wfSuppressWarnings() + // is not avaiable. @set_time_limit( 0 ); + // @codingStandardsIgnoreStart + $this->adjustMemoryLimit(); // Per-script profiling; useful for debugging diff --git a/maintenance/attachLatest.php b/maintenance/attachLatest.php index 3333408c92..fba6b92db2 100644 --- a/maintenance/attachLatest.php +++ b/maintenance/attachLatest.php @@ -33,7 +33,6 @@ require_once __DIR__ . '/Maintenance.php'; * @ingroup Maintenance */ class AttachLatest extends Maintenance { - public function __construct() { parent::__construct(); $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" ); @@ -70,7 +69,8 @@ class AttachLatest extends Maintenance { $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime ); if ( is_null( $revision ) ) { - $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, can't find revision id\n" ); + $this->output( wfWikiID() + . " $pageId [[$name]] latest time $latestTime, can't find revision id\n" ); continue; } $id = $revision->getId(); diff --git a/maintenance/backup.inc b/maintenance/backup.inc index 323a870e68..24f32c725a 100644 --- a/maintenance/backup.inc +++ b/maintenance/backup.inc @@ -37,28 +37,30 @@ class DumpDBZip2Output extends DumpPipeOutput { * @ingroup Dump Maintenance */ class BackupDumper { - var $reportingInterval = 100; - var $reporting = true; - var $pageCount = 0; - var $revCount = 0; - var $server = null; // use default - var $pages = null; // all pages - var $skipHeader = false; // don't output and - var $skipFooter = false; // don't output - var $startId = 0; - var $endId = 0; - var $revStartId = 0; - var $revEndId = 0; - var $sink = null; // Output filters - var $stubText = false; // include rev_text_id instead of text; for 2-pass dump - var $dumpUploads = false; - var $dumpUploadFileContents = false; - var $lastTime = 0; - var $pageCountLast = 0; - var $revCountLast = 0; - var $ID = 0; - - var $outputTypes = array(), $filterTypes = array(); + public $reporting = true; + public $pages = null; // all pages + public $skipHeader = false; // don't output and + public $skipFooter = false; // don't output + public $startId = 0; + public $endId = 0; + public $revStartId = 0; + public $revEndId = 0; + public $dumpUploads = false; + public $dumpUploadFileContents = false; + + protected $reportingInterval = 100; + protected $pageCount = 0; + protected $revCount = 0; + protected $server = null; // use default + protected $sink = null; // Output filters + protected $lastTime = 0; + protected $pageCountLast = 0; + protected $revCountLast = 0; + + protected $outputTypes = array(); + protected $filterTypes = array(); + + protected $ID = 0; /** * The dependency-injected database to use. @@ -69,11 +71,12 @@ class BackupDumper { */ protected $forcedDb = null; - /** - * @var LoadBalancer - */ + /** @var LoadBalancer */ protected $lb; + // @todo Unused? + private $stubText = false; // include rev_text_id instead of text; for 2-pass dump + function __construct( $args ) { $this->stderr = fopen( "php://stderr", "wt" ); @@ -132,7 +135,10 @@ class BackupDumper { foreach ( $args as $arg ) { $matches = array(); if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) { - @list( /* $full */ , $opt, $val, $param ) = $matches; + wfSuppressWarnings(); + list( /* $full */ , $opt, $val, $param ) = $matches; + wfRestoreWarnings(); + switch ( $opt ) { case "plugin": $this->loadPlugin( $val, $param ); @@ -351,8 +357,13 @@ class BackupDumper { $pageRatePart = '-'; $revRatePart = '-'; } - $this->progress( sprintf( "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), %d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]", - $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats, $this->maxCount ) ); + $this->progress( sprintf( + "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), " + . "%d revs (%0.1f|%0.1f/sec all|curr), ETA %s [max %d]", + $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, + $pageRatePart, $this->revCount, $revRate, $revRatePart, $etats, + $this->maxCount + ) ); $this->lastTime = $nowts; $this->revCountLast = $this->revCount; } diff --git a/maintenance/backupPrefetch.inc b/maintenance/backupPrefetch.inc index 6d41c18d83..869ba80715 100644 --- a/maintenance/backupPrefetch.inc +++ b/maintenance/backupPrefetch.inc @@ -40,14 +40,14 @@ * @ingroup Maintenance */ class BaseDump { - var $reader = null; - var $atEnd = false; - var $atPageEnd = false; - var $lastPage = 0; - var $lastRev = 0; - var $infiles = null; + protected $reader = null; + protected $atEnd = false; + protected $atPageEnd = false; + protected $lastPage = 0; + protected $lastRev = 0; + protected $infiles = null; - function BaseDump( $infile ) { + public function __construct( $infile ) { $this->infiles = explode( ';', $infile ); $this->reader = new XMLReader(); $infile = array_shift( $this->infiles ); @@ -76,18 +76,21 @@ class BaseDump { $this->nextPage(); } if ( $this->lastPage > $page || $this->atEnd ) { - $this->debug( "BaseDump::prefetch already past page $page looking for rev $rev [$this->lastPage, $this->lastRev]" ); + $this->debug( "BaseDump::prefetch already past page $page " + . "looking for rev $rev [$this->lastPage, $this->lastRev]" ); return null; } while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) { - $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, looking for $page, $rev" ); + $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, " + . "looking for $page, $rev" ); $this->nextRev(); } if ( $this->lastRev == $rev && !$this->atEnd ) { $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" ); return $this->nextText(); } else { - $this->debug( "BaseDump::prefetch already past rev $rev on page $page [$this->lastPage, $this->lastRev]" ); + $this->debug( "BaseDump::prefetch already past rev $rev on page $page " + . "[$this->lastPage, $this->lastRev]" ); return null; } } diff --git a/maintenance/backupTextPass.inc b/maintenance/backupTextPass.inc index d9c4d4f8d6..fd31844f09 100644 --- a/maintenance/backupTextPass.inc +++ b/maintenance/backupTextPass.inc @@ -30,52 +30,52 @@ require_once __DIR__ . '/backup.inc'; * @ingroup Maintenance */ class TextPassDumper extends BackupDumper { - var $prefetch = null; - var $input = "php://stdin"; - var $history = WikiExporter::FULL; - var $fetchCount = 0; - var $prefetchCount = 0; - var $prefetchCountLast = 0; - var $fetchCountLast = 0; + public $prefetch = null; + protected $input = "php://stdin"; + protected $history = WikiExporter::FULL; + protected $fetchCount = 0; + protected $prefetchCount = 0; + protected $prefetchCountLast = 0; + protected $fetchCountLast = 0; - var $maxFailures = 5; - var $maxConsecutiveFailedTextRetrievals = 200; - var $failureTimeout = 5; // Seconds to sleep after db failure + protected $maxFailures = 5; + protected $maxConsecutiveFailedTextRetrievals = 200; + protected $failureTimeout = 5; // Seconds to sleep after db failure - var $php = "php"; - var $spawn = false; + protected $php = "php"; + protected $spawn = false; /** * @var bool|resource */ - var $spawnProc = false; + protected $spawnProc = false; /** * @var bool|resource */ - var $spawnWrite = false; + protected $spawnWrite = false; /** * @var bool|resource */ - var $spawnRead = false; + protected $spawnRead = false; /** * @var bool|resource */ - var $spawnErr = false; + protected $spawnErr = false; - var $xmlwriterobj = false; + protected $xmlwriterobj = false; // when we spend more than maxTimeAllowed seconds on this run, we continue // processing until we write out the next complete page, then save output file(s), // rename it/them and open new one(s) - var $maxTimeAllowed = 0; // 0 = no limit - var $timeExceeded = false; - var $firstPageWritten = false; - var $lastPageWritten = false; - var $checkpointJustWritten = false; - var $checkpointFiles = array(); + protected $maxTimeAllowed = 0; // 0 = no limit + protected $timeExceeded = false; + protected $firstPageWritten = false; + protected $lastPageWritten = false; + protected $checkpointJustWritten = false; + protected $checkpointFiles = array(); /** * @var DatabaseBase @@ -119,14 +119,16 @@ class TextPassDumper extends BackupDumper { try { $this->lb = wfGetLBFactory()->newMainLB(); } catch ( Exception $e ) { - throw new MWException( __METHOD__ . " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")" ); + throw new MWException( __METHOD__ + . " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")" ); } // 2. The Connection, through the load balancer. try { $this->db = $this->lb->getConnection( DB_SLAVE, 'dump' ); } catch ( Exception $e ) { - throw new MWException( __METHOD__ . " rotating DB failed to obtain new database (" . $e->getMessage() . ")" ); + throw new MWException( __METHOD__ + . " rotating DB failed to obtain new database (" . $e->getMessage() . ")" ); } } @@ -282,8 +284,14 @@ class TextPassDumper extends BackupDumper { $pageRatePart = '-'; $revRatePart = '-'; } - $this->progress( sprintf( "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), %d revs (%0.1f|%0.1f/sec all|curr), %0.1f%%|%0.1f%% prefetched (all|curr), ETA %s [max %d]", - $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, $pageRatePart, $this->revCount, $revRate, $revRatePart, $fetchRate, $fetchRatePart, $etats, $this->maxCount ) ); + $this->progress( sprintf( + "%s: %s (ID %d) %d pages (%0.1f|%0.1f/sec all|curr), " + . "%d revs (%0.1f|%0.1f/sec all|curr), %0.1f%%|%0.1f%% " + . "prefetched (all|curr), ETA %s [max %d]", + $now, wfWikiID(), $this->ID, $this->pageCount, $pageRate, + $pageRatePart, $this->revCount, $revRate, $revRatePart, + $fetchRate, $fetchRatePart, $etats, $this->maxCount + ) ); $this->lastTime = $nowts; $this->revCountLast = $this->revCount; $this->prefetchCountLast = $this->prefetchCount; @@ -296,9 +304,12 @@ class TextPassDumper extends BackupDumper { } function checkIfTimeExceeded() { - if ( $this->maxTimeAllowed && ( $this->lastTime - $this->timeOfCheckpoint > $this->maxTimeAllowed ) ) { + if ( $this->maxTimeAllowed + && ( $this->lastTime - $this->timeOfCheckpoint > $this->maxTimeAllowed ) + ) { return true; } + return false; } @@ -310,14 +321,17 @@ class TextPassDumper extends BackupDumper { foreach ( $this->checkpointFiles as $checkpointFile ) { $count = substr_count ( $checkpointFile, "%s" ); if ( $count != 2 ) { - throw new MWException( "Option checkpointfile must contain two '%s' for substitution of first and last pageids, count is $count instead, file is $checkpointFile.\n" ); + throw new MWException( "Option checkpointfile must contain two '%s' " + . "for substitution of first and last pageids, count is $count instead, " + . "file is $checkpointFile.\n" ); } } if ( $this->checkpointFiles ) { $filenameList = (array)$this->egress->getFilenames(); if ( count( $filenameList ) != count( $this->checkpointFiles ) ) { - throw new MWException( "One checkpointfile must be specified for each output option, if maxtime is used.\n" ); + throw new MWException( "One checkpointfile must be specified " + . "for each output option, if maxtime is used.\n" ); } } } @@ -338,7 +352,11 @@ class TextPassDumper extends BackupDumper { $parser = xml_parser_create( "UTF-8" ); xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); - xml_set_element_handler( $parser, array( &$this, 'startElement' ), array( &$this, 'endElement' ) ); + xml_set_element_handler( + $parser, + array( &$this, 'startElement' ), + array( &$this, 'endElement' ) + ); xml_set_character_data_handler( $parser, array( &$this, 'characterData' ) ); $offset = 0; // for context extraction on error reporting @@ -378,12 +396,13 @@ class TextPassDumper extends BackupDumper { if ( ! $this->firstPageWritten ) { $firstPageID = str_pad( 0, 9, "0", STR_PAD_LEFT ); $lastPageID = str_pad( 0, 9, "0", STR_PAD_LEFT ); - } - else { + } else { $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT ); $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT ); } - for ( $i = 0; $i < count( $filenameList ); $i++ ) { + + $filenameCount = count( $filenameList ); + for ( $i = 0; $i < $filenameCount; $i++ ) { $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID ); $fileinfo = pathinfo( $filenameList[$i] ); $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn; @@ -775,7 +794,8 @@ class TextPassDumper extends BackupDumper { $newFilenames = array(); $firstPageID = str_pad( $this->firstPageWritten, 9, "0", STR_PAD_LEFT ); $lastPageID = str_pad( $this->lastPageWritten, 9, "0", STR_PAD_LEFT ); - for ( $i = 0; $i < count( $filenameList ); $i++ ) { + $filenamesCount = count( $filenameList ); + for ( $i = 0; $i < $filenamesCount; $i++ ) { $checkpointNameFilledIn = sprintf( $this->checkpointFiles[$i], $firstPageID, $lastPageID ); $fileinfo = pathinfo( $filenameList[$i] ); $newFilenames[] = $fileinfo['dirname'] . '/' . $checkpointNameFilledIn; @@ -786,8 +806,7 @@ class TextPassDumper extends BackupDumper { $this->timeOfCheckpoint = $this->lastTime; $this->firstPageWritten = false; $this->checkpointJustWritten = true; - } - else { + } else { $this->egress->writeClosePage( $this->buffer ); $this->buffer = ""; $this->thisPage = ""; -- 2.20.1