cleanupSpam.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Cleanup all spam from a given hostname.
  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 to cleanup all spam from a given hostname.
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class CleanupSpam extends Maintenance {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Cleanup all spam from a given hostname' );
  33. $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
  34. $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
  35. $this->addArg(
  36. 'hostname',
  37. 'Hostname that was spamming, single * wildcard in the beginning allowed'
  38. );
  39. }
  40. public function execute() {
  41. global $IP, $wgLocalDatabases, $wgUser;
  42. $username = wfMessage( 'spambot_username' )->text();
  43. $wgUser = User::newSystemUser( $username );
  44. if ( !$wgUser ) {
  45. $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
  46. }
  47. // Create the user if necessary
  48. if ( !$wgUser->getId() ) {
  49. $wgUser->addToDatabase();
  50. }
  51. $spec = $this->getArg();
  52. $like = LinkFilter::makeLikeArray( $spec );
  53. if ( !$like ) {
  54. $this->error( "Not a valid hostname specification: $spec", true );
  55. }
  56. if ( $this->hasOption( 'all' ) ) {
  57. // Clean up spam on all wikis
  58. $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
  59. $found = false;
  60. foreach ( $wgLocalDatabases as $wikiID ) {
  61. $dbr = $this->getDB( DB_REPLICA, [], $wikiID );
  62. $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
  63. [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__ );
  64. if ( $count ) {
  65. $found = true;
  66. $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
  67. [ '--wiki', $wikiID, $spec ] );
  68. passthru( "$cmd | sed 's/^/$wikiID: /'" );
  69. }
  70. }
  71. if ( $found ) {
  72. $this->output( "All done\n" );
  73. } else {
  74. $this->output( "None found\n" );
  75. }
  76. } else {
  77. // Clean up spam on this wiki
  78. $dbr = $this->getDB( DB_REPLICA );
  79. $res = $dbr->select( 'externallinks', [ 'DISTINCT el_from' ],
  80. [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__ );
  81. $count = $dbr->numRows( $res );
  82. $this->output( "Found $count articles containing $spec\n" );
  83. foreach ( $res as $row ) {
  84. $this->cleanupArticle( $row->el_from, $spec );
  85. }
  86. if ( $count ) {
  87. $this->output( "Done\n" );
  88. }
  89. }
  90. }
  91. private function cleanupArticle( $id, $domain ) {
  92. $title = Title::newFromID( $id );
  93. if ( !$title ) {
  94. $this->error( "Internal error: no page for ID $id" );
  95. return;
  96. }
  97. $this->output( $title->getPrefixedDBkey() . " ..." );
  98. $rev = Revision::newFromTitle( $title );
  99. $currentRevId = $rev->getId();
  100. while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
  101. || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) )
  102. ) {
  103. $rev = $rev->getPrevious();
  104. }
  105. if ( $rev && $rev->getId() == $currentRevId ) {
  106. // The regex didn't match the current article text
  107. // This happens e.g. when a link comes from a template rather than the page itself
  108. $this->output( "False match\n" );
  109. } else {
  110. $dbw = $this->getDB( DB_MASTER );
  111. $this->beginTransaction( $dbw, __METHOD__ );
  112. $page = WikiPage::factory( $title );
  113. if ( $rev ) {
  114. // Revert to this revision
  115. $content = $rev->getContent( Revision::RAW );
  116. $this->output( "reverting\n" );
  117. $page->doEditContent(
  118. $content,
  119. wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
  120. EDIT_UPDATE,
  121. $rev->getId()
  122. );
  123. } elseif ( $this->hasOption( 'delete' ) ) {
  124. // Didn't find a non-spammy revision, blank the page
  125. $this->output( "deleting\n" );
  126. $page->doDeleteArticle(
  127. wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text()
  128. );
  129. } else {
  130. // Didn't find a non-spammy revision, blank the page
  131. $handler = ContentHandler::getForTitle( $title );
  132. $content = $handler->makeEmptyContent();
  133. $this->output( "blanking\n" );
  134. $page->doEditContent(
  135. $content,
  136. wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text()
  137. );
  138. }
  139. $this->commitTransaction( $dbw, __METHOD__ );
  140. }
  141. }
  142. }
  143. $maintClass = "CleanupSpam";
  144. require_once RUN_MAINTENANCE_IF_MAIN;