MediaWiki  master
populateRecentChangesSource.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription(
38  'Populates rc_source field of the recentchanges table with the data in rc_type.' );
39  $this->setBatchSize( 100 );
40  }
41 
42  protected function doDBUpdates() {
43  $dbw = $this->getDB( DB_PRIMARY );
44  $batchSize = $this->getBatchSize();
45  if ( !$dbw->fieldExists( 'recentchanges', 'rc_source', __METHOD__ ) ) {
46  $this->error( 'rc_source field in recentchanges table does not exist.' );
47  }
48 
49  $start = $dbw->newSelectQueryBuilder()
50  ->select( 'MIN(rc_id)' )
51  ->from( 'recentchanges' )
52  ->caller( __METHOD__ )->fetchField();
53  if ( !$start ) {
54  $this->output( "Nothing to do.\n" );
55 
56  return true;
57  }
58  $end = $dbw->newSelectQueryBuilder()
59  ->select( 'MAX(rc_id)' )
60  ->from( 'recentchanges' )
61  ->caller( __METHOD__ )->fetchField();
62  $end += $batchSize - 1;
63  $blockStart = $start;
64  $blockEnd = $start + $batchSize - 1;
65 
66  $updatedValues = $this->buildUpdateCondition( $dbw );
67 
68  while ( $blockEnd <= $end ) {
69  $dbw->update(
70  'recentchanges',
71  [ $updatedValues ],
72  [
73  "rc_source = ''",
74  "rc_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
75  ],
76  __METHOD__
77  );
78 
79  $this->output( "." );
80  $this->waitForReplication();
81 
82  $blockStart += $batchSize;
83  $blockEnd += $batchSize;
84  }
85 
86  $this->output( "\nDone.\n" );
87  }
88 
89  protected function getUpdateKey() {
90  return __CLASS__;
91  }
92 
93  protected function buildUpdateCondition( IDatabase $dbw ) {
94  $rcNew = $dbw->addQuotes( RC_NEW );
95  $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
96  $rcEdit = $dbw->addQuotes( RC_EDIT );
97  $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
98  $rcLog = $dbw->addQuotes( RC_LOG );
99  $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
100  $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
101  $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
102 
103  return "rc_source = CASE
104  WHEN rc_type = $rcNew THEN $rcSrcNew
105  WHEN rc_type = $rcEdit THEN $rcSrcEdit
106  WHEN rc_type = $rcLog THEN $rcSrcLog
107  WHEN rc_type = $rcExternal THEN $rcSrcExternal
108  ELSE ''
109  END";
110  }
111 }
112 
113 $maintClass = PopulateRecentChangesSource::class;
114 require_once RUN_MAINTENANCE_IF_MAIN;
const RC_NEW
Definition: Defines.php:117
const RC_LOG
Definition: Defines.php:118
const RC_EXTERNAL
Definition: Defines.php:119
const RC_EDIT
Definition: Defines.php:116
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Maintenance script to populate the rc_source field.
getUpdateKey()
Get the update key name to go in the update log table.
const SRC_EXTERNAL
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
const DB_PRIMARY
Definition: defines.php:28