48 private const ENQUEUE_FUDGE_SEC = 60;
57 'categoryMembershipChange',
59 'pageId' => $page->
getId(),
60 'revTimestamp' => $revisionTimestamp,
63 'removeDuplicates' =>
true,
64 'removeDuplicatesIgnoreParams' => [
'revTimestamp' ]
77 parent::__construct(
'categoryMembershipChange', $page, $params );
80 $this->removeDuplicates =
true;
83 public function run() {
84 $services = MediaWikiServices::getInstance();
85 $lbFactory = $services->getDBLoadBalancerFactory();
86 $lb = $lbFactory->getMainLB();
89 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
91 $page = $services->getWikiPageFactory()->
newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
93 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
99 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
100 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
105 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
106 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
107 if ( !$scopedLock ) {
108 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
113 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
114 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
118 $dbr->flushSnapshot( __METHOD__ );
120 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
123 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
127 $row =
$dbr->selectRow(
129 [
'rev_timestamp',
'rev_id' ],
131 'rev_page' => $page->
getId(),
132 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
133 'EXISTS (' .
$dbr->selectSQLText(
137 'rc_this_oldid = rev_id',
138 'rc_source' => RecentChange::SRC_CATEGORIZE,
144 [
'ORDER BY' => [
'rev_timestamp DESC',
'rev_id DESC' ] ]
148 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
149 $lastRevId = (int)$row->rev_id;
156 $revisionStore = $services->getRevisionStore();
157 $revQuery = $revisionStore->getQueryInfo();
162 'rev_page' => $page->
getId(),
163 $dbr->buildComparison(
'>', [
164 'rev_timestamp' =>
$dbr->timestamp( $cutoffUnix ),
165 'rev_id' => $lastRevId,
169 [
'ORDER BY' => [
'rev_timestamp ASC',
'rev_id ASC' ] ],
174 foreach (
$res as $row ) {
193 if ( $newRev->
isDeleted( RevisionRecord::DELETED_TEXT ) ) {
197 $services = MediaWikiServices::getInstance();
200 $oldRev = $services->getRevisionLookup()
201 ->getRevisionById( $newRev->
getParentId(), RevisionLookup::READ_LATEST );
202 if ( !$oldRev || $oldRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
210 $categoryChanges = $this->getExplicitCategoriesChanges( $page, $newRev, $oldRev );
211 [ $categoryInserts, $categoryDeletes ] = $categoryChanges;
212 if ( !$categoryInserts && !$categoryDeletes ) {
216 $blc = $services->getBacklinkCacheFactory()->getBacklinkCache(
$title );
218 $catMembChange->checkTemplateLinks();
220 $batchSize = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
223 foreach ( $categoryInserts as $categoryName ) {
224 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
225 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
226 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
231 foreach ( $categoryDeletes as $categoryName ) {
232 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
233 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
234 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
240 private function getExplicitCategoriesChanges(
250 $oldCategories = $oldRev
251 ? $this->getCategoriesAtRev( $page, $oldRev, $parseTimestamp )
254 $newCategories = $this->getCategoriesAtRev( $page, $newRev, $parseTimestamp );
256 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
257 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
259 return [ $categoryInserts, $categoryDeletes ];
270 $services = MediaWikiServices::getInstance();
272 $options->setTimestamp( $parseTimestamp );
273 $options->setRenderReason(
'CategoryMembershipChangeJob' );
276 ? $services->getParserCache()->get( $page, $options )
279 if ( !$output || $output->getCacheRevisionId() !== $rev->
getId() ) {
280 $output = $services->getRevisionRenderer()->getRenderedRevision( $rev, $options )
281 ->getRevisionParserOutput();
287 return $output->getCategoryNames();
291 $info = parent::getDeduplicationInfo();
292 unset( $info[
'params'][
'revTimestamp'] );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Job to add recent change entries mentioning category membership changes.
static newSpec(PageIdentity $page, $revisionTimestamp)
__construct(PageIdentity $page, array $params)
Constructor for use by the Job Queue infrastructure.
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
notifyUpdatesForRevision(LBFactory $lbFactory, WikiPage $page, RevisionRecord $newRev)
Job queue task description base code.
Class to both describe a background job and handle jobs.
A class containing constants representing the names of configuration variables.
Base representation for an editable wiki page.
static newFromID( $id, $from='fromdb')
Constructor from a page id.
makeParserOptions( $context)
Get parser options suitable for rendering the primary article wikitext.
getId( $wikiId=self::LOCAL)
getTitle()
Get the title object of the article.
Interface for objects (potentially) representing an editable wiki page.
getId( $wikiId=self::LOCAL)
Returns the page ID.