deleteBatch.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Deletes a batch of pages.
  4. * Usage: php deleteBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
  5. * where
  6. * [listfile] is a file where each line contains the title of a page to be
  7. * deleted, standard input is used if listfile is not given.
  8. * <user> is the username
  9. * <reason> is the delete reason
  10. * <interval> is the number of seconds to sleep for after each delete
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @ingroup Maintenance
  29. */
  30. require_once __DIR__ . '/Maintenance.php';
  31. /**
  32. * Maintenance script to delete a batch of pages.
  33. *
  34. * @ingroup Maintenance
  35. */
  36. class DeleteBatch extends Maintenance {
  37. public function __construct() {
  38. parent::__construct();
  39. $this->addDescription( 'Deletes a batch of pages' );
  40. $this->addOption( 'u', "User to perform deletion", false, true );
  41. $this->addOption( 'r', "Reason to delete page", false, true );
  42. $this->addOption( 'i', "Interval to sleep between deletions" );
  43. $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
  44. 'If not given, stdin will be used.', false );
  45. }
  46. public function execute() {
  47. global $wgUser;
  48. # Change to current working directory
  49. $oldCwd = getcwd();
  50. chdir( $oldCwd );
  51. # Options processing
  52. $username = $this->getOption( 'u', false );
  53. $reason = $this->getOption( 'r', '' );
  54. $interval = $this->getOption( 'i', 0 );
  55. if ( $username === false ) {
  56. $user = User::newSystemUser( 'Delete page script', [ 'steal' => true ] );
  57. } else {
  58. $user = User::newFromName( $username );
  59. }
  60. if ( !$user ) {
  61. $this->error( "Invalid username", true );
  62. }
  63. $wgUser = $user;
  64. if ( $this->hasArg() ) {
  65. $file = fopen( $this->getArg(), 'r' );
  66. } else {
  67. $file = $this->getStdin();
  68. }
  69. # Setup
  70. if ( !$file ) {
  71. $this->error( "Unable to read file, exiting", true );
  72. }
  73. $dbw = $this->getDB( DB_MASTER );
  74. # Handle each entry
  75. // @codingStandardsIgnoreStart Ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
  76. for ( $linenum = 1; !feof( $file ); $linenum++ ) {
  77. // @codingStandardsIgnoreEnd
  78. $line = trim( fgets( $file ) );
  79. if ( $line == '' ) {
  80. continue;
  81. }
  82. $title = Title::newFromText( $line );
  83. if ( is_null( $title ) ) {
  84. $this->output( "Invalid title '$line' on line $linenum\n" );
  85. continue;
  86. }
  87. if ( !$title->exists() ) {
  88. $this->output( "Skipping nonexistent page '$line'\n" );
  89. continue;
  90. }
  91. $this->output( $title->getPrefixedText() );
  92. if ( $title->getNamespace() == NS_FILE ) {
  93. $img = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
  94. if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
  95. $this->output( " FAILED to delete associated file... " );
  96. }
  97. }
  98. $page = WikiPage::factory( $title );
  99. $error = '';
  100. $success = $page->doDeleteArticle( $reason, false, 0, true, $error, $user );
  101. if ( $success ) {
  102. $this->output( " Deleted!\n" );
  103. } else {
  104. $this->output( " FAILED to delete article\n" );
  105. }
  106. if ( $interval ) {
  107. sleep( $interval );
  108. }
  109. wfWaitForSlaves();
  110. }
  111. }
  112. }
  113. $maintClass = "DeleteBatch";
  114. require_once RUN_MAINTENANCE_IF_MAIN;