From c331f11991b9b13e29ecfde2e17934a0828b5351 Mon Sep 17 00:00:00 2001 From: Siebrand Mazeland Date: Wed, 23 Apr 2014 20:08:06 +0200 Subject: [PATCH] Update formatting in maintenance/ (1/4) Change-Id: I0be8111b6ff1feb9f3a4096a96a9c6a6e40feb05 --- maintenance/7zip.inc | 2 + maintenance/Maintenance.php | 32 ++++++--- maintenance/archives/upgradeLogging.php | 4 +- maintenance/backup.inc | 91 ++++++++++++------------ maintenance/backupPrefetch.inc | 35 ++++++---- maintenance/backupTextPass.inc | 92 +++++++++++++------------ maintenance/benchmarks/Benchmarker.php | 21 +++--- 7 files changed, 155 insertions(+), 122 deletions(-) diff --git a/maintenance/7zip.inc b/maintenance/7zip.inc index 02156c7ca6..751a131168 100644 --- a/maintenance/7zip.inc +++ b/maintenance/7zip.inc @@ -36,6 +36,7 @@ class SevenZipStream { private function stripPath( $path ) { $prefix = 'mediawiki.compress.7z://'; + return substr( $path, strlen( $prefix ) ); } @@ -91,4 +92,5 @@ class SevenZipStream { return fseek( $this->stream, $offset, $whence ); } } + stream_wrapper_register( 'mediawiki.compress.7z', 'SevenZipStream' ); diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 48d8a15bb6..479030fae3 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -150,6 +150,7 @@ abstract class Maintenance { return false; // previous calls should all be "requires" } } + return true; } @@ -204,6 +205,7 @@ abstract class Maintenance { } else { // Set it so we don't have to provide the default again $this->mOptions[$name] = $default; + return $this->mOptions[$name]; } } @@ -303,6 +305,7 @@ abstract class Maintenance { } $input = fgets( $f, $len ); fclose( $f ); + return rtrim( $input ); } @@ -375,6 +378,7 @@ abstract class Maintenance { public function outputChanneled( $msg, $channel = null ) { if ( $msg === false ) { $this->cleanupChanneled(); + return; } @@ -426,8 +430,8 @@ abstract class Maintenance { . '"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 ); + "http://en.wikipedia.org. This is sometimes necessary because " . + "server name detection may fail in command line scripts.", false, true ); $this->addOption( 'profiler', 'Set to "text" or "trace" to show profiling output', false, true ); # Save generic options to display them separately in help @@ -472,6 +476,7 @@ abstract class Maintenance { if ( !is_null( $this->mDb ) ) { $child->setDB( $this->mDb ); } + return $child; } @@ -596,6 +601,7 @@ abstract class Maintenance { # it's run again and again if ( $this->mInputLoaded ) { $this->loadSpecialVars(); + return; } @@ -643,7 +649,7 @@ abstract class Maintenance { # Short options $argLength = strlen( $arg ); for ( $p = 1; $p < $argLength; $p++ ) { - $option = $arg { $p }; + $option = $arg[$p]; if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) { $option = $this->mShortParamsMap[$option]; } @@ -769,7 +775,7 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); @@ -784,7 +790,7 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); @@ -808,7 +814,7 @@ abstract class Maintenance { } $this->output( wordwrap( "$tab--$par: " . $info['desc'], $descWidth, - "\n$tab$tab" ) . "\n" + "\n$tab$tab" ) . "\n" ); } $this->output( "\n" ); @@ -946,10 +952,11 @@ abstract class Maintenance { if ( !is_readable( $settingsFile ) ) { $this->error( "A copy of your installation's LocalSettings.php\n" . - "must exist and be readable in the source directory.\n" . - "Use --conf to specify it.", true ); + "must exist and be readable in the source directory.\n" . + "Use --conf to specify it.", true ); } $wgCommandLineMode = true; + return $settingsFile; } @@ -1103,7 +1110,6 @@ abstract class Maintenance { $this->unlockSearchindex( $dbw ); $this->output( "\n" ); } - } /** @@ -1125,6 +1131,7 @@ abstract class Maintenance { $u->doUpdate(); $this->output( "\n" ); } + return $title; } @@ -1171,6 +1178,7 @@ abstract class Maintenance { return false; } $resp = trim( $st ); + return $resp; } } @@ -1206,6 +1214,7 @@ abstract class Maintenance { return false; } print $prompt; + return fgets( STDIN, 1024 ); } } @@ -1215,6 +1224,7 @@ abstract class Maintenance { */ class FakeMaintenance extends Maintenance { protected $mSelf = "FakeMaintenanceScript"; + public function execute() { return; } @@ -1239,6 +1249,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance { && $db->selectRow( 'updatelog', '1', array( 'ul_key' => $key ), __METHOD__ ) ) { $this->output( "..." . $this->updateSkippedMessage() . "\n" ); + return true; } @@ -1250,6 +1261,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance { return true; } else { $this->output( $this->updatelogFailedMessage() . "\n" ); + return false; } } @@ -1260,6 +1272,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance { */ protected function updateSkippedMessage() { $key = $this->getUpdateKey(); + return "Update '{$key}' already logged as completed."; } @@ -1269,6 +1282,7 @@ abstract class LoggedUpdateMaintenance extends Maintenance { */ protected function updatelogFailedMessage() { $key = $this->getUpdateKey(); + return "Unable to log update '{$key}' as completed."; } diff --git a/maintenance/archives/upgradeLogging.php b/maintenance/archives/upgradeLogging.php index 0749bbf64b..9ac204d2c0 100644 --- a/maintenance/archives/upgradeLogging.php +++ b/maintenance/archives/upgradeLogging.php @@ -52,6 +52,7 @@ class UpdateLogging { if ( $this->dbw->tableExists( 'logging_pre_1_10' ) ) { echo "This script has already been run to completion\n"; + return; } @@ -158,7 +159,7 @@ EOT; $srcRes = $this->dbw->select( $srcTable, '*', $conds, __METHOD__, array( 'LIMIT' => $batchSize, 'ORDER BY' => 'log_timestamp' ) ); - if ( ! $srcRes->numRows() ) { + if ( !$srcRes->numRows() ) { # All done break; } @@ -205,6 +206,7 @@ EOT; } } } + return $numRowsCopied; } } diff --git a/maintenance/backup.inc b/maintenance/backup.inc index 24f32c725a..222c538b8b 100644 --- a/maintenance/backup.inc +++ b/maintenance/backup.inc @@ -136,52 +136,52 @@ class BackupDumper { $matches = array(); if ( preg_match( '/^--(.+?)(?:=(.+?)(?::(.+?))?)?$/', $arg, $matches ) ) { wfSuppressWarnings(); - list( /* $full */ , $opt, $val, $param ) = $matches; + list( /* $full */, $opt, $val, $param ) = $matches; wfRestoreWarnings(); switch ( $opt ) { - case "plugin": - $this->loadPlugin( $val, $param ); - break; - case "output": - if ( !is_null( $sink ) ) { - $sinks[] = $sink; - } - if ( !isset( $this->outputTypes[$val] ) ) { - $this->fatalError( "Unrecognized output sink type '$val'" ); - } - $type = $this->outputTypes[$val]; - $sink = new $type( $param ); - break; - case "filter": - if ( is_null( $sink ) ) { - $sink = new DumpOutput(); - } - if ( !isset( $this->filterTypes[$val] ) ) { - $this->fatalError( "Unrecognized filter type '$val'" ); - } - $type = $this->filterTypes[$val]; - $filter = new $type( $sink, $param ); - - // references are lame in php... - unset( $sink ); - $sink = $filter; - - break; - case "report": - $this->reportingInterval = intval( $val ); - break; - case "server": - $this->server = $val; - break; - case "force-normal": - if ( !function_exists( 'utf8_normalize' ) ) { - $this->fatalError( "UTF-8 normalization extension not loaded. " . - "Install or remove --force-normal parameter to use slower code." ); - } - break; - default: - $this->processOption( $opt, $val, $param ); + case "plugin": + $this->loadPlugin( $val, $param ); + break; + case "output": + if ( !is_null( $sink ) ) { + $sinks[] = $sink; + } + if ( !isset( $this->outputTypes[$val] ) ) { + $this->fatalError( "Unrecognized output sink type '$val'" ); + } + $type = $this->outputTypes[$val]; + $sink = new $type( $param ); + break; + case "filter": + if ( is_null( $sink ) ) { + $sink = new DumpOutput(); + } + if ( !isset( $this->filterTypes[$val] ) ) { + $this->fatalError( "Unrecognized filter type '$val'" ); + } + $type = $this->filterTypes[$val]; + $filter = new $type( $sink, $param ); + + // references are lame in php... + unset( $sink ); + $sink = $filter; + + break; + case "report": + $this->reportingInterval = intval( $val ); + break; + case "server": + $this->server = $val; + break; + case "force-normal": + if ( !function_exists( 'utf8_normalize' ) ) { + $this->fatalError( "UTF-8 normalization extension not loaded. " . + "Install or remove --force-normal parameter to use slower code." ); + } + break; + default: + $this->processOption( $opt, $val, $param ); } } } @@ -229,8 +229,8 @@ class BackupDumper { } else { $exporter->allLogs(); } - # Page dumps: all or by page ID range } elseif ( is_null( $this->pages ) ) { + # Page dumps: all or by page ID range if ( $this->startId || $this->endId ) { $exporter->pagesByRange( $this->startId, $this->endId ); } elseif ( $this->revStartId || $this->revEndId ) { @@ -238,8 +238,8 @@ class BackupDumper { } else { $exporter->allPages(); } - # Dump of specific pages } else { + # Dump of specific pages $exporter->pagesByName( $this->pages ); } @@ -310,6 +310,7 @@ class BackupDumper { function backupServer() { global $wgDBserver; + return $this->server ? $this->server : $wgDBserver; diff --git a/maintenance/backupPrefetch.inc b/maintenance/backupPrefetch.inc index 869ba80715..7bfb7345d9 100644 --- a/maintenance/backupPrefetch.inc +++ b/maintenance/backupPrefetch.inc @@ -53,8 +53,7 @@ class BaseDump { $infile = array_shift( $this->infiles ); if ( defined( 'LIBXML_PARSEHUGE' ) ) { $this->reader->open( $infile, null, LIBXML_PARSEHUGE ); - } - else { + } else { $this->reader->open( $infile ); } } @@ -78,6 +77,7 @@ class BaseDump { if ( $this->lastPage > $page || $this->atEnd ) { $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 ) { @@ -87,10 +87,12 @@ class BaseDump { } 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]" ); + return null; } } @@ -140,6 +142,7 @@ class BaseDump { */ function nextText() { $this->skipTo( 'text' ); + return strval( $this->nodeContents() ); } @@ -154,16 +157,20 @@ class BaseDump { return false; } while ( $this->reader->read() ) { - if ( $this->reader->nodeType == XMLReader::ELEMENT && - $this->reader->name == $name ) { + if ( $this->reader->nodeType == XMLReader::ELEMENT + && $this->reader->name == $name + ) { return true; } - if ( $this->reader->nodeType == XMLReader::END_ELEMENT && - $this->reader->name == $parent ) { + if ( $this->reader->nodeType == XMLReader::END_ELEMENT + && $this->reader->name == $parent + ) { $this->debug( "BaseDump::skipTo found searching for <$name>" ); + return false; } } + return $this->close(); } @@ -185,15 +192,16 @@ class BaseDump { $buffer = ""; while ( $this->reader->read() ) { switch ( $this->reader->nodeType ) { - case XMLReader::TEXT: -// case XMLReader::WHITESPACE: - case XMLReader::SIGNIFICANT_WHITESPACE: - $buffer .= $this->reader->value; - break; - case XMLReader::END_ELEMENT: - return $buffer; + case XMLReader::TEXT: + //case XMLReader::WHITESPACE: + case XMLReader::SIGNIFICANT_WHITESPACE: + $buffer .= $this->reader->value; + break; + case XMLReader::END_ELEMENT: + return $buffer; } } + return $this->close(); } @@ -204,6 +212,7 @@ class BaseDump { function close() { $this->reader->close(); $this->atEnd = true; + return null; } } diff --git a/maintenance/backupTextPass.inc b/maintenance/backupTextPass.inc index fd31844f09..a794750f1e 100644 --- a/maintenance/backupTextPass.inc +++ b/maintenance/backupTextPass.inc @@ -70,7 +70,7 @@ class TextPassDumper extends BackupDumper { // 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) - protected $maxTimeAllowed = 0; // 0 = no limit + protected $maxTimeAllowed = 0; // 0 = no limit protected $timeExceeded = false; protected $firstPageWritten = false; protected $lastPageWritten = false; @@ -102,6 +102,7 @@ class TextPassDumper extends BackupDumper { if ( $this->forcedDb !== null ) { $this->db = $this->forcedDb; + return; } @@ -183,31 +184,31 @@ class TextPassDumper extends BackupDumper { $url = $this->processFileOpt( $val, $param ); switch ( $opt ) { - case 'prefetch': - require_once "$IP/maintenance/backupPrefetch.inc"; - $this->prefetch = new BaseDump( $url ); - break; - case 'stub': - $this->input = $url; - break; - case 'maxtime': - $this->maxTimeAllowed = intval( $val ) * 60; - break; - case 'checkpointfile': - $this->checkpointFiles[] = $val; - break; - case 'current': - $this->history = WikiExporter::CURRENT; - break; - case 'full': - $this->history = WikiExporter::FULL; - break; - case 'spawn': - $this->spawn = true; - if ( $val ) { - $this->php = $val; - } - break; + case 'prefetch': + require_once "$IP/maintenance/backupPrefetch.inc"; + $this->prefetch = new BaseDump( $url ); + break; + case 'stub': + $this->input = $url; + break; + case 'maxtime': + $this->maxTimeAllowed = intval( $val ) * 60; + break; + case 'checkpointfile': + $this->checkpointFiles[] = $val; + break; + case 'current': + $this->history = WikiExporter::CURRENT; + break; + case 'full': + $this->history = WikiExporter::FULL; + break; + case 'spawn': + $this->spawn = true; + if ( $val ) { + $this->php = $val; + } + break; } } @@ -233,6 +234,7 @@ class TextPassDumper extends BackupDumper { $newFileURIs[] = $newURI; } $val = implode( ';', $newFileURIs ); + return $val; } @@ -242,6 +244,7 @@ class TextPassDumper extends BackupDumper { function showReport() { if ( !$this->prefetch ) { parent::showReport(); + return; } @@ -278,7 +281,6 @@ class TextPassDumper extends BackupDumper { } $pageRatePart = $this->pageCountPart / $deltaPart; $revRatePart = $this->revCountPart / $deltaPart; - } else { $fetchRatePart = '-'; $pageRatePart = '-'; @@ -314,12 +316,13 @@ class TextPassDumper extends BackupDumper { } function finalOptionCheck() { - if ( ( $this->checkpointFiles && ! $this->maxTimeAllowed ) || - ( $this->maxTimeAllowed && !$this->checkpointFiles ) ) { + if ( ( $this->checkpointFiles && !$this->maxTimeAllowed ) + || ( $this->maxTimeAllowed && !$this->checkpointFiles ) + ) { throw new MWException( "Options checkpointfile and maxtime must be specified together.\n" ); } foreach ( $this->checkpointFiles as $checkpointFile ) { - $count = substr_count ( $checkpointFile, "%s" ); + $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, " @@ -374,7 +377,7 @@ class TextPassDumper extends BackupDumper { 'XML import parse failure', xml_get_current_line_number( $parser ), xml_get_current_column_number( $parser ), - $byte . ( is_null( $chunk ) ? null : ( '; "' . substr( $chunk, $byte -$offset, 16 ) . '"' ) ), + $byte . ( is_null( $chunk ) ? null : ( '; "' . substr( $chunk, $byte - $offset, 16 ) . '"' ) ), xml_error_string( xml_get_error_code( $parser ) ) )->escaped(); xml_parser_free( $parser ); @@ -393,7 +396,7 @@ class TextPassDumper extends BackupDumper { # there's no pageID 0 so we use that. the caller is responsible # for deciding what to do with a file containing only the # siteinfo information and the mw tags. - if ( ! $this->firstPageWritten ) { + if ( !$this->firstPageWritten ) { $firstPageID = str_pad( 0, 9, "0", STR_PAD_LEFT ); $lastPageID = str_pad( 0, 9, "0", STR_PAD_LEFT ); } else { @@ -494,7 +497,7 @@ class TextPassDumper extends BackupDumper { // Step 2: Checking for plausibility and return the text if it is // plausible $revID = intval( $this->thisRev ); - if ( ! isset( $this->db ) ) { + if ( !isset( $this->db ) ) { throw new MWException( "No database available" ); } @@ -513,9 +516,7 @@ class TextPassDumper extends BackupDumper { $revLength = $row->rev_len; } } - - } - else { + } else { $revLength = $this->db->selectField( 'revision', 'rev_len', array( 'rev_id' => $revID ) ); } @@ -523,12 +524,12 @@ class TextPassDumper extends BackupDumper { if ( $tryIsPrefetch ) { $this->prefetchCount++; } + return $text; } $text = false; throw new MWException( "Received text is unplausible for id " . $id ); - } catch ( Exception $e ) { $msg = "getting/checking text " . $id . " failed (" . $e->getMessage() . ")"; if ( $failures + 1 < $this->maxFailures ) { @@ -541,7 +542,7 @@ class TextPassDumper extends BackupDumper { $failures++; // A failure in a prefetch hit does not warrant resetting db connection etc. - if ( ! $tryIsPrefetch ) { + if ( !$tryIsPrefetch ) { // After backing off for some time, we try to reboot the whole process as // much as possible to not carry over failures from one part to the other // parts @@ -580,7 +581,7 @@ class TextPassDumper extends BackupDumper { */ private function getTextDb( $id ) { global $wgContLang; - if ( ! isset( $this->db ) ) { + if ( !isset( $this->db ) ) { throw new MWException( __METHOD__ . "No database available" ); } $row = $this->db->selectRow( 'text', @@ -593,6 +594,7 @@ class TextPassDumper extends BackupDumper { } $stripped = str_replace( "\r", "", $text ); $normalized = $wgContLang->normalize( $stripped ); + return $normalized; } @@ -604,6 +606,7 @@ class TextPassDumper extends BackupDumper { } $text = $this->getTextSpawnedOnce( $id ); wfRestoreWarnings(); + return $text; } @@ -618,8 +621,7 @@ class TextPassDumper extends BackupDumper { "$IP/../multiversion/MWScript.php", "fetchText.php", '--wiki', wfWikiID() ) ) ); - } - else { + } else { $cmd = implode( " ", array_map( 'wfEscapeShellArg', array( @@ -638,11 +640,12 @@ class TextPassDumper extends BackupDumper { if ( !$this->spawnProc ) { // shit $this->progress( "Subprocess spawn failed." ); + return false; } list( $this->spawnWrite, // -> stdin - $this->spawnRead, // <- stdout + $this->spawnRead, // <- stdout ) = $pipes; return true; @@ -720,12 +723,14 @@ class TextPassDumper extends BackupDumper { $gotbytes = strlen( $text ); if ( $gotbytes != $nbytes ) { $this->progress( "Expected $nbytes bytes from database subprocess, got $gotbytes " ); + return false; } // Do normalization in the dump thread... $stripped = str_replace( "\r", "", $text ); $normalized = $wgContLang->normalize( $stripped ); + return $normalized; } @@ -773,7 +778,7 @@ class TextPassDumper extends BackupDumper { $this->buffer = ""; $this->thisRev = ""; } elseif ( $name == 'page' ) { - if ( ! $this->firstPageWritten ) { + if ( !$this->firstPageWritten ) { $this->firstPageWritten = trim( $this->thisPage ); } $this->lastPageWritten = trim( $this->thisPage ); @@ -811,7 +816,6 @@ class TextPassDumper extends BackupDumper { $this->buffer = ""; $this->thisPage = ""; } - } elseif ( $name == 'mediawiki' ) { $this->egress->writeCloseStream( $this->buffer ); $this->buffer = ""; diff --git a/maintenance/benchmarks/Benchmarker.php b/maintenance/benchmarks/Benchmarker.php index dd558f3232..3f8a899043 100644 --- a/maintenance/benchmarks/Benchmarker.php +++ b/maintenance/benchmarks/Benchmarker.php @@ -46,38 +46,38 @@ abstract class Benchmarker extends Maintenance { $bench_number = 0; $count = $this->getOption( 'count', 100 ); - foreach( $benchs as $bench ) { + foreach ( $benchs as $bench ) { // handle empty args - if( !array_key_exists( 'args', $bench ) ) { + if ( !array_key_exists( 'args', $bench ) ) { $bench['args'] = array(); } $bench_number++; $start = microtime( true ); - for( $i = 0; $i < $count; $i++ ) { + for ( $i = 0; $i < $count; $i++ ) { call_user_func_array( $bench['function'], $bench['args'] ); } $delta = microtime( true ) - $start; // function passed as a callback - if( is_array( $bench['function'] ) ) { + if ( is_array( $bench['function'] ) ) { $ret = get_class( $bench['function'][0] ) . '->' . $bench['function'][1]; $bench['function'] = $ret; } $this->results[$bench_number] = array( - 'function' => $bench['function'], + 'function' => $bench['function'], 'arguments' => $bench['args'], - 'count' => $count, - 'delta' => $delta, - 'average' => $delta / $count, - ); + 'count' => $count, + 'delta' => $delta, + 'average' => $delta / $count, + ); } } public function getFormattedResults() { $ret = ''; - foreach( $this->results as $res ) { + foreach ( $this->results as $res ) { // show function with args $ret .= sprintf( "%s times: function %s(%s) :\n", $res['count'], @@ -89,6 +89,7 @@ abstract class Benchmarker extends Maintenance { $res['average'] * 1000 ); } + return $ret; } } -- 2.20.1