MediaWiki REL1_33
ActorMigration.php
Go to the documentation of this file.
1<?php
26
36
42
51 private static $tempTables = [
52 'rev_user' => [
53 'table' => 'revision_actor_temp',
54 'pk' => 'revactor_rev',
55 'field' => 'revactor_actor',
56 'joinPK' => 'rev_id',
57 'extra' => [
58 'revactor_timestamp' => 'rev_timestamp',
59 'revactor_page' => 'rev_page',
60 ],
61 ],
62 ];
63
69 private static $formerTempTables = [];
70
76 private static $specialFields = [
77 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
78 ];
79
81 private $joinCache = null;
82
84 private $stage;
85
87 public function __construct( $stage ) {
88 if ( ( $stage & SCHEMA_COMPAT_WRITE_BOTH ) === 0 ) {
89 throw new InvalidArgumentException( '$stage must include a write mode' );
90 }
91 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === 0 ) {
92 throw new InvalidArgumentException( '$stage must include a read mode' );
93 }
95 throw new InvalidArgumentException( 'Cannot read both schemas' );
96 }
98 throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
99 }
101 throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
102 }
103
104 $this->stage = $stage;
105 }
106
111 public static function newMigration() {
112 return MediaWikiServices::getInstance()->getActorMigration();
113 }
114
120 public function isAnon( $field ) {
121 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0";
122 }
123
129 public function isNotAnon( $field ) {
130 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0";
131 }
132
138 private static function getFieldNames( $key ) {
139 return self::$specialFields[$key] ?? [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
140 }
141
153 public function getJoin( $key ) {
154 if ( !isset( $this->joinCache[$key] ) ) {
155 $tables = [];
156 $fields = [];
157 $joins = [];
158
159 list( $text, $actor ) = self::getFieldNames( $key );
160
161 if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
162 $fields[$key] = $key;
163 $fields[$text] = $text;
164 $fields[$actor] = 'NULL';
165 } else {
166 if ( isset( self::$tempTables[$key] ) ) {
167 $t = self::$tempTables[$key];
168 $alias = "temp_$key";
169 $tables[$alias] = $t['table'];
170 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
171 $joinField = "{$alias}.{$t['field']}";
172 } else {
173 $joinField = $actor;
174 }
175
176 $alias = "actor_$key";
177 $tables[$alias] = 'actor';
178 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
179
180 $fields[$key] = "{$alias}.actor_user";
181 $fields[$text] = "{$alias}.actor_name";
182 $fields[$actor] = $joinField;
183 }
184
185 $this->joinCache[$key] = [
186 'tables' => $tables,
187 'fields' => $fields,
188 'joins' => $joins,
189 ];
190 }
191
192 return $this->joinCache[$key];
193 }
194
204 public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
205 if ( isset( self::$tempTables[$key] ) ) {
206 throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
207 }
208
209 list( $text, $actor ) = self::getFieldNames( $key );
210 $ret = [];
211 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
212 $ret[$key] = $user->getId();
213 $ret[$text] = $user->getName();
214 }
215 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
216 // We need to be able to assign an actor ID if none exists
217 if ( !$user instanceof User && !$user->getActorId() ) {
218 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
219 }
220 $ret[$actor] = $user->getActorId( $dbw );
221 }
222 return $ret;
223 }
224
237 public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
238 if ( isset( self::$formerTempTables[$key] ) ) {
239 wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
240 } elseif ( !isset( self::$tempTables[$key] ) ) {
241 throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
242 }
243
244 list( $text, $actor ) = self::getFieldNames( $key );
245 $ret = [];
246 $callback = null;
247 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
248 $ret[$key] = $user->getId();
249 $ret[$text] = $user->getName();
250 }
251 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
252 // We need to be able to assign an actor ID if none exists
253 if ( !$user instanceof User && !$user->getActorId() ) {
254 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
255 }
256 $id = $user->getActorId( $dbw );
257
258 if ( isset( self::$tempTables[$key] ) ) {
259 $func = __METHOD__;
260 $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
261 $t = self::$tempTables[$key];
262 $set = [ $t['field'] => $id ];
263 foreach ( $t['extra'] as $to => $from ) {
264 if ( !array_key_exists( $from, $extra ) ) {
265 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
266 }
267 $set[$to] = $extra[$from];
268 }
269 $dbw->upsert(
270 $t['table'],
271 [ $t['pk'] => $pk ] + $set,
272 [ $t['pk'] ],
273 $set,
274 $func
275 );
276 };
277 } else {
278 $ret[$actor] = $id;
279 $callback = function ( $pk, array $extra ) {
280 };
281 }
282 } elseif ( isset( self::$tempTables[$key] ) ) {
283 $func = __METHOD__;
284 $callback = function ( $pk, array $extra ) use ( $key, $func ) {
285 $t = self::$tempTables[$key];
286 foreach ( $t['extra'] as $to => $from ) {
287 if ( !array_key_exists( $from, $extra ) ) {
288 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
289 }
290 }
291 };
292 } else {
293 $callback = function ( $pk, array $extra ) {
294 };
295 }
296 return [ $ret, $callback ];
297 }
298
320 public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
321 $tables = [];
322 $conds = [];
323 $joins = [];
324
325 if ( $users instanceof UserIdentity ) {
326 $users = [ $users ];
327 }
328
329 // Get information about all the passed users
330 $ids = [];
331 $names = [];
332 $actors = [];
333 foreach ( $users as $user ) {
334 if ( $useId && $user->getId() ) {
335 $ids[] = $user->getId();
336 } else {
337 $names[] = $user->getName();
338 }
339 $actorId = $user->getActorId();
340 if ( $actorId ) {
341 $actors[] = $actorId;
342 }
343 }
344
345 list( $text, $actor ) = self::getFieldNames( $key );
346
347 // Combine data into conditions to be ORed together
348 if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
349 if ( $actors ) {
350 if ( isset( self::$tempTables[$key] ) ) {
351 $t = self::$tempTables[$key];
352 $alias = "temp_$key";
353 $tables[$alias] = $t['table'];
354 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
355 $joinField = "{$alias}.{$t['field']}";
356 } else {
357 $joinField = $actor;
358 }
359 $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
360 }
361 } else {
362 if ( $ids ) {
363 $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
364 }
365 if ( $names ) {
366 $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
367 }
368 }
369
370 return [
371 'tables' => $tables,
372 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
373 'orconds' => $conds,
374 'joins' => $joins,
375 ];
376 }
377
378}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
This class handles the logic for the actor table migration.
const MIGRATION_STAGE_SCHEMA_COMPAT
Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage expects MIGRATION_*...
static array $formerTempTables
Fields that formerly used $tempTables Key is '$key', value is the MediaWiki version in which it was r...
isAnon( $field)
Return an SQL condition to test if a user field is anonymous.
getInsertValues(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
static array $tempTables
Define fields that use temporary tables for transitional purposes Keys are '$key',...
static array $specialFields
Define fields that use non-standard mapping Keys are the user id column name, values are arrays with ...
static newMigration()
Static constructor.
array null $joinCache
Cache for self::getJoin()
getInsertValuesWithTempTable(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
int $stage
Combination of SCHEMA_COMPAT_* constants.
isNotAnon( $field)
Return an SQL condition to test if a user field is non-anonymous.
getWhere(IDatabase $db, $key, $users, $useId=true)
Get WHERE condition for the actor.
getJoin( $key)
Get SELECT fields and joins for the actor key.
static getFieldNames( $key)
MediaWikiServices is the service locator for the application scope of MediaWiki.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:676
getActorId(IDatabase $dbw=null)
Get the user's actor ID.
Definition User.php:2491
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this hook is for auditing only RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:996
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
const SCHEMA_COMPAT_WRITE_BOTH
Definition Defines.php:297
const SCHEMA_COMPAT_READ_NEW
Definition Defines.php:296
const SCHEMA_COMPAT_READ_BOTH
Definition Defines.php:298
const SCHEMA_COMPAT_WRITE_OLD
Definition Defines.php:293
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:294
const SCHEMA_COMPAT_WRITE_NEW
Definition Defines.php:295
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))