nukeNS.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Remove pages with only 1 revision from the MediaWiki namespace, without
  4. * flooding recent changes, delete logs, etc.
  5. * Irreversible (can't use standard undelete) and does not update link tables
  6. *
  7. * This is mainly useful to run before maintenance/update.php when upgrading
  8. * to 1.9, to prevent flooding recent changes/deletion logs. It's intended
  9. * to be conservative, so it's possible that a few entries will be left for
  10. * deletion by the upgrade script. It's also possible that it hasn't been
  11. * tested thouroughly enough, and will delete something it shouldn't; so
  12. * back up your DB if there's anything in the MediaWiki that is important to
  13. * you.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  28. * http://www.gnu.org/copyleft/gpl.html
  29. *
  30. * @file
  31. * @ingroup Maintenance
  32. * @author Steve Sanbeg
  33. * based on nukePage by Rob Church
  34. */
  35. require_once __DIR__ . '/Maintenance.php';
  36. /**
  37. * Maintenance script that removes pages with only one revision from the
  38. * MediaWiki namespace.
  39. *
  40. * @ingroup Maintenance
  41. */
  42. class NukeNS extends Maintenance {
  43. public function __construct() {
  44. parent::__construct();
  45. $this->addDescription( 'Remove pages with only 1 revision from any namespace' );
  46. $this->addOption( 'delete', "Actually delete the page" );
  47. $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
  48. $this->addOption( 'all', 'Delete everything regardless of revision count' );
  49. }
  50. public function execute() {
  51. $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
  52. $delete = $this->getOption( 'delete', false );
  53. $all = $this->getOption( 'all', false );
  54. $dbw = $this->getDB( DB_MASTER );
  55. $this->beginTransaction( $dbw, __METHOD__ );
  56. $tbl_pag = $dbw->tableName( 'page' );
  57. $tbl_rev = $dbw->tableName( 'revision' );
  58. $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
  59. $n_deleted = 0;
  60. foreach ( $res as $row ) {
  61. // echo "$ns_name:".$row->page_title, "\n";
  62. $title = Title::makeTitle( $ns, $row->page_title );
  63. $id = $title->getArticleID();
  64. // Get corresponding revisions
  65. $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
  66. $revs = [];
  67. foreach ( $res2 as $row2 ) {
  68. $revs[] = $row2->rev_id;
  69. }
  70. $count = count( $revs );
  71. // skip anything that looks modified (i.e. multiple revs)
  72. if ( $all || $count == 1 ) {
  73. # echo $title->getPrefixedText(), "\t", $count, "\n";
  74. $this->output( "delete: " . $title->getPrefixedText() . "\n" );
  75. // as much as I hate to cut & paste this, it's a little different, and
  76. // I already have the id & revs
  77. if ( $delete ) {
  78. $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
  79. $this->commitTransaction( $dbw, __METHOD__ );
  80. // Delete revisions as appropriate
  81. $child = $this->runChild( 'NukePage', 'nukePage.php' );
  82. $child->deleteRevisions( $revs );
  83. $this->purgeRedundantText( true );
  84. $n_deleted++;
  85. }
  86. } else {
  87. $this->output( "skip: " . $title->getPrefixedText() . "\n" );
  88. }
  89. }
  90. $this->commitTransaction( $dbw, __METHOD__ );
  91. if ( $n_deleted > 0 ) {
  92. # update statistics - better to decrement existing count, or just count
  93. # the page table?
  94. $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
  95. $pages -= $n_deleted;
  96. $dbw->update(
  97. 'site_stats',
  98. [ 'ss_total_pages' => $pages ],
  99. [ 'ss_row_id' => 1 ],
  100. __METHOD__
  101. );
  102. }
  103. if ( !$delete ) {
  104. $this->output( "To update the database, run the script with the --delete option.\n" );
  105. }
  106. }
  107. }
  108. $maintClass = "NukeNS";
  109. require_once RUN_MAINTENANCE_IF_MAIN;