MediaWiki  1.32.0
populateChangeTagDef.php
Go to the documentation of this file.
1 <?php
19 require_once __DIR__ . '/Maintenance.php';
20 
28  protected $lbFactory;
29 
30  public function __construct() {
31  parent::__construct();
32  $this->addDescription( 'Populate and improve accuracy of change_tag_def statistics' );
33  $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
34  $this->setBatchSize( 1000 );
35  $this->addOption(
36  'sleep',
37  'Sleep time (in seconds) between every batch, defaults to zero',
38  false,
39  true
40  );
41  $this->addOption( 'populate-only', 'Do not update change_tag_def table' );
42  $this->addOption( 'set-user-tags-only', 'Only update ctd_user_defined from valid_tag table' );
43  }
44 
45  public function execute() {
48  // Return "success", but don't flag it as done so the next run will retry
49  $this->output( '... Not run, $wgChangeTagsSchemaMigrationStage === MIGRATION_OLD' . "\n" );
50  return true;
51  }
52  return parent::execute();
53  }
54 
55  protected function doDBUpdates() {
56  $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
57  $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
58 
59  if ( $this->lbFactory->getMainLB()->getConnection( DB_REPLICA )->fieldExists(
60  'change_tag',
61  'ct_tag',
62  __METHOD__
63  )
64  ) {
65  if ( $this->hasOption( 'set-user-tags-only' ) ) {
66  $this->setUserDefinedTags();
67  return true;
68  }
69  if ( !$this->hasOption( 'populate-only' ) ) {
70  $this->updateCountTag();
71  }
72  $this->backpopulateChangeTagId();
73  $this->setUserDefinedTags();
74  } else {
75  $this->updateCountTagId();
76  }
77 
78  // TODO: Implement
79  // $this->cleanZeroCountRows();
80 
81  return true;
82  }
83 
84  private function setUserDefinedTags() {
85  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
86 
87  $userTags = $dbr->selectFieldValues(
88  'valid_tag',
89  'vt_tag',
90  [],
91  __METHOD__
92  );
93 
94  if ( empty( $userTags ) ) {
95  $this->output( "No user defined tags to set, moving on...\n" );
96  return;
97  }
98 
99  if ( $this->hasOption( 'dry-run' ) ) {
100  $this->output(
101  'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
102  );
103  return;
104  }
105 
106  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
107 
108  $dbw->update(
109  'change_tag_def',
110  [ 'ctd_user_defined' => 1 ],
111  [ 'ctd_name' => $userTags ],
112  __METHOD__
113  );
114  $this->lbFactory->waitForReplication();
115  $this->output( "Finished setting user defined tags in change_tag_def table\n" );
116  }
117 
118  private function updateCountTagId() {
119  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
120 
121  // This query can be pretty expensive, don't run it on master
122  $res = $dbr->select(
123  'change_tag',
124  [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
125  [],
126  __METHOD__,
127  [ 'GROUP BY' => 'ct_tag_id' ]
128  );
129 
130  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
131 
132  foreach ( $res as $row ) {
133  if ( !$row->ct_tag_id ) {
134  continue;
135  }
136 
137  if ( $this->hasOption( 'dry-run' ) ) {
138  $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
139  continue;
140  }
141 
142  $dbw->update(
143  'change_tag_def',
144  [ 'ctd_count' => $row->hitcount ],
145  [ 'ctd_id' => $row->ct_tag_id ],
146  __METHOD__
147  );
148  }
149  $this->lbFactory->waitForReplication();
150  }
151 
152  private function updateCountTag() {
153  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
154 
155  // This query can be pretty expensive, don't run it on master
156  $res = $dbr->select(
157  'change_tag',
158  [ 'ct_tag', 'hitcount' => 'count(*)' ],
159  [],
160  __METHOD__,
161  [ 'GROUP BY' => 'ct_tag' ]
162  );
163 
164  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
165 
166  foreach ( $res as $row ) {
167  // Hygiene check
168  if ( !$row->ct_tag ) {
169  continue;
170  }
171 
172  if ( $this->hasOption( 'dry-run' ) ) {
173  $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" );
174  continue;
175  }
176 
177  $dbw->upsert(
178  'change_tag_def',
179  [
180  'ctd_name' => $row->ct_tag,
181  'ctd_user_defined' => 0,
182  'ctd_count' => $row->hitcount
183  ],
184  [ 'ctd_name' ],
185  [ 'ctd_count' => $row->hitcount ],
186  __METHOD__
187  );
188  }
189  $this->lbFactory->waitForReplication();
190  }
191 
192  private function backpopulateChangeTagId() {
193  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
194  $changeTagDefs = $dbr->select(
195  'change_tag_def',
196  [ 'ctd_name', 'ctd_id' ],
197  [],
198  __METHOD__,
199  [ 'ORDER BY' => 'ctd_id' ]
200  );
201 
202  foreach ( $changeTagDefs as $row ) {
203  $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
204  }
205  }
206 
207  private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
208  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
209  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
210  $sleep = (int)$this->getOption( 'sleep', 0 );
211  $lastId = 0;
212  $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
213  while ( true ) {
214  // Given that indexes might not be there, it's better to use replica
215  $ids = $dbr->selectFieldValues(
216  'change_tag',
217  'ct_id',
218  [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
219  __METHOD__,
220  [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
221  );
222 
223  if ( !$ids ) {
224  break;
225  }
226  $lastId = end( $ids );
227 
228  if ( $this->hasOption( 'dry-run' ) ) {
229  $this->output(
230  "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
231  );
232  continue;
233  } else {
234  $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
235  }
236 
237  $dbw->update(
238  'change_tag',
239  [ 'ct_tag_id' => $tagId ],
240  [ 'ct_id' => $ids ],
241  __METHOD__
242  );
243 
244  $this->lbFactory->waitForReplication();
245  if ( $sleep > 0 ) {
246  sleep( $sleep );
247  }
248  }
249 
250  $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
251  }
252 
253  protected function getUpdateKey() {
254  return __CLASS__;
255  }
256 }
257 
259 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1679
PopulateChangeTagDef\setUserDefinedTags
setUserDefinedTags()
Definition: populateChangeTagDef.php:84
$maintClass
$maintClass
Definition: populateChangeTagDef.php:258
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
PopulateChangeTagDef\updateCountTag
updateCountTag()
Definition: populateChangeTagDef.php:152
PopulateChangeTagDef\$lbFactory
Wikimedia Rdbms ILBFactory $lbFactory
Definition: populateChangeTagDef.php:28
PopulateChangeTagDef\backpopulateChangeTagPerTag
backpopulateChangeTagPerTag( $tagName, $tagId)
Definition: populateChangeTagDef.php:207
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateChangeTagDef
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: populateChangeTagDef.php:26
MIGRATION_OLD
const MIGRATION_OLD
Definition: Defines.php:315
$wgChangeTagsSchemaMigrationStage
int $wgChangeTagsSchemaMigrationStage
change_tag table schema migration stage.
Definition: DefaultSettings.php:9020
execute
$batch execute()
PopulateChangeTagDef\execute
execute()
Do the actual work.
Definition: populateChangeTagDef.php:45
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:120
PopulateChangeTagDef\__construct
__construct()
Default constructor.
Definition: populateChangeTagDef.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
PopulateChangeTagDef\updateCountTagId
updateCountTagId()
Definition: populateChangeTagDef.php:118
PopulateChangeTagDef\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateChangeTagDef.php:55
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
PopulateChangeTagDef\backpopulateChangeTagId
backpopulateChangeTagId()
Definition: populateChangeTagDef.php:192
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
PopulateChangeTagDef\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateChangeTagDef.php:253
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:355