MediaWiki  1.34.0
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  $this->output( "Populating rev_len column\n" );
60  $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
61 
62  $this->output( "Populating ar_len column\n" );
63  $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
64 
65  $this->output( "rev_len and ar_len population complete "
66  . "[$rev revision rows, $ar archive rows].\n" );
67 
68  return true;
69  }
70 
78  protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
79  $dbr = $this->getDB( DB_REPLICA );
80  $dbw = $this->getDB( DB_MASTER );
81  $batchSize = $this->getBatchSize();
82  $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
83  $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
84  if ( !$start || !$end ) {
85  $this->output( "...$table table seems to be empty.\n" );
86 
87  return 0;
88  }
89 
90  # Do remaining chunks
91  $blockStart = intval( $start );
92  $blockEnd = intval( $start ) + $batchSize - 1;
93  $count = 0;
94 
95  while ( $blockStart <= $end ) {
96  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
97  $res = $dbr->select(
98  $queryInfo['tables'],
99  $queryInfo['fields'],
100  [
101  "$idCol >= $blockStart",
102  "$idCol <= $blockEnd",
103  $dbr->makeList( [
104  "{$prefix}_len IS NULL",
105  $dbr->makeList( [
106  "{$prefix}_len = 0",
107  "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ), // sha1( "" )
109  ], IDatabase::LIST_OR )
110  ],
111  __METHOD__,
112  [],
113  $queryInfo['joins']
114  );
115 
116  if ( $res->numRows() > 0 ) {
117  $this->beginTransaction( $dbw, __METHOD__ );
118  # Go through and update rev_len from these rows.
119  foreach ( $res as $row ) {
120  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
121  $count++;
122  }
123  }
124  $this->commitTransaction( $dbw, __METHOD__ );
125  }
126 
127  $blockStart += $batchSize;
128  $blockEnd += $batchSize;
129  }
130 
131  return $count;
132  }
133 
141  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
142  $dbw = $this->getDB( DB_MASTER );
143 
144  $rev = ( $table === 'archive' )
146  : new Revision( $row );
147 
148  $content = $rev->getContent( RevisionRecord::RAW );
149  if ( !$content ) {
150  # This should not happen, but sometimes does (T22757)
151  $id = $row->$idCol;
152  $this->output( "Content of $table $id unavailable!\n" );
153 
154  return false;
155  }
156 
157  # Update the row...
158  $dbw->update( $table,
159  [ "{$prefix}_len" => $content->getSize() ],
160  [ $idCol => $row->$idCol ],
161  __METHOD__
162  );
163 
164  return true;
165  }
166 }
167 
168 $maintClass = PopulateRevisionLength::class;
169 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:172
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
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
PopulateRevisionLength\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionLength.php:141
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Revision\getArchiveQueryInfo
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:329
$maintClass
$maintClass
Definition: populateRevisionLength.php:168
$res
$res
Definition: testCompression.php:52
LIST_AND
const LIST_AND
Definition: Defines.php:39
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:1426
LIST_OR
const LIST_OR
Definition: Defines.php:42
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:315
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
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
$content
$content
Definition: router.php:78
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1441
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:1396
PopulateRevisionLength\doLenUpdates
doLenUpdates( $table, $idCol, $prefix, $queryInfo)
Definition: populateRevisionLength.php:78
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394