wiki.techinc.nl/maintenance/deleteTag.php
Bartosz Dziewoński 4108bbe1af Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.

Thoughts on the previously commonly used methods:

- $status->getMessage( false, false, 'en' )->text()
  Almost the same as the new output, but it allows on-wiki message
  overrides, and if there is more than one error, it prefixes each
  line with a '*' (like a wikitext list).

- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
  As above, but these forms do not expand parser functions
  such as {{GENDER:}}.

- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
  These forms output the message keys instead of the message text,
  which is not very human-readable.

The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.

Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-12 00:07:02 +02:00

81 lines
2.1 KiB
PHP

<?php
/**
* Remove a revision tag from edits and log entries it was applied to.
* @see bug T75181
*/
use MediaWiki\Storage\NameTableAccessException;
require_once __DIR__ . '/Maintenance.php';
class DeleteTag extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Deletes a change tag' );
$this->addArg( 'tag name', 'Name of the tag to delete' );
$this->setBatchSize( 500 );
}
public function execute() {
$dbw = $this->getPrimaryDB();
$services = $this->getServiceContainer();
$defStore = $services->getChangeTagDefStore();
$tag = $this->getArg( 0 );
try {
$tagId = $defStore->getId( $tag );
} catch ( NameTableAccessException $ex ) {
$this->fatalError( "Tag '$tag' not found" );
}
$status = ChangeTags::canDeleteTag( $tag, null, ChangeTags::BYPASS_MAX_USAGE_CHECK );
if ( !$status->isOK() ) {
$this->fatalError( $status );
}
$this->output( "Deleting tag '$tag'...\n" );
// Make the tag impossible to add by users while we're deleting it and drop the
// usage counter to zero
$dbw->newUpdateQueryBuilder()
->update( 'change_tag_def' )
->set( [
'ctd_user_defined' => 0,
'ctd_count' => 0,
] )
->where( [ 'ctd_id' => $tagId ] )
->caller( __METHOD__ )->execute();
ChangeTags::purgeTagCacheAll();
// Iterate over change_tag, deleting rows in batches
$count = 0;
do {
$ids = $dbw->newSelectQueryBuilder()
->select( 'ct_id' )
->from( 'change_tag' )
->where( [ 'ct_tag_id' => $tagId ] )
->limit( $this->getBatchSize() )
->caller( __METHOD__ )
->fetchFieldValues();
if ( !$ids ) {
break;
}
$dbw->newDeleteQueryBuilder()
->deleteFrom( 'change_tag' )
->where( [ 'ct_id' => $ids ] )
->caller( __METHOD__ )->execute();
$count += $dbw->affectedRows();
$this->output( "$count\n" );
$this->waitForReplication();
} while ( true );
$this->output( "The tag has been removed from $count revisions, deleting the tag itself...\n" );
ChangeTags::deleteTagEverywhere( $tag );
$this->output( "Done.\n" );
}
}
$maintClass = DeleteTag::class;
require_once RUN_MAINTENANCE_IF_MAIN;