Update IPSet use statements
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
1 <?php
2 /**
3 * Populates the rev_len and ar_len fields when they are NULL.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
28 * This is the case for all revisions created before MW 1.10, as well as those affected
29 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
30 *
31 * @ingroup Maintenance
32 */
33 class PopulateRevisionLength extends LoggedUpdateMaintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( '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 $dbw = $this->getDB( DB_MASTER );
46 if ( !$dbw->tableExists( 'revision' ) ) {
47 $this->fatalError( "revision table does not exist" );
48 } elseif ( !$dbw->tableExists( 'archive' ) ) {
49 $this->fatalError( "archive table does not exist" );
50 } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
51 $this->output( "rev_len column does not exist\n\n", true );
52
53 return false;
54 }
55
56 $this->output( "Populating rev_len column\n" );
57 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
58
59 $this->output( "Populating ar_len column\n" );
60 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
61
62 $this->output( "rev_len and ar_len population complete "
63 . "[$rev revision rows, $ar archive rows].\n" );
64
65 return true;
66 }
67
68 /**
69 * @param string $table
70 * @param string $idCol
71 * @param string $prefix
72 * @param array $queryInfo
73 * @return int
74 */
75 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
76 $dbr = $this->getDB( DB_REPLICA );
77 $dbw = $this->getDB( DB_MASTER );
78 $batchSize = $this->getBatchSize();
79 $start = $dbw->selectField( $table, "MIN($idCol)", false, __METHOD__ );
80 $end = $dbw->selectField( $table, "MAX($idCol)", false, __METHOD__ );
81 if ( !$start || !$end ) {
82 $this->output( "...$table table seems to be empty.\n" );
83
84 return 0;
85 }
86
87 # Do remaining chunks
88 $blockStart = intval( $start );
89 $blockEnd = intval( $start ) + $batchSize - 1;
90 $count = 0;
91
92 while ( $blockStart <= $end ) {
93 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
94 $res = $dbr->select(
95 $queryInfo['tables'],
96 $queryInfo['fields'],
97 [
98 "$idCol >= $blockStart",
99 "$idCol <= $blockEnd",
100 "{$prefix}_len IS NULL"
101 ],
102 __METHOD__,
103 [],
104 $queryInfo['joins']
105 );
106
107 if ( $res->numRows() > 0 ) {
108 $this->beginTransaction( $dbw, __METHOD__ );
109 # Go through and update rev_len from these rows.
110 foreach ( $res as $row ) {
111 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
112 $count++;
113 }
114 }
115 $this->commitTransaction( $dbw, __METHOD__ );
116 }
117
118 $blockStart += $batchSize;
119 $blockEnd += $batchSize;
120 wfWaitForSlaves();
121 }
122
123 return $count;
124 }
125
126 /**
127 * @param stdClass $row
128 * @param string $table
129 * @param string $idCol
130 * @param string $prefix
131 * @return bool
132 */
133 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
134 $dbw = $this->getDB( DB_MASTER );
135
136 $rev = ( $table === 'archive' )
137 ? Revision::newFromArchiveRow( $row )
138 : new Revision( $row );
139
140 $content = $rev->getContent();
141 if ( !$content ) {
142 # This should not happen, but sometimes does (T22757)
143 $id = $row->$idCol;
144 $this->output( "Content of $table $id unavailable!\n" );
145
146 return false;
147 }
148
149 # Update the row...
150 $dbw->update( $table,
151 [ "{$prefix}_len" => $content->getSize() ],
152 [ $idCol => $row->$idCol ],
153 __METHOD__
154 );
155
156 return true;
157 }
158 }
159
160 $maintClass = "PopulateRevisionLength";
161 require_once RUN_MAINTENANCE_IF_MAIN;