r94419 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94418‎ | r94419 | r94420 >
Date:16:14, 13 August 2011
Author:ialex
Status:ok
Tags:
Comment:
* Made rebuildTitleKeys.php script use Maintenance class
* Dropped pre-1.17 compatibility by requiring the $updater parameter of the LoadExtensionSchemaUpdates hook
* Removed usage of wfOut(); this was the last usage of that function in extensions
Modified paths:
  • /trunk/extensions/TitleKey/TitleKey.php (modified) (history)
  • /trunk/extensions/TitleKey/TitleKey_body.php (modified) (history)
  • /trunk/extensions/TitleKey/rebuildTitleKeys.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TitleKey/TitleKey.php
@@ -57,3 +57,4 @@
5858 $dir = dirname(__FILE__) . '/';
5959 $wgExtensionMessagesFiles['TitleKey'] = $dir . 'TitleKey.i18n.php';
6060 $wgAutoloadClasses['TitleKey'] = $dir . 'TitleKey_body.php';
 61+$wgAutoloadClasses['RebuildTitleKeys'] = $dir . 'rebuildTitleKeys.php';
Index: trunk/extensions/TitleKey/rebuildTitleKeys.php
@@ -1,18 +1,63 @@
22 <?php
33
4 -require_once dirname( dirname( dirname( __FILE__ ) ) ) . "/maintenance/commandLine.inc";
 4+$IP = getenv( 'MW_INSTALL_PATH' );
 5+if ( $IP === false )
 6+ $IP = dirname( __FILE__ ) . '/../..';
57
 8+require_once( "$IP/maintenance/Maintenance.php" );
 9+
610 // In case we want to do offline initialization...
711 if( !class_exists( 'TitleKey' ) ) {
812 require dirname( __FILE__ ) . '/TitleKey_body.php';
913 }
1014
11 -if( isset( $options['help'] ) ) {
12 - echo "Rebuilds titlekey table entries for all pages in DB.\n";
13 - echo "Usage:\n";
14 - echo " php extensions/TitleKey/rebuildTitleKeys.php [--start=<page_id>]\n";
15 -} else {
16 - $start = intval( @$options['start'] );
17 - echo "Rebuilding titlekey table...\n";
18 - TitleKey::populateKeys( $start );
19 -}
\ No newline at end of file
 15+class RebuildTitleKeys extends Maintenance {
 16+ function __construct() {
 17+ parent::__construct();
 18+ $this->mDescription = "Rebuilds titlekey table entries for all pages in DB.";
 19+ $this->addOption( 'start', 'Page ID to start from', false, true );
 20+ }
 21+
 22+ function execute() {
 23+ $start = $this->getOption( 'start', 0 );
 24+ $this->output( "Rebuilding titlekey table...\n" );
 25+ $dbr = $this->getDB( DB_SLAVE );
 26+
 27+ $maxId = $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
 28+ $chunkSize = 1000;
 29+
 30+ $lastId = 0;
 31+ for( ; $start <= $maxId; $start += $chunkSize ) {
 32+ if( $start != 0 ) {
 33+ $this->output( "... $start...\n" );
 34+ }
 35+ $result = $dbr->select( 'page',
 36+ array( 'page_id', 'page_namespace', 'page_title' ),
 37+ array( 'page_id > ' . intval( $start ) ),
 38+ __METHOD__,
 39+ array(
 40+ 'ORDER BY' => 'page_id',
 41+ 'LIMIT' => $chunkSize ) );
 42+
 43+ $titles = array();
 44+ foreach( $result as $row ) {
 45+ $titles[$row->page_id] =
 46+ Title::makeTitle( $row->page_namespace, $row->page_title );
 47+ $lastId = $row->page_id;
 48+ }
 49+ $result->free();
 50+
 51+ TitleKey::setBatchKeys( $titles );
 52+
 53+ wfWaitForSlaves( 20 );
 54+ }
 55+ if( $lastId ) {
 56+ $this->output( "... $lastId ok.\n" );
 57+ } else {
 58+ $this->output( "... no pages.\n" );
 59+ }
 60+ }
 61+}
 62+
 63+$maintClass = 'RebuildTitleKeys';
 64+require_once( DO_MAINTENANCE );
