MediaWiki  1.34.0
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_MASTER );
44  $batchSize = $this->getBatchSize();
45  if ( !$dbw->fieldExists( 'recentchanges', 'rc_source' ) ) {
46  $this->error( 'rc_source field in recentchanges table does not exist.' );
47  }
48 
49  $start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', '', __METHOD__ );
50  if ( !$start ) {
51  $this->output( "Nothing to do.\n" );
52 
53  return true;
54  }
55  $end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', '', __METHOD__ );
56  $end += $batchSize - 1;
57  $blockStart = $start;
58  $blockEnd = $start + $batchSize - 1;
59 
60  $updatedValues = $this->buildUpdateCondition( $dbw );
61 
62  while ( $blockEnd <= $end ) {
63  $dbw->update(
64  'recentchanges',
65  [ $updatedValues ],
66  [
67  "rc_source = ''",
68  "rc_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
69  ],
70  __METHOD__
71  );
72 
73  $this->output( "." );
75 
76  $blockStart += $batchSize;
77  $blockEnd += $batchSize;
78  }
79 
80  $this->output( "\nDone.\n" );
81  }
82 
83  protected function getUpdateKey() {
84  return __CLASS__;
85  }
86 
87  protected function buildUpdateCondition( IDatabase $dbw ) {
88  $rcNew = $dbw->addQuotes( RC_NEW );
89  $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
90  $rcEdit = $dbw->addQuotes( RC_EDIT );
91  $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
92  $rcLog = $dbw->addQuotes( RC_LOG );
93  $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
94  $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
95  $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
96 
97  return "rc_source = CASE
98  WHEN rc_type = $rcNew THEN $rcSrcNew
99  WHEN rc_type = $rcEdit THEN $rcSrcEdit
100  WHEN rc_type = $rcLog THEN $rcSrcLog
101  WHEN rc_type = $rcExternal THEN $rcSrcExternal
102  ELSE ''
103  END";
104  }
105 }
106 
107 $maintClass = PopulateRecentChangesSource::class;
108 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
PopulateRecentChangesSource\buildUpdateCondition
buildUpdateCondition(IDatabase $dbw)
Definition: populateRecentChangesSource.php:87
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:125
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
PopulateRecentChangesSource\__construct
__construct()
Default constructor.
Definition: populateRecentChangesSource.php:35
RC_LOG
const RC_LOG
Definition: Defines.php:124
PopulateRecentChangesSource
Maintenance script to populate the rc_source field.
Definition: populateRecentChangesSource.php:34
RC_EDIT
const RC_EDIT
Definition: Defines.php:122
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:2718
RecentChange\SRC_LOG
const SRC_LOG
Definition: RecentChange.php:75
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
RecentChange\SRC_EDIT
const SRC_EDIT
Definition: RecentChange.php:73
DB_MASTER
const DB_MASTER
Definition: defines.php:26
RecentChange\SRC_NEW
const SRC_NEW
Definition: RecentChange.php:74
PopulateRecentChangesSource\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRecentChangesSource.php:42
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
RC_NEW
const RC_NEW
Definition: Defines.php:123
$maintClass
$maintClass
Definition: populateRecentChangesSource.php:107
Wikimedia\Rdbms\IDatabase\addQuotes
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
PopulateRecentChangesSource\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRecentChangesSource.php:83
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394
RecentChange\SRC_EXTERNAL
const SRC_EXTERNAL
Definition: RecentChange.php:76