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