Merge "Add attributes parameter to ShowSearchHitTitle"
[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::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 $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 $queryInfo['tables'],
95 $queryInfo['fields'],
96 [
97 "$idCol >= $blockStart",
98 "$idCol <= $blockEnd",
99 "{$prefix}_len IS NULL"
100 ],
101 __METHOD__,
102 [],
103 $queryInfo['joins']
104 );
105
106 if ( $res->numRows() > 0 ) {
107 $this->beginTransaction( $dbw, __METHOD__ );
108 # Go through and update rev_len from these rows.
109 foreach ( $res as $row ) {
110 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
111 $count++;
112 }
113 }
114 $this->commitTransaction( $dbw, __METHOD__ );
115 }
116
117 $blockStart += $this->mBatchSize;
118 $blockEnd += $this->mBatchSize;
119 wfWaitForSlaves();
120 }
121
122 return $count;
123 }
124
125 /**
126 * @param stdClass $row
127 * @param string $table
128 * @param string $idCol
129 * @param string $prefix
130 * @return bool
131 */
132 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
133 $dbw = $this->getDB( DB_MASTER );
134
135 $rev = ( $table === 'archive' )
136 ? Revision::newFromArchiveRow( $row )
137 : new Revision( $row );
138
139 $content = $rev->getContent();
140 if ( !$content ) {
141 # This should not happen, but sometimes does (T22757)
142 $id = $row->$idCol;
143 $this->output( "Content of $table $id unavailable!\n" );
144
145 return false;
146 }
147
148 # Update the row...
149 $dbw->update( $table,
150 [ "{$prefix}_len" => $content->getSize() ],
151 [ $idCol => $row->$idCol ],
152 __METHOD__
153 );
154
155 return true;
156 }
157 }
158
159 $maintClass = "PopulateRevisionLength";
160 require_once RUN_MAINTENANCE_IF_MAIN;