MediaWiki  1.23.2
populateRevisionLength.php
Go to the documentation of this file.
1 <?php
25 require_once __DIR__ . '/Maintenance.php';
26 
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 [$rev revision rows, $ar archive rows].\n" );
62  return true;
63  }
64 
72  protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
73  $db = $this->getDB( DB_MASTER );
74  $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
75  $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
76  if ( !$start || !$end ) {
77  $this->output( "...$table table seems to be empty.\n" );
78  return 0;
79  }
80 
81  # Do remaining chunks
82  $blockStart = intval( $start );
83  $blockEnd = intval( $start ) + $this->mBatchSize - 1;
84  $count = 0;
85 
86  while ( $blockStart <= $end ) {
87  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
88  $res = $db->select(
89  $table,
90  $fields,
91  array(
92  "$idCol >= $blockStart",
93  "$idCol <= $blockEnd",
94  "{$prefix}_len IS NULL"
95  ),
96  __METHOD__
97  );
98 
99  $db->begin( __METHOD__ );
100  # Go through and update rev_len from these rows.
101  foreach ( $res as $row ) {
102  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
103  $count++;
104  }
105  }
106  $db->commit( __METHOD__ );
107 
108  $blockStart += $this->mBatchSize;
109  $blockEnd += $this->mBatchSize;
110  wfWaitForSlaves();
111  }
112 
113  return $count;
114  }
115 
123  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
124  $db = $this->getDB( DB_MASTER );
125 
126  $rev = ( $table === 'archive' )
128  : new Revision( $row );
129 
130  $content = $rev->getContent();
131  if ( !$content ) {
132  # This should not happen, but sometimes does (bug 20757)
133  $id = $row->$idCol;
134  $this->output( "Content of $table $id unavailable!\n" );
135  return false;
136  }
137 
138  # Update the row...
139  $db->update( $table,
140  array( "{$prefix}_len" => $content->getSize() ),
141  array( $idCol => $row->$idCol ),
142  __METHOD__
143  );
144 
145  return true;
146  }
147 }
148 
149 $maintClass = "PopulateRevisionLength";
150 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:97
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
PopulateRevisionLength
Maintenance script that populates the rev_len and ar_len fields for old revisions created before MW 1...
Definition: populateRevisionLength.php:33
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
PopulateRevisionLength\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRevisionLength.php:44
PopulateRevisionLength\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionLength.php:123
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: populateRevisionLength.php:149
Revision
Definition: Revision.php:26
Maintenance\getDB
& getDB( $db, $groups=array(), $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1007
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1209
PopulateRevisionLength\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRevisionLength.php:40
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3795
PopulateRevisionLength\doLenUpdates
doLenUpdates( $table, $idCol, $prefix, $fields)
Definition: populateRevisionLength.php:72
Revision\selectArchiveFields
static selectArchiveFields()
Return the list of revision fields that should be selected to create a new revision from an archive r...
Definition: Revision.php:436
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=array())
Make a fake revision object from an archive table row.
Definition: Revision.php:159
PopulateRevisionLength\__construct
__construct()
Default constructor.
Definition: populateRevisionLength.php:34
$count
$count
Definition: UtfNormalTest2.php:96
$rev
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:1337
as
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
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Revision\selectFields
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition: Revision.php:405
$res
$res
Definition: database.txt:21
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:254