MediaWiki REL1_35
convertUserOptions.php
Go to the documentation of this file.
1<?php
24require_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;
126require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Maintenance script to convert user options to the new user_properties table.
convertOptionBatch( $res, $dbw)
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getDefaultOption( $opt)
Get a given default option value.
Definition User.php:1553
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Result wrapper for grabbing data queried from an IDatabase object.
const DB_MASTER
Definition defines.php:29