Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.96% |
48 / 49 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DeleteDefaultMessages | |
97.96% |
48 / 49 |
|
50.00% |
1 / 2 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
97.78% |
44 / 45 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Deletes all pages in the MediaWiki namespace which were last edited by |
| 4 | * "MediaWiki default". |
| 5 | * |
| 6 | * @license GPL-2.0-or-later |
| 7 | * @file |
| 8 | * @ingroup Maintenance |
| 9 | */ |
| 10 | |
| 11 | // @codeCoverageIgnoreStart |
| 12 | require_once __DIR__ . '/Maintenance.php'; |
| 13 | // @codeCoverageIgnoreEnd |
| 14 | |
| 15 | use MediaWiki\Maintenance\Maintenance; |
| 16 | use MediaWiki\StubObject\StubGlobalUser; |
| 17 | use MediaWiki\Title\Title; |
| 18 | use MediaWiki\User\ActorMigration; |
| 19 | use MediaWiki\User\User; |
| 20 | |
| 21 | /** |
| 22 | * Maintenance script that deletes all pages in the MediaWiki namespace |
| 23 | * which were last edited by "MediaWiki default". |
| 24 | * |
| 25 | * @ingroup Maintenance |
| 26 | */ |
| 27 | class DeleteDefaultMessages extends Maintenance { |
| 28 | public function __construct() { |
| 29 | parent::__construct(); |
| 30 | $this->addDescription( 'Deletes all pages in the MediaWiki namespace' . |
| 31 | ' which were last edited by "MediaWiki default"' ); |
| 32 | $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' ); |
| 33 | } |
| 34 | |
| 35 | public function execute() { |
| 36 | $services = $this->getServiceContainer(); |
| 37 | |
| 38 | $this->output( "Checking existence of old default messages..." ); |
| 39 | $dbr = $this->getReplicaDB(); |
| 40 | |
| 41 | $userFactory = $services->getUserFactory(); |
| 42 | $actorQuery = ActorMigration::newMigration() |
| 43 | ->getWhere( $dbr, 'rev_user', $userFactory->newFromName( 'MediaWiki default' ) ); |
| 44 | |
| 45 | $res = $dbr->newSelectQueryBuilder() |
| 46 | ->select( [ 'page_namespace', 'page_title' ] ) |
| 47 | ->from( 'page' ) |
| 48 | ->join( 'revision', null, 'page_latest=rev_id' ) |
| 49 | ->tables( $actorQuery['tables'] ) |
| 50 | ->where( [ |
| 51 | 'page_namespace' => NS_MEDIAWIKI, |
| 52 | $actorQuery['conds'], |
| 53 | ] ) |
| 54 | ->joinConds( $actorQuery['joins'] ) |
| 55 | ->caller( __METHOD__ ) |
| 56 | ->fetchResultSet(); |
| 57 | |
| 58 | if ( $res->numRows() == 0 ) { |
| 59 | // No more messages left |
| 60 | $this->output( "done.\n" ); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $dryrun = $this->hasOption( 'dry-run' ); |
| 65 | if ( $dryrun ) { |
| 66 | foreach ( $res as $row ) { |
| 67 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 68 | $this->output( "\n* [[$title]]" ); |
| 69 | } |
| 70 | $this->output( "\n\nRun again without --dry-run to delete these pages.\n" ); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Deletions will be made by $user temporarily added to the bot group |
| 75 | // in order to hide it in RecentChanges. |
| 76 | $user = User::newSystemUser( 'MediaWiki default', [ 'steal' => true ] ); |
| 77 | if ( !$user ) { |
| 78 | $this->fatalError( "Invalid username" ); |
| 79 | } |
| 80 | $userGroupManager = $services->getUserGroupManager(); |
| 81 | $userGroupManager->addUserToGroup( $user, 'bot' ); |
| 82 | StubGlobalUser::setUser( $user ); |
| 83 | |
| 84 | // Handle deletion |
| 85 | $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' ); |
| 86 | $dbw = $this->getPrimaryDB(); |
| 87 | |
| 88 | $wikiPageFactory = $services->getWikiPageFactory(); |
| 89 | $delPageFactory = $services->getDeletePageFactory(); |
| 90 | |
| 91 | foreach ( $res as $row ) { |
| 92 | $this->waitForReplication(); |
| 93 | $dbw->ping(); |
| 94 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 95 | $page = $wikiPageFactory->newFromTitle( $title ); |
| 96 | // FIXME: Deletion failures should be reported, not silently ignored. |
| 97 | $delPageFactory->newDeletePage( $page, $user )->deleteUnsafe( 'No longer required' ); |
| 98 | } |
| 99 | |
| 100 | $this->output( "done!\n", 'msg' ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // @codeCoverageIgnoreStart |
| 105 | $maintClass = DeleteDefaultMessages::class; |
| 106 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 107 | // @codeCoverageIgnoreEnd |