| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MainConfigNames; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Page\PageIdentity; |
| 24 |
use MediaWiki\Revision\RevisionRecord; |
| 25 |
use MediaWiki\Revision\RevisionStoreRecord; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 28 |
use Wikimedia\Rdbms\LBFactory; |
| 29 |
use Wikimedia\Rdbms\RawSQLExpression; |
| 30 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class CategoryMembershipChangeJob extends Job { |
| 47 |
|
| 48 |
private $ticket; |
| 49 |
|
| 50 |
private const ENQUEUE_FUDGE_SEC = 60; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static function newSpec( PageIdentity $page, $revisionTimestamp, bool $forImport ) { |
| 59 |
return new JobSpecification( |
| 60 |
'categoryMembershipChange', |
| 61 |
[ |
| 62 |
'pageId' => $page->getId(), |
| 63 |
'revTimestamp' => $revisionTimestamp, |
| 64 |
'forImport' => $forImport, |
| 65 |
], |
| 66 |
[ |
| 67 |
'removeDuplicates' => true, |
| 68 |
'removeDuplicatesIgnoreParams' => [ 'revTimestamp' ] |
| 69 |
], |
| 70 |
$page |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function __construct( PageIdentity $page, array $params ) { |
| 81 |
parent::__construct( 'categoryMembershipChange', $page, $params ); |
| 82 |
|
| 83 |
|
| 84 |
$this->removeDuplicates = true; |
| 85 |
} |
| 86 |
|
| 87 |
public function run() { |
| 88 |
$services = MediaWikiServices::getInstance(); |
| 89 |
$lbFactory = $services->getDBLoadBalancerFactory(); |
| 90 |
$lb = $lbFactory->getMainLB(); |
| 91 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 92 |
|
| 93 |
$this->ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 94 |
|
| 95 |
$page = $services->getWikiPageFactory()->newFromID( $this->params['pageId'], IDBAccessObject::READ_LATEST ); |
| 96 |
if ( !$page ) { |
| 97 |
$this->setLastError( "Could not find page #{$this->params['pageId']}" ); |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 103 |
if ( !$lb->waitForPrimaryPos( $dbr ) ) { |
| 104 |
$this->setLastError( "Timed out while pre-waiting for replica DB to catch up" ); |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
$lockKey = "{$dbw->getDomainID()}:CategoryMembershipChange:{$page->getId()}"; |
| 110 |
$scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 3 ); |
| 111 |
if ( !$scopedLock ) { |
| 112 |
$this->setLastError( "Could not acquire lock '$lockKey'" ); |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if ( !$lb->waitForPrimaryPos( $dbr ) ) { |
| 118 |
$this->setLastError( "Timed out while waiting for replica DB to catch up" ); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
$dbr->flushSnapshot( __METHOD__ ); |
| 123 |
|
| 124 |
$cutoffUnix = wfTimestamp( TS_UNIX, $this->params['revTimestamp'] ); |
| 125 |
|
| 126 |
|
| 127 |
$cutoffUnix -= self::ENQUEUE_FUDGE_SEC; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
$subQuery = $dbr->newSelectQueryBuilder() |
| 132 |
->select( '1' ) |
| 133 |
->from( 'recentchanges' ) |
| 134 |
->where( 'rc_this_oldid = rev_id' ) |
| 135 |
->andWhere( [ 'rc_source' => RecentChange::SRC_CATEGORIZE ] ); |
| 136 |
$row = $dbr->newSelectQueryBuilder() |
| 137 |
->select( [ 'rev_timestamp', 'rev_id' ] ) |
| 138 |
->from( 'revision' ) |
| 139 |
->where( [ 'rev_page' => $page->getId() ] ) |
| 140 |
->andWhere( $dbr->expr( 'rev_timestamp', '>=', $dbr->timestamp( $cutoffUnix ) ) ) |
| 141 |
->andWhere( new RawSQLExpression( 'EXISTS (' . $subQuery->getSQL() . ')' ) ) |
| 142 |
->orderBy( [ 'rev_timestamp', 'rev_id' ], SelectQueryBuilder::SORT_DESC ) |
| 143 |
->caller( __METHOD__ )->fetchRow(); |
| 144 |
|
| 145 |
|
| 146 |
if ( $row ) { |
| 147 |
$cutoffUnix = wfTimestamp( TS_UNIX, $row->rev_timestamp ); |
| 148 |
$lastRevId = (int)$row->rev_id; |
| 149 |
} else { |
| 150 |
$lastRevId = 0; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
$revisionStore = $services->getRevisionStore(); |
| 156 |
$res = $revisionStore->newSelectQueryBuilder( $dbr ) |
| 157 |
->joinComment() |
| 158 |
->where( [ |
| 159 |
'rev_page' => $page->getId(), |
| 160 |
$dbr->buildComparison( '>', [ |
| 161 |
'rev_timestamp' => $dbr->timestamp( $cutoffUnix ), |
| 162 |
'rev_id' => $lastRevId, |
| 163 |
] ) |
| 164 |
] ) |
| 165 |
->orderBy( [ 'rev_timestamp', 'rev_id' ], SelectQueryBuilder::SORT_ASC ) |
| 166 |
->caller( __METHOD__ )->fetchResultSet(); |
| 167 |
|
| 168 |
|
| 169 |
foreach ( $res as $row ) { |
| 170 |
$this->notifyUpdatesForRevision( $lbFactory, $page, $revisionStore->newRevisionFromRow( $row ) ); |
| 171 |
} |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
protected function notifyUpdatesForRevision( |
| 182 |
LBFactory $lbFactory, WikiPage $page, RevisionRecord $newRev |
| 183 |
) { |
| 184 |
$title = $page->getTitle(); |
| 185 |
|
| 186 |
|
| 187 |
if ( $newRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$services = MediaWikiServices::getInstance(); |
| 192 |
|
| 193 |
if ( $newRev->getParentId() ) { |
| 194 |
$oldRev = $services->getRevisionLookup() |
| 195 |
->getRevisionById( $newRev->getParentId(), IDBAccessObject::READ_LATEST ); |
| 196 |
if ( !$oldRev || $oldRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 197 |
return; |
| 198 |
} |
| 199 |
} else { |
| 200 |
$oldRev = null; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
$categoryChanges = $this->getExplicitCategoriesChanges( $page, $newRev, $oldRev ); |
| 205 |
[ $categoryInserts, $categoryDeletes ] = $categoryChanges; |
| 206 |
if ( !$categoryInserts && !$categoryDeletes ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$blc = $services->getBacklinkCacheFactory()->getBacklinkCache( $title ); |
| 211 |
$catMembChange = new CategoryMembershipChange( $title, $blc, $newRev, $this->params['forImport'] ?? false ); |
| 212 |
$catMembChange->checkTemplateLinks(); |
| 213 |
|
| 214 |
$batchSize = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery ); |
| 215 |
$insertCount = 0; |
| 216 |
|
| 217 |
foreach ( $categoryInserts as $categoryName ) { |
| 218 |
$categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName ); |
| 219 |
$catMembChange->triggerCategoryAddedNotification( $categoryTitle ); |
| 220 |
if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) { |
| 221 |
$lbFactory->commitAndWaitForReplication( __METHOD__, $this->ticket ); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
foreach ( $categoryDeletes as $categoryName ) { |
| 226 |
$categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName ); |
| 227 |
$catMembChange->triggerCategoryRemovedNotification( $categoryTitle ); |
| 228 |
if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) { |
| 229 |
$lbFactory->commitAndWaitForReplication( __METHOD__, $this->ticket ); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
private function getExplicitCategoriesChanges( |
| 235 |
WikiPage $page, RevisionRecord $newRev, ?RevisionRecord $oldRev = null |
| 236 |
) { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
$parseTimestamp = $newRev->getTimestamp(); |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
$oldCategories = $oldRev |
| 245 |
? $this->getCategoriesAtRev( $page, $oldRev, $parseTimestamp ) |
| 246 |
: []; |
| 247 |
|
| 248 |
$newCategories = $this->getCategoriesAtRev( $page, $newRev, $parseTimestamp ); |
| 249 |
|
| 250 |
$categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) ); |
| 251 |
$categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) ); |
| 252 |
|
| 253 |
return [ $categoryInserts, $categoryDeletes ]; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private function getCategoriesAtRev( WikiPage $page, RevisionRecord $rev, $parseTimestamp ) { |
| 264 |
$services = MediaWikiServices::getInstance(); |
| 265 |
$options = $page->makeParserOptions( 'canonical' ); |
| 266 |
$options->setTimestamp( $parseTimestamp ); |
| 267 |
$options->setRenderReason( 'CategoryMembershipChangeJob' ); |
| 268 |
|
| 269 |
$output = $rev instanceof RevisionStoreRecord && $rev->isCurrent() |
| 270 |
? $services->getParserCache()->get( $page, $options ) |
| 271 |
: null; |
| 272 |
|
| 273 |
if ( !$output || $output->getCacheRevisionId() !== $rev->getId() ) { |
| 274 |
$output = $services->getRevisionRenderer()->getRenderedRevision( $rev, $options ) |
| 275 |
->getRevisionParserOutput(); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
return $output->getCategoryNames(); |
| 282 |
} |
| 283 |
|
| 284 |
public function getDeduplicationInfo() { |
| 285 |
$info = parent::getDeduplicationInfo(); |
| 286 |
unset( $info['params']['revTimestamp'] ); |
| 287 |
|
| 288 |
return $info; |
| 289 |
} |
| 290 |
} |