MediaWiki master
deleteTag.php
Go to the documentation of this file.
1<?php
2
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
15class DeleteTag extends Maintenance {
16 public function __construct() {
17 parent::__construct();
18 $this->addDescription( 'Deletes a change tag' );
19 $this->addArg( 'tag name', 'Name of the tag to delete' );
20 $this->setBatchSize( 500 );
21 }
22
23 public function execute() {
24 $dbw = $this->getPrimaryDB();
25 $services = $this->getServiceContainer();
26 $defStore = $services->getChangeTagDefStore();
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 $this->fatalError( $status );
38 }
39
40 $this->output( "Deleting tag '$tag'...\n" );
41
42 // Make the tag impossible to add by users while we're deleting it and drop the
43 // usage counter to zero
44 $dbw->newUpdateQueryBuilder()
45 ->update( 'change_tag_def' )
46 ->set( [
47 'ctd_user_defined' => 0,
48 'ctd_count' => 0,
49 ] )
50 ->where( [ 'ctd_id' => $tagId ] )
51 ->caller( __METHOD__ )->execute();
52 $this->getServiceContainer()->getChangeTagsStore()->purgeTagCacheAll();
53
54 // Iterate over change_tag, deleting rows in batches
55 $count = 0;
56 do {
57 $ids = $dbw->newSelectQueryBuilder()
58 ->select( 'ct_id' )
59 ->from( 'change_tag' )
60 ->where( [ 'ct_tag_id' => $tagId ] )
61 ->limit( $this->getBatchSize() )
62 ->caller( __METHOD__ )
63 ->fetchFieldValues();
64
65 if ( !$ids ) {
66 break;
67 }
68 $dbw->newDeleteQueryBuilder()
69 ->deleteFrom( 'change_tag' )
70 ->where( [ 'ct_id' => $ids ] )
71 ->caller( __METHOD__ )->execute();
72 $count += $dbw->affectedRows();
73 $this->output( "$count\n" );
74 $this->waitForReplication();
75 } while ( true );
76 $this->output( "The tag has been removed from $count revisions, deleting the tag itself...\n" );
77
78 $this->getServiceContainer()->getChangeTagsStore()->deleteTagEverywhere( $tag );
79 $this->output( "Done.\n" );
80 }
81}
82
83// @codeCoverageIgnoreStart
84$maintClass = DeleteTag::class;
85require_once RUN_MAINTENANCE_IF_MAIN;
86// @codeCoverageIgnoreEnd
const BYPASS_MAX_USAGE_CHECK
Flag for canDeleteTag().
static canDeleteTag( $tag, ?Authority $performer=null, int $flags=0)
Is it OK to allow the user to delete this tag?
__construct()
Default constructor.
Definition deleteTag.php:16
execute()
Do the actual work.
Definition deleteTag.php:23
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
waitForReplication()
Wait for replica DB servers to catch up.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Exception representing a failure to look up a row from a name table.
$maintClass
Definition deleteTag.php:84