MediaWiki  master
deleteBatch.php
Go to the documentation of this file.
1 <?php
32 
33 require_once __DIR__ . '/Maintenance.php';
34 
40 class 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 {
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  # Handle each entry
86  for ( $linenum = 1; !feof( $file ); $linenum++ ) {
87  $line = trim( fgets( $file ) );
88  if ( $line == '' ) {
89  continue;
90  }
92  if ( is_null( $title ) ) {
93  $this->output( "Invalid title '$line' on line $linenum\n" );
94  continue;
95  }
96  if ( !$title->exists() ) {
97  $this->output( "Skipping nonexistent page '$line'\n" );
98  continue;
99  }
100 
101  $this->output( $title->getPrefixedText() );
102  if ( $title->getNamespace() == NS_FILE ) {
103  $img = MediaWikiServices::getInstance()->getRepoGroup()->findFile(
104  $title, [ 'ignoreRedirect' => true ]
105  );
106  if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
107  $this->output( " FAILED to delete associated file... " );
108  }
109  }
110  $page = WikiPage::factory( $title );
111  $error = '';
112  $success = $page->doDeleteArticle( $reason, false, null, null, $error, $user, true );
113  if ( $success ) {
114  $this->output( " Deleted!\n" );
115  } else {
116  $this->output( " FAILED to delete article\n" );
117  }
118 
119  if ( $interval ) {
120  sleep( $interval );
121  }
122  wfWaitForSlaves();
123  }
124  }
125 }
126 
128 require_once RUN_MAINTENANCE_IF_MAIN;
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
getArg( $argId=0, $default=null)
Get an argument.
$success
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getOption( $name, $default=null)
Get an option, or return the default.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
hasArg( $argId=0)
Does a given argument exist?
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
addDescription( $text)
Set the description text.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
addArg( $arg, $description, $required=true)
Add some args that are needed.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
const NS_FILE
Definition: Defines.php:66
output( $out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:780
Maintenance script to delete a batch of pages.
Definition: deleteBatch.php:40
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$line
Definition: cdb.php:59
$maintClass
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getStdin( $len=null)
Return input from stdin.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:594
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1473
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:831
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319