Documentations!
[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 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
27 public function __construct() {
28 parent::__construct();
29 $this->mDescription = "Populates the rev_sha1 and ar_sha1 fields";
30 $this->setBatchSize( 200 );
31 }
32
33 protected function getUpdateKey() {
34 return 'populate rev_sha1';
35 }
36
37 protected function doDBUpdates() {
38 $db = $this->getDB( DB_MASTER );
39 if ( !$db->tableExists( 'revision' ) ) {
40 $this->error( "revision table does not exist", true );
41 }
42 if ( !$db->tableExists( 'archive' ) ) {
43 $this->error( "archive table does not exist", true );
44 }
45
46 $this->output( "Populating rev_sha1 column\n" );
47 $rc = $this->doSha1Updates( $db, 'revision', 'rev_id', 'rev' );
48
49 $this->output( "Populating ar_sha1 column\n" );
50 $ac = $this->doSha1Updates( $db, 'archive', 'ar_rev_id', 'ar' );
51
52 $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
53 return true;
54 }
55
56 /**
57 * @param $db DatabaseBase
58 * @param $table string
59 * @param $idCol
60 * @param $prefix string
61 * @return Integer Rows changed
62 */
63 protected function doSha1Updates( $db, $table, $idCol, $prefix ) {
64 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
65 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
66 if ( !$start || !$end ) {
67 $this->output( "...$table table seems to be empty.\n" );
68 return true;
69 }
70
71 $count = 0;
72 # Do remaining chunk
73 $end += $this->mBatchSize - 1;
74 $blockStart = $start;
75 $blockEnd = $start + $this->mBatchSize - 1;
76 while ( $blockEnd <= $end ) {
77 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
78 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
79 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
80 $res = $db->select( $table, '*', $cond, __METHOD__ );
81
82 $db->begin();
83 foreach ( $res as $row ) {
84 if ( $table === 'archive' ) {
85 $rev = Revision::newFromArchiveRow( $row );
86 } else {
87 $rev = new Revision( $row );
88 }
89 $text = $rev->getRawText();
90 if ( !is_string( $text ) ) {
91 # This should not happen, but sometimes does (bug 20757)
92 $this->output( "Text of revision {$row->$idCol} unavailable!\n" );
93 } else {
94 $db->update( $table,
95 array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
96 array( $idCol => $row->$idCol ),
97 __METHOD__ );
98 $count++;
99 }
100 }
101 $db->commit();
102
103 $blockStart += $this->mBatchSize;
104 $blockEnd += $this->mBatchSize;
105 wfWaitForSlaves();
106 }
107 return $count;
108 }
109 }
110
111 $maintClass = "PopulateRevisionSha1";
112 require_once( RUN_MAINTENANCE_IF_MAIN );