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