MediaWiki  1.28.1
CategoryMembershipChange.php
Go to the documentation of this file.
1 <?php
27 
29 
30  const CATEGORY_ADDITION = 1;
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 ) {
126  $this->notifyCategorization(
127  $this->timestamp,
128  $categoryTitle,
129  $this->getUser(),
130  $this->getChangeMessageText(
131  $type,
132  [ 'prefixedText' => $this->pageTitle->getPrefixedText() ],
134  ),
135  $this->pageTitle,
138  );
139  }
140 
152  private function notifyCategorization(
153  $timestamp,
154  Title $categoryTitle,
155  User $user = null,
156  $comment,
158  $lastTimestamp,
159  $revision
160  ) {
162  $newRevId = $revision ? $revision->getId() : 0;
163 
169  $bot = 1;
170  $lastRevId = 0;
171  $ip = '';
172 
173  # If no revision is given, the change was probably triggered by parser functions
174  if ( $revision !== null ) {
175  $correspondingRc = $this->revision->getRecentChange();
176  if ( $correspondingRc === null ) {
177  $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
178  }
179  if ( $correspondingRc !== null ) {
180  $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
181  $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
182  $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
183  }
184  }
185 
187  $rc = call_user_func_array(
188  $this->newForCategorizationCallback,
189  [
190  $timestamp,
191  $categoryTitle,
192  $user,
193  $comment,
194  $pageTitle,
195  $lastRevId,
196  $newRevId,
197  $lastTimestamp,
198  $bot,
199  $ip,
200  $deleted
201  ]
202  );
203  $rc->save();
204  }
205 
217  private function getUser() {
218  if ( $this->revision ) {
219  $userId = $this->revision->getUser( Revision::RAW );
220  if ( $userId === 0 ) {
221  return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
222  } else {
223  return User::newFromId( $userId );
224  }
225  }
226 
227  $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
229  # User::newFromName() can return false on a badly configured wiki.
230  if ( $user && !$user->isLoggedIn() ) {
231  $user->addToDatabase();
232  }
233 
234  return $user;
235  }
236 
255  $array = [
256  self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
257  self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
258  ];
259 
260  $msgKey = $array[$type];
261 
262  if ( intval( $numTemplateLinks ) > 0 ) {
263  $msgKey .= '-bundled';
264  }
265 
266  return wfMessage( $msgKey, $params )->inContentLanguage()->text();
267  }
268 
275  private function getPreviousRevisionTimestamp() {
276  $previousRev = Revision::newFromId(
277  $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
278  );
279 
280  return $previousRev ? $previousRev->getTimestamp() : null;
281  }
282 
283 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
getUser()
Get the user associated with this change.
the array() calling protocol came about after MediaWiki 1.4rc1.
getChangeMessageText($type, array $params, $numTemplateLinks)
Returns the change message according to the type of category membership change.
__construct(Title $pageTitle, Revision $revision=null)
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getTimestamp()
Definition: Revision.php:1194
$comment
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks() ...
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:548
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor'$rcid is used in generating this variable which contains information about the new revision
Definition: hooks.txt:1156
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
getId()
Get revision ID.
Definition: Revision.php:729
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
$params
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Title $pageTitle
Title instance of the categorized page.
const RAW
Definition: Revision.php:94
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:242
const SUPPRESSED_USER
Definition: Revision.php:89
Revision null $revision
Latest Revision instance of the categorized page.
static newFromId($id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:110
getVisibility()
Get the deletion bitfield of the revision.
Definition: Revision.php:1031
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:802
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
checkTemplateLinks()
Determines the number of template links for recursive link updates.
overrideNewForCategorizationCallback($callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
notifyCategorization($timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $lastTimestamp, $revision)
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.