56 'categoryMembershipChange',
58 'pageId' =>
$title->getArticleID(),
59 'revTimestamp' => $revisionTimestamp,
74 parent::__construct(
'categoryMembershipChange',
$title, $params );
77 $this->removeDuplicates =
true;
78 $this->parserCache = $parserCache;
81 public function run() {
82 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
83 $lb = $lbFactory->getMainLB();
84 $dbw = $lb->getConnectionRef(
DB_MASTER );
86 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
88 $page = WikiPage::newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
90 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
96 if ( !$lb->waitForMasterPos(
$dbr ) ) {
97 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
102 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
103 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
104 if ( !$scopedLock ) {
105 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
110 if ( !$lb->waitForMasterPos(
$dbr ) ) {
111 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
115 $dbr->flushSnapshot( __METHOD__ );
117 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
120 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
124 $row =
$dbr->selectRow(
126 [
'rev_timestamp',
'rev_id' ],
128 'rev_page' => $page->getId(),
129 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
130 'EXISTS (' .
$dbr->selectSQLText(
134 'rc_this_oldid = rev_id',
135 'rc_source' => RecentChange::SRC_CATEGORIZE,
137 'rc_cur_id = rev_page',
138 'rc_timestamp = rev_timestamp'
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 ) );
161 'rev_page' => $page->getId(),
162 "rev_timestamp > $encCutoff" .
163 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
166 [
'ORDER BY' =>
'rev_timestamp ASC, rev_id ASC' ],
171 foreach (
$res as $row ) {
187 $config = RequestContext::getMain()->getConfig();
198 if ( !$oldRev || !$oldRev->getContent() ) {
207 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
208 if ( !$categoryInserts && !$categoryDeletes ) {
213 $catMembChange->checkTemplateLinks();
215 $batchSize = $config->get(
'UpdateRowsPerQuery' );
218 foreach ( $categoryInserts as $categoryName ) {
219 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
220 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
221 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
226 foreach ( $categoryDeletes as $categoryName ) {
227 $categoryTitle = Title::makeTitle(
NS_CATEGORY, $categoryName );
228 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
229 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
245 $oldCategories = $oldRev
249 $newCategories = $this->getCategoriesAtRev( $page, $newRev, $parseTimestamp );
251 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
252 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
254 return [ $categoryInserts, $categoryDeletes ];
265 $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
267 $options->setTimestamp( $parseTimestamp );
269 $output = $rev->
isCurrent() ? $this->parserCache->get( $page, $options ) :
null;
271 if ( !$output || $output->getCacheRevisionId() !== $rev->
getId() ) {
273 ->getRevisionParserOutput();
278 return array_map(
'strval', array_keys( $output->getCategories() ) );
282 $info = parent::getDeduplicationInfo();
283 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.
getExplicitCategoriesChanges(WikiPage $page, Revision $newRev, Revision $oldRev=null)
__construct(ParserCache $parserCache, Title $title, array $params)
Constructor for use by the Job Queue infrastructure.
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
static newSpec(Title $title, $revisionTimestamp)
notifyUpdatesForRevision(LBFactory $lbFactory, WikiPage $page, Revision $newRev)
getCategoriesAtRev(WikiPage $page, Revision $rev, $parseTimestamp)
Job queue task description base code.
Class to both describe a background job and handle jobs.
getContent( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision content if it's available to the specified audience.
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
getParentId()
Get parent revision ID (the original previous page revision)
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Represents a title within MediaWiki.
Class representing a MediaWiki article and history.
makeParserOptions( $context)
Get parser options suitable for rendering the primary article wikitext.
getTitle()
Get the title object of the article.