MediaWiki  1.30.0
populateRevisionLength.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
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 
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 
130  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
131  $dbw = $this->getDB( DB_MASTER );
132 
133  $rev = ( $table === 'archive' )
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;
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:189
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
PopulateRevisionLength
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
Definition: populateRevisionLength.php:33
PopulateRevisionLength\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRevisionLength.php:44
PopulateRevisionLength\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionLength.php:130
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: populateRevisionLength.php:157
$res
$res
Definition: database.txt:21
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:3010
php
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:35
Revision
Definition: Revision.php:33
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1562
PopulateRevisionLength\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRevisionLength.php:40
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateRevisionLength\doLenUpdates
doLenUpdates( $table, $idCol, $prefix, $fields)
Definition: populateRevisionLength.php:75
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:486
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
PopulateRevisionLength\__construct
__construct()
Default constructor.
Definition: populateRevisionLength.php:34
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$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:1750
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\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Revision\selectFields
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition: Revision.php:452
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314