55 'categoryMembershipChange',
57 'pageId' => $page->
getId(),
58 'revTimestamp' => $revisionTimestamp,
61 'removeDuplicates' =>
true,
62 'removeDuplicatesIgnoreParams' => [
'revTimestamp' ]
75 parent::__construct(
'categoryMembershipChange', $page, $params );
78 $this->removeDuplicates =
true;
81 public function run() {
82 $services = MediaWikiServices::getInstance();
83 $lbFactory = $services->getDBLoadBalancerFactory();
84 $lb = $lbFactory->getMainLB();
87 $this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
89 $page = $services->getWikiPageFactory()->newFromID( $this->params[
'pageId'], WikiPage::READ_LATEST );
91 $this->
setLastError(
"Could not find page #{$this->params['pageId']}" );
97 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
98 $this->
setLastError(
"Timed out while pre-waiting for replica DB to catch up" );
103 $lockKey =
"{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}";
104 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 );
105 if ( !$scopedLock ) {
106 $this->
setLastError(
"Could not acquire lock '$lockKey'" );
111 if ( !$lb->waitForPrimaryPos(
$dbr ) ) {
112 $this->
setLastError(
"Timed out while waiting for replica DB to catch up" );
116 $dbr->flushSnapshot( __METHOD__ );
118 $cutoffUnix =
wfTimestamp( TS_UNIX, $this->params[
'revTimestamp'] );
121 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
125 $row =
$dbr->selectRow(
127 [
'rev_timestamp',
'rev_id' ],
129 'rev_page' => $page->getId(),
130 'rev_timestamp >= ' .
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) ),
131 'EXISTS (' .
$dbr->selectSQLText(
135 'rc_this_oldid = rev_id',
136 'rc_source' => RecentChange::SRC_CATEGORIZE,
142 [
'ORDER BY' => [
'rev_timestamp DESC',
'rev_id DESC' ] ]
146 $cutoffUnix =
wfTimestamp( TS_UNIX, $row->rev_timestamp );
147 $lastRevId = (int)$row->rev_id;
154 $encCutoff =
$dbr->addQuotes(
$dbr->timestamp( $cutoffUnix ) );
155 $revisionStore = $services->getRevisionStore();
156 $revQuery = $revisionStore->getQueryInfo();
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();
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 ) ) {
209 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
210 if ( !$categoryInserts && !$categoryDeletes ) {
214 $blc = $services->getBacklinkCacheFactory()->getBacklinkCache(
$title );
216 $catMembChange->checkTemplateLinks();
218 $batchSize = $config->get(
'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 ) {
248 $oldCategories = $oldRev
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.
getCategoriesAtRev(WikiPage $page, RevisionRecord $rev, $parseTimestamp)
getExplicitCategoriesChanges(WikiPage $page, RevisionRecord $newRev, RevisionRecord $oldRev=null)
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.
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.
Interface for objects (potentially) representing an editable wiki page.
getId( $wikiId=self::LOCAL)
Returns the page ID.