MediaWiki REL1_32
populateRevisionLength.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Populates the rev_len and ar_len fields' );
39 $this->setBatchSize( 200 );
40 }
41
42 protected function getUpdateKey() {
43 return 'populate rev_len and ar_len';
44 }
45
46 public function doDBUpdates() {
47 $dbw = $this->getDB( DB_MASTER );
48 if ( !$dbw->tableExists( 'revision' ) ) {
49 $this->fatalError( "revision table does not exist" );
50 } elseif ( !$dbw->tableExists( 'archive' ) ) {
51 $this->fatalError( "archive table does not exist" );
52 } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
53 $this->output( "rev_len column does not exist\n\n", true );
54
55 return false;
56 }
57
58 $this->output( "Populating rev_len column\n" );
59 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
60
61 $this->output( "Populating ar_len column\n" );
62 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
63
64 $this->output( "rev_len and ar_len population complete "
65 . "[$rev revision rows, $ar archive rows].\n" );
66
67 return true;
68 }
69
77 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
78 $dbr = $this->getDB( DB_REPLICA );
79 $dbw = $this->getDB( DB_MASTER );
80 $batchSize = $this->getBatchSize();
81 $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
82 $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
83 if ( !$start || !$end ) {
84 $this->output( "...$table table seems to be empty.\n" );
85
86 return 0;
87 }
88
89 # Do remaining chunks
90 $blockStart = intval( $start );
91 $blockEnd = intval( $start ) + $batchSize - 1;
92 $count = 0;
93
94 while ( $blockStart <= $end ) {
95 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
96 $res = $dbr->select(
97 $queryInfo['tables'],
98 $queryInfo['fields'],
99 [
100 "$idCol >= $blockStart",
101 "$idCol <= $blockEnd",
102 $dbr->makeList( [
103 "{$prefix}_len IS NULL",
104 $dbr->makeList( [
105 "{$prefix}_len = 0",
106 "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ), // sha1( "" )
107 ], IDatabase::LIST_AND )
108 ], IDatabase::LIST_OR )
109 ],
110 __METHOD__,
111 [],
112 $queryInfo['joins']
113 );
114
115 if ( $res->numRows() > 0 ) {
116 $this->beginTransaction( $dbw, __METHOD__ );
117 # Go through and update rev_len from these rows.
118 foreach ( $res as $row ) {
119 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
120 $count++;
121 }
122 }
123 $this->commitTransaction( $dbw, __METHOD__ );
124 }
125
126 $blockStart += $batchSize;
127 $blockEnd += $batchSize;
128 }
129
130 return $count;
131 }
132
140 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
141 $dbw = $this->getDB( DB_MASTER );
142
143 $rev = ( $table === 'archive' )
145 : new Revision( $row );
146
147 $content = $rev->getContent( Revision::RAW );
148 if ( !$content ) {
149 # This should not happen, but sometimes does (T22757)
150 $id = $row->$idCol;
151 $this->output( "Content of $table $id unavailable!\n" );
152
153 return false;
154 }
155
156 # Update the row...
157 $dbw->update( $table,
158 [ "{$prefix}_len" => $content->getSize() ],
159 [ $idCol => $row->$idCol ],
160 __METHOD__
161 );
162
163 return true;
164 }
165}
166
167$maintClass = PopulateRevisionLength::class;
168require_once RUN_MAINTENANCE_IF_MAIN;
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
__construct()
Default constructor.
doDBUpdates()
Do the actual work.
upgradeRow( $row, $table, $idCol, $prefix)
getUpdateKey()
Get the update key name to go in the update log table.
doLenUpdates( $table, $idCol, $prefix, $queryInfo)
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition Revision.php:535
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition Revision.php:167
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition Revision.php:521
const RAW
Definition Revision.php:57
$res
Definition database.txt:21
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1818
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
require_once RUN_MAINTENANCE_IF_MAIN
$content
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26