4 * Remove a revision tag from edits and log entries it was applied to.
8 use MediaWiki\Maintenance\Maintenance
;
9 use MediaWiki\Storage\NameTableAccessException
;
11 // @codeCoverageIgnoreStart
12 require_once __DIR__
. '/Maintenance.php';
13 // @codeCoverageIgnoreEnd
15 class 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 );
23 public function execute() {
24 $dbw = $this->getPrimaryDB();
25 $services = $this->getServiceContainer();
26 $defStore = $services->getChangeTagDefStore();
28 $tag = $this->getArg( 0 );
30 $tagId = $defStore->getId( $tag );
31 } catch ( NameTableAccessException
$ex ) {
32 $this->fatalError( "Tag '$tag' not found" );
35 $status = ChangeTags
::canDeleteTag( $tag, null, ChangeTags
::BYPASS_MAX_USAGE_CHECK
);
36 if ( !$status->isOK() ) {
37 $this->fatalError( $status );
40 $this->output( "Deleting tag '$tag'...\n" );
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' )
47 'ctd_user_defined' => 0,
50 ->where( [ 'ctd_id' => $tagId ] )
51 ->caller( __METHOD__
)->execute();
52 $this->getServiceContainer()->getChangeTagsStore()->purgeTagCacheAll();
54 // Iterate over change_tag, deleting rows in batches
57 $ids = $dbw->newSelectQueryBuilder()
59 ->from( 'change_tag' )
60 ->where( [ 'ct_tag_id' => $tagId ] )
61 ->limit( $this->getBatchSize() )
62 ->caller( __METHOD__
)
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();
76 $this->output( "The tag has been removed from $count revisions, deleting the tag itself...\n" );
78 $this->getServiceContainer()->getChangeTagsStore()->deleteTagEverywhere( $tag );
79 $this->output( "Done.\n" );
83 // @codeCoverageIgnoreStart
84 $maintClass = DeleteTag
::class;
85 require_once RUN_MAINTENANCE_IF_MAIN
;
86 // @codeCoverageIgnoreEnd