convertUserOptions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Convert user options to the new `user_properties` table.
  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. use Wikimedia\Rdbms\ResultWrapper;
  25. use Wikimedia\Rdbms\IDatabase;
  26. /**
  27. * Maintenance script to convert user options to the new `user_properties` table.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class ConvertUserOptions extends Maintenance {
  32. private $mConversionCount = 0;
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Convert user options from old to new system' );
  36. $this->setBatchSize( 50 );
  37. }
  38. public function execute() {
  39. $this->output( "...batch conversion of user_options: " );
  40. $id = 0;
  41. $dbw = $this->getDB( DB_MASTER );
  42. if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
  43. $this->output( "nothing to migrate. " );
  44. return;
  45. }
  46. while ( $id !== null ) {
  47. $res = $dbw->select( 'user',
  48. [ 'user_id', 'user_options' ],
  49. [
  50. 'user_id > ' . $dbw->addQuotes( $id ),
  51. "user_options != " . $dbw->addQuotes( '' ),
  52. ],
  53. __METHOD__,
  54. [
  55. 'ORDER BY' => 'user_id',
  56. 'LIMIT' => $this->getBatchSize(),
  57. ]
  58. );
  59. $id = $this->convertOptionBatch( $res, $dbw );
  60. wfWaitForSlaves();
  61. if ( $id ) {
  62. $this->output( "--Converted to ID $id\n" );
  63. }
  64. }
  65. $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
  66. }
  67. /**
  68. * @param ResultWrapper $res
  69. * @param IDatabase $dbw
  70. * @return null|int
  71. */
  72. function convertOptionBatch( $res, $dbw ) {
  73. $id = null;
  74. foreach ( $res as $row ) {
  75. $this->mConversionCount++;
  76. $insertRows = [];
  77. foreach ( explode( "\n", $row->user_options ) as $s ) {
  78. $m = [];
  79. if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
  80. continue;
  81. }
  82. // MW < 1.16 would save even default values. Filter them out
  83. // here (as in User) to avoid adding many unnecessary rows.
  84. $defaultOption = User::getDefaultOption( $m[1] );
  85. if ( is_null( $defaultOption ) || $m[2] != $defaultOption ) {
  86. $insertRows[] = [
  87. 'up_user' => $row->user_id,
  88. 'up_property' => $m[1],
  89. 'up_value' => $m[2],
  90. ];
  91. }
  92. }
  93. if ( count( $insertRows ) ) {
  94. $dbw->insert( 'user_properties', $insertRows, __METHOD__, [ 'IGNORE' ] );
  95. }
  96. $dbw->update(
  97. 'user',
  98. [ 'user_options' => '' ],
  99. [ 'user_id' => $row->user_id ],
  100. __METHOD__
  101. );
  102. $id = $row->user_id;
  103. }
  104. return $id;
  105. }
  106. }
  107. $maintClass = ConvertUserOptions::class;
  108. require_once RUN_MAINTENANCE_IF_MAIN;