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_PRIMARY );
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  }
34 
36  if ( !$status->isOK() ) {
37  $message = $status->getHTML( false, false, 'en' );
38  $this->fatalError( Sanitizer::stripAllTags( $message ) );
39  }
40 
41  $this->output( "Deleting tag '$tag'...\n" );
42 
43  // Make the tag imposssible to add by users while we're deleting it and drop the
44  // usage counter to zero
45  $dbw->update(
46  'change_tag_def',
47  [
48  'ctd_user_defined' => 0,
49  'ctd_count' => 0,
50  ],
51  [ 'ctd_id' => $tagId ],
52  __METHOD__
53  );
55 
56  // Iterate over change_tag, deleting rows in batches
57  $count = 0;
58  do {
59  $ids = $dbw->selectFieldValues(
60  'change_tag',
61  'ct_id',
62  [ 'ct_tag_id' => $tagId ],
63  __METHOD__,
64  [ 'LIMIT' => $this->getBatchSize() ]
65  );
66 
67  if ( !$ids ) {
68  break;
69  }
70  $dbw->delete( 'change_tag', [ 'ct_id' => $ids ], __METHOD__ );
71  $count += $dbw->affectedRows();
72  $this->output( "$count\n" );
73  $lbFactory->waitForReplication( $options );
74  } while ( true );
75  $this->output( "The tag has been removed from $count revisions, deleting the tag itself...\n" );
76 
78  $this->output( "Done.\n" );
79  }
80 }
81 
82 $maintClass = DeleteTag::class;
83 require_once RUN_MAINTENANCE_IF_MAIN;
ChangeTags\purgeTagCacheAll
static purgeTagCacheAll()
Invalidates the short-term cache of defined tags used by the list*DefinedTags functions,...
Definition: ChangeTags.php:1691
Sanitizer\stripAllTags
static stripAllTags( $html)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed,...
Definition: Sanitizer.php:1576
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
DeleteTag\execute
execute()
Do the actual work.
Definition: deleteTag.php:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
ChangeTags\BYPASS_MAX_USAGE_CHECK
const BYPASS_MAX_USAGE_CHECK
Flag for canDeleteTag().
Definition: ChangeTags.php:111
ChangeTags\deleteTagEverywhere
static deleteTagEverywhere( $tag)
Permanently removes all traces of a tag from the DB.
Definition: ChangeTags.php:1439
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
DeleteTag
Definition: deleteTag.php:13
DeleteTag\__construct
__construct()
Default constructor.
Definition: deleteTag.php:14
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
$maintClass
$maintClass
Definition: deleteTag.php:82
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:300
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:368
MediaWiki\Storage\NameTableAccessException
Exception representing a failure to look up a row from a name table.
Definition: NameTableAccessException.php:33
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:353
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375
ChangeTags\canDeleteTag
static canDeleteTag( $tag, Authority $performer=null, int $flags=0)
Is it OK to allow the user to delete this tag?
Definition: ChangeTags.php:1481