removeUnusedAccounts.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Remove unused user accounts from the database
  4. * An unused account is one which has made no edits
  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. * @author Rob Church <robchur@gmail.com>
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script that removes unused user accounts from the database.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class RemoveUnusedAccounts extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addOption( 'delete', 'Actually delete the account' );
  35. $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
  36. $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
  37. }
  38. public function execute() {
  39. $this->output( "Remove unused accounts\n\n" );
  40. # Do an initial scan for inactive accounts and report the result
  41. $this->output( "Checking for unused user accounts...\n" );
  42. $del = [];
  43. $dbr = $this->getDB( DB_SLAVE );
  44. $res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ );
  45. if ( $this->hasOption( 'ignore-groups' ) ) {
  46. $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
  47. } else {
  48. $excludedGroups = [];
  49. }
  50. $touched = $this->getOption( 'ignore-touched', "1" );
  51. if ( !ctype_digit( $touched ) ) {
  52. $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
  53. }
  54. $touchedSeconds = 86400 * $touched;
  55. foreach ( $res as $row ) {
  56. # Check the account, but ignore it if it's within a $excludedGroups
  57. # group or if it's touched within the $touchedSeconds seconds.
  58. $instance = User::newFromId( $row->user_id );
  59. if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
  60. && $this->isInactiveAccount( $row->user_id, true )
  61. && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
  62. ) {
  63. # Inactive; print out the name and flag it
  64. $del[] = $row->user_id;
  65. $this->output( $row->user_name . "\n" );
  66. }
  67. }
  68. $count = count( $del );
  69. $this->output( "...found {$count}.\n" );
  70. # If required, go back and delete each marked account
  71. if ( $count > 0 && $this->hasOption( 'delete' ) ) {
  72. $this->output( "\nDeleting unused accounts..." );
  73. $dbw = $this->getDB( DB_MASTER );
  74. $dbw->delete( 'user', [ 'user_id' => $del ], __METHOD__ );
  75. $dbw->delete( 'user_groups', [ 'ug_user' => $del ], __METHOD__ );
  76. $dbw->delete( 'user_former_groups', [ 'ufg_user' => $del ], __METHOD__ );
  77. $dbw->delete( 'user_properties', [ 'up_user' => $del ], __METHOD__ );
  78. $dbw->delete( 'logging', [ 'log_user' => $del ], __METHOD__ );
  79. $dbw->delete( 'recentchanges', [ 'rc_user' => $del ], __METHOD__ );
  80. $this->output( "done.\n" );
  81. # Update the site_stats.ss_users field
  82. $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
  83. $dbw->update(
  84. 'site_stats',
  85. [ 'ss_users' => $users ],
  86. [ 'ss_row_id' => 1 ],
  87. __METHOD__
  88. );
  89. } elseif ( $count > 0 ) {
  90. $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
  91. }
  92. $this->output( "\n" );
  93. }
  94. /**
  95. * Could the specified user account be deemed inactive?
  96. * (No edits, no deleted edits, no log entries, no current/old uploads)
  97. *
  98. * @param int $id User's ID
  99. * @param bool $master Perform checking on the master
  100. * @return bool
  101. */
  102. private function isInactiveAccount( $id, $master = false ) {
  103. $dbo = $this->getDB( $master ? DB_MASTER : DB_SLAVE );
  104. $checks = [
  105. 'revision' => 'rev',
  106. 'archive' => 'ar',
  107. 'image' => 'img',
  108. 'oldimage' => 'oi',
  109. 'filearchive' => 'fa'
  110. ];
  111. $count = 0;
  112. $this->beginTransaction( $dbo, __METHOD__ );
  113. foreach ( $checks as $table => $fprefix ) {
  114. $conds = [ $fprefix . '_user' => $id ];
  115. $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
  116. }
  117. $conds = [ 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ];
  118. $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
  119. $this->commitTransaction( $dbo, __METHOD__ );
  120. return $count == 0;
  121. }
  122. }
  123. $maintClass = "RemoveUnusedAccounts";
  124. require_once RUN_MAINTENANCE_IF_MAIN;