phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
1 <?php
2 /**
3 * Populates the rev_len field for old revisions created before MW 1.10.
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 field for old revisions
28 * created before MW 1.10.
29 *
30 * @ingroup Maintenance
31 */
32 class PopulateRevisionLength extends LoggedUpdateMaintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Populates the rev_len field";
36 $this->setBatchSize( 200 );
37 }
38
39 protected function getUpdateKey() {
40 return 'populate rev_len';
41 }
42
43 protected function updateSkippedMessage() {
44 return 'rev_len column of revision table already populated.';
45 }
46
47 public function doDBUpdates() {
48 $db = $this->getDB( DB_MASTER );
49 if ( !$db->tableExists( 'revision' ) ) {
50 $this->error( "revision table does not exist", true );
51 } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
52 $this->output( "rev_len column does not exist\n\n", true );
53 return false;
54 }
55
56 $this->output( "Populating rev_len column\n" );
57
58 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __METHOD__ );
59 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
60 if ( !$start || !$end ) {
61 $this->output( "...revision table seems to be empty.\n" );
62 return true;
63 }
64
65 # Do remaining chunks
66 $blockStart = intval( $start );
67 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
68 $count = 0;
69 $missing = 0;
70 $fields = Revision::selectFields();
71 while ( $blockStart <= $end ) {
72 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
73 $res = $db->select(
74 'revision',
75 $fields,
76 array(
77 "rev_id >= $blockStart",
78 "rev_id <= $blockEnd",
79 "rev_len IS NULL"
80 ),
81 __METHOD__
82 );
83 # Go through and update rev_len from these rows.
84 foreach ( $res as $row ) {
85 $rev = new Revision( $row );
86 $content = $rev->getContent();
87 if ( !$content ) {
88 # This should not happen, but sometimes does (bug 20757)
89 $this->output( "Content of revision {$row->rev_id} unavailable!\n" );
90 $missing++;
91 }
92 else {
93 # Update the row...
94 $db->update( 'revision',
95 array( 'rev_len' => $content->getSize() ),
96 array( 'rev_id' => $row->rev_id ),
97 __METHOD__ );
98 $count++;
99 }
100 }
101 $blockStart += $this->mBatchSize;
102 $blockEnd += $this->mBatchSize;
103 wfWaitForSlaves();
104 }
105
106 $this->output( "rev_len population complete ... {$count} rows changed ({$missing} missing)\n" );
107 return true;
108 }
109 }
110
111 $maintClass = "PopulateRevisionLength";
112 require_once RUN_MAINTENANCE_IF_MAIN;