MediaWiki  1.32.5
populateContentModel.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
28 
34  protected $wikiId;
36  protected $wanCache;
37 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Populate the various content_* fields' );
41  $this->addOption( 'ns', 'Namespace to run in, or "all" for all namespaces', true, true );
42  $this->addOption( 'table', 'Table to run in', true, true );
43  $this->setBatchSize( 100 );
44  }
45 
46  public function execute() {
47  $dbw = $this->getDB( DB_MASTER );
48 
49  $this->wikiId = $dbw->getDomainID();
50  $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
51 
52  $ns = $this->getOption( 'ns' );
53  if ( !ctype_digit( $ns ) && $ns !== 'all' ) {
54  $this->fatalError( 'Invalid namespace' );
55  }
56  $ns = $ns === 'all' ? 'all' : (int)$ns;
57  $table = $this->getOption( 'table' );
58  switch ( $table ) {
59  case 'revision':
60  case 'archive':
61  $this->populateRevisionOrArchive( $dbw, $table, $ns );
62  break;
63  case 'page':
64  $this->populatePage( $dbw, $ns );
65  break;
66  default:
67  $this->fatalError( "Invalid table name: $table" );
68  }
69  }
70 
71  protected function clearCache( $page_id, $rev_id ) {
72  $contentModelKey = $this->wanCache->makeKey( 'page-content-model', $rev_id );
73  $revisionKey =
74  $this->wanCache->makeGlobalKey( 'revision', $this->wikiId, $page_id, $rev_id );
75 
76  // WikiPage content model cache
77  $this->wanCache->delete( $contentModelKey );
78 
79  // Revision object cache, which contains a content model
80  $this->wanCache->delete( $revisionKey );
81  }
82 
83  private function updatePageRows( IDatabase $dbw, $pageIds, $model ) {
84  $count = count( $pageIds );
85  $this->output( "Setting $count rows to $model..." );
86  $dbw->update(
87  'page',
88  [ 'page_content_model' => $model ],
89  [ 'page_id' => $pageIds ],
90  __METHOD__
91  );
93  $this->output( "done.\n" );
94  }
95 
96  protected function populatePage( IDatabase $dbw, $ns ) {
97  $toSave = [];
98  $lastId = 0;
99  $nsCondition = $ns === 'all' ? [] : [ 'page_namespace' => $ns ];
100  $batchSize = $this->getBatchSize();
101  do {
102  $rows = $dbw->select(
103  'page',
104  [ 'page_namespace', 'page_title', 'page_id' ],
105  [
106  'page_content_model' => null,
107  'page_id > ' . $dbw->addQuotes( $lastId ),
108  ] + $nsCondition,
109  __METHOD__,
110  [ 'LIMIT' => $batchSize, 'ORDER BY' => 'page_id ASC' ]
111  );
112  $this->output( "Fetched {$rows->numRows()} rows.\n" );
113  foreach ( $rows as $row ) {
114  $title = Title::newFromRow( $row );
116  $toSave[$model][] = $row->page_id;
117  if ( count( $toSave[$model] ) >= $batchSize ) {
118  $this->updatePageRows( $dbw, $toSave[$model], $model );
119  unset( $toSave[$model] );
120  }
121  $lastId = $row->page_id;
122  }
123  } while ( $rows->numRows() >= $batchSize );
124  foreach ( $toSave as $model => $pages ) {
125  $this->updatePageRows( $dbw, $pages, $model );
126  }
127  }
128 
129  private function updateRevisionOrArchiveRows( IDatabase $dbw, $ids, $model, $table ) {
130  $prefix = $table === 'archive' ? 'ar' : 'rev';
131  $model_column = "{$prefix}_content_model";
132  $format_column = "{$prefix}_content_format";
133  $key = "{$prefix}_id";
134 
135  $count = count( $ids );
136  $format = ContentHandler::getForModelID( $model )->getDefaultFormat();
137  $this->output( "Setting $count rows to $model / $format..." );
138  $dbw->update(
139  $table,
140  [ $model_column => $model, $format_column => $format ],
141  [ $key => $ids ],
142  __METHOD__
143  );
144 
145  $this->output( "done.\n" );
146  }
147 
148  protected function populateRevisionOrArchive( IDatabase $dbw, $table, $ns ) {
149  $prefix = $table === 'archive' ? 'ar' : 'rev';
150  $model_column = "{$prefix}_content_model";
151  $format_column = "{$prefix}_content_format";
152  $key = "{$prefix}_id";
153  if ( $table === 'archive' ) {
154  $selectTables = 'archive';
155  $fields = [ 'ar_namespace', 'ar_title' ];
156  $join_conds = [];
157  $where = $ns === 'all' ? [] : [ 'ar_namespace' => $ns ];
158  $page_id_column = 'ar_page_id';
159  $rev_id_column = 'ar_rev_id';
160  } else { // revision
161  $selectTables = [ 'revision', 'page' ];
162  $fields = [ 'page_title', 'page_namespace' ];
163  $join_conds = [ 'page' => [ 'INNER JOIN', 'rev_page=page_id' ] ];
164  $where = $ns === 'all' ? [] : [ 'page_namespace' => $ns ];
165  $page_id_column = 'rev_page';
166  $rev_id_column = 'rev_id';
167  }
168 
169  $toSave = [];
170  $idsToClear = [];
171  $lastId = 0;
172  $batchSize = $this->getBatchSize();
173  do {
174  $rows = $dbw->select(
175  $selectTables,
176  array_merge(
177  $fields,
178  [ $model_column, $format_column, $key, $page_id_column, $rev_id_column ]
179  ),
180  // @todo support populating format if model is already set
181  [
182  $model_column => null,
183  "$key > " . $dbw->addQuotes( $lastId ),
184  ] + $where,
185  __METHOD__,
186  [ 'LIMIT' => $batchSize, 'ORDER BY' => "$key ASC" ],
187  $join_conds
188  );
189  $this->output( "Fetched {$rows->numRows()} rows.\n" );
190  foreach ( $rows as $row ) {
191  if ( $table === 'archive' ) {
192  $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
193  } else {
194  $title = Title::newFromRow( $row );
195  }
196  $lastId = $row->{$key};
197  try {
199  } catch ( MWException $e ) {
200  $this->error( "Invalid content model for $title" );
201  continue;
202  }
203  $defaultModel = $handler->getModelID();
204  $defaultFormat = $handler->getDefaultFormat();
205  $dbModel = $row->{$model_column};
206  $dbFormat = $row->{$format_column};
207  $id = $row->{$key};
208  if ( $dbModel === null && $dbFormat === null ) {
209  // Set the defaults
210  $toSave[$defaultModel][] = $row->{$key};
211  $idsToClear[] = [
212  'page_id' => $row->{$page_id_column},
213  'rev_id' => $row->{$rev_id_column},
214  ];
215  } else { // $dbModel === null, $dbFormat set.
216  if ( $dbFormat === $defaultFormat ) {
217  $toSave[$defaultModel][] = $row->{$key};
218  $idsToClear[] = [
219  'page_id' => $row->{$page_id_column},
220  'rev_id' => $row->{$rev_id_column},
221  ];
222  } else { // non-default format, just update now
223  $this->output( "Updating model to match format for $table $id of $title... " );
224  $dbw->update(
225  $table,
226  [ $model_column => $defaultModel ],
227  [ $key => $id ],
228  __METHOD__
229  );
230  wfWaitForSlaves();
231  $this->clearCache( $row->{$page_id_column}, $row->{$rev_id_column} );
232  $this->output( "done.\n" );
233  continue;
234  }
235  }
236 
237  if ( count( $toSave[$defaultModel] ) >= $batchSize ) {
238  $this->updateRevisionOrArchiveRows( $dbw, $toSave[$defaultModel], $defaultModel, $table );
239  unset( $toSave[$defaultModel] );
240  }
241  }
242  } while ( $rows->numRows() >= $batchSize );
243  foreach ( $toSave as $model => $ids ) {
244  $this->updateRevisionOrArchiveRows( $dbw, $ids, $model, $table );
245  }
246 
247  foreach ( $idsToClear as $idPair ) {
248  $this->clearCache( $idPair['page_id'], $idPair['rev_id'] );
249  }
250  }
251 }
252 
254 require_once RUN_MAINTENANCE_IF_MAIN;
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:297
PopulateContentModel\updatePageRows
updatePageRows(IDatabase $dbw, $pageIds, $model)
Definition: populateContentModel.php:83
PopulateContentModel\updateRevisionOrArchiveRows
updateRevisionOrArchiveRows(IDatabase $dbw, $ids, $model, $table)
Definition: populateContentModel.php:129
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
PopulateContentModel\populatePage
populatePage(IDatabase $dbw, $ns)
Definition: populateContentModel.php:96
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:244
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2847
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
PopulateContentModel\__construct
__construct()
Default constructor.
Definition: populateContentModel.php:38
PopulateContentModel\populateRevisionOrArchive
populateRevisionOrArchive(IDatabase $dbw, $table, $ns)
Definition: populateContentModel.php:148
ContentHandler\getDefaultModelFor
static getDefaultModelFor(Title $title)
Returns the name of the default content model to be used for the page with the given title.
Definition: ContentHandler.php:182
MWException
MediaWiki exception.
Definition: MWException.php:26
$maintClass
$maintClass
Definition: populateContentModel.php:253
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:475
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:545
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateContentModel\clearCache
clearCache( $page_id, $rev_id)
Definition: populateContentModel.php:71
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
PopulateContentModel
Usage: populateContentModel.php –ns=1 –table=page.
Definition: populateContentModel.php:33
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:118
Wikimedia\Rdbms\IDatabase\update
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:813
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2683
Wikimedia\Rdbms\IDatabase\addQuotes
addQuotes( $s)
Adds quotes and backslashes.
PopulateContentModel\execute
execute()
Do the actual work.
Definition: populateContentModel.php:46
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1351
PopulateContentModel\$wanCache
WANObjectCache $wanCache
Definition: populateContentModel.php:36
Wikimedia\Rdbms\IDatabase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
PopulateContentModel\$wikiId
$wikiId
Definition: populateContentModel.php:34
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:442
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
captcha-old.output
output
Definition: captcha-old.py:240
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:355