r52519 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52518‎ | r52519 | r52520 >
Date:16:43, 28 June 2009
Author:demon
Status:deferred
Tags:
Comment:
* Port generateSitemap, rebuildLocalisationCache
* (bug 18566) Maintenance script to un/protect pages, based on patch by Charlie Nolan
Modified paths:
  • /branches/maintenance-work/maintenance/generateSitemap.php (modified) (history)
  • /branches/maintenance-work/maintenance/protect.php (added) (history)
  • /branches/maintenance-work/maintenance/rebuildLocalisationCache.php (modified) (history)

Diff [purge]

Index: branches/maintenance-work/maintenance/protect.php
@@ -0,0 +1,54 @@
 2+<?php
 3+/**
 4+ * @file
 5+ * @ingroup Maintenance
 6+ */
 7+
 8+require_once( "Maintenance.php" );
 9+
 10+class Protect extends Maintenance {
 11+ public function __construct() {
 12+ parent::__construct();
 13+ $this->mDescription = "Protect or unprotect an article from the command line.";
 14+ $this->addOption( 'unprotect', 'Removes protection' );
 15+ $this->addOption( 'semiprotect', 'Adds semi-protection' );
 16+ $this->addOption( 'u', 'Username to protect with', false, true );
 17+ $this->addOption( 'r', 'Reason for un/protection', false, true );
 18+ }
 19+
 20+ public function execute() {
 21+ global $wgUser, $wgTitle, $wgArticle;
 22+
 23+ $userName = $this->getOption( 'u', 'Maintenance script' );
 24+ $reason = $this->getOption( 'r', '' );
 25+
 26+ $protection = "sysop";
 27+ if ( $this->hasOption('semiprotect') ) {
 28+ $protection = "autoconfirmed";
 29+ } elseif ( $this->hasOption('unprotect') ) {
 30+ $protection = "";
 31+ }
 32+
 33+ $wgUser = User::newFromName( $userName );
 34+ $restrictions = array( 'edit' => $protection, 'move' => $protection );
 35+
 36+ $wgTitle = Title::newFromText( $args[0] );
 37+ if ( !$wgTitle ) {
 38+ $this->error( "Invalid title\n", true );
 39+ }
 40+
 41+ $wgArticle = new Article( $wgTitle );
 42+
 43+ # un/protect the article
 44+ $this->output( "Updating protection status... " );
 45+ $success = $wgArticle->updateRestrictions($restrictions, $reason);
 46+ if ( $success ) {
 47+ $this->output( "done\n" );
 48+ } else {
 49+ $this->output( "failed\n" );
 50+ }
 51+ }
 52+}
 53+
 54+$maintClass = "Protect";
 55+require_once( DO_MAINTENANCE );
