rebuildrecentchanges.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * Rebuild recent changes from scratch. This takes several hours,
  4. * depending on the database size and server configuration.
  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. * @todo Document
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script that rebuilds recent changes from scratch.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class RebuildRecentchanges extends Maintenance {
  32. /** @var integer UNIX timestamp */
  33. private $cutoffFrom;
  34. /** @var integer UNIX timestamp */
  35. private $cutoffTo;
  36. public function __construct() {
  37. parent::__construct();
  38. $this->addDescription( 'Rebuild recent changes' );
  39. $this->addOption(
  40. 'from',
  41. "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
  42. false,
  43. true
  44. );
  45. $this->addOption(
  46. 'to',
  47. "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
  48. false,
  49. true
  50. );
  51. $this->setBatchSize( 200 );
  52. }
  53. public function execute() {
  54. if (
  55. ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
  56. ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
  57. ) {
  58. $this->error( "Both 'from' and 'to' must be given, or neither", 1 );
  59. }
  60. $this->rebuildRecentChangesTablePass1();
  61. $this->rebuildRecentChangesTablePass2();
  62. $this->rebuildRecentChangesTablePass3();
  63. $this->rebuildRecentChangesTablePass4();
  64. $this->rebuildRecentChangesTablePass5();
  65. if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
  66. $this->purgeFeeds();
  67. }
  68. $this->output( "Done.\n" );
  69. }
  70. /**
  71. * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
  72. */
  73. private function rebuildRecentChangesTablePass1() {
  74. $dbw = $this->getDB( DB_MASTER );
  75. if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
  76. $this->cutoffFrom = wfTimestamp( TS_UNIX, $this->getOption( 'from' ) );
  77. $this->cutoffTo = wfTimestamp( TS_UNIX, $this->getOption( 'to' ) );
  78. $sec = $this->cutoffTo - $this->cutoffFrom;
  79. $days = $sec / 24 / 3600;
  80. $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
  81. } else {
  82. global $wgRCMaxAge;
  83. $days = $wgRCMaxAge / 24 / 3600;
  84. $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
  85. $this->cutoffFrom = time() - $wgRCMaxAge;
  86. $this->cutoffTo = time();
  87. }
  88. $this->output( "Clearing recentchanges table for time range...\n" );
  89. $rcids = $dbw->selectFieldValues(
  90. 'recentchanges',
  91. 'rc_id',
  92. [
  93. 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  94. 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
  95. ]
  96. );
  97. foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
  98. $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__ );
  99. wfGetLBFactory()->waitForReplication();
  100. }
  101. $this->output( "Loading from page and revision tables...\n" );
  102. $res = $dbw->select(
  103. [ 'page', 'revision' ],
  104. [
  105. 'rev_timestamp',
  106. 'rev_user',
  107. 'rev_user_text',
  108. 'rev_comment',
  109. 'rev_minor_edit',
  110. 'rev_id',
  111. 'rev_deleted',
  112. 'page_namespace',
  113. 'page_title',
  114. 'page_is_new',
  115. 'page_id'
  116. ],
  117. [
  118. 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  119. 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
  120. 'rev_page=page_id'
  121. ],
  122. __METHOD__,
  123. [ 'ORDER BY' => 'rev_timestamp DESC' ]
  124. );
  125. $this->output( "Inserting from page and revision tables...\n" );
  126. $inserted = 0;
  127. foreach ( $res as $row ) {
  128. $dbw->insert(
  129. 'recentchanges',
  130. [
  131. 'rc_timestamp' => $row->rev_timestamp,
  132. 'rc_user' => $row->rev_user,
  133. 'rc_user_text' => $row->rev_user_text,
  134. 'rc_namespace' => $row->page_namespace,
  135. 'rc_title' => $row->page_title,
  136. 'rc_comment' => $row->rev_comment,
  137. 'rc_minor' => $row->rev_minor_edit,
  138. 'rc_bot' => 0,
  139. 'rc_new' => $row->page_is_new,
  140. 'rc_cur_id' => $row->page_id,
  141. 'rc_this_oldid' => $row->rev_id,
  142. 'rc_last_oldid' => 0, // is this ok?
  143. 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
  144. 'rc_source' => $row->page_is_new
  145. ? $dbw->addQuotes( RecentChange::SRC_NEW )
  146. : $dbw->addQuotes( RecentChange::SRC_EDIT )
  147. ,
  148. 'rc_deleted' => $row->rev_deleted
  149. ],
  150. __METHOD__
  151. );
  152. if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
  153. wfGetLBFactory()->waitForReplication();
  154. }
  155. }
  156. }
  157. /**
  158. * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
  159. * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
  160. */
  161. private function rebuildRecentChangesTablePass2() {
  162. $dbw = $this->getDB( DB_MASTER );
  163. $this->output( "Updating links and size differences...\n" );
  164. # Fill in the rc_last_oldid field, which points to the previous edit
  165. $res = $dbw->select(
  166. 'recentchanges',
  167. [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
  168. [
  169. "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  170. "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
  171. ],
  172. __METHOD__,
  173. [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
  174. );
  175. $lastCurId = 0;
  176. $lastOldId = 0;
  177. $lastSize = null;
  178. $updated = 0;
  179. foreach ( $res as $obj ) {
  180. $new = 0;
  181. if ( $obj->rc_cur_id != $lastCurId ) {
  182. # Switch! Look up the previous last edit, if any
  183. $lastCurId = intval( $obj->rc_cur_id );
  184. $emit = $obj->rc_timestamp;
  185. $row = $dbw->selectRow(
  186. 'revision',
  187. [ 'rev_id', 'rev_len' ],
  188. [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
  189. __METHOD__,
  190. [ 'ORDER BY' => 'rev_timestamp DESC' ]
  191. );
  192. if ( $row ) {
  193. $lastOldId = intval( $row->rev_id );
  194. # Grab the last text size if available
  195. $lastSize = !is_null( $row->rev_len ) ? intval( $row->rev_len ) : null;
  196. } else {
  197. # No previous edit
  198. $lastOldId = 0;
  199. $lastSize = null;
  200. $new = 1; // probably true
  201. }
  202. }
  203. if ( $lastCurId == 0 ) {
  204. $this->output( "Uhhh, something wrong? No curid\n" );
  205. } else {
  206. # Grab the entry's text size
  207. $size = (int)$dbw->selectField(
  208. 'revision',
  209. 'rev_len',
  210. [ 'rev_id' => $obj->rc_this_oldid ],
  211. __METHOD__
  212. );
  213. $dbw->update(
  214. 'recentchanges',
  215. [
  216. 'rc_last_oldid' => $lastOldId,
  217. 'rc_new' => $new,
  218. 'rc_type' => $new ? RC_NEW : RC_EDIT,
  219. 'rc_source' => $new === 1
  220. ? $dbw->addQuotes( RecentChange::SRC_NEW )
  221. : $dbw->addQuotes( RecentChange::SRC_EDIT ),
  222. 'rc_old_len' => $lastSize,
  223. 'rc_new_len' => $size,
  224. ],
  225. [
  226. 'rc_cur_id' => $lastCurId,
  227. 'rc_this_oldid' => $obj->rc_this_oldid,
  228. 'rc_timestamp' => $obj->rc_timestamp // index usage
  229. ],
  230. __METHOD__
  231. );
  232. $lastOldId = intval( $obj->rc_this_oldid );
  233. $lastSize = $size;
  234. if ( ( ++$updated % $this->mBatchSize ) == 0 ) {
  235. wfGetLBFactory()->waitForReplication();
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Rebuild pass 3: Insert `recentchanges` entries for action logs.
  242. */
  243. private function rebuildRecentChangesTablePass3() {
  244. global $wgLogTypes, $wgLogRestrictions;
  245. $dbw = $this->getDB( DB_MASTER );
  246. $this->output( "Loading from user, page, and logging tables...\n" );
  247. $res = $dbw->select(
  248. [ 'user', 'logging', 'page' ],
  249. [
  250. 'log_timestamp',
  251. 'log_user',
  252. 'user_name',
  253. 'log_namespace',
  254. 'log_title',
  255. 'log_comment',
  256. 'page_id',
  257. 'log_type',
  258. 'log_action',
  259. 'log_id',
  260. 'log_params',
  261. 'log_deleted'
  262. ],
  263. [
  264. 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  265. 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
  266. 'log_user=user_id',
  267. // Some logs don't go in RC since they are private.
  268. // @FIXME: core/extensions also have spammy logs that don't go in RC.
  269. 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
  270. ],
  271. __METHOD__,
  272. [ 'ORDER BY' => 'log_timestamp DESC' ],
  273. [
  274. 'page' =>
  275. [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
  276. ]
  277. );
  278. $inserted = 0;
  279. foreach ( $res as $row ) {
  280. $dbw->insert(
  281. 'recentchanges',
  282. [
  283. 'rc_timestamp' => $row->log_timestamp,
  284. 'rc_user' => $row->log_user,
  285. 'rc_user_text' => $row->user_name,
  286. 'rc_namespace' => $row->log_namespace,
  287. 'rc_title' => $row->log_title,
  288. 'rc_comment' => $row->log_comment,
  289. 'rc_minor' => 0,
  290. 'rc_bot' => 0,
  291. 'rc_patrolled' => 1,
  292. 'rc_new' => 0,
  293. 'rc_this_oldid' => 0,
  294. 'rc_last_oldid' => 0,
  295. 'rc_type' => RC_LOG,
  296. 'rc_source' => $dbw->addQuotes( RecentChange::SRC_LOG ),
  297. 'rc_cur_id' => $dbw->cascadingDeletes()
  298. ? $row->page_id
  299. : (int)$row->page_id, // NULL => 0,
  300. 'rc_log_type' => $row->log_type,
  301. 'rc_log_action' => $row->log_action,
  302. 'rc_logid' => $row->log_id,
  303. 'rc_params' => $row->log_params,
  304. 'rc_deleted' => $row->log_deleted
  305. ],
  306. __METHOD__
  307. );
  308. if ( ( ++$inserted % $this->mBatchSize ) == 0 ) {
  309. wfGetLBFactory()->waitForReplication();
  310. }
  311. }
  312. }
  313. /**
  314. * Rebuild pass 4: Mark bot and autopatrolled entries.
  315. */
  316. private function rebuildRecentChangesTablePass4() {
  317. global $wgUseRCPatrol, $wgMiserMode;
  318. $dbw = $this->getDB( DB_MASTER );
  319. list( $recentchanges, $usergroups, $user ) =
  320. $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
  321. # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
  322. # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
  323. # may be lost at this point (aside from joining on the patrol log table entries).
  324. $botgroups = [ 'bot' ];
  325. $autopatrolgroups = $wgUseRCPatrol ? User::getGroupsWithPermission( 'autopatrol' ) : [];
  326. # Flag our recent bot edits
  327. if ( $botgroups ) {
  328. $botwhere = $dbw->makeList( $botgroups );
  329. $this->output( "Flagging bot account edits...\n" );
  330. # Find all users that are bots
  331. $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
  332. "WHERE ug_group IN($botwhere) AND user_id = ug_user";
  333. $res = $dbw->query( $sql, __METHOD__ );
  334. $botusers = [];
  335. foreach ( $res as $obj ) {
  336. $botusers[] = $obj->user_name;
  337. }
  338. # Fill in the rc_bot field
  339. if ( $botusers ) {
  340. $rcids = $dbw->selectFieldValues(
  341. 'recentchanges',
  342. 'rc_id',
  343. [
  344. 'rc_user_text' => $botusers,
  345. "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  346. "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) )
  347. ],
  348. __METHOD__
  349. );
  350. foreach ( array_chunk( $rcids, $this->mBatchSize ) as $rcidBatch ) {
  351. $dbw->update(
  352. 'recentchanges',
  353. [ 'rc_bot' => 1 ],
  354. [ 'rc_id' => $rcidBatch ],
  355. __METHOD__
  356. );
  357. wfGetLBFactory()->waitForReplication();
  358. }
  359. }
  360. }
  361. # Flag our recent autopatrolled edits
  362. if ( !$wgMiserMode && $autopatrolgroups ) {
  363. $patrolwhere = $dbw->makeList( $autopatrolgroups );
  364. $patrolusers = [];
  365. $this->output( "Flagging auto-patrolled edits...\n" );
  366. # Find all users in RC with autopatrol rights
  367. $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
  368. "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
  369. $res = $dbw->query( $sql, __METHOD__ );
  370. foreach ( $res as $obj ) {
  371. $patrolusers[] = $dbw->addQuotes( $obj->user_name );
  372. }
  373. # Fill in the rc_patrolled field
  374. if ( $patrolusers ) {
  375. $patrolwhere = implode( ',', $patrolusers );
  376. $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
  377. "WHERE rc_user_text IN($patrolwhere) " .
  378. "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ) . ' ' .
  379. "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) );
  380. $dbw->query( $sql2 );
  381. }
  382. }
  383. }
  384. /**
  385. * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
  386. * for a single action (upload only, at the moment, but potentially also move, protect, ...).
  387. */
  388. private function rebuildRecentChangesTablePass5() {
  389. $dbw = wfGetDB( DB_MASTER );
  390. $this->output( "Removing duplicate revision and logging entries...\n" );
  391. $res = $dbw->select(
  392. [ 'logging', 'log_search' ],
  393. [ 'ls_value', 'ls_log_id' ],
  394. [
  395. 'ls_log_id = log_id',
  396. 'ls_field' => 'associated_rev_id',
  397. 'log_type' => 'upload',
  398. 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom ) ),
  399. 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo ) ),
  400. ],
  401. __METHOD__
  402. );
  403. $updates = 0;
  404. foreach ( $res as $obj ) {
  405. $rev_id = $obj->ls_value;
  406. $log_id = $obj->ls_log_id;
  407. // Mark the logging row as having an associated rev id
  408. $dbw->update(
  409. 'recentchanges',
  410. /*SET*/ [ 'rc_this_oldid' => $rev_id ],
  411. /*WHERE*/ [ 'rc_logid' => $log_id ],
  412. __METHOD__
  413. );
  414. // Delete the revision row
  415. $dbw->delete(
  416. 'recentchanges',
  417. /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
  418. __METHOD__
  419. );
  420. if ( ( ++$updates % $this->mBatchSize ) == 0 ) {
  421. wfGetLBFactory()->waitForReplication();
  422. }
  423. }
  424. }
  425. /**
  426. * Purge cached feeds in $messageMemc
  427. */
  428. private function purgeFeeds() {
  429. global $wgFeedClasses, $messageMemc;
  430. $this->output( "Deleting feed timestamps.\n" );
  431. foreach ( $wgFeedClasses as $feed => $className ) {
  432. $messageMemc->delete( wfMemcKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
  433. }
  434. }
  435. }
  436. $maintClass = "RebuildRecentchanges";
  437. require_once RUN_MAINTENANCE_IF_MAIN;