54 'categoryMembershipChange',
56 'pageId' =>
$title->getArticleID(),
57 'revTimestamp' => $revisionTimestamp,
60 'removeDuplicates' =>
true,
61 'removeDuplicatesIgnoreParams' => [
'revTimestamp' ]
74 parent::__construct(
'categoryMembershipChange',
$title, $params );
77 $this->removeDuplicates =
true;
80 public function run() {
81 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
82 $lb = $lbFactory->getMainLB();
83 $dbw = $lb->getConnectionRef(
DB_MASTER );
85 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
87 $page = WikiPage::newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
89 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
95 if ( !$lb->waitForMasterPos(
$dbr ) ) {
96 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
101 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
102 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
103 if ( !$scopedLock ) {
104 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
109 if ( !$lb->waitForMasterPos(
$dbr ) ) {
110 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
114 $dbr->flushSnapshot( __METHOD__ );
116 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
119 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
123 $row =
$dbr->selectRow(
125 [
'rev_timestamp',
'rev_id' ],
127 'rev_page' => $page->getId(),
128 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
129 'EXISTS (' .
$dbr->selectSQLText(
133 'rc_this_oldid = rev_id',
134 'rc_source' => RecentChange::SRC_CATEGORIZE,
140 [
'ORDER BY' => [
'rev_timestamp DESC',
'rev_id DESC' ] ]
144 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
145 $lastRevId = (int)$row->rev_id;
152 $encCutoff =
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) );
153 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
154 $revQuery = $revisionStore->getQueryInfo();
159 'rev_page' => $page->getId(),
160 "rev_timestamp > $encCutoff" .
161 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
164 [
'ORDER BY' => [
'rev_timestamp ASC',
'rev_id ASC' ] ],
169 foreach (
$res as $row ) {
185 $config = RequestContext::getMain()->getConfig();
189 if ( $newRev->
isDeleted( RevisionRecord::DELETED_TEXT ) ) {
195 $oldRev = MediaWikiServices::getInstance()
196 ->getRevisionLookup()
197 ->getRevisionById( $newRev->
getParentId(), RevisionLookup::READ_LATEST );
198 if ( !$oldRev || $oldRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
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 $services = MediaWikiServices::getInstance();
267 $options->setTimestamp( $parseTimestamp );
270 ? $services->getParserCache()->get( $page, $options )
273 if ( !$output || $output->getCacheRevisionId() !== $rev->
getId() ) {
274 $output = $services->getRevisionRenderer()->getRenderedRevision( $rev, $options )
275 ->getRevisionParserOutput();
280 return array_map(
'strval', array_keys( $output->getCategories() ) );
284 $info = parent::getDeduplicationInfo();
285 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.
getCategoriesAtRev(WikiPage $page, RevisionRecord $rev, $parseTimestamp)
getExplicitCategoriesChanges(WikiPage $page, RevisionRecord $newRev, RevisionRecord $oldRev=null)
__construct(Title $title, 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)
static newSpec(Title $title, $revisionTimestamp)
Job queue task description base code.
Class to both describe a background job and handle jobs.
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.