Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[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', Revision::selectFields() );
57
58 $this->output( "Populating ar_len column\n" );
59 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
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 string $table
67 * @param string $idCol
68 * @param string $prefix
69 * @param array $fields
70 * @return int
71 */
72 protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
73 $db = $this->getDB( DB_MASTER );
74 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
75 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
76 if ( !$start || !$end ) {
77 $this->output( "...$table table seems to be empty.\n" );
78 return 0;
79 }
80
81 # Do remaining chunks
82 $blockStart = intval( $start );
83 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
84 $count = 0;
85
86 while ( $blockStart <= $end ) {
87 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
88 $res = $db->select(
89 $table,
90 $fields,
91 array(
92 "$idCol >= $blockStart",
93 "$idCol <= $blockEnd",
94 "{$prefix}_len IS NULL"
95 ),
96 __METHOD__
97 );
98
99 $db->begin( __METHOD__ );
100 # Go through and update rev_len from these rows.
101 foreach ( $res as $row ) {
102 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
103 $count++;
104 }
105 }
106 $db->commit( __METHOD__ );
107
108 $blockStart += $this->mBatchSize;
109 $blockEnd += $this->mBatchSize;
110 wfWaitForSlaves();
111 }
112
113 return $count;
114 }
115
116 /**
117 * @param $row
118 * @param string $table
119 * @param string $idCol
120 * @param string $prefix
121 * @return bool
122 */
123 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
124 $db = $this->getDB( DB_MASTER );
125
126 $rev = ( $table === 'archive' )
127 ? Revision::newFromArchiveRow( $row )
128 : new Revision( $row );
129
130 $content = $rev->getContent();
131 if ( !$content ) {
132 # This should not happen, but sometimes does (bug 20757)
133 $id = $row->$idCol;
134 $this->output( "Content of $table $id unavailable!\n" );
135 return false;
136 }
137
138 # Update the row...
139 $db->update( $table,
140 array( "{$prefix}_len" => $content->getSize() ),
141 array( $idCol => $row->$idCol ),
142 __METHOD__
143 );
144
145 return true;
146 }
147 }
148
149 $maintClass = "PopulateRevisionLength";
150 require_once RUN_MAINTENANCE_IF_MAIN;