MediaWiki 1.41.2
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_PRIMARY );
48 if ( !$dbw->tableExists( 'revision', __METHOD__ ) ) {
49 $this->fatalError( "revision table does not exist" );
50 } elseif ( !$dbw->tableExists( 'archive', __METHOD__ ) ) {
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 $revisionStore = $this->getServiceContainer()->getRevisionStore();
59
60 $this->output( "Populating rev_len column\n" );
61 $rev = $this->doLenUpdates(
62 'revision',
63 'rev_id',
64 'rev',
65 $revisionStore->getQueryInfo()
66 );
67
68 $this->output( "Populating ar_len column\n" );
69 $ar = $this->doLenUpdates(
70 'archive',
71 'ar_id',
72 'ar',
73 $revisionStore->getArchiveQueryInfo()
74 );
75
76 $this->output( "rev_len and ar_len population complete "
77 . "[$rev revision rows, $ar archive rows].\n" );
78
79 return true;
80 }
81
89 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
90 $dbr = $this->getDB( DB_REPLICA );
91 $dbw = $this->getDB( DB_PRIMARY );
92 $batchSize = $this->getBatchSize();
93 $start = $dbw->newSelectQueryBuilder()
94 ->select( "MIN($idCol)" )
95 ->from( $table )
96 ->caller( __METHOD__ )->fetchField();
97 $end = $dbw->newSelectQueryBuilder()
98 ->select( "MAX($idCol)" )
99 ->from( $table )
100 ->caller( __METHOD__ )->fetchField();
101 if ( !$start || !$end ) {
102 $this->output( "...$table table seems to be empty.\n" );
103
104 return 0;
105 }
106
107 # Do remaining chunks
108 $blockStart = intval( $start );
109 $blockEnd = intval( $start ) + $batchSize - 1;
110 $count = 0;
111
112 while ( $blockStart <= $end ) {
113 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
114 $res = $dbr->select(
115 $queryInfo['tables'],
116 $queryInfo['fields'],
117 [
118 "$idCol >= $blockStart",
119 "$idCol <= $blockEnd",
120 $dbr->makeList( [
121 "{$prefix}_len" => null,
122 $dbr->makeList( [
123 "{$prefix}_len" => 0,
124 // sha1( "" )
125 "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ),
126 ], IDatabase::LIST_AND )
127 ], IDatabase::LIST_OR )
128 ],
129 __METHOD__,
130 [],
131 $queryInfo['joins']
132 );
133
134 if ( $res->numRows() > 0 ) {
135 $this->beginTransaction( $dbw, __METHOD__ );
136 # Go through and update rev_len from these rows.
137 foreach ( $res as $row ) {
138 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
139 $count++;
140 }
141 }
142 $this->commitTransaction( $dbw, __METHOD__ );
143 }
144
145 $blockStart += $batchSize;
146 $blockEnd += $batchSize;
147 }
148
149 return $count;
150 }
151
159 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
160 $dbw = $this->getDB( DB_PRIMARY );
161
162 $revFactory = $this->getServiceContainer()->getRevisionFactory();
163 if ( $table === 'archive' ) {
164 $revRecord = $revFactory->newRevisionFromArchiveRow( $row );
165 } else {
166 $revRecord = $revFactory->newRevisionFromRow( $row );
167 }
168
169 if ( !$revRecord ) {
170 # This should not happen, but sometimes does (T22757)
171 $id = $row->$idCol;
172 $this->output( "RevisionRecord of $table $id unavailable!\n" );
173
174 return false;
175 }
176
177 # Update the row...
178 $dbw->update( $table,
179 [ "{$prefix}_len" => $revRecord->getSize() ],
180 [ $idCol => $row->$idCol ],
181 __METHOD__
182 );
183
184 return true;
185 }
186}
187
188$maintClass = PopulateRevisionLength::class;
189require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
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)
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28