34 private const CATEGORY_ADDITION = 1;
35 private const CATEGORY_REMOVAL = -1;
57 private $numTemplateLinks = 0;
62 private $newForCategorizationCallback =
null;
65 private $backlinkCache;
77 $this->pageTitle = $pageTitle;
78 $this->revision = $revision;
79 if ( $revision ===
null ) {
84 $this->newForCategorizationCallback = [ RecentChange::class,
'newForCategorization' ];
85 $this->backlinkCache = $backlinkCache;
98 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
99 throw new MWException(
'Cannot override newForCategorization callback in operation.' );
101 $this->newForCategorizationCallback = $callback;
108 $this->numTemplateLinks = $this->backlinkCache->getNumLinks(
'templatelinks' );
117 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
126 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
135 private function createRecentChangesEntry(
Title $categoryTitle,
$type ) {
136 $this->notifyCategorization(
140 $this->getChangeMessageText(
142 $this->pageTitle->getPrefixedText(),
143 $this->numTemplateLinks
146 $this->getPreviousRevisionTimestamp(),
148 $type === self::CATEGORY_ADDITION
164 private function notifyCategorization(
166 Title $categoryTitle,
174 $deleted = $revision ? $revision->
getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
175 $newRevId = $revision ? $revision->
getId() : 0;
186 # If no revision is given, the change was probably triggered by parser functions
187 if ( $revision !==
null ) {
188 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
190 $correspondingRc = $revisionStore->getRecentChange( $this->revision );
191 if ( $correspondingRc ===
null ) {
192 $correspondingRc = $revisionStore->getRecentChange(
194 RevisionStore::READ_LATEST
197 if ( $correspondingRc !==
null ) {
198 $bot = $correspondingRc->getAttribute(
'rc_bot' ) ?: 0;
199 $ip = $correspondingRc->getAttribute(
'rc_ip' ) ?:
'';
200 $lastRevId = $correspondingRc->getAttribute(
'rc_last_oldid' ) ?: 0;
205 $rc = ( $this->newForCategorizationCallback )(
234 if ( $this->revision ) {
235 $user = $this->revision->getUser( RevisionRecord::RAW );
241 $username =
wfMessage(
'autochange-username' )->inContentLanguage()->text();
245 $user->addToDatabase();
248 return $user ?:
null;
267 private function getChangeMessageText(
$type, $prefixedText, $numTemplateLinks ) {
269 self::CATEGORY_ADDITION =>
'recentchanges-page-added-to-category',
270 self::CATEGORY_REMOVAL =>
'recentchanges-page-removed-from-category',
273 $msgKey = $array[
$type];
275 if ( intval( $numTemplateLinks ) > 0 ) {
276 $msgKey .=
'-bundled';
279 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
288 private function getPreviousRevisionTimestamp() {
289 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
290 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
292 $previousRev = $rl->getPreviousRevision( $latestRev );
293 if ( $previousRev ) {
294 return $previousRev->getTimestamp();