Merge "Make users API cache mode public if blockinfo is not queried"
[lhc/web/wiklou.git] / maintenance / populateRevisionSha1.php
1 <?php
2 /**
3 * Fills the rev_sha1 and ar_sha1 columns of revision
4 * and archive tables for revisions created before MW 1.19.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision
29 * and archive tables for revisions created before MW 1.19.
30 *
31 * @ingroup Maintenance
32 */
33 class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Populates the rev_sha1 and ar_sha1 fields";
37 $this->setBatchSize( 200 );
38 }
39
40 protected function getUpdateKey() {
41 return 'populate rev_sha1';
42 }
43
44 protected function doDBUpdates() {
45 $db = $this->getDB( DB_MASTER );
46
47 if ( !$db->tableExists( 'revision' ) ) {
48 $this->error( "revision table does not exist", true );
49 } elseif ( !$db->tableExists( 'archive' ) ) {
50 $this->error( "archive table does not exist", true );
51 } elseif ( !$db->fieldExists( 'revision', 'rev_sha1', __METHOD__ ) ) {
52 $this->output( "rev_sha1 column does not exist\n\n", true );
53 return false;
54 }
55
56 $this->output( "Populating rev_sha1 column\n" );
57 $rc = $this->doSha1Updates( 'revision', 'rev_id', 'rev' );
58
59 $this->output( "Populating ar_sha1 column\n" );
60 $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', 'ar' );
61 $this->output( "Populating ar_sha1 column legacy rows\n" );
62 $ac += $this->doSha1LegacyUpdates();
63
64 $this->output( "rev_sha1 and ar_sha1 population complete "
65 . "[$rc revision rows, $ac archive rows].\n" );
66 return true;
67 }
68
69 /**
70 * @param string $table
71 * @param string $idCol
72 * @param string $prefix
73 * @return int Rows changed
74 */
75 protected function doSha1Updates( $table, $idCol, $prefix ) {
76 $db = $this->getDB( DB_MASTER );
77 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
78 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
79 if ( !$start || !$end ) {
80 $this->output( "...$table table seems to be empty.\n" );
81 return 0;
82 }
83
84 $count = 0;
85 # Do remaining chunk
86 $end += $this->mBatchSize - 1;
87 $blockStart = $start;
88 $blockEnd = $start + $this->mBatchSize - 1;
89 while ( $blockEnd <= $end ) {
90 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
91 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
92 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
93 $res = $db->select( $table, '*', $cond, __METHOD__ );
94
95 $db->begin( __METHOD__ );
96 foreach ( $res as $row ) {
97 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
98 $count++;
99 }
100 }
101 $db->commit( __METHOD__ );
102
103 $blockStart += $this->mBatchSize;
104 $blockEnd += $this->mBatchSize;
105 wfWaitForSlaves();
106 }
107 return $count;
108 }
109
110 /**
111 * @return int
112 */
113 protected function doSha1LegacyUpdates() {
114 $count = 0;
115 $db = $this->getDB( DB_MASTER );
116 $res = $db->select( 'archive', '*',
117 array( 'ar_rev_id IS NULL', 'ar_sha1' => '' ), __METHOD__ );
118
119 $updateSize = 0;
120 $db->begin( __METHOD__ );
121 foreach ( $res as $row ) {
122 if ( $this->upgradeLegacyArchiveRow( $row ) ) {
123 ++$count;
124 }
125 if ( ++$updateSize >= 100 ) {
126 $updateSize = 0;
127 $db->commit( __METHOD__ );
128 $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
129 wfWaitForSlaves();
130 $db->begin( __METHOD__ );
131 }
132 }
133 $db->commit( __METHOD__ );
134 return $count;
135 }
136
137 /**
138 * @param stdClass $row
139 * @param string $table
140 * @param string $idCol
141 * @param string $prefix
142 * @return bool
143 */
144 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
145 $db = $this->getDB( DB_MASTER );
146 try {
147 $rev = ( $table === 'archive' )
148 ? Revision::newFromArchiveRow( $row )
149 : new Revision( $row );
150 $text = $rev->getSerializedData();
151 } catch ( MWException $e ) {
152 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
153 return false; // bug 22624?
154 }
155 if ( !is_string( $text ) ) {
156 # This should not happen, but sometimes does (bug 20757)
157 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
158 return false;
159 } else {
160 $db->update( $table,
161 array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
162 array( $idCol => $row->$idCol ),
163 __METHOD__
164 );
165 return true;
166 }
167 }
168
169 /**
170 * @param stdClass $row
171 * @return bool
172 */
173 protected function upgradeLegacyArchiveRow( $row ) {
174 $db = $this->getDB( DB_MASTER );
175 try {
176 $rev = Revision::newFromArchiveRow( $row );
177 } catch ( MWException $e ) {
178 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
179 return false; // bug 22624?
180 }
181 $text = $rev->getSerializedData();
182 if ( !is_string( $text ) ) {
183 # This should not happen, but sometimes does (bug 20757)
184 $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
185 return false;
186 } else {
187 # Archive table as no PK, but (NS,title,time) should be near unique.
188 # Any duplicates on those should also have duplicated text anyway.
189 $db->update( 'archive',
190 array( 'ar_sha1' => Revision::base36Sha1( $text ) ),
191 array(
192 'ar_namespace' => $row->ar_namespace,
193 'ar_title' => $row->ar_title,
194 'ar_timestamp' => $row->ar_timestamp,
195 'ar_len' => $row->ar_len // extra sanity
196 ),
197 __METHOD__
198 );
199 return true;
200 }
201 }
202 }
203
204 $maintClass = "PopulateRevisionSha1";
205 require_once RUN_MAINTENANCE_IF_MAIN;