| 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 |
require_once __DIR__ . '/Maintenance.php'; |
| 27 |
|
| 28 |
|
| 29 |
use MediaWiki\StubObject\StubGlobalUser; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use MediaWiki\User\ActorMigration; |
| 32 |
use MediaWiki\User\User; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class DeleteDefaultMessages extends Maintenance { |
| 41 |
public function __construct() { |
| 42 |
parent::__construct(); |
| 43 |
$this->addDescription( 'Deletes all pages in the MediaWiki namespace' . |
| 44 |
' which were last edited by "MediaWiki default"' ); |
| 45 |
$this->addOption( 'dry-run', 'Perform a dry run, delete nothing' ); |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$services = $this->getServiceContainer(); |
| 50 |
|
| 51 |
$this->output( "Checking existence of old default messages..." ); |
| 52 |
$dbr = $this->getReplicaDB(); |
| 53 |
|
| 54 |
$userFactory = $services->getUserFactory(); |
| 55 |
$actorQuery = ActorMigration::newMigration() |
| 56 |
->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) ); |
| 57 |
|
| 58 |
$res = $dbr->newSelectQueryBuilder() |
| 59 |
->select( [ 'page_namespace', 'page_title' ] ) |
| 60 |
->from( 'page' ) |
| 61 |
->join( 'revision', null, 'page_latest=rev_id' ) |
| 62 |
->tables( $actorQuery['tables'] ) |
| 63 |
->where( [ |
| 64 |
'page_namespace' => NS_MEDIAWIKI, |
| 65 |
$actorQuery['conds'], |
| 66 |
] ) |
| 67 |
->joinConds( $actorQuery['joins'] ) |
| 68 |
->caller( __METHOD__ ) |
| 69 |
->fetchResultSet(); |
| 70 |
|
| 71 |
if ( $res->numRows() == 0 ) { |
| 72 |
|
| 73 |
$this->output( "done.\n" ); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$dryrun = $this->hasOption( 'dry-run' ); |
| 78 |
if ( $dryrun ) { |
| 79 |
foreach ( $res as $row ) { |
| 80 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 81 |
$this->output( "\n* [[$title]]" ); |
| 82 |
} |
| 83 |
$this->output( "\n\nRun again without --dry-run to delete these pages.\n" ); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
$user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] ); |
| 90 |
if ( !$user ) { |
| 91 |
$this->fatalError( "Invalid username" ); |
| 92 |
} |
| 93 |
$userGroupManager = $services->getUserGroupManager(); |
| 94 |
$userGroupManager->addUserToGroup( $user, 'bot' ); |
| 95 |
StubGlobalUser::setUser( $user ); |
| 96 |
|
| 97 |
|
| 98 |
$this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' ); |
| 99 |
$dbw = $this->getPrimaryDB(); |
| 100 |
|
| 101 |
$wikiPageFactory = $services->getWikiPageFactory(); |
| 102 |
$delPageFactory = $services->getDeletePageFactory(); |
| 103 |
|
| 104 |
foreach ( $res as $row ) { |
| 105 |
$this->waitForReplication(); |
| 106 |
$dbw->ping(); |
| 107 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 108 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 109 |
|
| 110 |
$delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' ); |
| 111 |
} |
| 112 |
|
| 113 |
$this->output( "done!\n", 'msg' ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
$maintClass = DeleteDefaultMessages::class; |
| 119 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 120 |
|