MediaWiki  master
populateRevisionLength.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Populates the rev_len and ar_len fields' );
40  $this->setBatchSize( 200 );
41  }
42 
43  protected function getUpdateKey() {
44  return 'populate rev_len and ar_len';
45  }
46 
47  public function doDBUpdates() {
48  $dbw = $this->getDB( DB_MASTER );
49  if ( !$dbw->tableExists( 'revision' ) ) {
50  $this->fatalError( "revision table does not exist" );
51  } elseif ( !$dbw->tableExists( 'archive' ) ) {
52  $this->fatalError( "archive table does not exist" );
53  } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
54  $this->output( "rev_len column does not exist\n\n", true );
55 
56  return false;
57  }
58 
59  $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
60 
61  $this->output( "Populating rev_len column\n" );
62  $rev = $this->doLenUpdates(
63  'revision',
64  'rev_id',
65  'rev',
66  $revisionStore->getQueryInfo()
67  );
68 
69  $this->output( "Populating ar_len column\n" );
70  $ar = $this->doLenUpdates(
71  'archive',
72  'ar_id',
73  'ar',
74  $revisionStore->getArchiveQueryInfo()
75  );
76 
77  $this->output( "rev_len and ar_len population complete "
78  . "[$rev revision rows, $ar archive rows].\n" );
79 
80  return true;
81  }
82 
90  protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
91  $dbr = $this->getDB( DB_REPLICA );
92  $dbw = $this->getDB( DB_MASTER );
93  $batchSize = $this->getBatchSize();
94  $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
95  $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
96  if ( !$start || !$end ) {
97  $this->output( "...$table table seems to be empty.\n" );
98 
99  return 0;
100  }
101 
102  # Do remaining chunks
103  $blockStart = intval( $start );
104  $blockEnd = intval( $start ) + $batchSize - 1;
105  $count = 0;
106 
107  while ( $blockStart <= $end ) {
108  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
109  $res = $dbr->select(
110  $queryInfo['tables'],
111  $queryInfo['fields'],
112  [
113  "$idCol >= $blockStart",
114  "$idCol <= $blockEnd",
115  $dbr->makeList( [
116  "{$prefix}_len IS NULL",
117  $dbr->makeList( [
118  "{$prefix}_len = 0",
119  // sha1( "" )
120  "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ),
122  ], IDatabase::LIST_OR )
123  ],
124  __METHOD__,
125  [],
126  $queryInfo['joins']
127  );
128 
129  if ( $res->numRows() > 0 ) {
130  $this->beginTransaction( $dbw, __METHOD__ );
131  # Go through and update rev_len from these rows.
132  foreach ( $res as $row ) {
133  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
134  $count++;
135  }
136  }
137  $this->commitTransaction( $dbw, __METHOD__ );
138  }
139 
140  $blockStart += $batchSize;
141  $blockEnd += $batchSize;
142  }
143 
144  return $count;
145  }
146 
154  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
155  $dbw = $this->getDB( DB_MASTER );
156 
157  $revFactory = MediaWikiServices::getInstance()->getRevisionFactory();
158  if ( $table === 'archive' ) {
159  $revRecord = $revFactory->newRevisionFromArchiveRow( $row );
160  } else {
161  $revRecord = $revFactory->newRevisionFromRow( $row );
162  }
163 
164  if ( !$revRecord ) {
165  # This should not happen, but sometimes does (T22757)
166  $id = $row->$idCol;
167  $this->output( "RevisionRecord of $table $id unavailable!\n" );
168 
169  return false;
170  }
171 
172  # Update the row...
173  $dbw->update( $table,
174  [ "{$prefix}_len" => $revRecord->getSize() ],
175  [ $idCol => $row->$idCol ],
176  __METHOD__
177  );
178 
179  return true;
180  }
181 }
182 
183 $maintClass = PopulateRevisionLength::class;
184 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
PopulateRevisionLength
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
Definition: populateRevisionLength.php:36
PopulateRevisionLength\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRevisionLength.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
PopulateRevisionLength\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionLength.php:154
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:480
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
$maintClass
$maintClass
Definition: populateRevisionLength.php:183
$res
$res
Definition: testCompression.php:57
LIST_AND
const LIST_AND
Definition: Defines.php:48
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:54
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1380
LIST_OR
const LIST_OR
Definition: Defines.php:51
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: LoggedUpdateMaintenance.php:26
PopulateRevisionLength\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRevisionLength.php:43
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:1395
PopulateRevisionLength\__construct
__construct()
Default constructor.
Definition: populateRevisionLength.php:37
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
PopulateRevisionLength\doLenUpdates
doLenUpdates( $table, $idCol, $prefix, $queryInfo)
Definition: populateRevisionLength.php:90
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:362
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:370