Property changes on: branches/maintenance-work/maintenance/protect.php
___________________________________________________________________
Added: svn:eol-style
156 + native
Index: branches/maintenance-work/maintenance/generateSitemap.php
@@ -16,7 +16,9 @@
1717 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1818 */
1919
20 -class GenerateSitemap {
 20+require_once( "Maintenance.php" );
 21+
 22+class GenerateSitemap extends Maintenance {
2123 /**
2224 * The maximum amount of urls in a sitemap file
2325 *
@@ -129,36 +131,34 @@
130132 var $file;
131133
132134 /**
133 - * A resource pointing to php://stderr
134 - *
135 - * @var resource
 135+ * Constructor
136136 */
137 - var $stderr;
 137+ public function __construct() {
 138+ parent::__construct();
 139+ $this->mDescription = "Creates a sitemap for the site";
 140+ $this->addOption( 'fspath', 'The file system path to save to, e.g. /tmp/sitemap' .
 141+ "\n\t\tdefaults to current directory", false, true );
 142+ $this->addOption( 'server', "The protocol and server name to use in URLs, e.g.\n" .
 143+ "\t\thttp://en.wikipedia.org. This is sometimes necessary because\n" .
 144+ "\t\tserver name detection may fail in command line scripts.", false, true );
 145+ $this->addOption( 'compress', 'Compress the sitemap files, can take value yes|no, default yes' );
 146+ }
138147
139148 /**
140 - * Constructor
141 - *
142 - * @param string $fspath The path to prepend to the filenames, used to
143 - * save them somewhere else than in the root directory
144 - * @param string $path The path to append to the domain name
145 - * @param bool $compress Whether to compress the sitemap files
 149+ * Execute
146150 */
147 - function GenerateSitemap( $fspath, $compress ) {
 151+ public function execute() {
148152 global $wgScriptPath;
149153
150154 $this->url_limit = 50000;
151155 $this->size_limit = pow( 2, 20 ) * 10;
152 - $this->fspath = self::init_path( $fspath );
153 -
154 - $this->compress = $compress;
155 -
156 - $this->stderr = fopen( 'php://stderr', 'wt' );
 156+ $this->fspath = self::init_path( $this->getOption( 'fspath', getcwd() ) );
 157+ $this->compress = $this->getOption( 'compress', 'yes' ) !== 'no';
157158 $this->dbr = wfGetDB( DB_SLAVE );
158159 $this->generateNamespaces();
159160 $this->timestamp = wfTimestamp( TS_ISO_8601, wfTimestampNow() );
160 -
161 -
162161 $this->findex = fopen( "{$this->fspath}sitemap-index-" . wfWikiID() . ".xml", 'wb' );
 162+ $this->main();
163163 }
164164
165165 /**
@@ -170,7 +170,7 @@
171171 }
172172 # Create directory if needed
173173 if( $fspath && !is_dir( $fspath ) ) {
174 - mkdir( $fspath, 0755 ) or die("Can not create directory $fspath.\n");
 174+ wfMkdirParents( $fspath ) or die("Can not create directory $fspath.\n");
175175 }
176176
177177 return realpath( $fspath ). DIRECTORY_SEPARATOR ;
@@ -180,8 +180,6 @@
181181 * Generate a one-dimensional array of existing namespaces
182182 */
183183 function generateNamespaces() {
184 - $fname = 'GenerateSitemap::generateNamespaces';
185 -
186184 // Only generate for specific namespaces if $wgSitemapNamespaces is an array.
187185 global $wgSitemapNamespaces;
188186 if( is_array( $wgSitemapNamespaces ) ) {
@@ -192,7 +190,7 @@
193191 $res = $this->dbr->select( 'page',
194192 array( 'page_namespace' ),
195193 array(),
196 - $fname,
 194+ __METHOD__,
197195 array(
198196 'GROUP BY' => 'page_namespace',
199197 'ORDER BY' => 'page_namespace',
@@ -236,8 +234,6 @@
237235 * @return resource
238236 */
239237 function getPageRes( $namespace ) {
240 - $fname = 'GenerateSitemap::getPageRes';
241 -
242238 return $this->dbr->select( 'page',
243239 array(
244240 'page_namespace',
@@ -245,7 +241,7 @@
246242 'page_touched',
247243 ),
248244 array( 'page_namespace' => $namespace ),
249 - $fname
 245+ __METHOD__
250246 );
251247 }
252248
@@ -267,7 +263,7 @@
268264 $i = $smcount = 0;
269265
270266 $fns = $wgContLang->getFormattedNsText( $namespace );
271 - $this->debug( "$namespace ($fns)" );
 267+ $this->output( "$namespace ($fns)" );
272268 while ( $row = $this->dbr->fetchObject( $res ) ) {
273269 if ( $i++ === 0 || $i === $this->url_limit + 1 || $length + $this->limit[1] + $this->limit[2] > $this->size_limit ) {
274270 if ( $this->file !== false ) {
@@ -278,7 +274,7 @@
279275 $this->file = $this->open( $this->fspath . $filename, 'wb' );
280276 $this->write( $this->file, $this->openFile() );
281277 fwrite( $this->findex, $this->indexEntry( $filename ) );
282 - $this->debug( "\t$this->fspath$filename" );
 278+ $this->output( "\t$this->fspath$filename" );
283279 $length = $this->limit[0];
284280 $i = 1;
285281 }
@@ -450,13 +446,6 @@
451447 }
452448
453449 /**
454 - * Write a string to stderr followed by a UNIX newline
455 - */
456 - function debug( $str ) {
457 - fwrite( $this->stderr, "$str\n" );
458 - }
459 -
460 - /**
461450 * Populate $this->limit
462451 */
463452 function generateLimit( $namespace ) {
@@ -470,31 +459,5 @@
471460 }
472461 }
473462
474 -if ( in_array( '--help', $argv ) ) {
475 - echo <<<EOT
476 -Usage: php generateSitemap.php [options]
477 - --help show this message
478 -
479 - --fspath=<path> The file system path to save to, e.g /tmp/sitemap
480 - Saves to current directory if not given.
481 -
482 - --server=<server> The protocol and server name to use in URLs, e.g.
483 - http://en.wikipedia.org. This is sometimes necessary because
484 - server name detection may fail in command line scripts.
485 -
486 - --compress=[yes|no] compress the sitemap files, default yes
487 -
488 -EOT;
489 - die( -1 );
490 -}
491 -
492 -$optionsWithArgs = array( 'fspath', 'server', 'compress' );
493 -require_once( dirname( __FILE__ ) . '/commandLine.inc' );
494 -
495 -if ( isset( $options['server'] ) ) {
496 - $wgServer = $options['server'];
497 -}
498 -
499 -$gs = new GenerateSitemap( @$options['fspath'], @$options['compress'] !== 'no' );
500 -$gs->main();
501 -
 463+$maintClass = "GenerateSitemap";
 464+require_once( DO_MAINTENANCE );
Index: branches/maintenance-work/maintenance/rebuildLocalisationCache.php
@@ -10,32 +10,45 @@
1111 * Use --force to rebuild all files, even the ones that are not out of date.
1212 */
1313
14 -require( dirname(__FILE__).'/commandLine.inc' );
15 -ini_set( 'memory_limit', '200M' );
 14+require_once( "Maintenance.php" );
1615
17 -$force = isset( $options['force'] );
 16+class RebuildLocalisationCache extends Maintenance {
 17+ public function __construct() {
 18+ parent::__construct();
 19+ $this->mDescription = "Rebuild the localisation cache";
 20+ $this->addOption( 'force', 'Rebuild all files, even ones not out of date' );
 21+ }
1822
19 -$conf = $wgLocalisationCacheConf;
20 -$conf['manualRecache'] = false; // Allow fallbacks to create CDB files
21 -if ( $force ) {
22 - $conf['forceRecache'] = true;
23 -}
24 -$lc = new LocalisationCache_BulkLoad( $conf );
 23+ public function execute() {
 24+ global $wgLocalisationCacheConf;
2525
26 -$codes = array_keys( Language::getLanguageNames( true ) );
27 -sort( $codes );
28 -$numRebuilt = 0;
29 -foreach ( $codes as $code ) {
30 - if ( $force || $lc->isExpired( $code ) ) {
31 - echo "Rebuilding $code...\n";
32 - $lc->recache( $code );
33 - $numRebuilt++;
 26+ ini_set( 'memory_limit', '200M' );
 27+
 28+ $force = $this->hasOption('force');
 29+
 30+ $conf = $wgLocalisationCacheConf;
 31+ $conf['manualRecache'] = false; // Allow fallbacks to create CDB files
 32+ if ( $force ) {
 33+ $conf['forceRecache'] = true;
 34+ }
 35+ $lc = new LocalisationCache_BulkLoad( $conf );
 36+
 37+ $codes = array_keys( Language::getLanguageNames( true ) );
 38+ sort( $codes );
 39+ $numRebuilt = 0;
 40+ foreach ( $codes as $code ) {
 41+ if ( $force || $lc->isExpired( $code ) ) {
 42+ $this->output( "Rebuilding $code...\n" );
 43+ $lc->recache( $code );
 44+ $numRebuilt++;
 45+ }
 46+ }
 47+ $this->output( "$numRebuilt languages rebuilt out of " . count( $codes ) . ".\n" );
 48+ if ( $numRebuilt == 0 ) {
 49+ $this->output( "Use --force to rebuild the caches which are still fresh.\n" );
 50+ }
3451 }
3552 }
36 -echo "$numRebuilt languages rebuilt out of " . count( $codes ) . ".\n";
37 -if ( $numRebuilt == 0 ) {
38 - echo "Use --force to rebuild the caches which are still fresh.\n";
39 -}
4053
41 -
42 -
 54+$maintClass = "RebuildLocalisationCache";
 55+require_once( DO_MAINTENANCE );

Follow-up revisions

RevisionCommit summaryAuthorDate
r54225Merge maintenance-work branch (now with less errors!):...demon19:35, 2 August 2009

Status & tagging log