MediaWiki REL1_34
CategoryMembershipChange.php
Go to the documentation of this file.
1<?php
2
5
31
33 const CATEGORY_REMOVAL = -1;
34
38 private $timestamp;
39
43 private $pageTitle;
44
48 private $revision;
49
55 private $numTemplateLinks = 0;
56
61
68 public function __construct( Title $pageTitle, Revision $revision = null ) {
69 $this->pageTitle = $pageTitle;
70 if ( $revision === null ) {
71 $this->timestamp = wfTimestampNow();
72 } else {
73 $this->timestamp = $revision->getTimestamp();
74 }
75 $this->revision = $revision;
76 $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
77 }
78
88 public function overrideNewForCategorizationCallback( callable $callback ) {
89 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
90 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
91 }
92 $this->newForCategorizationCallback = $callback;
93 }
94
98 public function checkTemplateLinks() {
99 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
100 }
101
107 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
108 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
109 }
110
116 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
117 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
118 }
119
126 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
128 $this->timestamp,
129 $categoryTitle,
130 $this->getUser(),
132 $type,
133 $this->pageTitle->getPrefixedText(),
134 $this->numTemplateLinks
135 ),
136 $this->pageTitle,
137 $this->getPreviousRevisionTimestamp(),
138 $this->revision,
139 $type === self::CATEGORY_ADDITION
140 );
141 }
142
155 private function notifyCategorization(
157 Title $categoryTitle,
158 User $user = null,
159 $comment,
161 $lastTimestamp,
162 $revision,
163 $added
164 ) {
165 $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
166 $newRevId = $revision ? $revision->getId() : 0;
167
173 $bot = 1;
174 $lastRevId = 0;
175 $ip = '';
176
177 # If no revision is given, the change was probably triggered by parser functions
178 if ( $revision !== null ) {
179 $correspondingRc = $this->revision->getRecentChange();
180 if ( $correspondingRc === null ) {
181 $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
182 }
183 if ( $correspondingRc !== null ) {
184 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
185 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
186 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
187 }
188 }
189
193 $categoryTitle,
194 $user,
195 $comment,
197 $lastRevId,
198 $newRevId,
199 $lastTimestamp,
200 $bot,
201 $ip,
202 $deleted,
203 $added
204 );
205 $rc->save();
206 }
207
219 private function getUser() {
220 if ( $this->revision ) {
221 $userId = $this->revision->getUser( RevisionRecord::RAW );
222 if ( $userId === 0 ) {
223 return User::newFromName( $this->revision->getUserText( RevisionRecord::RAW ), false );
224 } else {
225 return User::newFromId( $userId );
226 }
227 }
228
229 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
230 $user = User::newFromName( $username );
231 # User::newFromName() can return false on a badly configured wiki.
232 if ( $user && !$user->isLoggedIn() ) {
233 $user->addToDatabase();
234 }
235
236 return $user;
237 }
238
255 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
256 $array = [
257 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
258 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
259 ];
260
261 $msgKey = $array[$type];
262
263 if ( intval( $numTemplateLinks ) > 0 ) {
264 $msgKey .= '-bundled';
265 }
266
267 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
268 }
269
276 private function getPreviousRevisionTimestamp() {
277 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
278 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
279 if ( $latestRev ) {
280 $previousRev = $rl->getPreviousRevision( $latestRev );
281 if ( $previousRev ) {
282 return $previousRev->getTimestamp();
283 }
284 }
285 return null;
286 }
287
288}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
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 $revision=null)
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()
Revision null $revision
Latest Revision instance of the categorized page.
Title $pageTitle
Title instance of the categorized page.
notifyCategorization( $timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
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.
getId()
Get revision ID.
Definition Revision.php:442
getTimestamp()
Definition Revision.php:798
getVisibility()
Get the deletion bitfield of the revision.
Definition Revision.php:701
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:51