MediaWiki  master
populateRevisionLength.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Populates the rev_len and ar_len fields' );
40  $this->setBatchSize( 200 );
41  }
42 
43  protected function getUpdateKey() {
44  return 'populate rev_len and ar_len';
45  }
46 
47  public function doDBUpdates() {
48  $dbw = $this->getDB( DB_MASTER );
49  if ( !$dbw->tableExists( 'revision' ) ) {
50  $this->fatalError( "revision table does not exist" );
51  } elseif ( !$dbw->tableExists( 'archive' ) ) {
52  $this->fatalError( "archive table does not exist" );
53  } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
54  $this->output( "rev_len column does not exist\n\n", true );
55 
56  return false;
57  }
58 
59  $this->output( "Populating rev_len column\n" );
60  $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
61 
62  $this->output( "Populating ar_len column\n" );
63  $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
64 
65  $this->output( "rev_len and ar_len population complete "
66  . "[$rev revision rows, $ar archive rows].\n" );
67 
68  return true;
69  }
70 
78  protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
79  $dbr = $this->getDB( DB_REPLICA );
80  $dbw = $this->getDB( DB_MASTER );
81  $batchSize = $this->getBatchSize();
82  $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
83  $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
84  if ( !$start || !$end ) {
85  $this->output( "...$table table seems to be empty.\n" );
86 
87  return 0;
88  }
89 
90  # Do remaining chunks
91  $blockStart = intval( $start );
92  $blockEnd = intval( $start ) + $batchSize - 1;
93  $count = 0;
94 
95  while ( $blockStart <= $end ) {
96  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
97  $res = $dbr->select(
98  $queryInfo['tables'],
99  $queryInfo['fields'],
100  [
101  "$idCol >= $blockStart",
102  "$idCol <= $blockEnd",
103  $dbr->makeList( [
104  "{$prefix}_len IS NULL",
105  $dbr->makeList( [
106  "{$prefix}_len = 0",
107  "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ), // sha1( "" )
109  ], IDatabase::LIST_OR )
110  ],
111  __METHOD__,
112  [],
113  $queryInfo['joins']
114  );
115 
116  if ( $res->numRows() > 0 ) {
117  $this->beginTransaction( $dbw, __METHOD__ );
118  # Go through and update rev_len from these rows.
119  foreach ( $res as $row ) {
120  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
121  $count++;
122  }
123  }
124  $this->commitTransaction( $dbw, __METHOD__ );
125  }
126 
127  $blockStart += $batchSize;
128  $blockEnd += $batchSize;
129  }
130 
131  return $count;
132  }
133 
141  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
142  $dbw = $this->getDB( DB_MASTER );
143 
144  $rev = ( $table === 'archive' )
146  : new Revision( $row );
147 
148  $content = $rev->getContent( RevisionRecord::RAW );
149  if ( !$content ) {
150  # This should not happen, but sometimes does (T22757)
151  $id = $row->$idCol;
152  $this->output( "Content of $table $id unavailable!\n" );
153 
154  return false;
155  }
156 
157  # Update the row...
158  $dbw->update( $table,
159  [ "{$prefix}_len" => $content->getSize() ],
160  [ $idCol => $row->$idCol ],
161  __METHOD__
162  );
163 
164  return true;
165  }
166 }
167 
169 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:171
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
setBatchSize( $s=0)
Set the batch size.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
const DB_MASTER
Definition: defines.php:26
const LIST_AND
Definition: Defines.php:39
$res
Definition: database.txt:21
addDescription( $text)
Set the description text.
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:525
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object...
Definition: Revision.php:511
output( $out, $channel=null)
Throw some output to the user.
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:1754
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
const LIST_OR
Definition: Defines.php:42
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
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
getBatchSize()
Returns batch size.
Maintenance script that populates the rev_len and ar_len fields when they are NULL.
const DB_REPLICA
Definition: defines.php:25
upgradeRow( $row, $table, $idCol, $prefix)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
$content
Definition: pageupdater.txt:72
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.