MediaWiki  1.33.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  protected function doDBUpdates() {
46  $this->lbFactory = MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
47  $this->setBatchSize( $this->getOption( 'batch-size', $this->getBatchSize() ) );
48 
49  if ( $this->lbFactory->getMainLB()->getConnection( DB_REPLICA )->fieldExists(
50  'change_tag',
51  'ct_tag',
52  __METHOD__
53  )
54  ) {
55  if ( $this->hasOption( 'set-user-tags-only' ) ) {
56  $this->setUserDefinedTags();
57  return true;
58  }
59  if ( !$this->hasOption( 'populate-only' ) ) {
60  $this->updateCountTag();
61  }
62  $this->backpopulateChangeTagId();
63  $this->setUserDefinedTags();
64  } else {
65  $this->updateCountTagId();
66  }
67 
68  // TODO: Implement
69  // $this->cleanZeroCountRows();
70 
71  return true;
72  }
73 
74  private function setUserDefinedTags() {
75  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
76 
77  $userTags = $dbr->selectFieldValues(
78  'valid_tag',
79  'vt_tag',
80  [],
81  __METHOD__
82  );
83 
84  if ( empty( $userTags ) ) {
85  $this->output( "No user defined tags to set, moving on...\n" );
86  return;
87  }
88 
89  if ( $this->hasOption( 'dry-run' ) ) {
90  $this->output(
91  'These tags will have ctd_user_defined=1 : ' . implode( ', ', $userTags ) . "\n"
92  );
93  return;
94  }
95 
96  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
97 
98  $dbw->update(
99  'change_tag_def',
100  [ 'ctd_user_defined' => 1 ],
101  [ 'ctd_name' => $userTags ],
102  __METHOD__
103  );
104  $this->lbFactory->waitForReplication();
105  $this->output( "Finished setting user defined tags in change_tag_def table\n" );
106  }
107 
108  private function updateCountTagId() {
109  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
110 
111  // This query can be pretty expensive, don't run it on master
112  $res = $dbr->select(
113  'change_tag',
114  [ 'ct_tag_id', 'hitcount' => 'count(*)' ],
115  [],
116  __METHOD__,
117  [ 'GROUP BY' => 'ct_tag_id' ]
118  );
119 
120  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
121 
122  foreach ( $res as $row ) {
123  if ( !$row->ct_tag_id ) {
124  continue;
125  }
126 
127  if ( $this->hasOption( 'dry-run' ) ) {
128  $this->output( 'This row will be updated: ' . implode( ', ', $row ) . "\n" );
129  continue;
130  }
131 
132  $dbw->update(
133  'change_tag_def',
134  [ 'ctd_count' => $row->hitcount ],
135  [ 'ctd_id' => $row->ct_tag_id ],
136  __METHOD__
137  );
138  }
139  $this->lbFactory->waitForReplication();
140  }
141 
142  private function updateCountTag() {
143  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
144 
145  // This query can be pretty expensive, don't run it on master
146  $res = $dbr->select(
147  'change_tag',
148  [ 'ct_tag', 'hitcount' => 'count(*)' ],
149  [],
150  __METHOD__,
151  [ 'GROUP BY' => 'ct_tag' ]
152  );
153 
154  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
155 
156  foreach ( $res as $row ) {
157  // Hygiene check
158  if ( !$row->ct_tag ) {
159  continue;
160  }
161 
162  if ( $this->hasOption( 'dry-run' ) ) {
163  $this->output( 'This row will be updated: ' . $row->ct_tag . $row->hitcount . "\n" );
164  continue;
165  }
166 
167  $dbw->upsert(
168  'change_tag_def',
169  [
170  'ctd_name' => $row->ct_tag,
171  'ctd_user_defined' => 0,
172  'ctd_count' => $row->hitcount
173  ],
174  [ 'ctd_name' ],
175  [ 'ctd_count' => $row->hitcount ],
176  __METHOD__
177  );
178  }
179  $this->lbFactory->waitForReplication();
180  }
181 
182  private function backpopulateChangeTagId() {
183  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
184  $changeTagDefs = $dbr->select(
185  'change_tag_def',
186  [ 'ctd_name', 'ctd_id' ],
187  [],
188  __METHOD__,
189  [ 'ORDER BY' => 'ctd_id' ]
190  );
191 
192  foreach ( $changeTagDefs as $row ) {
193  $this->backpopulateChangeTagPerTag( $row->ctd_name, $row->ctd_id );
194  }
195  }
196 
197  private function backpopulateChangeTagPerTag( $tagName, $tagId ) {
198  $dbr = $this->lbFactory->getMainLB()->getConnection( DB_REPLICA );
199  $dbw = $this->lbFactory->getMainLB()->getConnection( DB_MASTER );
200  $sleep = (int)$this->getOption( 'sleep', 0 );
201  $lastId = 0;
202  $this->output( "Starting to add ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
203  while ( true ) {
204  // Given that indexes might not be there, it's better to use replica
205  $ids = $dbr->selectFieldValues(
206  'change_tag',
207  'ct_id',
208  [ 'ct_tag' => $tagName, 'ct_tag_id' => null, 'ct_id > ' . $lastId ],
209  __METHOD__,
210  [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => 'ct_id' ]
211  );
212 
213  if ( !$ids ) {
214  break;
215  }
216  $lastId = end( $ids );
217 
218  if ( $this->hasOption( 'dry-run' ) ) {
219  $this->output(
220  "These ids will be changed to have \"{$tagId}\" as tag id: " . implode( ', ', $ids ) . "\n"
221  );
222  continue;
223  } else {
224  $this->output( "Updating ct_tag_id = {$tagId} up to row ct_id = {$lastId}\n" );
225  }
226 
227  $dbw->update(
228  'change_tag',
229  [ 'ct_tag_id' => $tagId ],
230  [ 'ct_id' => $ids ],
231  __METHOD__
232  );
233 
234  $this->lbFactory->waitForReplication();
235  if ( $sleep > 0 ) {
236  sleep( $sleep );
237  }
238  }
239 
240  $this->output( "Finished adding ct_tag_id = {$tagId} for ct_tag = {$tagName}\n" );
241  }
242 
243  protected function getUpdateKey() {
244  return __CLASS__;
245  }
246 }
247 
249 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
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:1700
PopulateChangeTagDef\setUserDefinedTags
setUserDefinedTags()
Definition: populateChangeTagDef.php:74
$maintClass
$maintClass
Definition: populateChangeTagDef.php:248
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
PopulateChangeTagDef\updateCountTag
updateCountTag()
Definition: populateChangeTagDef.php:142
PopulateChangeTagDef\$lbFactory
Wikimedia Rdbms ILBFactory $lbFactory
Definition: populateChangeTagDef.php:28
PopulateChangeTagDef\backpopulateChangeTagPerTag
backpopulateChangeTagPerTag( $tagName, $tagId)
Definition: populateChangeTagDef.php:197
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
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
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:283
PopulateChangeTagDef\updateCountTagId
updateCountTagId()
Definition: populateChangeTagDef.php:108
PopulateChangeTagDef\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateChangeTagDef.php:45
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
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:182
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
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:269
PopulateChangeTagDef\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateChangeTagDef.php:243
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375