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