ActorMigration.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * Methods to help with the actor table migration
  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. */
  22. use MediaWiki\MediaWikiServices;
  23. use MediaWiki\User\UserIdentity;
  24. use Wikimedia\Rdbms\IDatabase;
  25. /**
  26. * This class handles the logic for the actor table migration.
  27. *
  28. * This is not intended to be a long-term part of MediaWiki; it will be
  29. * deprecated and removed along with $wgActorTableSchemaMigrationStage.
  30. *
  31. * @since 1.31
  32. */
  33. class ActorMigration {
  34. /**
  35. * Define fields that use temporary tables for transitional purposes
  36. * @var array Keys are '$key', values are arrays with four fields:
  37. * - table: Temporary table name
  38. * - pk: Temporary table column referring to the main table's primary key
  39. * - field: Temporary table column referring actor.actor_id
  40. * - joinPK: Main table's primary key
  41. */
  42. private static $tempTables = [
  43. 'rev_user' => [
  44. 'table' => 'revision_actor_temp',
  45. 'pk' => 'revactor_rev',
  46. 'field' => 'revactor_actor',
  47. 'joinPK' => 'rev_id',
  48. 'extra' => [
  49. 'revactor_timestamp' => 'rev_timestamp',
  50. 'revactor_page' => 'rev_page',
  51. ],
  52. ],
  53. ];
  54. /**
  55. * Fields that formerly used $tempTables
  56. * @var array Key is '$key', value is the MediaWiki version in which it was
  57. * removed from $tempTables.
  58. */
  59. private static $formerTempTables = [];
  60. /**
  61. * Define fields that use non-standard mapping
  62. * @var array Keys are the user id column name, values are arrays with two
  63. * elements (the user text column name and the actor id column name)
  64. */
  65. private static $specialFields = [
  66. 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
  67. ];
  68. /** @var array|null Cache for `self::getJoin()` */
  69. private $joinCache = null;
  70. /** @var int One of the MIGRATION_* constants */
  71. private $stage;
  72. /** @private */
  73. public function __construct( $stage ) {
  74. $this->stage = $stage;
  75. }
  76. /**
  77. * Static constructor
  78. * @return ActorMigration
  79. */
  80. public static function newMigration() {
  81. return MediaWikiServices::getInstance()->getActorMigration();
  82. }
  83. /**
  84. * Return an SQL condition to test if a user field is anonymous
  85. * @param string $field Field name or SQL fragment
  86. * @return string
  87. */
  88. public function isAnon( $field ) {
  89. return $this->stage === MIGRATION_NEW ? "$field IS NULL" : "$field = 0";
  90. }
  91. /**
  92. * Return an SQL condition to test if a user field is non-anonymous
  93. * @param string $field Field name or SQL fragment
  94. * @return string
  95. */
  96. public function isNotAnon( $field ) {
  97. return $this->stage === MIGRATION_NEW ? "$field IS NOT NULL" : "$field != 0";
  98. }
  99. /**
  100. * @param string $key A key such as "rev_user" identifying the actor
  101. * field being fetched.
  102. * @return string[] [ $text, $actor ]
  103. */
  104. private static function getFieldNames( $key ) {
  105. if ( isset( self::$specialFields[$key] ) ) {
  106. return self::$specialFields[$key];
  107. }
  108. return [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
  109. }
  110. /**
  111. * Get SELECT fields and joins for the actor key
  112. *
  113. * @param string $key A key such as "rev_user" identifying the actor
  114. * field being fetched.
  115. * @return array With three keys:
  116. * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
  117. * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
  118. * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
  119. * All tables, fields, and joins are aliased, so `+` is safe to use.
  120. */
  121. public function getJoin( $key ) {
  122. if ( !isset( $this->joinCache[$key] ) ) {
  123. $tables = [];
  124. $fields = [];
  125. $joins = [];
  126. list( $text, $actor ) = self::getFieldNames( $key );
  127. if ( $this->stage === MIGRATION_OLD ) {
  128. $fields[$key] = $key;
  129. $fields[$text] = $text;
  130. $fields[$actor] = 'NULL';
  131. } else {
  132. $join = $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN';
  133. if ( isset( self::$tempTables[$key] ) ) {
  134. $t = self::$tempTables[$key];
  135. $alias = "temp_$key";
  136. $tables[$alias] = $t['table'];
  137. $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
  138. $joinField = "{$alias}.{$t['field']}";
  139. } else {
  140. $joinField = $actor;
  141. }
  142. $alias = "actor_$key";
  143. $tables[$alias] = 'actor';
  144. $joins[$alias] = [ $join, "{$alias}.actor_id = {$joinField}" ];
  145. if ( $this->stage === MIGRATION_NEW ) {
  146. $fields[$key] = "{$alias}.actor_user";
  147. $fields[$text] = "{$alias}.actor_name";
  148. } else {
  149. $fields[$key] = "COALESCE( {$alias}.actor_user, $key )";
  150. $fields[$text] = "COALESCE( {$alias}.actor_name, $text )";
  151. }
  152. $fields[$actor] = $joinField;
  153. }
  154. $this->joinCache[$key] = [
  155. 'tables' => $tables,
  156. 'fields' => $fields,
  157. 'joins' => $joins,
  158. ];
  159. }
  160. return $this->joinCache[$key];
  161. }
  162. /**
  163. * Get UPDATE fields for the actor
  164. *
  165. * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
  166. * @param string $key A key such as "rev_user" identifying the actor
  167. * field being fetched.
  168. * @param UserIdentity $user User to set in the update
  169. * @return array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
  170. */
  171. public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
  172. if ( isset( self::$tempTables[$key] ) ) {
  173. throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
  174. }
  175. list( $text, $actor ) = self::getFieldNames( $key );
  176. $ret = [];
  177. if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
  178. $ret[$key] = $user->getId();
  179. $ret[$text] = $user->getName();
  180. }
  181. if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
  182. // We need to be able to assign an actor ID if none exists
  183. if ( !$user instanceof User && !$user->getActorId() ) {
  184. $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
  185. }
  186. $ret[$actor] = $user->getActorId( $dbw );
  187. }
  188. return $ret;
  189. }
  190. /**
  191. * Get UPDATE fields for the actor
  192. *
  193. * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
  194. * @param string $key A key such as "rev_user" identifying the actor
  195. * field being fetched.
  196. * @param UserIdentity $user User to set in the update
  197. * @return array with two values:
  198. * - array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
  199. * - callback to call with the the primary key for the main table insert
  200. * and extra fields needed for the temp table.
  201. */
  202. public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
  203. if ( isset( self::$formerTempTables[$key] ) ) {
  204. wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
  205. } elseif ( !isset( self::$tempTables[$key] ) ) {
  206. throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
  207. }
  208. list( $text, $actor ) = self::getFieldNames( $key );
  209. $ret = [];
  210. $callback = null;
  211. if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
  212. $ret[$key] = $user->getId();
  213. $ret[$text] = $user->getName();
  214. }
  215. if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
  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. $id = $user->getActorId( $dbw );
  221. if ( isset( self::$tempTables[$key] ) ) {
  222. $func = __METHOD__;
  223. $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
  224. $t = self::$tempTables[$key];
  225. $set = [ $t['field'] => $id ];
  226. foreach ( $t['extra'] as $to => $from ) {
  227. if ( !array_key_exists( $from, $extra ) ) {
  228. throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
  229. }
  230. $set[$to] = $extra[$from];
  231. }
  232. $dbw->upsert(
  233. $t['table'],
  234. [ $t['pk'] => $pk ] + $set,
  235. [ $t['pk'] ],
  236. $set,
  237. $func
  238. );
  239. };
  240. } else {
  241. $ret[$actor] = $id;
  242. $callback = function ( $pk, array $extra ) {
  243. };
  244. }
  245. } elseif ( isset( self::$tempTables[$key] ) ) {
  246. $func = __METHOD__;
  247. $callback = function ( $pk, array $extra ) use ( $key, $func ) {
  248. $t = self::$tempTables[$key];
  249. foreach ( $t['extra'] as $to => $from ) {
  250. if ( !array_key_exists( $from, $extra ) ) {
  251. throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
  252. }
  253. }
  254. };
  255. } else {
  256. $callback = function ( $pk, array $extra ) {
  257. };
  258. }
  259. return [ $ret, $callback ];
  260. }
  261. /**
  262. * Get WHERE condition for the actor
  263. *
  264. * @param IDatabase $db Database to use for quoting and list-making
  265. * @param string $key A key such as "rev_user" identifying the actor
  266. * field being fetched.
  267. * @param UserIdentity|UserIdentity[] $users Users to test for
  268. * @param bool $useId If false, don't try to query by the user ID.
  269. * Intended for use with rc_user since it has an index on
  270. * (rc_user_text,rc_timestamp) but not (rc_user,rc_timestamp).
  271. * @return array With three keys:
  272. * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
  273. * - conds: (string) to include in the `$cond` to `IDatabase->select()`
  274. * - orconds: (array[]) array of alternatives in case a union of multiple
  275. * queries would be more efficient than a query with OR. May have keys
  276. * 'actor', 'userid', 'username'.
  277. * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
  278. * All tables and joins are aliased, so `+` is safe to use.
  279. */
  280. public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
  281. $tables = [];
  282. $conds = [];
  283. $joins = [];
  284. if ( $users instanceof UserIdentity ) {
  285. $users = [ $users ];
  286. }
  287. // Get information about all the passed users
  288. $ids = [];
  289. $names = [];
  290. $actors = [];
  291. foreach ( $users as $user ) {
  292. if ( $useId && $user->getId() ) {
  293. $ids[] = $user->getId();
  294. } else {
  295. $names[] = $user->getName();
  296. }
  297. $actorId = $user->getActorId();
  298. if ( $actorId ) {
  299. $actors[] = $actorId;
  300. }
  301. }
  302. list( $text, $actor ) = self::getFieldNames( $key );
  303. // Combine data into conditions to be ORed together
  304. $actorNotEmpty = [];
  305. if ( $this->stage === MIGRATION_OLD ) {
  306. $actors = [];
  307. $actorEmpty = [];
  308. } elseif ( isset( self::$tempTables[$key] ) ) {
  309. $t = self::$tempTables[$key];
  310. $alias = "temp_$key";
  311. $tables[$alias] = $t['table'];
  312. $joins[$alias] = [
  313. $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
  314. "{$alias}.{$t['pk']} = {$t['joinPK']}"
  315. ];
  316. $joinField = "{$alias}.{$t['field']}";
  317. $actorEmpty = [ $joinField => null ];
  318. if ( $this->stage !== MIGRATION_NEW ) {
  319. // Otherwise the resulting test can evaluate to NULL, and
  320. // NOT(NULL) is NULL rather than true.
  321. $actorNotEmpty = [ "$joinField IS NOT NULL" ];
  322. }
  323. } else {
  324. $joinField = $actor;
  325. $actorEmpty = [ $joinField => 0 ];
  326. }
  327. if ( $actors ) {
  328. $conds['actor'] = $db->makeList(
  329. $actorNotEmpty + [ $joinField => $actors ], IDatabase::LIST_AND
  330. );
  331. }
  332. if ( $this->stage < MIGRATION_NEW && $ids ) {
  333. $conds['userid'] = $db->makeList(
  334. $actorEmpty + [ $key => $ids ], IDatabase::LIST_AND
  335. );
  336. }
  337. if ( $this->stage < MIGRATION_NEW && $names ) {
  338. $conds['username'] = $db->makeList(
  339. $actorEmpty + [ $text => $names ], IDatabase::LIST_AND
  340. );
  341. }
  342. return [
  343. 'tables' => $tables,
  344. 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
  345. 'orconds' => $conds,
  346. 'joins' => $joins,
  347. ];
  348. }
  349. }