MediaWiki  1.27.1
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->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  $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 
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  $db = $this->getDB( DB_MASTER );
77  $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
78  $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
79  if ( !$start || !$end ) {
80  $this->output( "...$table table seems to be empty.\n" );
81 
82  return 0;
83  }
84 
85  # Do remaining chunks
86  $blockStart = intval( $start );
87  $blockEnd = intval( $start ) + $this->mBatchSize - 1;
88  $count = 0;
89 
90  while ( $blockStart <= $end ) {
91  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
92  $res = $db->select(
93  $table,
94  $fields,
95  [
96  "$idCol >= $blockStart",
97  "$idCol <= $blockEnd",
98  "{$prefix}_len IS NULL"
99  ],
100  __METHOD__
101  );
102 
103  $this->beginTransaction( $db, __METHOD__ );
104  # Go through and update rev_len from these rows.
105  foreach ( $res as $row ) {
106  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
107  $count++;
108  }
109  }
110  $this->commitTransaction( $db, __METHOD__ );
111 
112  $blockStart += $this->mBatchSize;
113  $blockEnd += $this->mBatchSize;
114  wfWaitForSlaves();
115  }
116 
117  return $count;
118  }
119 
127  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
128  $db = $this->getDB( DB_MASTER );
129 
130  $rev = ( $table === 'archive' )
132  : new Revision( $row );
133 
134  $content = $rev->getContent();
135  if ( !$content ) {
136  # This should not happen, but sometimes does (bug 20757)
137  $id = $row->$idCol;
138  $this->output( "Content of $table $id unavailable!\n" );
139 
140  return false;
141  }
142 
143  # Update the row...
144  $db->update( $table,
145  [ "{$prefix}_len" => $content->getSize() ],
146  [ $idCol => $row->$idCol ],
147  __METHOD__
148  );
149 
150  return true;
151  }
152 }
153 
154 $maintClass = "PopulateRevisionLength";
155 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for slaves to catch up.
wfWaitForSlaves($ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the slaves to catch up to the master position.
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
static selectArchiveFields()
Return the list of revision fields that should be selected to create a new revision from an archive r...
Definition: Revision.php:460
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
Definition: database.txt:21
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition: Revision.php:429
upgradeRow($row, $table, $idCol, $prefix)
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:1584
addDescription($text)
Set the description text.
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
output($out, $channel=null)
Throw some output to the user.
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...
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1004
int $mBatchSize
Batch size.
Definition: Maintenance.php:99
error($err, $die=0)
Throw an error to the user.
doLenUpdates($table, $idCol, $prefix, $fields)
Maintenance script that populates the rev_len and ar_len fields for old revisions created before MW 1...
$count
const DB_MASTER
Definition: Defines.php:47
setBatchSize($s=0)
Set the batch size.
static newFromArchiveRow($row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:172
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.