37 private const CATEGORY_ADDITION = 1;
38 private const CATEGORY_REMOVAL = -1;
60 private $numTemplateLinks = 0;
65 private $newForCategorizationCallback =
null;
68 private $backlinkCache;
78 $this->pageTitle = $pageTitle;
79 $this->revision = $revision;
80 if ( $revision ===
null ) {
85 $this->newForCategorizationCallback = [ RecentChange::class,
'newForCategorization' ];
86 $this->backlinkCache = $backlinkCache;
97 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
98 throw new LogicException(
'Cannot override newForCategorization callback in operation.' );
100 $this->newForCategorizationCallback = $callback;
107 $this->numTemplateLinks = $this->backlinkCache->getNumLinks(
'templatelinks' );
116 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_ADDITION );
125 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_REMOVAL );
134 private function createRecentChangesEntry(
PageIdentity $categoryPage, $type ) {
135 $this->notifyCategorization(
139 $this->getChangeMessageText(
141 $this->pageTitle->getPrefixedText(),
142 $this->numTemplateLinks
145 $this->getPreviousRevisionTimestamp(),
147 $type === self::CATEGORY_ADDITION
161 private function notifyCategorization(
171 $deleted = $revision ? $revision->
getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
172 $newRevId = $revision ? $revision->
getId() : 0;
183 # If no revision is given, the change was probably triggered by parser functions
184 if ( $revision !==
null ) {
185 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
187 $correspondingRc = $revisionStore->getRecentChange( $this->revision ) ??
188 $revisionStore->getRecentChange( $this->revision, IDBAccessObject::READ_LATEST );
189 if ( $correspondingRc !==
null ) {
190 $bot = $correspondingRc->getAttribute(
'rc_bot' ) ?: 0;
191 $ip = $correspondingRc->getAttribute(
'rc_ip' ) ?:
'';
192 $lastRevId = $correspondingRc->getAttribute(
'rc_last_oldid' ) ?: 0;
197 $rc = ( $this->newForCategorizationCallback )(
226 if ( $this->revision ) {
227 $user = $this->revision->getUser( RevisionRecord::RAW );
233 $username =
wfMessage(
'autochange-username' )->inContentLanguage()->text();
235 $user = User::newSystemUser( $username );
237 $user->addToDatabase();
240 return $user ?:
null;
259 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
261 self::CATEGORY_ADDITION =>
'recentchanges-page-added-to-category',
262 self::CATEGORY_REMOVAL =>
'recentchanges-page-removed-from-category',
265 $msgKey = $array[$type];
267 if ( intval( $numTemplateLinks ) > 0 ) {
268 $msgKey .=
'-bundled';
271 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
280 private function getPreviousRevisionTimestamp() {
281 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
282 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
284 $previousRev = $rl->getPreviousRevision( $latestRev );
285 if ( $previousRev ) {
286 return $previousRev->getTimestamp();