rollbackEdits.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Rollback all edits by a given user or IP provided they're the most
  4. * recent edit (just like real rollback)
  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 to rollback all edits by a given user or IP provided
  27. * they're the most recent edit.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class RollbackEdits extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription(
  35. "Rollback all edits by a given user or IP provided they're the most recent edit" );
  36. $this->addOption(
  37. 'titles',
  38. 'A list of titles, none means all titles where the given user is the most recent',
  39. false,
  40. true
  41. );
  42. $this->addOption( 'user', 'A user or IP to rollback all edits for', true, true );
  43. $this->addOption( 'summary', 'Edit summary to use', false, true );
  44. $this->addOption( 'bot', 'Mark the edits as bot' );
  45. }
  46. public function execute() {
  47. $user = $this->getOption( 'user' );
  48. $username = User::isIP( $user ) ? $user : User::getCanonicalName( $user );
  49. if ( !$username ) {
  50. $this->error( 'Invalid username', true );
  51. }
  52. $bot = $this->hasOption( 'bot' );
  53. $summary = $this->getOption( 'summary', $this->mSelf . ' mass rollback' );
  54. $titles = [];
  55. $results = [];
  56. if ( $this->hasOption( 'titles' ) ) {
  57. foreach ( explode( '|', $this->getOption( 'titles' ) ) as $title ) {
  58. $t = Title::newFromText( $title );
  59. if ( !$t ) {
  60. $this->error( 'Invalid title, ' . $title );
  61. } else {
  62. $titles[] = $t;
  63. }
  64. }
  65. } else {
  66. $titles = $this->getRollbackTitles( $user );
  67. }
  68. if ( !$titles ) {
  69. $this->output( 'No suitable titles to be rolled back' );
  70. return;
  71. }
  72. $doer = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
  73. foreach ( $titles as $t ) {
  74. $page = WikiPage::factory( $t );
  75. $this->output( 'Processing ' . $t->getPrefixedText() . '... ' );
  76. if ( !$page->commitRollback( $user, $summary, $bot, $results, $doer ) ) {
  77. $this->output( "done\n" );
  78. } else {
  79. $this->output( "failed\n" );
  80. }
  81. }
  82. }
  83. /**
  84. * Get all pages that should be rolled back for a given user
  85. * @param string $user A name to check against rev_user_text
  86. * @return array
  87. */
  88. private function getRollbackTitles( $user ) {
  89. $dbr = $this->getDB( DB_SLAVE );
  90. $titles = [];
  91. $results = $dbr->select(
  92. [ 'page', 'revision' ],
  93. [ 'page_namespace', 'page_title' ],
  94. [ 'page_latest = rev_id', 'rev_user_text' => $user ],
  95. __METHOD__
  96. );
  97. foreach ( $results as $row ) {
  98. $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
  99. }
  100. return $titles;
  101. }
  102. }
  103. $maintClass = 'RollbackEdits';
  104. require_once RUN_MAINTENANCE_IF_MAIN;