MediaWiki master
deleteTag.php
Go to the documentation of this file.
1<?php
2
10
11require_once __DIR__ . '/Maintenance.php';
12
13class 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->getPrimaryDB();
23 $services = $this->getServiceContainer();
24 $defStore = $services->getChangeTagDefStore();
25
26 $tag = $this->getArg( 0 );
27 try {
28 $tagId = $defStore->getId( $tag );
29 } catch ( NameTableAccessException $ex ) {
30 $this->fatalError( "Tag '$tag' not found" );
31 }
32
34 if ( !$status->isOK() ) {
35 $message = $status->getHTML( false, false, 'en' );
36 $this->fatalError( Sanitizer::stripAllTags( $message ) );
37 }
38
39 $this->output( "Deleting tag '$tag'...\n" );
40
41 // Make the tag impossible to add by users while we're deleting it and drop the
42 // usage counter to zero
43 $dbw->newUpdateQueryBuilder()
44 ->update( 'change_tag_def' )
45 ->set( [
46 'ctd_user_defined' => 0,
47 'ctd_count' => 0,
48 ] )
49 ->where( [ 'ctd_id' => $tagId ] )
50 ->caller( __METHOD__ )->execute();
52
53 // Iterate over change_tag, deleting rows in batches
54 $count = 0;
55 do {
56 $ids = $dbw->newSelectQueryBuilder()
57 ->select( 'ct_id' )
58 ->from( 'change_tag' )
59 ->where( [ 'ct_tag_id' => $tagId ] )
60 ->limit( $this->getBatchSize() )
61 ->caller( __METHOD__ )
62 ->fetchFieldValues();
63
64 if ( !$ids ) {
65 break;
66 }
67 $dbw->newDeleteQueryBuilder()
68 ->deleteFrom( 'change_tag' )
69 ->where( [ 'ct_id' => $ids ] )
70 ->caller( __METHOD__ )->execute();
71 $count += $dbw->affectedRows();
72 $this->output( "$count\n" );
73 $this->waitForReplication();
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;
83require_once RUN_MAINTENANCE_IF_MAIN;
const BYPASS_MAX_USAGE_CHECK
Flag for canDeleteTag().
static purgeTagCacheAll()
Invalidates the short-term cache of defined tags used by the list*DefinedTags functions,...
static canDeleteTag( $tag, Authority $performer=null, int $flags=0)
Is it OK to allow the user to delete this tag?
static deleteTagEverywhere( $tag)
Permanently removes all traces of a tag from the DB.
__construct()
Default constructor.
Definition deleteTag.php:14
execute()
Do the actual work.
Definition deleteTag.php:21
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.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Exception representing a failure to look up a row from a name table.
$maintClass
Definition deleteTag.php:82