Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / populateRevisionSha1.php
blob89bfb85b4eac4a09e0dd82b0a07837b5a154e1f4
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.
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
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.
31 * @ingroup Maintenance
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 );
40 protected function getUpdateKey() {
41 return 'populate rev_sha1';
44 protected function doDBUpdates() {
45 $db = $this->getDB( DB_MASTER );
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;
56 $this->output( "Populating rev_sha1 column\n" );
57 $rc = $this->doSha1Updates( 'revision', 'rev_id', 'rev' );
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();
64 $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
65 return true;
68 /**
69 * @param $table string
70 * @param $idCol
71 * @param $prefix string
72 * @return Integer Rows changed
74 protected function doSha1Updates( $table, $idCol, $prefix ) {
75 $db = $this->getDB( DB_MASTER );
76 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
77 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
78 if ( !$start || !$end ) {
79 $this->output( "...$table table seems to be empty.\n" );
80 return 0;
83 $count = 0;
84 # Do remaining chunk
85 $end += $this->mBatchSize - 1;
86 $blockStart = $start;
87 $blockEnd = $start + $this->mBatchSize - 1;
88 while ( $blockEnd <= $end ) {
89 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
90 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
91 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
92 $res = $db->select( $table, '*', $cond, __METHOD__ );
94 $db->begin( __METHOD__ );
95 foreach ( $res as $row ) {
96 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
97 $count++;
100 $db->commit( __METHOD__ );
102 $blockStart += $this->mBatchSize;
103 $blockEnd += $this->mBatchSize;
104 wfWaitForSlaves();
106 return $count;
110 * @return int
112 protected function doSha1LegacyUpdates() {
113 $count = 0;
114 $db = $this->getDB( DB_MASTER );
115 $res = $db->select( 'archive', '*',
116 array( 'ar_rev_id IS NULL', 'ar_sha1' => '' ), __METHOD__ );
118 $updateSize = 0;
119 $db->begin( __METHOD__ );
120 foreach ( $res as $row ) {
121 if ( $this->upgradeLegacyArchiveRow( $row ) ) {
122 ++$count;
124 if ( ++$updateSize >= 100 ) {
125 $updateSize = 0;
126 $db->commit( __METHOD__ );
127 $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
128 wfWaitForSlaves();
129 $db->begin( __METHOD__ );
132 $db->commit( __METHOD__ );
133 return $count;
137 * @param $row
138 * @param $table
139 * @param $idCol
140 * @param $prefix
141 * @return bool
143 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
144 $db = $this->getDB( DB_MASTER );
145 try {
146 $rev = ( $table === 'archive' )
147 ? Revision::newFromArchiveRow( $row )
148 : new Revision( $row );
149 $text = $rev->getSerializedData();
150 } catch ( MWException $e ) {
151 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
152 return false; // bug 22624?
154 if ( !is_string( $text ) ) {
155 # This should not happen, but sometimes does (bug 20757)
156 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
157 return false;
158 } else {
159 $db->update( $table,
160 array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
161 array( $idCol => $row->$idCol ),
162 __METHOD__
164 return true;
169 * @param $row
170 * @return bool
172 protected function upgradeLegacyArchiveRow( $row ) {
173 $db = $this->getDB( DB_MASTER );
174 try {
175 $rev = Revision::newFromArchiveRow( $row );
176 } catch ( MWException $e ) {
177 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
178 return false; // bug 22624?
180 $text = $rev->getSerializedData();
181 if ( !is_string( $text ) ) {
182 # This should not happen, but sometimes does (bug 20757)
183 $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
184 return false;
185 } else {
186 # Archive table as no PK, but (NS,title,time) should be near unique.
187 # Any duplicates on those should also have duplicated text anyway.
188 $db->update( 'archive',
189 array( 'ar_sha1' => Revision::base36Sha1( $text ) ),
190 array(
191 'ar_namespace' => $row->ar_namespace,
192 'ar_title' => $row->ar_title,
193 'ar_timestamp' => $row->ar_timestamp,
194 'ar_len' => $row->ar_len // extra sanity
196 __METHOD__
198 return true;
203 $maintClass = "PopulateRevisionSha1";
204 require_once RUN_MAINTENANCE_IF_MAIN;