MediaWiki master
populateRecentChangesSource.php
Go to the documentation of this file.
1<?php
24require_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->newUpdateQueryBuilder()
70 ->update( 'recentchanges' )
71 ->set( [ $updatedValues ] )
72 ->where( [
73 'rc_source' => '',
74 $dbw->expr( 'rc_id', '>=', (int)$blockStart ),
75 $dbw->expr( 'rc_id', '<=', (int)$blockEnd ),
76 ] )
77 ->caller( __METHOD__ )
78 ->execute();
79
80 $this->output( "." );
81 $this->waitForReplication();
82
83 $blockStart += $batchSize;
84 $blockEnd += $batchSize;
85 }
86
87 $this->output( "\nDone.\n" );
88 }
89
90 protected function getUpdateKey() {
91 return __CLASS__;
92 }
93
94 protected function buildUpdateCondition( IDatabase $dbw ) {
95 $rcNew = $dbw->addQuotes( RC_NEW );
96 $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
97 $rcEdit = $dbw->addQuotes( RC_EDIT );
98 $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
99 $rcLog = $dbw->addQuotes( RC_LOG );
100 $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
101 $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
102 $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
103
104 return "rc_source = CASE
105 WHEN rc_type = $rcNew THEN $rcSrcNew
106 WHEN rc_type = $rcEdit THEN $rcSrcEdit
107 WHEN rc_type = $rcLog THEN $rcSrcLog
108 WHEN rc_type = $rcExternal THEN $rcSrcExternal
109 ELSE ''
110 END";
111 }
112}
113
114$maintClass = PopulateRecentChangesSource::class;
115require_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.
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.
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