51 public function run() {
52 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
53 $lb = $lbFactory->getMainLB();
56 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
58 $page = WikiPage::newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
60 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
66 if ( !$lb->safeWaitForMasterPos(
$dbr ) ) {
67 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
72 $lockKey =
"CategoryMembershipUpdates:{$page->getId()}";
73 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
75 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
80 if ( !$lb->safeWaitForMasterPos(
$dbr ) ) {
81 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
85 $dbr->flushSnapshot( __METHOD__ );
87 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
94 $row =
$dbr->selectRow(
96 [
'rev_timestamp',
'rev_id' ],
98 'rev_page' => $page->getId(),
99 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
100 'EXISTS (' .
$dbr->selectSQLText(
104 'rc_this_oldid = rev_id',
105 'rc_source' => RecentChange::SRC_CATEGORIZE,
107 'rc_cur_id = rev_page',
108 'rc_timestamp = rev_timestamp'
113 [
'ORDER BY' =>
'rev_timestamp DESC, rev_id DESC' ]
117 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
118 $lastRevId = (int)$row->rev_id;
125 $encCutoff =
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) );
131 'rev_page' => $page->getId(),
132 "rev_timestamp > $encCutoff" .
133 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
136 [
'ORDER BY' =>
'rev_timestamp ASC, rev_id ASC' ],
141 foreach (
$res as $row ) {
161 if ( !$newRev->getContent() ) {
166 if ( $newRev->getParentId() ) {
167 $oldRev = Revision::newFromId( $newRev->getParentId(), Revision::READ_LATEST );
168 if ( !$oldRev->getContent() ) {
177 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
178 if ( !$categoryInserts && !$categoryDeletes ) {
183 $catMembChange->checkTemplateLinks();
185 $batchSize = $config->get(
'UpdateRowsPerQuery' );
188 foreach ( $categoryInserts as $categoryName ) {
189 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
190 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
191 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
192 $lbFactory->commitAndWaitForReplication( __METHOD__, $this->ticket );
196 foreach ( $categoryDeletes as $categoryName ) {
197 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
198 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
199 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
200 $lbFactory->commitAndWaitForReplication( __METHOD__, $this->ticket );
211 $parseTimestamp = $newRev->getTimestamp();
215 $oldCategories = $oldRev
221 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
222 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
224 return [ $categoryInserts, $categoryDeletes ];