MediaWiki REL1_35
populateRevisionLength.php
Go to the documentation of this file.
1<?php
26
27require_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', __METHOD__ ) ) {
50 $this->fatalError( "revision table does not exist" );
51 } elseif ( !$dbw->tableExists( 'archive', __METHOD__ ) ) {
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' ),
121 ], IDatabase::LIST_AND )
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;
184require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const 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.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:38
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29