deleteDefaultMessages.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Deletes all pages in the MediaWiki namespace which were last edited by
  4. * "MediaWiki default".
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that deletes all pages in the MediaWiki namespace
  27. * which were last edited by "MediaWiki default".
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class DeleteDefaultMessages extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription( 'Deletes all pages in the MediaWiki namespace' .
  35. ' which were last edited by "MediaWiki default"' );
  36. $this->addOption( 'dry-run', 'Perform a dry run, delete nothing' );
  37. }
  38. public function execute() {
  39. global $wgUser;
  40. $this->output( "Checking existence of old default messages..." );
  41. $dbr = $this->getDB( DB_REPLICA );
  42. $actorQuery = ActorMigration::newMigration()
  43. ->getWhere( $dbr, 'rev_user', User::newFromName( 'MediaWiki default' ) );
  44. $res = $dbr->select(
  45. [ 'page', 'revision' ] + $actorQuery['tables'],
  46. [ 'page_namespace', 'page_title' ],
  47. [
  48. 'page_namespace' => NS_MEDIAWIKI,
  49. $actorQuery['conds'],
  50. ],
  51. __METHOD__,
  52. [],
  53. [ 'revision' => [ 'JOIN', 'page_latest=rev_id' ] ] + $actorQuery['joins']
  54. );
  55. if ( $dbr->numRows( $res ) == 0 ) {
  56. // No more messages left
  57. $this->output( "done.\n" );
  58. return;
  59. }
  60. $dryrun = $this->hasOption( 'dry-run' );
  61. if ( $dryrun ) {
  62. foreach ( $res as $row ) {
  63. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  64. $this->output( "\n* [[$title]]" );
  65. }
  66. $this->output( "\n\nRun again without --dry-run to delete these pages.\n" );
  67. return;
  68. }
  69. // Deletions will be made by $user temporarly added to the bot group
  70. // in order to hide it in RecentChanges.
  71. $user = User::newFromName( 'MediaWiki default' );
  72. if ( !$user ) {
  73. $this->fatalError( "Invalid username" );
  74. }
  75. $user->addGroup( 'bot' );
  76. $wgUser = $user;
  77. // Handle deletion
  78. $this->output( "\n...deleting old default messages (this may take a long time!)...", 'msg' );
  79. $dbw = $this->getDB( DB_MASTER );
  80. foreach ( $res as $row ) {
  81. wfWaitForSlaves();
  82. $dbw->ping();
  83. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  84. $page = WikiPage::factory( $title );
  85. $error = ''; // Passed by ref
  86. // FIXME: Deletion failures should be reported, not silently ignored.
  87. $page->doDeleteArticle( 'No longer required', false, 0, true, $error, $user );
  88. }
  89. $this->output( "done!\n", 'msg' );
  90. }
  91. }
  92. $maintClass = DeleteDefaultMessages::class;
  93. require_once RUN_MAINTENANCE_IF_MAIN;