cleanupWatchlist.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Remove broken, unparseable titles in the watchlist table.
  4. *
  5. * Usage: php cleanupWatchlist.php [--fix]
  6. * Options:
  7. * --fix Actually remove entries; without will only report.
  8. *
  9. * Copyright © 2005,2006 Brion Vibber <brion@pobox.com>
  10. * https://www.mediawiki.org/
  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. * @author Brion Vibber <brion at pobox.com>
  29. * @ingroup Maintenance
  30. */
  31. require_once __DIR__ . '/cleanupTable.inc';
  32. /**
  33. * Maintenance script to remove broken, unparseable titles in the watchlist table.
  34. *
  35. * @ingroup Maintenance
  36. */
  37. class WatchlistCleanup extends TableCleanup {
  38. protected $defaultParams = [
  39. 'table' => 'watchlist',
  40. 'index' => [ 'wl_user', 'wl_namespace', 'wl_title' ],
  41. 'conds' => [],
  42. 'callback' => 'processRow'
  43. ];
  44. public function __construct() {
  45. parent::__construct();
  46. $this->addDescription( 'Script to remove broken, unparseable titles in the Watchlist' );
  47. $this->addOption( 'fix', 'Actually remove entries; without will only report.' );
  48. }
  49. function execute() {
  50. if ( !$this->hasOption( 'fix' ) ) {
  51. $this->output( "Dry run only: use --fix to enable updates\n" );
  52. }
  53. parent::execute();
  54. }
  55. protected function processRow( $row ) {
  56. global $wgContLang;
  57. $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
  58. $display = $current->getPrefixedText();
  59. $verified = $wgContLang->normalize( $display );
  60. $title = Title::newFromText( $verified );
  61. if ( $row->wl_user == 0 || is_null( $title ) || !$title->equals( $current ) ) {
  62. $this->output( "invalid watch by {$row->wl_user} for "
  63. . "({$row->wl_namespace}, \"{$row->wl_title}\")\n" );
  64. $updated = $this->removeWatch( $row );
  65. $this->progress( $updated );
  66. return;
  67. }
  68. $this->progress( 0 );
  69. }
  70. private function removeWatch( $row ) {
  71. if ( !$this->dryrun && $this->hasOption( 'fix' ) ) {
  72. $dbw = $this->getDB( DB_MASTER );
  73. $dbw->delete(
  74. 'watchlist', [
  75. 'wl_user' => $row->wl_user,
  76. 'wl_namespace' => $row->wl_namespace,
  77. 'wl_title' => $row->wl_title ],
  78. __METHOD__
  79. );
  80. $this->output( "- removed\n" );
  81. return 1;
  82. } else {
  83. return 0;
  84. }
  85. }
  86. }
  87. $maintClass = "WatchlistCleanup";
  88. require_once RUN_MAINTENANCE_IF_MAIN;