Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[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->addDescription( '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->fatalError( "revision table does not exist" );
49 } elseif ( !$db->tableExists( 'archive' ) ) {
50 $this->fatalError( "archive table does not exist" );
51 } elseif ( !$db->fieldExists( 'revision', 'rev_sha1', __METHOD__ ) ) {
52 $this->output( "rev_sha1 column does not exist\n\n", true );
53
54 return false;
55 }
56
57 $this->output( "Populating rev_sha1 column\n" );
58 $rc = $this->doSha1Updates( 'revision', 'rev_id', Revision::getQueryInfo(), 'rev' );
59
60 $this->output( "Populating ar_sha1 column\n" );
61 $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', Revision::getArchiveQueryInfo(), 'ar' );
62 $this->output( "Populating ar_sha1 column legacy rows\n" );
63 $ac += $this->doSha1LegacyUpdates();
64
65 $this->output( "rev_sha1 and ar_sha1 population complete "
66 . "[$rc revision rows, $ac archive rows].\n" );
67
68 return true;
69 }
70
71 /**
72 * @param string $table
73 * @param string $idCol
74 * @param array $queryInfo
75 * @param string $prefix
76 * @return int Rows changed
77 */
78 protected function doSha1Updates( $table, $idCol, $queryInfo, $prefix ) {
79 $db = $this->getDB( DB_MASTER );
80 $batchSize = $this->getBatchSize();
81 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
82 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
83 if ( !$start || !$end ) {
84 $this->output( "...$table table seems to be empty.\n" );
85
86 return 0;
87 }
88
89 $count = 0;
90 # Do remaining chunk
91 $end += $batchSize - 1;
92 $blockStart = $start;
93 $blockEnd = $start + $batchSize - 1;
94 while ( $blockEnd <= $end ) {
95 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
96 $cond = "$idCol BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
97 " AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
98 $res = $db->select(
99 $queryInfo['tables'], $queryInfo['fields'], $cond, __METHOD__, [], $queryInfo['joins']
100 );
101
102 $this->beginTransaction( $db, __METHOD__ );
103 foreach ( $res as $row ) {
104 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
105 $count++;
106 }
107 }
108 $this->commitTransaction( $db, __METHOD__ );
109
110 $blockStart += $batchSize;
111 $blockEnd += $batchSize;
112 wfWaitForSlaves();
113 }
114
115 return $count;
116 }
117
118 /**
119 * @return int
120 */
121 protected function doSha1LegacyUpdates() {
122 $count = 0;
123 $db = $this->getDB( DB_MASTER );
124 $arQuery = Revision::getArchiveQueryInfo();
125 $res = $db->select( $arQuery['tables'], $arQuery['fields'],
126 [ 'ar_rev_id IS NULL', 'ar_sha1' => '' ], __METHOD__, [], $arQuery['joins'] );
127
128 $updateSize = 0;
129 $this->beginTransaction( $db, __METHOD__ );
130 foreach ( $res as $row ) {
131 if ( $this->upgradeLegacyArchiveRow( $row ) ) {
132 ++$count;
133 }
134 if ( ++$updateSize >= 100 ) {
135 $updateSize = 0;
136 $this->commitTransaction( $db, __METHOD__ );
137 $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
138 wfWaitForSlaves();
139 $this->beginTransaction( $db, __METHOD__ );
140 }
141 }
142 $this->commitTransaction( $db, __METHOD__ );
143
144 return $count;
145 }
146
147 /**
148 * @param stdClass $row
149 * @param string $table
150 * @param string $idCol
151 * @param string $prefix
152 * @return bool
153 */
154 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
155 $db = $this->getDB( DB_MASTER );
156 try {
157 $rev = ( $table === 'archive' )
158 ? Revision::newFromArchiveRow( $row )
159 : new Revision( $row );
160 $text = $rev->getSerializedData();
161 } catch ( Exception $e ) {
162 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
163
164 return false; // T24624?
165 }
166 if ( !is_string( $text ) ) {
167 # This should not happen, but sometimes does (T22757)
168 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
169
170 return false;
171 } else {
172 $db->update( $table,
173 [ "{$prefix}_sha1" => Revision::base36Sha1( $text ) ],
174 [ $idCol => $row->$idCol ],
175 __METHOD__
176 );
177
178 return true;
179 }
180 }
181
182 /**
183 * @param stdClass $row
184 * @return bool
185 */
186 protected function upgradeLegacyArchiveRow( $row ) {
187 $db = $this->getDB( DB_MASTER );
188 try {
189 $rev = Revision::newFromArchiveRow( $row );
190 } catch ( Exception $e ) {
191 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
192
193 return false; // T24624?
194 }
195 $text = $rev->getSerializedData();
196 if ( !is_string( $text ) ) {
197 # This should not happen, but sometimes does (T22757)
198 $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
199
200 return false;
201 } else {
202 # Archive table as no PK, but (NS,title,time) should be near unique.
203 # Any duplicates on those should also have duplicated text anyway.
204 $db->update( 'archive',
205 [ 'ar_sha1' => Revision::base36Sha1( $text ) ],
206 [
207 'ar_namespace' => $row->ar_namespace,
208 'ar_title' => $row->ar_title,
209 'ar_timestamp' => $row->ar_timestamp,
210 'ar_len' => $row->ar_len // extra sanity
211 ],
212 __METHOD__
213 );
214
215 return true;
216 }
217 }
218 }
219
220 $maintClass = PopulateRevisionSha1::class;
221 require_once RUN_MAINTENANCE_IF_MAIN;