populateLogSearch.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Makes the required database updates for populating the
  4. * log_search table retroactively
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that makes the required database updates for populating the
  27. * log_search table retroactively
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class PopulateLogSearch extends LoggedUpdateMaintenance {
  32. private static $tableMap = [
  33. 'rev' => 'revision',
  34. 'fa' => 'filearchive',
  35. 'oi' => 'oldimage',
  36. 'ar' => 'archive'
  37. ];
  38. public function __construct() {
  39. parent::__construct();
  40. $this->addDescription( 'Migrate log params to new table and index for searching' );
  41. $this->setBatchSize( 100 );
  42. }
  43. protected function getUpdateKey() {
  44. return 'populate log_search';
  45. }
  46. protected function updateSkippedMessage() {
  47. return 'log_search table already populated.';
  48. }
  49. protected function doDBUpdates() {
  50. global $wgActorTableSchemaMigrationStage;
  51. $batchSize = $this->getBatchSize();
  52. $db = $this->getDB( DB_MASTER );
  53. if ( !$db->tableExists( 'log_search' ) ) {
  54. $this->error( "log_search does not exist" );
  55. return false;
  56. }
  57. $start = $db->selectField( 'logging', 'MIN(log_id)', '', __FUNCTION__ );
  58. if ( !$start ) {
  59. $this->output( "Nothing to do.\n" );
  60. return true;
  61. }
  62. $end = $db->selectField( 'logging', 'MAX(log_id)', '', __FUNCTION__ );
  63. # Do remaining chunk
  64. $end += $batchSize - 1;
  65. $blockStart = $start;
  66. $blockEnd = $start + $batchSize - 1;
  67. $delTypes = [ 'delete', 'suppress' ]; // revisiondelete types
  68. while ( $blockEnd <= $end ) {
  69. $this->output( "...doing log_id from $blockStart to $blockEnd\n" );
  70. $cond = "log_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
  71. $res = $db->select(
  72. 'logging', [ 'log_id', 'log_type', 'log_action', 'log_params' ], $cond, __FUNCTION__
  73. );
  74. foreach ( $res as $row ) {
  75. if ( LogEventsList::typeAction( $row, $delTypes, 'revision' ) ) {
  76. // RevisionDelete logs - revisions
  77. $params = LogPage::extractParams( $row->log_params );
  78. // Param format: <urlparam> <item CSV> [<ofield> <nfield>]
  79. if ( count( $params ) < 2 ) {
  80. continue; // bad row?
  81. }
  82. $field = RevisionDeleter::getRelationType( $params[0] );
  83. // B/C, the params may start with a title key (<title> <urlparam> <CSV>)
  84. if ( $field == null ) {
  85. array_shift( $params ); // remove title param
  86. $field = RevisionDeleter::getRelationType( $params[0] );
  87. if ( $field == null ) {
  88. $this->output( "Invalid param type for {$row->log_id}\n" );
  89. continue; // skip this row
  90. } else {
  91. // Clean up the row...
  92. $db->update( 'logging',
  93. [ 'log_params' => implode( ',', $params ) ],
  94. [ 'log_id' => $row->log_id ] );
  95. }
  96. }
  97. $items = explode( ',', $params[1] );
  98. $log = new LogPage( $row->log_type );
  99. // Add item relations...
  100. $log->addRelations( $field, $items, $row->log_id );
  101. // Query item author relations...
  102. $prefix = substr( $field, 0, strpos( $field, '_' ) ); // db prefix
  103. if ( !isset( self::$tableMap[$prefix] ) ) {
  104. continue; // bad row?
  105. }
  106. $tables = [ self::$tableMap[$prefix] ];
  107. $fields = [];
  108. $joins = [];
  109. if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW ) {
  110. $fields['userid'] = $prefix . '_user';
  111. $fields['username'] = $prefix . '_user_text';
  112. }
  113. if ( $wgActorTableSchemaMigrationStage > MIGRATION_OLD ) {
  114. if ( $prefix === 'rev' ) {
  115. $tables[] = 'revision_actor_temp';
  116. $joins['revision_actor_temp'] = [
  117. $wgActorTableSchemaMigrationStage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
  118. 'rev_id = revactor_rev',
  119. ];
  120. $fields['actorid'] = 'revactor_actor';
  121. } else {
  122. $fields['actorid'] = $prefix . '_actor';
  123. }
  124. }
  125. $sres = $db->select( $tables, $fields, [ $field => $items ], __METHOD__, [], $joins );
  126. } elseif ( LogEventsList::typeAction( $row, $delTypes, 'event' ) ) {
  127. // RevisionDelete logs - log events
  128. $params = LogPage::extractParams( $row->log_params );
  129. // Param format: <item CSV> [<ofield> <nfield>]
  130. if ( count( $params ) < 1 ) {
  131. continue; // bad row
  132. }
  133. $items = explode( ',', $params[0] );
  134. $log = new LogPage( $row->log_type );
  135. // Add item relations...
  136. $log->addRelations( 'log_id', $items, $row->log_id );
  137. // Query item author relations...
  138. $fields = [];
  139. if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW ) {
  140. $fields['userid'] = 'log_user';
  141. $fields['username'] = 'log_user_text';
  142. }
  143. if ( $wgActorTableSchemaMigrationStage > MIGRATION_OLD ) {
  144. $fields['actorid'] = 'log_actor';
  145. }
  146. $sres = $db->select( 'logging', $fields, [ 'log_id' => $items ], __METHOD__ );
  147. } else {
  148. continue;
  149. }
  150. // Add item author relations...
  151. $userIds = $userIPs = $userActors = [];
  152. foreach ( $sres as $srow ) {
  153. if ( $wgActorTableSchemaMigrationStage < MIGRATION_NEW ) {
  154. if ( $srow->userid > 0 ) {
  155. $userIds[] = intval( $srow->userid );
  156. } elseif ( $srow->username != '' ) {
  157. $userIPs[] = $srow->username;
  158. }
  159. }
  160. if ( $wgActorTableSchemaMigrationStage > MIGRATION_OLD ) {
  161. if ( $srow->actorid ) {
  162. $userActors[] = intval( $srow->actorid );
  163. } elseif ( $srow->userid > 0 ) {
  164. $userActors[] = User::newFromId( $srow->userid )->getActorId( $db );
  165. } else {
  166. $userActors[] = User::newFromName( $srow->username, false )->getActorId( $db );
  167. }
  168. }
  169. }
  170. // Add item author relations...
  171. if ( $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH ) {
  172. $log->addRelations( 'target_author_id', $userIds, $row->log_id );
  173. $log->addRelations( 'target_author_ip', $userIPs, $row->log_id );
  174. }
  175. if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH ) {
  176. $log->addRelations( 'target_author_actor', $userActors, $row->log_id );
  177. }
  178. }
  179. $blockStart += $batchSize;
  180. $blockEnd += $batchSize;
  181. wfWaitForSlaves();
  182. }
  183. $this->output( "Done populating log_search table.\n" );
  184. return true;
  185. }
  186. }
  187. $maintClass = PopulateLogSearch::class;
  188. require_once RUN_MAINTENANCE_IF_MAIN;