MediaWiki REL1_37
deleteBatch.php
Go to the documentation of this file.
1<?php
32
33require_once __DIR__ . '/Maintenance.php';
34
40class DeleteBatch extends Maintenance {
41
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Deletes a batch of pages' );
45 $this->addOption( 'u', "User to perform deletion", false, true );
46 $this->addOption( 'r', "Reason to delete page", false, true );
47 $this->addOption( 'i', "Interval to sleep between deletions" );
48 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
49 'If not given, stdin will be used.', false );
50 }
51
52 public function execute() {
53 # Change to current working directory
54 $oldCwd = getcwd();
55 chdir( $oldCwd );
56
57 # Options processing
58 $username = $this->getOption( 'u', false );
59 $reason = $this->getOption( 'r', '' );
60 $interval = $this->getOption( 'i', 0 );
61
62 if ( $username === false ) {
63 $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
64 } else {
65 $user = User::newFromName( $username );
66 }
67 if ( !$user ) {
68 $this->fatalError( "Invalid username" );
69 }
71
72 if ( $this->hasArg( 0 ) ) {
73 $file = fopen( $this->getArg( 0 ), 'r' );
74 } else {
75 $file = $this->getStdin();
76 }
77
78 # Setup
79 if ( !$file ) {
80 $this->fatalError( "Unable to read file, exiting" );
81 }
82
83 $services = MediaWikiServices::getInstance();
84 $lbFactory = $services->getDBLoadBalancerFactory();
85 $wikiPageFactory = $services->getWikiPageFactory();
86 $repoGroup = $services->getRepoGroup();
87
88 # Handle each entry
89 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
90 $line = trim( fgets( $file ) );
91 if ( $line == '' ) {
92 continue;
93 }
94 $title = Title::newFromText( $line );
95 if ( $title === null ) {
96 $this->output( "Invalid title '$line' on line $linenum\n" );
97 continue;
98 }
99 if ( !$title->exists() ) {
100 $this->output( "Skipping nonexistent page '$line'\n" );
101 continue;
102 }
103
104 $this->output( $title->getPrefixedText() );
105 if ( $title->getNamespace() === NS_FILE ) {
106 $img = $repoGroup->findFile(
107 $title, [ 'ignoreRedirect' => true ]
108 );
109 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
110 $this->output( " FAILED to delete associated file..." );
111 }
112 }
113 $page = $wikiPageFactory->newFromTitle( $title );
114 $error = '';
115 $status = $page->doDeleteArticleReal(
116 $reason,
117 $user,
118 false,
119 null,
120 $error,
121 null,
122 [],
123 'delete',
124 true
125 );
126 if ( $status->isOK() ) {
127 $this->output( " Deleted!\n" );
128 } else {
129 $this->output( " FAILED to delete article\n" );
130 }
131
132 if ( $interval ) {
133 sleep( $interval );
134 }
135 $lbFactory->waitForReplication();
136 }
137 }
138}
139
140$maintClass = DeleteBatch::class;
141require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition Defines.php:70
Maintenance script to delete a batch of pages.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasArg( $argId=0)
Does a given argument exist?
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromName( $name, $validate='valid')
Definition User.php:607
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:810
$maintClass
$line
Definition mcc.php:119
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42