MediaWiki REL1_30
CategoryMembershipChange.php
Go to the documentation of this file.
1<?php
26use Wikimedia\Assert\Assert;
27
29
31 const CATEGORY_REMOVAL = -1;
32
36 private $timestamp;
37
41 private $pageTitle;
42
46 private $revision;
47
53 private $numTemplateLinks = 0;
54
59
66 public function __construct( Title $pageTitle, Revision $revision = null ) {
67 $this->pageTitle = $pageTitle;
68 if ( $revision === null ) {
69 $this->timestamp = wfTimestampNow();
70 } else {
71 $this->timestamp = $revision->getTimestamp();
72 }
73 $this->revision = $revision;
74 $this->newForCategorizationCallback = [ 'RecentChange', 'newForCategorization' ];
75 }
76
86 public function overrideNewForCategorizationCallback( $callback ) {
87 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
88 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
89 }
90 Assert::parameterType( 'callable', $callback, '$callback' );
91 $this->newForCategorizationCallback = $callback;
92 }
93
97 public function checkTemplateLinks() {
98 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
99 }
100
106 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
107 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
108 }
109
115 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
116 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
117 }
118
125 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
127 $this->timestamp,
128 $categoryTitle,
129 $this->getUser(),
131 $type,
132 $this->pageTitle->getPrefixedText(),
133 $this->numTemplateLinks
134 ),
135 $this->pageTitle,
136 $this->getPreviousRevisionTimestamp(),
137 $this->revision,
138 $type === self::CATEGORY_ADDITION
139 );
140 }
141
154 private function notifyCategorization(
156 Title $categoryTitle,
157 User $user = null,
158 $comment,
159 Title $pageTitle,
160 $lastTimestamp,
161 $revision,
162 $added
163 ) {
165 $newRevId = $revision ? $revision->getId() : 0;
166
172 $bot = 1;
173 $lastRevId = 0;
174 $ip = '';
175
176 # If no revision is given, the change was probably triggered by parser functions
177 if ( $revision !== null ) {
178 $correspondingRc = $this->revision->getRecentChange();
179 if ( $correspondingRc === null ) {
180 $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
181 }
182 if ( $correspondingRc !== null ) {
183 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
184 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
185 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
186 }
187 }
188
190 $rc = call_user_func_array(
191 $this->newForCategorizationCallback,
192 [
194 $categoryTitle,
195 $user,
196 $comment,
197 $pageTitle,
198 $lastRevId,
199 $newRevId,
200 $lastTimestamp,
201 $bot,
202 $ip,
203 $deleted,
204 $added
205 ]
206 );
207 $rc->save();
208 }
209
221 private function getUser() {
222 if ( $this->revision ) {
223 $userId = $this->revision->getUser( Revision::RAW );
224 if ( $userId === 0 ) {
225 return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
226 } else {
227 return User::newFromId( $userId );
228 }
229 }
230
231 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
232 $user = User::newFromName( $username );
233 # User::newFromName() can return false on a badly configured wiki.
234 if ( $user && !$user->isLoggedIn() ) {
235 $user->addToDatabase();
236 }
237
238 return $user;
239 }
240
257 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
258 $array = [
259 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
260 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
261 ];
262
263 $msgKey = $array[$type];
264
265 if ( intval( $numTemplateLinks ) > 0 ) {
266 $msgKey .= '-bundled';
267 }
268
269 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
270 }
271
278 private function getPreviousRevisionTimestamp() {
279 $previousRev = Revision::newFromId(
280 $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
281 );
282
283 return $previousRev ? $previousRev->getTimestamp() : null;
284 }
285
286}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
overrideNewForCategorizationCallback( $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
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)
checkTemplateLinks()
Determines the number of template links for recursive link updates.
MediaWiki exception.
getId()
Get revision ID.
Definition Revision.php:743
getVisibility()
Get the deletion bitfield of the revision.
const RAW
Definition Revision.php:100
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition Revision.php:116
const SUPPRESSED_USER
Definition Revision.php:94
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:783
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247