MediaWiki REL1_35
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 global $wgUser;
54
55 # Change to current working directory
56 $oldCwd = getcwd();
57 chdir( $oldCwd );
58
59 # Options processing
60 $username = $this->getOption( 'u', false );
61 $reason = $this->getOption( 'r', '' );
62 $interval = $this->getOption( 'i', 0 );
63
64 if ( $username === false ) {
65 $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
66 } else {
67 $user = User::newFromName( $username );
68 }
69 if ( !$user ) {
70 $this->fatalError( "Invalid username" );
71 }
72 $wgUser = $user;
73
74 if ( $this->hasArg( 0 ) ) {
75 $file = fopen( $this->getArg( 0 ), 'r' );
76 } else {
77 $file = $this->getStdin();
78 }
79
80 # Setup
81 if ( !$file ) {
82 $this->fatalError( "Unable to read file, exiting" );
83 }
84
85 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
86
87 # Handle each entry
88 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
89 $line = trim( fgets( $file ) );
90 if ( $line == '' ) {
91 continue;
92 }
93 $title = Title::newFromText( $line );
94 if ( $title === null ) {
95 $this->output( "Invalid title '$line' on line $linenum\n" );
96 continue;
97 }
98 if ( !$title->exists() ) {
99 $this->output( "Skipping nonexistent page '$line'\n" );
100 continue;
101 }
102
103 $this->output( $title->getPrefixedText() );
104 if ( $title->getNamespace() == NS_FILE ) {
105 $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
106 $title, [ 'ignoreRedirect' => true ]
107 );
108 if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) {
109 $this->output( " FAILED to delete associated file... " );
110 }
111 }
112 $page = WikiPage::factory( $title );
113 $error = '';
114 $status = $page->doDeleteArticleReal(
115 $reason,
116 $user,
117 false,
118 null,
119 $error,
120 null,
121 [],
122 'delete',
123 true
124 );
125 if ( $status->isOK() ) {
126 $this->output( " Deleted!\n" );
127 } else {
128 $this->output( " FAILED to delete article\n" );
129 }
130
131 if ( $interval ) {
132 sleep( $interval );
133 }
134 $lbFactory->waitForReplication();
135 }
136 }
137}
138
139$maintClass = DeleteBatch::class;
140require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
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 newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:758
$maintClass
const NS_FILE
Definition Defines.php:76
$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