Index: trunk/extensions/TitleKey/TitleKey_body.php
@@ -108,82 +108,30 @@
109109 *
110110 * Status info is sent to stdout.
111111 */
112 - static function schemaUpdates( $updater = null ) {
113 - if ( $updater === null ) { // < 1.17
114 - self::runUpdates( wfGetDB( DB_MASTER ) );
115 - } else {
116 - $updater->addExtensionUpdate( array( array( __CLASS__, 'addDBtable' ) ) );
117 - }
 112+ public static function schemaUpdates( $updater = null ) {
 113+ $updater->addExtensionUpdate( array( array( __CLASS__, 'runUpdates' ) ) );
118114 return true;
119115 }
120116
121 - public static function addDBtable( $updater ) {
122 - self::runUpdates( $updater->getDB() );
123 - }
124 -
125 - private static function runUpdates( $db ) {
 117+ public static function runUpdates( $updater ) {
 118+ $db = $updater->getDB();
126119 if( $db->tableExists( 'titlekey' ) ) {
127 - wfOut( "...titlekey table already exists.\n" );
 120+ $updater->output( "...titlekey table already exists.\n" );
128121 } else {
129 - wfOut( "Creating titlekey table..." );
 122+ $updater->output( "Creating titlekey table..." );
130123 $sourcefile = $db->getType() == 'postgres' ? '/titlekey.pg.sql' : '/titlekey.sql';
131124 $err = $db->sourceFile( dirname( __FILE__ ) . $sourcefile );
132125 if( $err !== true ) {
133126 throw new MWException( $err );
134127 }
135128
136 - wfOut( "ok.\nPopulating titlekey table...\n" );
137 - self::populateKeys();
 129+ $updater->output( "ok.\n" );
 130+ $task = $updater->maintenance->runChild( 'RebuildTitleKeys' );
 131+ $task->execute();
138132 }
139133 }
140134
141135 /**
142 - * (Re)populate the titlekeys table with all page titles,
143 - * optionally starting from a given page id.
144 - *
145 - * Status info is sent to stdout.
146 - *
147 - * @param $start int page_id
148 - */
149 - static function populateKeys( $start=0 ) {
150 - $dbr = wfGetDB( DB_SLAVE );
151 -
152 - $maxId = $dbr->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
153 - $chunkSize = 1000;
154 -
155 - $lastId = 0;
156 - for( ; $start <= $maxId; $start += $chunkSize ) {
157 - if( $start != 0 ) {
158 - echo "... $start...\n";
159 - }
160 - $result = $dbr->select( 'page',
161 - array( 'page_id', 'page_namespace', 'page_title' ),
162 - array( 'page_id > ' . intval( $start ) ),
163 - __METHOD__,
164 - array(
165 - 'ORDER BY' => 'page_id',
166 - 'LIMIT' => $chunkSize ) );
167 -
168 - $titles = array();
169 - foreach( $result as $row ) {
170 - $titles[$row->page_id] =
171 - Title::makeTitle( $row->page_namespace, $row->page_title );
172 - $lastId = $row->page_id;
173 - }
174 - $result->free();
175 -
176 - self::setBatchKeys( $titles );
177 -
178 - wfWaitForSlaves( 20 );
179 - }
180 - if( $lastId ) {
181 - echo "... $lastId ok.\n";
182 - } else {
183 - echo "... no pages.\n";
184 - }
185 - }
186 -
187 - /**
188136 * Override the default OpenSearch backend...
189137 * @param string $search term
190138 * @param int $limit max number of items to return

Follow-up revisions

RevisionCommit summaryAuthorDate
r94671Minor tweaks to r94419: make batch size configurable, use RUN_MAINTENANCE_IF_...demon18:09, 16 August 2011

Status & tagging log