| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use MediaWiki\StubObject\StubGlobalUser; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\User; |
| 34 |
|
| 35 |
|
| 36 |
require_once __DIR__ . '/Maintenance.php'; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class DeleteBatch extends Maintenance { |
| 45 |
|
| 46 |
public function __construct() { |
| 47 |
parent::__construct(); |
| 48 |
$this->addDescription( 'Deletes a batch of pages' ); |
| 49 |
$this->addOption( 'u', 'User to perform deletion', false, true ); |
| 50 |
$this->addOption( 'r', 'Reason to delete page', false, true ); |
| 51 |
$this->addOption( 'i', 'Interval to sleep (in seconds) between deletions' ); |
| 52 |
$this->addOption( 'by-id', 'Delete by page ID instead of by page name', false, false ); |
| 53 |
$this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' . |
| 54 |
'If not given, stdin will be used.', false ); |
| 55 |
} |
| 56 |
|
| 57 |
public function execute() { |
| 58 |
|
| 59 |
$oldCwd = getcwd(); |
| 60 |
chdir( $oldCwd ); |
| 61 |
|
| 62 |
|
| 63 |
$username = $this->getOption( 'u', false ); |
| 64 |
$reason = $this->getOption( 'r', '' ); |
| 65 |
$interval = $this->getOption( 'i', 0 ); |
| 66 |
$byId = $this->hasOption( 'by-id' ); |
| 67 |
|
| 68 |
if ( $username === false ) { |
| 69 |
$user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] ); |
| 70 |
} else { |
| 71 |
$user = User::newFromName( $username ); |
| 72 |
} |
| 73 |
if ( !$user ) { |
| 74 |
$this->fatalError( "Invalid username" ); |
| 75 |
} |
| 76 |
StubGlobalUser::setUser( $user ); |
| 77 |
|
| 78 |
if ( $this->hasArg( 0 ) ) { |
| 79 |
$file = fopen( $this->getArg( 0 ), 'r' ); |
| 80 |
} else { |
| 81 |
$file = $this->getStdin(); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
if ( !$file ) { |
| 86 |
$this->fatalError( "Unable to read file, exiting" ); |
| 87 |
} |
| 88 |
|
| 89 |
$services = $this->getServiceContainer(); |
| 90 |
$wikiPageFactory = $services->getWikiPageFactory(); |
| 91 |
$repoGroup = $services->getRepoGroup(); |
| 92 |
$delPageFactory = $services->getDeletePageFactory(); |
| 93 |
|
| 94 |
|
| 95 |
for ( $linenum = 1; !feof( $file ); $linenum++ ) { |
| 96 |
$line = trim( fgets( $file ) ); |
| 97 |
if ( $line == '' ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
if ( $byId === false ) { |
| 101 |
$target = Title::newFromText( $line ); |
| 102 |
if ( $target === null ) { |
| 103 |
$this->output( "Invalid title '$line' on line $linenum\n" ); |
| 104 |
continue; |
| 105 |
} |
| 106 |
if ( !$target->exists() ) { |
| 107 |
$this->output( "Skipping nonexistent page '$line'\n" ); |
| 108 |
continue; |
| 109 |
} |
| 110 |
} else { |
| 111 |
$target = Title::newFromID( (int)$line ); |
| 112 |
if ( $target === null ) { |
| 113 |
$this->output( "Invalid page ID '$line' on line $linenum\n" ); |
| 114 |
continue; |
| 115 |
} |
| 116 |
if ( !$target->exists() ) { |
| 117 |
$this->output( "Skipping nonexistent page ID '$line'\n" ); |
| 118 |
continue; |
| 119 |
} |
| 120 |
} |
| 121 |
if ( $target->getNamespace() === NS_FILE ) { |
| 122 |
$img = $repoGroup->findFile( |
| 123 |
$target, [ 'ignoreRedirect' => true ] |
| 124 |
); |
| 125 |
if ( $img && $img->isLocal() && !$img->deleteFile( $reason, $user ) ) { |
| 126 |
$this->output( " FAILED to delete associated file..." ); |
| 127 |
} |
| 128 |
} |
| 129 |
$page = $wikiPageFactory->newFromTitle( $target ); |
| 130 |
$delPage = $delPageFactory->newDeletePage( $page, $user ); |
| 131 |
$status = $delPage |
| 132 |
->forceImmediate( true ) |
| 133 |
->deleteUnsafe( $reason ); |
| 134 |
|
| 135 |
if ( $status->isOK() ) { |
| 136 |
$this->output( " Deleted!\n" ); |
| 137 |
} else { |
| 138 |
$this->output( " FAILED to delete article\n" ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $interval ) { |
| 142 |
sleep( $interval ); |
| 143 |
} |
| 144 |
$this->waitForReplication(); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
$maintClass = DeleteBatch::class; |
| 151 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 152 |
|