MediaWiki  1.23.8
deleteBatch.php
Go to the documentation of this file.
1 <?php
31 require_once __DIR__ . '/Maintenance.php';
32 
38 class DeleteBatch extends Maintenance {
39 
40  public function __construct() {
41  parent::__construct();
42  $this->mDescription = "Deletes a batch of pages";
43  $this->addOption( 'u', "User to perform deletion", false, true );
44  $this->addOption( 'r', "Reason to delete page", false, true );
45  $this->addOption( 'i', "Interval to sleep between deletions" );
46  $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
47  'If not given, stdin will be used.', false );
48  }
49 
50  public function execute() {
52 
53  # Change to current working directory
54  $oldCwd = getcwd();
55  chdir( $oldCwd );
56 
57  # Options processing
58  $username = $this->getOption( 'u', 'Delete page script' );
59  $reason = $this->getOption( 'r', '' );
60  $interval = $this->getOption( 'i', 0 );
61 
62  $user = User::newFromName( $username );
63  if ( !$user ) {
64  $this->error( "Invalid username", true );
65  }
66  $wgUser = $user;
67 
68  if ( $this->hasArg() ) {
69  $file = fopen( $this->getArg(), 'r' );
70  } else {
71  $file = $this->getStdin();
72  }
73 
74  # Setup
75  if ( !$file ) {
76  $this->error( "Unable to read file, exiting", true );
77  }
78 
79  $dbw = wfGetDB( DB_MASTER );
80 
81  # Handle each entry
82  for ( $linenum = 1; !feof( $file ); $linenum++ ) {
83  $line = trim( fgets( $file ) );
84  if ( $line == '' ) {
85  continue;
86  }
88  if ( is_null( $title ) ) {
89  $this->output( "Invalid title '$line' on line $linenum\n" );
90  continue;
91  }
92  if ( !$title->exists() ) {
93  $this->output( "Skipping nonexistent page '$line'\n" );
94  continue;
95  }
96 
97  $this->output( $title->getPrefixedText() );
98  $dbw->begin( __METHOD__ );
99  if ( $title->getNamespace() == NS_FILE ) {
100  $img = wfFindFile( $title );
101  if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
102  $this->output( " FAILED to delete associated file... " );
103  }
104  }
105  $page = WikiPage::factory( $title );
106  $error = '';
107  $success = $page->doDeleteArticle( $reason, false, 0, false, $error, $user );
108  $dbw->commit( __METHOD__ );
109  if ( $success ) {
110  $this->output( " Deleted!\n" );
111  } else {
112  $this->output( " FAILED to delete article\n" );
113  }
114 
115  if ( $interval ) {
116  sleep( $interval );
117  }
118  wfWaitForSlaves();
119  }
120  }
121 }
122 
123 $maintClass = "DeleteBatch";
124 require_once RUN_MAINTENANCE_IF_MAIN;
$wgUser
$wgUser
Definition: Setup.php:552
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:287
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
NS_FILE
const NS_FILE
Definition: Defines.php:85
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:389
Maintenance\hasArg
hasArg( $argId=0)
Does a given argument exist?
Definition: Maintenance.php:236
DeleteBatch\execute
execute()
Do the actual work.
Definition: deleteBatch.php:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
DeleteBatch\__construct
__construct()
Default constructor.
Definition: deleteBatch.php:40
DeleteBatch
Maintenance script to delete a batch of pages.
Definition: deleteBatch.php:38
$success
$success
Definition: Utf8Test.php:91
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3804
$line
$line
Definition: cdb.php:57
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$maintClass
$maintClass
Definition: deleteBatch.php:123
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
wfFindFile
wfFindFile( $title, $options=array())
Find a file.
Definition: GlobalFunctions.php:3702
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246