MediaWiki  REL1_33
populateRevisionLength.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Populates the rev_len and ar_len fields' );
39  $this->setBatchSize( 200 );
40  }
41 
42  protected function getUpdateKey() {
43  return 'populate rev_len and ar_len';
44  }
45 
46  public function doDBUpdates() {
47  $dbw = $this->getDB( DB_MASTER );
48  if ( !$dbw->tableExists( 'revision' ) ) {
49  $this->fatalError( "revision table does not exist" );
50  } elseif ( !$dbw->tableExists( 'archive' ) ) {
51  $this->fatalError( "archive table does not exist" );
52  } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
53  $this->output( "rev_len column does not exist\n\n", true );
54 
55  return false;
56  }
57 
58  $this->output( "Populating rev_len column\n" );
59  $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
60 
61  $this->output( "Populating ar_len column\n" );
62  $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
63 
64  $this->output( "rev_len and ar_len population complete "
65  . "[$rev revision rows, $ar archive rows].\n" );
66 
67  return true;
68  }
69 
77  protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
78  $dbr = $this->getDB( DB_REPLICA );
79  $dbw = $this->getDB( DB_MASTER );
80  $batchSize = $this->getBatchSize();
81  $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
82  $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
83  if ( !$start || !$end ) {
84  $this->output( "...$table table seems to be empty.\n" );
85 
86  return 0;
87  }
88 
89  # Do remaining chunks
90  $blockStart = intval( $start );
91  $blockEnd = intval( $start ) + $batchSize - 1;
92  $count = 0;
93 
94  while ( $blockStart <= $end ) {
95  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
96  $res = $dbr->select(
97  $queryInfo['tables'],
98  $queryInfo['fields'],
99  [
100  "$idCol >= $blockStart",
101  "$idCol <= $blockEnd",
102  $dbr->makeList( [
103  "{$prefix}_len IS NULL",
104  $dbr->makeList( [
105  "{$prefix}_len = 0",
106  "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ), // sha1( "" )
108  ], IDatabase::LIST_OR )
109  ],
110  __METHOD__,
111  [],
112  $queryInfo['joins']
113  );
114 
115  if ( $res->numRows() > 0 ) {
116  $this->beginTransaction( $dbw, __METHOD__ );
117  # Go through and update rev_len from these rows.
118  foreach ( $res as $row ) {
119  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
120  $count++;
121  }
122  }
123  $this->commitTransaction( $dbw, __METHOD__ );
124  }
125 
126  $blockStart += $batchSize;
127  $blockEnd += $batchSize;
128  }
129 
130  return $count;
131  }
132 
140  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
141  $dbw = $this->getDB( DB_MASTER );
142 
143  $rev = ( $table === 'archive' )
145  : new Revision( $row );
146 
147  $content = $rev->getContent( Revision::RAW );
148  if ( !$content ) {
149  # This should not happen, but sometimes does (T22757)
150  $id = $row->$idCol;
151  $this->output( "Content of $table $id unavailable!\n" );
152 
153  return false;
154  }
155 
156  # Update the row...
157  $dbw->update( $table,
158  [ "{$prefix}_len" => $content->getSize() ],
159  [ $idCol => $row->$idCol ],
160  __METHOD__
161  );
162 
163  return true;
164  }
165 }
166 
168 require_once RUN_MAINTENANCE_IF_MAIN;
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:171
PopulateRevisionLength
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
Definition: populateRevisionLength.php:35
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
PopulateRevisionLength\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRevisionLength.php:46
PopulateRevisionLength\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionLength.php:140
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Revision\getArchiveQueryInfo
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:525
$maintClass
$maintClass
Definition: populateRevisionLength.php:167
$res
$res
Definition: database.txt:21
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:37
LIST_AND
const LIST_AND
Definition: Defines.php:52
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:50
Revision
Definition: Revision.php:40
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1399
LIST_OR
const LIST_OR
Definition: Defines.php:55
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:511
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1700
PopulateRevisionLength\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRevisionLength.php:42
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1414
Revision\RAW
const RAW
Definition: Revision.php:56
PopulateRevisionLength\__construct
__construct()
Default constructor.
Definition: populateRevisionLength.php:36
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1779
PopulateRevisionLength\doLenUpdates
doLenUpdates( $table, $idCol, $prefix, $queryInfo)
Definition: populateRevisionLength.php:77
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
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:22
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
$content
$content
Definition: pageupdater.txt:72
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:56
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375