Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / maintenance / populateContentModel.php
index c158cb4..b41e0e0 100644 (file)
@@ -28,6 +28,10 @@ require_once __DIR__ . '/Maintenance.php';
  *  populateContentModel.php --ns=1 --table=page
  */
 class PopulateContentModel extends Maintenance {
+       protected $wikiId;
+
+       protected $wanCache;
+
        public function __construct() {
                parent::__construct();
                $this->addDescription( 'Populate the various content_* fields' );
@@ -38,6 +42,11 @@ class PopulateContentModel extends Maintenance {
 
        public function execute() {
                $dbw = $this->getDB( DB_MASTER );
+
+               $this->wikiId = $dbw->getWikiID();
+
+               $this->wanCache = ObjectCache::getMainWANInstance();
+
                $ns = $this->getOption( 'ns' );
                if ( !ctype_digit( $ns ) && $ns !== 'all' ) {
                        $this->error( 'Invalid namespace', 1 );
@@ -57,33 +66,45 @@ class PopulateContentModel extends Maintenance {
                }
        }
 
-       private function updatePageRows( DatabaseBase $dbw, $pageIds, $model ) {
+       protected function clearCache( $page_id, $rev_id ) {
+               $contentModelKey = $this->wanCache->makeKey( 'page', 'content-model', $rev_id );
+               $revisionKey =
+                       $this->wanCache->makeGlobalKey( 'revision', $this->wikiId, $page_id, $rev_id );
+
+               // WikiPage content model cache
+               $this->wanCache->delete( $contentModelKey );
+
+               // Revision object cache, which contains a content model
+               $this->wanCache->delete( $revisionKey );
+       }
+
+       private function updatePageRows( Database $dbw, $pageIds, $model ) {
                $count = count( $pageIds );
                $this->output( "Setting $count rows to $model..." );
                $dbw->update(
                        'page',
-                       array( 'page_content_model' => $model ),
-                       array( 'page_id' => $pageIds ),
+                       [ 'page_content_model' => $model ],
+                       [ 'page_id' => $pageIds ],
                        __METHOD__
                );
                wfWaitForSlaves();
                $this->output( "done.\n" );
        }
 
-       protected function populatePage( DatabaseBase $dbw, $ns ) {
-               $toSave = array();
+       protected function populatePage( Database $dbw, $ns ) {
+               $toSave = [];
                $lastId = 0;
-               $nsCondition = $ns === 'all' ? array() : array( 'page_namespace' => $ns );
+               $nsCondition = $ns === 'all' ? [] : [ 'page_namespace' => $ns ];
                do {
                        $rows = $dbw->select(
                                'page',
-                               array( 'page_namespace', 'page_title', 'page_id' ),
-                               array(
+                               [ 'page_namespace', 'page_title', 'page_id' ],
+                               [
                                        'page_content_model' => null,
                                        'page_id > ' . $dbw->addQuotes( $lastId ),
-                               ) + $nsCondition,
+                               ] + $nsCondition,
                                __METHOD__,
-                               array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'page_id ASC' )
+                               [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => 'page_id ASC' ]
                        );
                        $this->output( "Fetched {$rows->numRows()} rows.\n" );
                        foreach ( $rows as $row ) {
@@ -102,7 +123,7 @@ class PopulateContentModel extends Maintenance {
                }
        }
 
-       private function updateRevisionOrArchiveRows( DatabaseBase $dbw, $ids, $model, $table ) {
+       private function updateRevisionOrArchiveRows( Database $dbw, $ids, $model, $table ) {
                $prefix = $table === 'archive' ? 'ar' : 'rev';
                $model_column = "{$prefix}_content_model";
                $format_column = "{$prefix}_content_format";
@@ -113,43 +134,52 @@ class PopulateContentModel extends Maintenance {
                $this->output( "Setting $count rows to $model / $format..." );
                $dbw->update(
                        $table,
-                       array( $model_column => $model, $format_column => $format ),
-                       array( $key => $ids ),
+                       [ $model_column => $model, $format_column => $format ],
+                       [ $key => $ids ],
                        __METHOD__
                );
+
                $this->output( "done.\n" );
        }
 
-       protected function populateRevisionOrArchive( DatabaseBase $dbw, $table, $ns ) {
+       protected function populateRevisionOrArchive( Database $dbw, $table, $ns ) {
                $prefix = $table === 'archive' ? 'ar' : 'rev';
                $model_column = "{$prefix}_content_model";
                $format_column = "{$prefix}_content_format";
                $key = "{$prefix}_id";
                if ( $table === 'archive' ) {
                        $selectTables = 'archive';
-                       $fields = array( 'ar_namespace', 'ar_title' );
-                       $join_conds = array();
-                       $where = $ns === 'all' ? array() : array( 'ar_namespace' => $ns );
+                       $fields = [ 'ar_namespace', 'ar_title' ];
+                       $join_conds = [];
+                       $where = $ns === 'all' ? [] : [ 'ar_namespace' => $ns ];
+                       $page_id_column = 'ar_page_id';
+                       $rev_id_column = 'ar_rev_id';
                } else { // revision
-                       $selectTables = array( 'revision', 'page' );
-                       $fields = array( 'page_title', 'page_namespace' );
-                       $join_conds = array( 'page' => array( 'INNER JOIN', 'rev_page=page_id' ) );
-                       $where = $ns === 'all' ? array() : array( 'page_namespace' => $ns );
+                       $selectTables = [ 'revision', 'page' ];
+                       $fields = [ 'page_title', 'page_namespace' ];
+                       $join_conds = [ 'page' => [ 'INNER JOIN', 'rev_page=page_id' ] ];
+                       $where = $ns === 'all' ? [] : [ 'page_namespace' => $ns ];
+                       $page_id_column = 'rev_page';
+                       $rev_id_column = 'rev_id';
                }
 
-               $toSave = array();
+               $toSave = [];
+               $idsToClear = [];
                $lastId = 0;
                do {
                        $rows = $dbw->select(
                                $selectTables,
-                               array_merge( $fields, array( $model_column, $format_column, $key ) ),
+                               array_merge(
+                                       $fields,
+                                       [ $model_column, $format_column, $key, $page_id_column, $rev_id_column ]
+                               ),
                                // @todo support populating format if model is already set
-                               array(
+                               [
                                        $model_column => null,
                                        "$key > " . $dbw->addQuotes( $lastId ),
-                               ) + $where,
+                               ] + $where,
                                __METHOD__,
-                               array( 'LIMIT' => $this->mBatchSize, 'ORDER BY' => "$key ASC" ),
+                               [ 'LIMIT' => $this->mBatchSize, 'ORDER BY' => "$key ASC" ],
                                $join_conds
                        );
                        $this->output( "Fetched {$rows->numRows()} rows.\n" );
@@ -174,18 +204,27 @@ class PopulateContentModel extends Maintenance {
                                if ( $dbModel === null && $dbFormat === null ) {
                                        // Set the defaults
                                        $toSave[$defaultModel][] = $row->{$key};
+                                       $idsToClear[] = [
+                                               'page_id' => $row->{$page_id_column},
+                                               'rev_id' => $row->{$rev_id_column},
+                                       ];
                                } else { // $dbModel === null, $dbFormat set.
                                        if ( $dbFormat === $defaultFormat ) {
                                                $toSave[$defaultModel][] = $row->{$key};
+                                               $idsToClear[] = [
+                                                       'page_id' => $row->{$page_id_column},
+                                                       'rev_id' => $row->{$rev_id_column},
+                                               ];
                                        } else { // non-default format, just update now
                                                $this->output( "Updating model to match format for $table $id of $title... " );
                                                $dbw->update(
                                                        $table,
-                                                       array( $model_column => $defaultModel ),
-                                                       array( $key => $id ),
+                                                       [ $model_column => $defaultModel ],
+                                                       [ $key => $id ],
                                                        __METHOD__
                                                );
                                                wfWaitForSlaves();
+                                               $this->clearCache( $row->{$page_id_column}, $row->{$rev_id_column} );
                                                $this->output( "done.\n" );
                                                continue;
                                        }
@@ -200,6 +239,10 @@ class PopulateContentModel extends Maintenance {
                foreach ( $toSave as $model => $ids ) {
                        $this->updateRevisionOrArchiveRows( $dbw, $ids, $model, $table );
                }
+
+               foreach ( $idsToClear as $idPair ) {
+                       $this->clearCache( $idPair['page_id'], $idPair['rev_id'] );
+               }
        }
 }