migrateActors.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * Migrate actors from pre-1.31 columns to the 'actor' table
  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. * @ingroup Maintenance
  22. */
  23. use Wikimedia\Rdbms\IDatabase;
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that migrates actors from pre-1.31 columns to the
  27. * 'actor' table
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class MigrateActors extends LoggedUpdateMaintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription( 'Migrates actors from pre-1.31 columns to the \'actor\' table' );
  35. $this->setBatchSize( 100 );
  36. }
  37. protected function getUpdateKey() {
  38. return __CLASS__;
  39. }
  40. protected function doDBUpdates() {
  41. global $wgActorTableSchemaMigrationStage;
  42. if ( $wgActorTableSchemaMigrationStage < MIGRATION_WRITE_NEW ) {
  43. $this->output(
  44. "...cannot update while \$wgActorTableSchemaMigrationStage < MIGRATION_WRITE_NEW\n"
  45. );
  46. return false;
  47. }
  48. $this->output( "Creating actor entries for all registered users\n" );
  49. $end = 0;
  50. $dbw = $this->getDB( DB_MASTER );
  51. $max = $dbw->selectField( 'user', 'MAX(user_id)', '', __METHOD__ );
  52. $count = 0;
  53. while ( $end < $max ) {
  54. $start = $end + 1;
  55. $end = min( $start + $this->mBatchSize, $max );
  56. $this->output( "... $start - $end\n" );
  57. $dbw->insertSelect(
  58. 'actor',
  59. 'user',
  60. [ 'actor_user' => 'user_id', 'actor_name' => 'user_name' ],
  61. [ "user_id >= $start", "user_id <= $end" ],
  62. __METHOD__,
  63. [ 'IGNORE' ],
  64. [ 'ORDER BY' => [ 'user_id' ] ]
  65. );
  66. $count += $dbw->affectedRows();
  67. wfWaitForSlaves();
  68. }
  69. $this->output( "Completed actor creation, added $count new actor(s)\n" );
  70. $errors = 0;
  71. $errors += $this->migrateToTemp(
  72. 'revision', 'rev_id', [ 'revactor_timestamp' => 'rev_timestamp', 'revactor_page' => 'rev_page' ],
  73. 'rev_user', 'rev_user_text', 'revactor_rev', 'revactor_actor'
  74. );
  75. $errors += $this->migrate( 'archive', 'ar_id', 'ar_user', 'ar_user_text', 'ar_actor' );
  76. $errors += $this->migrate( 'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_by_actor' );
  77. $errors += $this->migrate( 'image', 'img_name', 'img_user', 'img_user_text', 'img_actor' );
  78. $errors += $this->migrate(
  79. 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text', 'oi_actor'
  80. );
  81. $errors += $this->migrate( 'filearchive', 'fa_id', 'fa_user', 'fa_user_text', 'fa_actor' );
  82. $errors += $this->migrate( 'recentchanges', 'rc_id', 'rc_user', 'rc_user_text', 'rc_actor' );
  83. $errors += $this->migrate( 'logging', 'log_id', 'log_user', 'log_user_text', 'log_actor' );
  84. $errors += $this->migrateLogSearch();
  85. return $errors === 0;
  86. }
  87. /**
  88. * Calculate a "next" condition and a display string
  89. * @param IDatabase $dbw
  90. * @param string[] $primaryKey Primary key of the table.
  91. * @param object $row Database row
  92. * @return array [ string $next, string $display ]
  93. */
  94. private function makeNextCond( $dbw, $primaryKey, $row ) {
  95. $next = '';
  96. $display = [];
  97. for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
  98. $field = $primaryKey[$i];
  99. $display[] = $field . '=' . $row->$field;
  100. $value = $dbw->addQuotes( $row->$field );
  101. if ( $next === '' ) {
  102. $next = "$field > $value";
  103. } else {
  104. $next = "$field > $value OR $field = $value AND ($next)";
  105. }
  106. }
  107. $display = implode( ' ', array_reverse( $display ) );
  108. return [ $next, $display ];
  109. }
  110. /**
  111. * Add actors for anons in a set of rows
  112. * @param IDatabase $dbw
  113. * @param string $nameField
  114. * @param object[] &$rows
  115. * @param array &$complainedAboutUsers
  116. * @param int &$countErrors
  117. * @return int Count of actors inserted
  118. */
  119. private function addActorsForRows(
  120. IDatabase $dbw, $nameField, array &$rows, array &$complainedAboutUsers, &$countErrors
  121. ) {
  122. $needActors = [];
  123. $countActors = 0;
  124. $keep = [];
  125. foreach ( $rows as $index => $row ) {
  126. $keep[$index] = true;
  127. if ( $row->actor_id === null ) {
  128. // All registered users should have an actor_id already. So
  129. // if we have a usable name here, it means they didn't run
  130. // maintenance/cleanupUsersWithNoId.php
  131. $name = $row->$nameField;
  132. if ( User::isUsableName( $name ) ) {
  133. if ( !isset( $complainedAboutUsers[$name] ) ) {
  134. $complainedAboutUsers[$name] = true;
  135. $this->error(
  136. "User name \"$name\" is usable, cannot create an anonymous actor for it."
  137. . " Run maintenance/cleanupUsersWithNoId.php to fix this situation.\n"
  138. );
  139. }
  140. unset( $keep[$index] );
  141. $countErrors++;
  142. } else {
  143. $needActors[$name] = 0;
  144. }
  145. }
  146. }
  147. $rows = array_intersect_key( $rows, $keep );
  148. if ( $needActors ) {
  149. $dbw->insert(
  150. 'actor',
  151. array_map( function ( $v ) {
  152. return [
  153. 'actor_name' => $v,
  154. ];
  155. }, array_keys( $needActors ) ),
  156. __METHOD__
  157. );
  158. $countActors += $dbw->affectedRows();
  159. $res = $dbw->select(
  160. 'actor',
  161. [ 'actor_id', 'actor_name' ],
  162. [ 'actor_name' => array_keys( $needActors ) ],
  163. __METHOD__
  164. );
  165. foreach ( $res as $row ) {
  166. $needActors[$row->actor_name] = $row->actor_id;
  167. }
  168. foreach ( $rows as $row ) {
  169. if ( $row->actor_id === null ) {
  170. $row->actor_id = $needActors[$row->$nameField];
  171. }
  172. }
  173. }
  174. return $countActors;
  175. }
  176. /**
  177. * Migrate actors in a table.
  178. *
  179. * Assumes any row with the actor field non-zero have already been migrated.
  180. * Blanks the name field when migrating.
  181. *
  182. * @param string $table Table to migrate
  183. * @param string|string[] $primaryKey Primary key of the table.
  184. * @param string $userField User ID field name
  185. * @param string $nameField User name field name
  186. * @param string $actorField Actor field name
  187. * @return int Number of errors
  188. */
  189. protected function migrate( $table, $primaryKey, $userField, $nameField, $actorField ) {
  190. $complainedAboutUsers = [];
  191. $primaryKey = (array)$primaryKey;
  192. $pkFilter = array_flip( $primaryKey );
  193. $this->output(
  194. "Beginning migration of $table.$userField and $table.$nameField to $table.$actorField\n"
  195. );
  196. wfWaitForSlaves();
  197. $dbw = $this->getDB( DB_MASTER );
  198. $next = '1=1';
  199. $countUpdated = 0;
  200. $countActors = 0;
  201. $countErrors = 0;
  202. while ( true ) {
  203. // Fetch the rows needing update
  204. $res = $dbw->select(
  205. [ $table, 'actor' ],
  206. array_merge( $primaryKey, [ $userField, $nameField, 'actor_id' ] ),
  207. [
  208. $actorField => 0,
  209. $next,
  210. ],
  211. __METHOD__,
  212. [
  213. 'ORDER BY' => $primaryKey,
  214. 'LIMIT' => $this->mBatchSize,
  215. ],
  216. [
  217. 'actor' => [
  218. 'LEFT JOIN',
  219. "$userField != 0 AND actor_user = $userField OR "
  220. . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
  221. ]
  222. ]
  223. );
  224. if ( !$res->numRows() ) {
  225. break;
  226. }
  227. // Insert new actors for rows that need one
  228. $rows = iterator_to_array( $res );
  229. $lastRow = end( $rows );
  230. $countActors += $this->addActorsForRows(
  231. $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
  232. );
  233. // Update the existing rows
  234. foreach ( $rows as $row ) {
  235. if ( !$row->actor_id ) {
  236. list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
  237. $this->error(
  238. "Could not make actor for row with $display "
  239. . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
  240. );
  241. $countErrors++;
  242. continue;
  243. }
  244. $dbw->update(
  245. $table,
  246. [
  247. $actorField => $row->actor_id,
  248. $nameField => '',
  249. ],
  250. array_intersect_key( (array)$row, $pkFilter ) + [
  251. $actorField => 0
  252. ],
  253. __METHOD__
  254. );
  255. $countUpdated += $dbw->affectedRows();
  256. }
  257. list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
  258. $this->output( "... $display\n" );
  259. wfWaitForSlaves();
  260. }
  261. $this->output(
  262. "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
  263. . "$countErrors error(s)\n"
  264. );
  265. return $countErrors;
  266. }
  267. /**
  268. * Migrate actors in a table to a temporary table.
  269. *
  270. * Assumes the new table is named "{$table}_actor_temp", and it has two
  271. * columns, in order, being the primary key of the original table and the
  272. * actor ID field.
  273. * Blanks the name field when migrating.
  274. *
  275. * @param string $table Table to migrate
  276. * @param string $primaryKey Primary key of the table.
  277. * @param array $extra Extra fields to copy
  278. * @param string $userField User ID field name
  279. * @param string $nameField User name field name
  280. * @param string $newPrimaryKey Primary key of the new table.
  281. * @param string $actorField Actor field name
  282. */
  283. protected function migrateToTemp(
  284. $table, $primaryKey, $extra, $userField, $nameField, $newPrimaryKey, $actorField
  285. ) {
  286. $complainedAboutUsers = [];
  287. $newTable = $table . '_actor_temp';
  288. $this->output(
  289. "Beginning migration of $table.$userField and $table.$nameField to $newTable.$actorField\n"
  290. );
  291. wfWaitForSlaves();
  292. $dbw = $this->getDB( DB_MASTER );
  293. $next = [];
  294. $countUpdated = 0;
  295. $countActors = 0;
  296. $countErrors = 0;
  297. while ( true ) {
  298. // Fetch the rows needing update
  299. $res = $dbw->select(
  300. [ $table, $newTable, 'actor' ],
  301. [ $primaryKey, $userField, $nameField, 'actor_id' ] + $extra,
  302. [ $newPrimaryKey => null ] + $next,
  303. __METHOD__,
  304. [
  305. 'ORDER BY' => $primaryKey,
  306. 'LIMIT' => $this->mBatchSize,
  307. ],
  308. [
  309. $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ],
  310. 'actor' => [
  311. 'LEFT JOIN',
  312. "$userField != 0 AND actor_user = $userField OR "
  313. . "($userField = 0 OR $userField IS NULL) AND actor_name = $nameField"
  314. ]
  315. ]
  316. );
  317. if ( !$res->numRows() ) {
  318. break;
  319. }
  320. // Insert new actors for rows that need one
  321. $rows = iterator_to_array( $res );
  322. $lastRow = end( $rows );
  323. $countActors += $this->addActorsForRows(
  324. $dbw, $nameField, $rows, $complainedAboutUsers, $countErrors
  325. );
  326. // Update rows
  327. if ( $rows ) {
  328. $inserts = [];
  329. $updates = [];
  330. foreach ( $rows as $row ) {
  331. if ( !$row->actor_id ) {
  332. list( , $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $row );
  333. $this->error(
  334. "Could not make actor for row with $display "
  335. . "$userField={$row->$userField} $nameField={$row->$nameField}\n"
  336. );
  337. $countErrors++;
  338. continue;
  339. }
  340. $ins = [
  341. $newPrimaryKey => $row->$primaryKey,
  342. $actorField => $row->actor_id,
  343. ];
  344. foreach ( $extra as $to => $from ) {
  345. $ins[$to] = $row->$to; // It's aliased
  346. }
  347. $inserts[] = $ins;
  348. $updates[] = $row->$primaryKey;
  349. }
  350. $this->beginTransaction( $dbw, __METHOD__ );
  351. $dbw->insert( $newTable, $inserts, __METHOD__ );
  352. $dbw->update( $table, [ $nameField => '' ], [ $primaryKey => $updates ], __METHOD__ );
  353. $countUpdated += $dbw->affectedRows();
  354. $this->commitTransaction( $dbw, __METHOD__ );
  355. }
  356. // Calculate the "next" condition
  357. list( $n, $display ) = $this->makeNextCond( $dbw, [ $primaryKey ], $lastRow );
  358. $next = [ $n ];
  359. $this->output( "... $display\n" );
  360. }
  361. $this->output(
  362. "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
  363. . "$countErrors error(s)\n"
  364. );
  365. return $countErrors;
  366. }
  367. /**
  368. * Migrate actors in the log_search table.
  369. * @return int Number of errors
  370. */
  371. protected function migrateLogSearch() {
  372. $complainedAboutUsers = [];
  373. $primaryKey = [ 'ls_field', 'ls_value' ];
  374. $pkFilter = array_flip( $primaryKey );
  375. $this->output( "Beginning migration of log_search\n" );
  376. wfWaitForSlaves();
  377. $dbw = $this->getDB( DB_MASTER );
  378. $countUpdated = 0;
  379. $countActors = 0;
  380. $countErrors = 0;
  381. $next = '1=1';
  382. while ( true ) {
  383. // Fetch the rows needing update
  384. $res = $dbw->select(
  385. [ 'log_search', 'actor' ],
  386. [ 'ls_field', 'ls_value', 'actor_id' ],
  387. [
  388. 'ls_field' => 'target_author_id',
  389. $next,
  390. ],
  391. __METHOD__,
  392. [
  393. 'DISTINCT',
  394. 'ORDER BY' => [ 'ls_value' ],
  395. 'LIMIT' => $this->mBatchSize,
  396. ],
  397. [ 'actor' => [ 'LEFT JOIN', 'ls_value = ' . $dbw->buildStringCast( 'actor_user' ) ] ]
  398. );
  399. if ( !$res->numRows() ) {
  400. break;
  401. }
  402. // Update the rows
  403. $del = [];
  404. foreach ( $res as $row ) {
  405. $lastRow = $row;
  406. if ( !$row->actor_id ) {
  407. list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
  408. $this->error( "No actor for row with $display\n" );
  409. $countErrors++;
  410. continue;
  411. }
  412. $dbw->update(
  413. 'log_search',
  414. [
  415. 'ls_field' => 'target_author_actor',
  416. 'ls_value' => $row->actor_id,
  417. ],
  418. [
  419. 'ls_field' => $row->ls_field,
  420. 'ls_value' => $row->ls_value,
  421. ],
  422. __METHOD__,
  423. [ 'IGNORE' ]
  424. );
  425. $countUpdated += $dbw->affectedRows();
  426. $del[] = $row->ls_value;
  427. }
  428. if ( $del ) {
  429. $dbw->delete(
  430. 'log_search', [ 'ls_field' => 'target_author_id', 'ls_value' => $del ], __METHOD__
  431. );
  432. $countUpdated += $dbw->affectedRows();
  433. }
  434. list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
  435. $this->output( "... $display\n" );
  436. wfWaitForSlaves();
  437. }
  438. $next = '1=1';
  439. while ( true ) {
  440. // Fetch the rows needing update
  441. $res = $dbw->select(
  442. [ 'log_search', 'actor' ],
  443. [ 'ls_field', 'ls_value', 'actor_id' ],
  444. [
  445. 'ls_field' => 'target_author_ip',
  446. $next,
  447. ],
  448. __METHOD__,
  449. [
  450. 'DISTINCT',
  451. 'ORDER BY' => [ 'ls_value' ],
  452. 'LIMIT' => $this->mBatchSize,
  453. ],
  454. [ 'actor' => [ 'LEFT JOIN', 'ls_value = actor_name' ] ]
  455. );
  456. if ( !$res->numRows() ) {
  457. break;
  458. }
  459. // Insert new actors for rows that need one
  460. $rows = iterator_to_array( $res );
  461. $lastRow = end( $rows );
  462. $countActors += $this->addActorsForRows(
  463. $dbw, 'ls_value', $rows, $complainedAboutUsers, $countErrors
  464. );
  465. // Update the rows
  466. $del = [];
  467. foreach ( $rows as $row ) {
  468. if ( !$row->actor_id ) {
  469. list( , $display ) = $this->makeNextCond( $dbw, $primaryKey, $row );
  470. $this->error( "Could not make actor for row with $display\n" );
  471. $countErrors++;
  472. continue;
  473. }
  474. $dbw->update(
  475. 'log_search',
  476. [
  477. 'ls_field' => 'target_author_actor',
  478. 'ls_value' => $row->actor_id,
  479. ],
  480. [
  481. 'ls_field' => $row->ls_field,
  482. 'ls_value' => $row->ls_value,
  483. ],
  484. __METHOD__,
  485. [ 'IGNORE' ]
  486. );
  487. $countUpdated += $dbw->affectedRows();
  488. $del[] = $row->ls_value;
  489. }
  490. if ( $del ) {
  491. $dbw->delete(
  492. 'log_search', [ 'ls_field' => 'target_author_ip', 'ls_value' => $del ], __METHOD__
  493. );
  494. $countUpdated += $dbw->affectedRows();
  495. }
  496. list( $next, $display ) = $this->makeNextCond( $dbw, $primaryKey, $lastRow );
  497. $this->output( "... $display\n" );
  498. wfWaitForSlaves();
  499. }
  500. $this->output(
  501. "Completed migration, updated $countUpdated row(s) with $countActors new actor(s), "
  502. . "$countErrors error(s)\n"
  503. );
  504. return $countErrors;
  505. }
  506. }
  507. $maintClass = "MigrateActors";
  508. require_once RUN_MAINTENANCE_IF_MAIN;