cleanupPreferences.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Clean up user preferences from the database.
  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. * @author TyA <tya.wiki@gmail.com>
  22. * @author Chad <chad@wikimedia.org>
  23. * @see https://phabricator.wikimedia.org/T32976
  24. * @ingroup Maintenance
  25. */
  26. require_once __DIR__ . '/Maintenance.php';
  27. /**
  28. * Maintenance script that removes bogus preferences from the database.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class CleanupPreferences extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->mDescription = 'Clean up hidden preferences, removed preferences, and normalizes values';
  36. $this->setBatchSize( 50 );
  37. $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
  38. $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
  39. $this->addOption( 'unknown',
  40. 'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
  41. // TODO: actually implement this
  42. // $this->addOption( 'bogus', 'Drop preferences that have invalid/unaccepted values' );
  43. }
  44. /**
  45. * We will do this in three passes
  46. * 1) The easiest is to drop the hidden preferences from the database. We
  47. * don't actually want them
  48. * 2) Drop preference keys that we don't know about. They could've been
  49. * removed from core, provided by a now-disabled extension, or the result
  50. * of a bug. We don't want them.
  51. * 3) TODO: Normalize accepted preference values. This is the biggest part of the work.
  52. * For each preference we know about, iterate over it and if it's got a
  53. * limited set of accepted values (so it's not text, basically), make sure
  54. * all values are in that range. Drop ones that aren't.
  55. */
  56. public function execute() {
  57. global $wgHiddenPrefs, $wgDefaultUserOptions;
  58. $dbw = $this->getDB( DB_MASTER );
  59. $didWork = false;
  60. $hidden = $this->hasOption( 'hidden' );
  61. $unknown = $this->hasOption( 'unknown' );
  62. $bogus = $this->hasOption( 'bogus' );
  63. if ( !$hidden && !$unknown && !$bogus ) {
  64. $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
  65. return;
  66. }
  67. // Remove hidden prefs. Iterate over them to avoid the IN on a large table
  68. if ( $hidden ) {
  69. if ( !$wgHiddenPrefs ) {
  70. $this->output( "No hidden preferences, skipping\n" );
  71. }
  72. foreach ( $wgHiddenPrefs as $hiddenPref ) {
  73. $this->deleteByWhere(
  74. $dbw,
  75. 'Dropping hidden preferences',
  76. [ 'up_property' => $hiddenPref ]
  77. );
  78. }
  79. }
  80. // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
  81. if ( $unknown ) {
  82. $where = [
  83. 'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
  84. 'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
  85. ];
  86. // Allow extensions to add to the where clause to prevent deletion of their own prefs.
  87. Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
  88. $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
  89. }
  90. // Something something phase 3
  91. if ( $bogus ) {
  92. }
  93. }
  94. /**
  95. *
  96. */
  97. private function deleteByWhere( $dbw, $startMessage, $where ) {
  98. $this->output( $startMessage . "...\n" );
  99. $total = 0;
  100. while ( true ) {
  101. $res = $dbw->select(
  102. 'user_properties',
  103. '*', // The table lacks a primary key, so select the whole row
  104. $where,
  105. __METHOD__,
  106. [ 'LIMIT' => $this->mBatchSize ]
  107. );
  108. $numRows = $res->numRows();
  109. $total += $numRows;
  110. if ( $res->numRows() <= 0 ) {
  111. // All done!
  112. $this->output( "DONE! (handled $total entries)\n" );
  113. break;
  114. }
  115. // Progress or something
  116. $this->output( "..doing $numRows entries\n" );
  117. // Delete our batch, then wait
  118. foreach ( $res as $row ) {
  119. if ( $this->hasOption( 'dry-run' ) ) {
  120. $this->output(
  121. " DRY RUN, would drop: " .
  122. "[up_user] => '{$row->up_user}' " .
  123. "[up_property] => '{$row->up_property}' " .
  124. "[up_value] => '{$row->up_value}'\n"
  125. );
  126. continue;
  127. }
  128. $this->beginTransaction( $dbw, __METHOD__ );
  129. $dbw->delete(
  130. 'user_properties',
  131. [
  132. 'up_user' => $row->up_user,
  133. 'up_property' => $row->up_property,
  134. 'up_value' => $row->up_value,
  135. ],
  136. __METHOD__
  137. );
  138. $this->commitTransaction( $dbw, __METHOD__ );
  139. }
  140. }
  141. }
  142. }
  143. $maintClass = CleanupPreferences::class; // Tells it to run the class
  144. require_once RUN_MAINTENANCE_IF_MAIN;