convertUserOptions.php 3.1 KB

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