MediaWiki REL1_32
populateRevisionLength.php
Go to the documentation of this file.
1<?php
25
26require_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( "" )
107 ], IDatabase::LIST_AND )
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
167$maintClass = PopulateRevisionLength::class;
168require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
__construct()
Default constructor.
doDBUpdates()
Do the actual work.
upgradeRow( $row, $table, $idCol, $prefix)
getUpdateKey()
Get the update key name to go in the update log table.
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:535
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition Revision.php:167
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition Revision.php:521
const RAW
Definition Revision.php:57
$res
Definition database.txt:21
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:1818
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
require_once RUN_MAINTENANCE_IF_MAIN
$content
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26