MediaWiki master
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->newSelectQueryBuilder()
51 ->select( 'MIN(rc_id)' )
52 ->from( 'recentchanges' )
53 ->caller( __METHOD__ )->fetchField();
54 if ( !$start ) {
55 $this->output( "Nothing to do.\n" );
56
57 return true;
58 }
59 $end = $dbw->newSelectQueryBuilder()
60 ->select( 'MAX(rc_id)' )
61 ->from( 'recentchanges' )
62 ->caller( __METHOD__ )->fetchField();
63 $end += $batchSize - 1;
64 $blockStart = $start;
65 $blockEnd = $start + $batchSize - 1;
66
67 $updatedValues = $this->buildUpdateCondition( $dbw );
68
69 while ( $blockEnd <= $end ) {
70 $dbw->newUpdateQueryBuilder()
71 ->update( 'recentchanges' )
72 ->set( $updatedValues )
73 ->where( [
74 'rc_source' => '',
75 $dbw->expr( 'rc_id', '>=', (int)$blockStart ),
76 $dbw->expr( 'rc_id', '<=', (int)$blockEnd ),
77 ] )
78 ->caller( __METHOD__ )
79 ->execute();
80
81 $this->output( "." );
82 $this->waitForReplication();
83
84 $blockStart += $batchSize;
85 $blockEnd += $batchSize;
86 }
87
88 $this->output( "\nDone.\n" );
89 }
90
91 protected function getUpdateKey() {
92 return __CLASS__;
93 }
94
95 protected function buildUpdateCondition( IDatabase $dbw ) {
96 $rcNew = $dbw->addQuotes( RC_NEW );
97 $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
98 $rcEdit = $dbw->addQuotes( RC_EDIT );
99 $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
100 $rcLog = $dbw->addQuotes( RC_LOG );
101 $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
102 $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
103 $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
104
105 return [ 'rc_source' => new RawSQLValue( "CASE
106 WHEN rc_type = $rcNew THEN $rcSrcNew
107 WHEN rc_type = $rcEdit THEN $rcSrcEdit
108 WHEN rc_type = $rcLog THEN $rcSrcLog
109 WHEN rc_type = $rcExternal THEN $rcSrcExternal
110 ELSE ''
111 END" ) ];
112 }
113}
114
115$maintClass = PopulateRecentChangesSource::class;
116require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RC_NEW
Definition Defines.php:118
const RC_LOG
Definition Defines.php:119
const RC_EXTERNAL
Definition Defines.php:120
const RC_EDIT
Definition Defines.php:117
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.
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.
Raw SQL value to be used in query builders.
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