Update populateRevisionLength maintenance script to populate archive.ar_len
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
1 <?php
2 /**
3 * Populates the rev_len and ar_len fields for old revisions created
4 * before MW 1.10.
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 populates the rev_len and ar_len fields
29 * for old revisions created before MW 1.10.
30 *
31 * @ingroup Maintenance
32 */
33 class PopulateRevisionLength extends LoggedUpdateMaintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Populates the rev_len and ar_len fields";
37 $this->setBatchSize( 200 );
38 }
39
40 protected function getUpdateKey() {
41 return 'populate rev_len and ar_len';
42 }
43
44 public function doDBUpdates() {
45 $db = $this->getDB( DB_MASTER );
46 if ( !$db->tableExists( 'revision' ) ) {
47 $this->error( "revision table does not exist", true );
48 } elseif ( !$db->tableExists( 'archive' ) ) {
49 $this->error( "archive table does not exist", true );
50 } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
51 $this->output( "rev_len column does not exist\n\n", true );
52 return false;
53 }
54
55 $this->output( "Populating rev_len column\n" );
56 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev' );
57
58 $this->output( "Populating ar_len column\n" );
59 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar' );
60
61 $this->output( "rev_len and ar_len population complete [$rev revision rows, $ar archive rows].\n" );
62 return true;
63 }
64
65 /**
66 * @param $table
67 * @param $idCol
68 * @param $prefix
69 * @return int
70 */
71 protected function doLenUpdates( $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 # Do remaining chunks
81 $blockStart = intval( $start );
82 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
83 $count = 0;
84 $fields = Revision::selectFields();
85 while ( $blockStart <= $end ) {
86 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
87 $res = $db->select(
88 $table,
89 $fields,
90 array(
91 "$idCol >= $blockStart",
92 "$idCol <= $blockEnd",
93 "{$prefix}_len IS NULL"
94 ),
95 __METHOD__
96 );
97
98 $db->begin( __METHOD__ );
99 # Go through and update rev_len from these rows.
100 foreach ( $res as $row ) {
101 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
102 $count++;
103 }
104 }
105 $db->commit( __METHOD__ );
106
107 $blockStart += $this->mBatchSize;
108 $blockEnd += $this->mBatchSize;
109 wfWaitForSlaves();
110 }
111
112 return $count;
113 }
114
115 /**
116 * @param $row
117 * @param $table
118 * @param $idCol
119 * @param $prefix
120 * @return bool
121 */
122 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
123 $db = $this->getDB( DB_MASTER );
124
125 $rev = ( $table === 'archive' )
126 ? Revision::newFromArchiveRow( $row )
127 : new Revision( $row );
128
129 $content = $rev->getContent();
130 if ( !$content ) {
131 # This should not happen, but sometimes does (bug 20757)
132 $id = $row->$idCol;
133 $this->output( "Content of $table $id unavailable!\n" );
134 return false;
135 }
136
137 # Update the row...
138 $db->update( $table,
139 array( "{$prefix}_len" => $content->getSize() ),
140 array( $idCol => $row->$idCol ),
141 __METHOD__
142 );
143
144 return true;
145 }
146 }
147
148 $maintClass = "PopulateRevisionLength";
149 require_once RUN_MAINTENANCE_IF_MAIN;