MediaWiki  master
deleteTag.php
Go to the documentation of this file.
1 <?php
2 
10 
11 require_once __DIR__ . '/Maintenance.php';
12 
13 class DeleteTag extends Maintenance {
14  public function __construct() {
15  parent::__construct();
16  $this->addDescription( 'Deletes a change tag' );
17  $this->addArg( 'tag name', 'Name of the tag to delete' );
18  $this->setBatchSize( 500 );
19  }
20 
21  public function execute() {
22  $dbw = $this->getDB( DB_MASTER );
23  $services = MediaWikiServices::getInstance();
24  $defStore = $services->getChangeTagDefStore();
25  $lbFactory = $services->getDBLoadBalancerFactory();
26  $options = [ 'domain' => $lbFactory->getLocalDomainID() ];
27 
28  $tag = $this->getArg( 0 );
29  try {
30  $tagId = $defStore->getId( $tag );
31  } catch ( NameTableAccessException $ex ) {
32  $this->fatalError( "Tag '$tag' not found" );
33  // To make analyzers happy
34  return;
35  }
36 
37  $status = ChangeTags::canDeleteTag( $tag, null, true );
38  if ( !$status->isOK() ) {
39  $message = $status->getHTML( false, false, 'en' );
40  $this->fatalError( Sanitizer::stripAllTags( $message ) );
41  }
42 
43  $this->output( "Deleting tag '$tag'...\n" );
44 
45  // Make the tag imposssible to add by users while we're deleting it and drop the
46  // usage counter to zero
47  $dbw->update(
48  'change_tag_def',
49  [
50  'ctd_user_defined' => 0,
51  'ctd_count' => 0,
52  ],
53  [ 'ctd_id' => $tagId ],
54  __METHOD__
55  );
57 
58  // Iterate over change_tag, deleting rows in batches
59  $count = 0;
60  do {
61  $ids = $dbw->selectFieldValues(
62  'change_tag',
63  'ct_id',
64  [ 'ct_tag_id' => $tagId ],
65  __METHOD__,
66  [ 'LIMIT' => $this->getBatchSize() ]
67  );
68 
69  if ( !$ids ) {
70  break;
71  }
72  $dbw->delete( 'change_tag', [ 'ct_id' => $ids ], __METHOD__ );
73  $count += $dbw->affectedRows();
74  $this->output( "$count\n" );
75  $lbFactory->waitForReplication( $options );
76  } while ( true );
77  $this->output( "The tag has been removed from $count revisions, deleting the tag itself...\n" );
78 
80  $this->output( "Done.\n" );
81  }
82 }
83 
84 $maintClass = DeleteTag::class;
85 require_once RUN_MAINTENANCE_IF_MAIN;
getArg( $argId=0, $default=null)
Get an argument.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Exception representing a failure to look up a row from a name table.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
setBatchSize( $s=0)
Set the batch size.
const DB_MASTER
Definition: defines.php:26
static deleteTagEverywhere( $tag)
Permanently removes all traces of a tag from the DB.
static stripAllTags( $html)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed, encoded as plain text.
Definition: Sanitizer.php:2041
__construct()
Definition: deleteTag.php:14
addDescription( $text)
Set the description text.
static canDeleteTag( $tag, User $user=null, bool $ignoreCounts=false)
Is it OK to allow the user to delete this tag?
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
static purgeTagCacheAll()
Invalidates the short-term cache of defined tags used by the list*DefinedTags functions, as well as the tag statistics cache.
$maintClass
Definition: deleteTag.php:84
getBatchSize()
Returns batch size.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.