47 private const ENQUEUE_FUDGE_SEC = 60;
56 'categoryMembershipChange',
58 'pageId' => $page->
getId(),
59 'revTimestamp' => $revisionTimestamp,
62 'removeDuplicates' =>
true,
63 'removeDuplicatesIgnoreParams' => [
'revTimestamp' ]
76 parent::__construct(
'categoryMembershipChange', $page, $params );
79 $this->removeDuplicates =
true;
82 public function run() {
83 $services = MediaWikiServices::getInstance();
84 $lbFactory = $services->getDBLoadBalancerFactory();
85 $lb = $lbFactory->getMainLB();
88 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
90 $page = $services->getWikiPageFactory()->
newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
92 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
98 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
99 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
104 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
105 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
106 if ( !$scopedLock ) {
107 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
112 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
113 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
117 $dbr->flushSnapshot( __METHOD__ );
119 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
122 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
126 $row =
$dbr->selectRow(
128 [
'rev_timestamp',
'rev_id' ],
130 'rev_page' => $page->
getId(),
131 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
132 'EXISTS (' .
$dbr->selectSQLText(
136 'rc_this_oldid = rev_id',
137 'rc_source' => RecentChange::SRC_CATEGORIZE,
143 [
'ORDER BY' => [
'rev_timestamp DESC',
'rev_id DESC' ] ]
147 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
148 $lastRevId = (int)$row->rev_id;
155 $encCutoff =
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) );
156 $revisionStore = $services->getRevisionStore();
157 $revQuery = $revisionStore->getQueryInfo();
162 'rev_page' => $page->
getId(),
163 "rev_timestamp > $encCutoff" .
164 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
167 [
'ORDER BY' => [
'rev_timestamp ASC',
'rev_id ASC' ] ],
172 foreach (
$res as $row ) {
191 if ( $newRev->
isDeleted( RevisionRecord::DELETED_TEXT ) ) {
195 $services = MediaWikiServices::getInstance();
198 $oldRev = $services->getRevisionLookup()
199 ->getRevisionById( $newRev->
getParentId(), RevisionLookup::READ_LATEST );
200 if ( !$oldRev || $oldRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
208 $categoryChanges = $this->getExplicitCategoriesChanges( $page, $newRev, $oldRev );
209 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
210 if ( !$categoryInserts && !$categoryDeletes ) {
214 $blc = $services->getBacklinkCacheFactory()->getBacklinkCache(
$title );
216 $catMembChange->checkTemplateLinks();
218 $batchSize = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
221 foreach ( $categoryInserts as $categoryName ) {
222 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
223 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
224 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
229 foreach ( $categoryDeletes as $categoryName ) {
230 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
231 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
232 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
238 private function getExplicitCategoriesChanges(
248 $oldCategories = $oldRev
249 ? $this->getCategoriesAtRev( $page, $oldRev, $parseTimestamp )
252 $newCategories = $this->getCategoriesAtRev( $page, $newRev, $parseTimestamp );
254 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
255 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
257 return [ $categoryInserts, $categoryDeletes ];
268 $services = MediaWikiServices::getInstance();
270 $options->setTimestamp( $parseTimestamp );
273 ? $services->getParserCache()->get( $page, $options )
276 if ( !$output || $output->getCacheRevisionId() !== $rev->
getId() ) {
277 $output = $services->getRevisionRenderer()->getRenderedRevision( $rev, $options )
278 ->getRevisionParserOutput();
283 return array_map(
'strval', array_keys( $output->getCategories() ) );
287 $info = parent::getDeduplicationInfo();
288 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.