MediaWiki  1.34.0
CategoryMembershipChange.php
Go to the documentation of this file.
1 <?php
2 
5 
31 
32  const CATEGORY_ADDITION = 1;
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 ) {
127  $this->notifyCategorization(
128  $this->timestamp,
129  $categoryTitle,
130  $this->getUser(),
131  $this->getChangeMessageText(
132  $type,
133  $this->pageTitle->getPrefixedText(),
135  ),
136  $this->pageTitle,
140  );
141  }
142 
155  private function notifyCategorization(
156  $timestamp,
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 
192  $timestamp,
193  $categoryTitle,
194  $user,
195  $comment,
196  $pageTitle,
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 }
Revision\getTimestamp
getTimestamp()
Definition: Revision.php:798
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
CategoryMembershipChange\$newForCategorizationCallback
callable null $newForCategorizationCallback
Definition: CategoryMembershipChange.php:60
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
CategoryMembershipChange\CATEGORY_ADDITION
const CATEGORY_ADDITION
Definition: CategoryMembershipChange.php:32
CategoryMembershipChange\overrideNewForCategorizationCallback
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
Definition: CategoryMembershipChange.php:88
CategoryMembershipChange\$pageTitle
Title $pageTitle
Title instance of the categorized page.
Definition: CategoryMembershipChange.php:43
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Revision\getId
getId()
Get revision ID.
Definition: Revision.php:442
Revision
Definition: Revision.php:40
MWException
MediaWiki exception.
Definition: MWException.php:26
CategoryMembershipChange\getChangeMessageText
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
Definition: CategoryMembershipChange.php:255
CategoryMembershipChange\triggerCategoryRemovedNotification
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
Definition: CategoryMembershipChange.php:116
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1898
CategoryMembershipChange\triggerCategoryAddedNotification
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
Definition: CategoryMembershipChange.php:107
CategoryMembershipChange\$timestamp
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Definition: CategoryMembershipChange.php:38
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:276
CategoryMembershipChange\$revision
Revision null $revision
Latest Revision instance of the categorized page.
Definition: CategoryMembershipChange.php:48
CategoryMembershipChange\getUser
getUser()
Get the user associated with this change.
Definition: CategoryMembershipChange.php:219
CategoryMembershipChange\notifyCategorization
notifyCategorization( $timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
Definition: CategoryMembershipChange.php:155
CategoryMembershipChange\createRecentChangesEntry
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Definition: CategoryMembershipChange.php:126
Revision\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: Revision.php:701
Title
Represents a title within MediaWiki.
Definition: Title.php:42
CategoryMembershipChange\CATEGORY_REMOVAL
const CATEGORY_REMOVAL
Definition: CategoryMembershipChange.php:33
CategoryMembershipChange\__construct
__construct(Title $pageTitle, Revision $revision=null)
Definition: CategoryMembershipChange.php:68
CategoryMembershipChange
Definition: CategoryMembershipChange.php:30
CategoryMembershipChange\$numTemplateLinks
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
Definition: CategoryMembershipChange.php:55
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
CategoryMembershipChange\checkTemplateLinks
checkTemplateLinks()
Determines the number of template links for recursive link updates.
Definition: CategoryMembershipChange.php:98
$type
$type
Definition: testCompression.php:48