MediaWiki REL1_35
CategoryMembershipChange.php
Go to the documentation of this file.
1<?php
2
6
32
33 private const CATEGORY_ADDITION = 1;
34 private const CATEGORY_REMOVAL = -1;
35
39 private $timestamp;
40
44 private $pageTitle;
45
49 private $revision;
50
56 private $numTemplateLinks = 0;
57
62
70 public function __construct( Title $pageTitle, $revision = null ) {
71 $this->pageTitle = $pageTitle;
72 if ( $revision instanceof Revision ) {
74 'Passing a Revision for the $revision parameter to ' . __METHOD__ .
75 ' was deprecated in MediaWiki 1.35',
76 '1.35'
77 );
78 $revision = $revision->getRevisionRecord();
79 }
80 $this->revision = $revision;
81 if ( $revision === null ) {
82 $this->timestamp = wfTimestampNow();
83 } else {
84 $this->timestamp = $revision->getTimestamp();
85 }
86 $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
87 }
88
98 public function overrideNewForCategorizationCallback( callable $callback ) {
99 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
100 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
101 }
102 $this->newForCategorizationCallback = $callback;
103 }
104
108 public function checkTemplateLinks() {
109 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
110 }
111
117 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
118 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
119 }
120
126 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
127 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
128 }
129
136 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
138 $this->timestamp,
139 $categoryTitle,
140 $this->getUser(),
142 $type,
143 $this->pageTitle->getPrefixedText(),
144 $this->numTemplateLinks
145 ),
146 $this->pageTitle,
147 $this->getPreviousRevisionTimestamp(),
148 $this->revision,
149 $type === self::CATEGORY_ADDITION
150 );
151 }
152
165 private function notifyCategorization(
167 Title $categoryTitle,
168 ?User $user,
169 $comment,
171 $lastTimestamp,
172 $revision,
173 $added
174 ) {
175 $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
176 $newRevId = $revision ? $revision->getId() : 0;
177
183 $bot = 1;
184 $lastRevId = 0;
185 $ip = '';
186
187 # If no revision is given, the change was probably triggered by parser functions
188 if ( $revision !== null ) {
189 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
190
191 $correspondingRc = $revisionStore->getRecentChange( $this->revision );
192 if ( $correspondingRc === null ) {
193 $correspondingRc = $revisionStore->getRecentChange(
194 $this->revision,
195 RevisionStore::READ_LATEST
196 );
197 }
198 if ( $correspondingRc !== null ) {
199 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
200 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
201 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
202 }
203 }
204
208 $categoryTitle,
209 $user,
210 $comment,
212 $lastRevId,
213 $newRevId,
214 $lastTimestamp,
215 $bot,
216 $ip,
217 $deleted,
218 $added
219 );
220 $rc->save();
221 }
222
234 private function getUser() {
235 if ( $this->revision ) {
236 $userIdentity = $this->revision->getUser( RevisionRecord::RAW );
237 if ( $userIdentity ) {
238 return User::newFromIdentity( $userIdentity );
239 }
240 }
241
242 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
243 $user = User::newFromName( $username );
244 # User::newFromName() can return false on a badly configured wiki.
245 if ( $user && !$user->isLoggedIn() ) {
246 $user->addToDatabase();
247 }
248
249 return $user;
250 }
251
268 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
269 $array = [
270 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
271 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
272 ];
273
274 $msgKey = $array[$type];
275
276 if ( intval( $numTemplateLinks ) > 0 ) {
277 $msgKey .= '-bundled';
278 }
279
280 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
281 }
282
289 private function getPreviousRevisionTimestamp() {
290 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
291 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
292 if ( $latestRev ) {
293 $previousRev = $rl->getPreviousRevision( $latestRev );
294 if ( $previousRev ) {
295 return $previousRev->getTimestamp();
296 }
297 }
298 return null;
299 }
300
301}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
__construct(Title $pageTitle, $revision=null)
notifyCategorization( $timestamp, Title $categoryTitle, ?User $user, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
getUser()
Get the user associated with this change.
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Title $pageTitle
Title instance of the categorized page.
RevisionRecord null $revision
Latest Revision instance of the categorized page.
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
checkTemplateLinks()
Determines the number of template links for recursive link updates.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
getVisibility()
Get the deletion bitfield of the revision.
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Service for looking up page revisions.
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:597