MediaWiki REL1_39
populateRecentChangesSource.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
28
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription(
39 'Populates rc_source field of the recentchanges table with the data in rc_type.' );
40 $this->setBatchSize( 100 );
41 }
42
43 protected function doDBUpdates() {
44 $dbw = $this->getDB( DB_PRIMARY );
45 $batchSize = $this->getBatchSize();
46 if ( !$dbw->fieldExists( 'recentchanges', 'rc_source', __METHOD__ ) ) {
47 $this->error( 'rc_source field in recentchanges table does not exist.' );
48 }
49
50 $start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', '', __METHOD__ );
51 if ( !$start ) {
52 $this->output( "Nothing to do.\n" );
53
54 return true;
55 }
56 $end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', '', __METHOD__ );
57 $end += $batchSize - 1;
58 $blockStart = $start;
59 $blockEnd = $start + $batchSize - 1;
60
61 $updatedValues = $this->buildUpdateCondition( $dbw );
62
63 while ( $blockEnd <= $end ) {
64 $dbw->update(
65 'recentchanges',
66 [ $updatedValues ],
67 [
68 "rc_source = ''",
69 "rc_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd
70 ],
71 __METHOD__
72 );
73
74 $this->output( "." );
75 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
76
77 $blockStart += $batchSize;
78 $blockEnd += $batchSize;
79 }
80
81 $this->output( "\nDone.\n" );
82 }
83
84 protected function getUpdateKey() {
85 return __CLASS__;
86 }
87
88 protected function buildUpdateCondition( IDatabase $dbw ) {
89 $rcNew = $dbw->addQuotes( RC_NEW );
90 $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
91 $rcEdit = $dbw->addQuotes( RC_EDIT );
92 $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
93 $rcLog = $dbw->addQuotes( RC_LOG );
94 $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
95 $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
96 $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
97
98 return "rc_source = CASE
99 WHEN rc_type = $rcNew THEN $rcSrcNew
100 WHEN rc_type = $rcEdit THEN $rcSrcEdit
101 WHEN rc_type = $rcLog THEN $rcSrcLog
102 WHEN rc_type = $rcExternal THEN $rcSrcExternal
103 ELSE ''
104 END";
105 }
106}
107
108$maintClass = PopulateRecentChangesSource::class;
109require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Service locator for MediaWiki core services.
Maintenance script to populate the rc_source field.
getUpdateKey()
Get the update key name to go in the update log table.
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:39
const DB_PRIMARY
Definition defines.php:28