MediaWiki  1.33.0
convertUserOptions.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
28 
35 
36  private $mConversionCount = 0;
37 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Convert user options from old to new system' );
41  $this->setBatchSize( 50 );
42  }
43 
44  public function execute() {
45  $this->output( "...batch conversion of user_options: " );
46  $id = 0;
47  $dbw = $this->getDB( DB_MASTER );
48 
49  if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
50  $this->output( "nothing to migrate. " );
51 
52  return;
53  }
54  while ( $id !== null ) {
55  $res = $dbw->select( 'user',
56  [ 'user_id', 'user_options' ],
57  [
58  'user_id > ' . $dbw->addQuotes( $id ),
59  "user_options != " . $dbw->addQuotes( '' ),
60  ],
61  __METHOD__,
62  [
63  'ORDER BY' => 'user_id',
64  'LIMIT' => $this->getBatchSize(),
65  ]
66  );
67  $id = $this->convertOptionBatch( $res, $dbw );
68 
70 
71  if ( $id ) {
72  $this->output( "--Converted to ID $id\n" );
73  }
74  }
75  $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
76  }
77 
83  function convertOptionBatch( $res, $dbw ) {
84  $id = null;
85  foreach ( $res as $row ) {
86  $this->mConversionCount++;
87  $insertRows = [];
88  foreach ( explode( "\n", $row->user_options ) as $s ) {
89  $m = [];
90  if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
91  continue;
92  }
93 
94  // MW < 1.16 would save even default values. Filter them out
95  // here (as in User) to avoid adding many unnecessary rows.
96  $defaultOption = User::getDefaultOption( $m[1] );
97  if ( is_null( $defaultOption ) || $m[2] != $defaultOption ) {
98  $insertRows[] = [
99  'up_user' => $row->user_id,
100  'up_property' => $m[1],
101  'up_value' => $m[2],
102  ];
103  }
104  }
105 
106  if ( count( $insertRows ) ) {
107  $dbw->insert( 'user_properties', $insertRows, __METHOD__, [ 'IGNORE' ] );
108  }
109 
110  $dbw->update(
111  'user',
112  [ 'user_options' => '' ],
113  [ 'user_id' => $row->user_id ],
114  __METHOD__
115  );
116  $id = $row->user_id;
117  }
118 
119  return $id;
120  }
121 }
122 
124 require_once RUN_MAINTENANCE_IF_MAIN;
User\getDefaultOption
static getDefaultOption( $opt)
Get a given default option value.
Definition: User.php:1818
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$s
$s
Definition: mergeMessageFileList.php:186
$res
$res
Definition: database.txt:21
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:24
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2790
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ConvertUserOptions\__construct
__construct()
Default constructor.
Definition: convertUserOptions.php:38
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ConvertUserOptions\convertOptionBatch
convertOptionBatch( $res, $dbw)
Definition: convertUserOptions.php:83
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
$maintClass
$maintClass
Definition: convertUserOptions.php:123
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
ConvertUserOptions\execute
execute()
Do the actual work.
Definition: convertUserOptions.php:44
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
ConvertUserOptions
Maintenance script to convert user options to the new user_properties table.
Definition: convertUserOptions.php:34
ConvertUserOptions\$mConversionCount
$mConversionCount
Definition: convertUserOptions.php:36
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375