87 public function run() {
88 $services = MediaWikiServices::getInstance();
89 $lbFactory = $services->getDBLoadBalancerFactory();
90 $lb = $lbFactory->getMainLB();
93 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
95 $page = $services->getWikiPageFactory()->newFromID( $this->params[
'pageId'], IDBAccessObject::READ_LATEST );
97 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
103 if ( !$lb->waitForPrimaryPos( $dbr ) ) {
104 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
109 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
110 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
111 if ( !$scopedLock ) {
112 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
117 if ( !$lb->waitForPrimaryPos( $dbr ) ) {
118 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
122 $dbr->flushSnapshot( __METHOD__ );
124 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
127 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
131 $subQuery = $dbr->newSelectQueryBuilder()
133 ->from(
'recentchanges' )
134 ->where(
'rc_this_oldid = rev_id' )
135 ->andWhere( [
'rc_source' => RecentChange::SRC_CATEGORIZE ] );
136 $row = $dbr->newSelectQueryBuilder()
137 ->select( [
'rev_timestamp',
'rev_id' ] )
139 ->where( [
'rev_page' => $page->
getId() ] )
140 ->andWhere( $dbr->expr(
'rev_timestamp',
'>=', $dbr->timestamp( $cutoffUnix ) ) )
141 ->andWhere(
new RawSQLExpression(
'EXISTS (' . $subQuery->getSQL() .
')' ) )
142 ->orderBy( [
'rev_timestamp',
'rev_id' ], SelectQueryBuilder::SORT_DESC )
143 ->caller( __METHOD__ )->fetchRow();
147 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
148 $lastRevId = (int)$row->rev_id;
155 $revisionStore = $services->getRevisionStore();
156 $res = $revisionStore->newSelectQueryBuilder( $dbr )
159 'rev_page' => $page->
getId(),
160 $dbr->buildComparison(
'>', [
161 'rev_timestamp' => $dbr->timestamp( $cutoffUnix ),
162 'rev_id' => $lastRevId,
165 ->orderBy( [
'rev_timestamp',
'rev_id' ], SelectQueryBuilder::SORT_ASC )
166 ->caller( __METHOD__ )->fetchResultSet();
169 foreach ( $res as $row ) {
187 if ( $newRev->
isDeleted( RevisionRecord::DELETED_TEXT ) ) {
191 $services = MediaWikiServices::getInstance();
194 $oldRev = $services->getRevisionLookup()
195 ->getRevisionById( $newRev->
getParentId(), IDBAccessObject::READ_LATEST );
196 if ( !$oldRev || $oldRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
204 $categoryChanges = $this->getExplicitCategoriesChanges( $page, $newRev, $oldRev );
205 [ $categoryInserts, $categoryDeletes ] = $categoryChanges;
206 if ( !$categoryInserts && !$categoryDeletes ) {
210 $blc = $services->getBacklinkCacheFactory()->getBacklinkCache(
$title );
212 $catMembChange->checkTemplateLinks();
214 $batchSize = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
217 foreach ( $categoryInserts as $categoryName ) {
218 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
219 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
220 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
225 foreach ( $categoryDeletes as $categoryName ) {
226 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
227 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
228 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {