MediaWiki  master
convertUserOptions.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
29 
36 
37  private $mConversionCount = 0;
38 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Convert user options from old to new system' );
42  $this->setBatchSize( 50 );
43  }
44 
45  public function execute() {
46  $this->output( "...batch conversion of user_options: " );
47  $id = 0;
48  $dbw = $this->getDB( DB_MASTER );
49 
50  if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
51  $this->output( "nothing to migrate. " );
52 
53  return;
54  }
55  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
56  while ( $id !== null ) {
57  $res = $dbw->select( 'user',
58  [ 'user_id', 'user_options' ],
59  [
60  'user_id > ' . $dbw->addQuotes( $id ),
61  "user_options != " . $dbw->addQuotes( '' ),
62  ],
63  __METHOD__,
64  [
65  'ORDER BY' => 'user_id',
66  'LIMIT' => $this->getBatchSize(),
67  ]
68  );
69  $id = $this->convertOptionBatch( $res, $dbw );
70 
71  $lbFactory->waitForReplication();
72 
73  if ( $id ) {
74  $this->output( "--Converted to ID $id\n" );
75  }
76  }
77  $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
78  }
79 
85  private function convertOptionBatch( $res, $dbw ) {
86  $id = null;
87  foreach ( $res as $row ) {
88  $this->mConversionCount++;
89  $insertRows = [];
90  foreach ( explode( "\n", $row->user_options ) as $s ) {
91  $m = [];
92  if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
93  continue;
94  }
95 
96  // MW < 1.16 would save even default values. Filter them out
97  // here (as in User) to avoid adding many unnecessary rows.
98  $defaultOption = User::getDefaultOption( $m[1] );
99  if ( $defaultOption === null || $m[2] != $defaultOption ) {
100  $insertRows[] = [
101  'up_user' => $row->user_id,
102  'up_property' => $m[1],
103  'up_value' => $m[2],
104  ];
105  }
106  }
107 
108  if ( count( $insertRows ) ) {
109  $dbw->insert( 'user_properties', $insertRows, __METHOD__, [ 'IGNORE' ] );
110  }
111 
112  $dbw->update(
113  'user',
114  [ 'user_options' => '' ],
115  [ 'user_id' => $row->user_id ],
116  __METHOD__
117  );
118  $id = $row->user_id;
119  }
120 
121  return $id;
122  }
123 }
124 
125 $maintClass = ConvertUserOptions::class;
126 require_once RUN_MAINTENANCE_IF_MAIN;
User\getDefaultOption
static getDefaultOption( $opt)
Get a given default option value.
Definition: User.php:1611
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:145
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
$s
$s
Definition: mergeMessageFileList.php:185
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
$res
$res
Definition: testCompression.php:57
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
ConvertUserOptions\__construct
__construct()
Default constructor.
Definition: convertUserOptions.php:39
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
ConvertUserOptions\convertOptionBatch
convertOptionBatch( $res, $dbw)
Definition: convertUserOptions.php:85
$maintClass
$maintClass
Definition: convertUserOptions.php:125
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
ConvertUserOptions\execute
execute()
Do the actual work.
Definition: convertUserOptions.php:45
ConvertUserOptions
Maintenance script to convert user options to the new user_properties table.
Definition: convertUserOptions.php:35
ConvertUserOptions\$mConversionCount
$mConversionCount
Definition: convertUserOptions.php:37
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:370