MediaWiki  master
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 ) {
137  $this->notifyCategorization(
138  $this->timestamp,
139  $categoryTitle,
140  $this->getUser(),
141  $this->getChangeMessageText(
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(
166  $timestamp,
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 
207  $timestamp,
208  $categoryTitle,
209  $user,
210  $comment,
211  $pageTitle,
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 }
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
CategoryMembershipChange\$newForCategorizationCallback
callable null $newForCategorizationCallback
Definition: CategoryMembershipChange.php:61
CategoryMembershipChange\notifyCategorization
notifyCategorization( $timestamp, Title $categoryTitle, ?User $user, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
Definition: CategoryMembershipChange.php:165
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
CategoryMembershipChange\CATEGORY_ADDITION
const CATEGORY_ADDITION
Definition: CategoryMembershipChange.php:33
Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:80
CategoryMembershipChange\overrideNewForCategorizationCallback
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
Definition: CategoryMembershipChange.php:98
Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaces Revision::getTimestamp.
Definition: RevisionRecord.php:442
CategoryMembershipChange\$pageTitle
Title $pageTitle
Title instance of the categorized page.
Definition: CategoryMembershipChange.php:44
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:540
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1219
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:596
Revision
Definition: Revision.php:40
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1058
MWException
MediaWiki exception.
Definition: MWException.php:28
CategoryMembershipChange\getChangeMessageText
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
Definition: CategoryMembershipChange.php:268
CategoryMembershipChange\triggerCategoryRemovedNotification
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
Definition: CategoryMembershipChange.php:126
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1837
CategoryMembershipChange\triggerCategoryAddedNotification
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
Definition: CategoryMembershipChange.php:117
CategoryMembershipChange\$timestamp
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Definition: CategoryMembershipChange.php:39
CategoryMembershipChange\__construct
__construct(Title $pageTitle, $revision=null)
Definition: CategoryMembershipChange.php:70
CategoryMembershipChange\getPreviousRevisionTimestamp
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
Definition: CategoryMembershipChange.php:289
Revision\RevisionRecord\getId
getId()
Get revision ID.
Definition: RevisionRecord.php:279
CategoryMembershipChange\getUser
getUser()
Get the user associated with this change.
Definition: CategoryMembershipChange.php:234
CategoryMembershipChange\createRecentChangesEntry
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Definition: CategoryMembershipChange.php:136
Revision\RevisionRecord\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: RevisionRecord.php:431
Title
Represents a title within MediaWiki.
Definition: Title.php:42
CategoryMembershipChange\CATEGORY_REMOVAL
const CATEGORY_REMOVAL
Definition: CategoryMembershipChange.php:34
CategoryMembershipChange
Definition: CategoryMembershipChange.php:31
CategoryMembershipChange\$numTemplateLinks
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
Definition: CategoryMembershipChange.php:56
CategoryMembershipChange\$revision
RevisionRecord null $revision
Latest Revision instance of the categorized page.
Definition: CategoryMembershipChange.php:49
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
CategoryMembershipChange\checkTemplateLinks
checkTemplateLinks()
Determines the number of template links for recursive link updates.
Definition: CategoryMembershipChange.php:108
$type
$type
Definition: testCompression.php:52