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