Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / deleteTag.php
blobe1f6f186f64cb2953529450643802e34cbb12f15
1 <?php
3 /**
4 * Remove a revision tag from edits and log entries it was applied to.
5 * @see bug T75181
6 */
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 );
29 try {
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' )
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();
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();
65 if ( !$ids ) {
66 break;
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" );
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