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  $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;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:173
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
setBatchSize( $s=0)
Set the batch size.
const DB_MASTER
Definition: defines.php:26
const LIST_AND
Definition: Defines.php:39
addDescription( $text)
Set the description text.
doLenUpdates( $table, $idCol, $prefix, $queryInfo)
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:330
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object...
Definition: Revision.php:316
output( $out, $channel=null)
Throw some output to the user.
const LIST_OR
Definition: Defines.php:42
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getBatchSize()
Returns batch size.
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
const DB_REPLICA
Definition: defines.php:25
$content
Definition: router.php:78
upgradeRow( $row, $table, $idCol, $prefix)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.