deleteRevision.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Delete one or more revisions by moving them to the archive table.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script that deletes one or more revisions by moving them
  26. * to the archive table.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class DeleteRevision extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Delete one or more revisions by moving them to the archive table' );
  34. }
  35. public function execute() {
  36. if ( count( $this->mArgs ) == 0 ) {
  37. $this->error( "No revisions specified", true );
  38. }
  39. $this->output( "Deleting revision(s) " . implode( ',', $this->mArgs ) .
  40. " from " . wfWikiID() . "...\n" );
  41. $dbw = $this->getDB( DB_MASTER );
  42. $affected = 0;
  43. foreach ( $this->mArgs as $revID ) {
  44. $dbw->insertSelect( 'archive', [ 'page', 'revision' ],
  45. [
  46. 'ar_namespace' => 'page_namespace',
  47. 'ar_title' => 'page_title',
  48. 'ar_page_id' => 'page_id',
  49. 'ar_comment' => 'rev_comment',
  50. 'ar_user' => 'rev_user',
  51. 'ar_user_text' => 'rev_user_text',
  52. 'ar_timestamp' => 'rev_timestamp',
  53. 'ar_minor_edit' => 'rev_minor_edit',
  54. 'ar_rev_id' => 'rev_id',
  55. 'ar_text_id' => 'rev_text_id',
  56. 'ar_deleted' => 'rev_deleted',
  57. 'ar_len' => 'rev_len',
  58. ],
  59. [
  60. 'rev_id' => $revID,
  61. 'page_id = rev_page'
  62. ],
  63. __METHOD__
  64. );
  65. if ( !$dbw->affectedRows() ) {
  66. $this->output( "Revision $revID not found\n" );
  67. } else {
  68. $affected += $dbw->affectedRows();
  69. $pageID = $dbw->selectField(
  70. 'revision',
  71. 'rev_page',
  72. [ 'rev_id' => $revID ],
  73. __METHOD__
  74. );
  75. $pageLatest = $dbw->selectField(
  76. 'page',
  77. 'page_latest',
  78. [ 'page_id' => $pageID ],
  79. __METHOD__
  80. );
  81. $dbw->delete( 'revision', [ 'rev_id' => $revID ] );
  82. if ( $pageLatest == $revID ) {
  83. // Database integrity
  84. $newLatest = $dbw->selectField(
  85. 'revision',
  86. 'rev_id',
  87. [ 'rev_page' => $pageID ],
  88. __METHOD__,
  89. [ 'ORDER BY' => 'rev_timestamp DESC' ]
  90. );
  91. $dbw->update(
  92. 'page',
  93. [ 'page_latest' => $newLatest ],
  94. [ 'page_id' => $pageID ],
  95. __METHOD__
  96. );
  97. }
  98. }
  99. }
  100. $this->output( "Deleted $affected revisions\n" );
  101. }
  102. }
  103. $maintClass = "DeleteRevision";
  104. require_once RUN_MAINTENANCE_IF_MAIN;