40 private const CATEGORY_ADDITION = 1;
41 private const CATEGORY_REMOVAL = -1;
66 private $numTemplateLinks = 0;
71 private $newForCategorizationCallback =
null;
74 private $backlinkCache;
86 $this->pageTitle = $pageTitle;
87 $this->revision = $revision;
93 if ( $revision ===
null || $forImport ) {
98 $this->newForCategorizationCallback = [ RecentChange::class,
'newForCategorization' ];
99 $this->backlinkCache = $backlinkCache;
100 $this->forImport = $forImport;
111 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
112 throw new LogicException(
'Cannot override newForCategorization callback in operation.' );
114 $this->newForCategorizationCallback = $callback;
121 $this->numTemplateLinks = $this->backlinkCache->getNumLinks(
'templatelinks' );
130 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_ADDITION );
139 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_REMOVAL );
148 private function createRecentChangesEntry(
PageIdentity $categoryPage, $type ) {
149 $this->notifyCategorization(
153 $this->getChangeMessageText(
155 $this->pageTitle->getPrefixedText(),
156 $this->numTemplateLinks
159 $this->getPreviousRevisionTimestamp(),
162 $type === self::CATEGORY_ADDITION
177 private function notifyCategorization(
188 $deleted = $revision ? $revision->
getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
189 $newRevId = $revision ? $revision->
getId() : 0;
200 # If no revision is given, the change was probably triggered by parser functions
201 if ( $revision !==
null ) {
202 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
204 $correspondingRc = $revisionStore->getRecentChange( $this->revision ) ??
205 $revisionStore->getRecentChange( $this->revision, IDBAccessObject::READ_LATEST );
206 if ( $correspondingRc !==
null ) {
207 $bot = $correspondingRc->getAttribute(
'rc_bot' ) ?: 0;
208 $ip = $correspondingRc->getAttribute(
'rc_ip' ) ?:
'';
209 $lastRevId = $correspondingRc->getAttribute(
'rc_last_oldid' ) ?: 0;
214 $rc = ( $this->newForCategorizationCallback )(
244 if ( $this->revision ) {
245 $user = $this->revision->getUser( RevisionRecord::RAW );
251 $username =
wfMessage(
'autochange-username' )->inContentLanguage()->text();
253 $user = User::newSystemUser( $username );
255 $user->addToDatabase();
258 return $user ?:
null;
277 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
279 self::CATEGORY_ADDITION =>
'recentchanges-page-added-to-category',
280 self::CATEGORY_REMOVAL =>
'recentchanges-page-removed-from-category',
283 $msgKey = $array[$type];
285 if ( intval( $numTemplateLinks ) > 0 ) {
286 $msgKey .=
'-bundled';
289 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
298 private function getPreviousRevisionTimestamp() {
299 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
300 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
302 $previousRev = $rl->getPreviousRevision( $latestRev );
303 if ( $previousRev ) {
304 return $previousRev->getTimestamp();