MediaWiki  1.33.0
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
23 
25 
26  /* @var IContextSource */
27  private $context;
28 
29  /* @var string[] */
30  private $messages;
31 
35  private $linkRenderer;
36 
42  public function __construct(
44  ) {
45  $this->context = $context;
46  $this->messages = $messages;
47  $this->linkRenderer = $linkRenderer;
48  }
49 
56  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
57  $user = $this->context->getUser();
58  $counter = $baseRC->counter;
59 
60  $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
61 
62  // Should patrol-related stuff be shown?
63  $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
64 
65  $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
66  $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
67 
68  $cacheEntry->link = $this->buildCLink( $cacheEntry );
69  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
70 
71  // Make "cur" and "diff" links. Do not use link(), it is too slow if
72  // called too many times (50% of CPU time on RecentChanges!).
73  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
74 
75  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
76  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
77  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
78 
79  // Make user links
80  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
81 
82  if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
83  $cacheEntry->usertalklink = Linker::userToolLinks(
84  $cacheEntry->mAttribs['rc_user'],
85  $cacheEntry->mAttribs['rc_user_text'],
86  // Should the contributions link be red if the user has no edits (using default)
87  false,
88  // Customisation flags (using default 0)
89  0,
90  // User edit count (using default )
91  null,
92  // do not wrap the message in parentheses
93  false
94  );
95  }
96 
97  return $cacheEntry;
98  }
99 
106  private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
107  return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
108  }
109 
115  private function buildCLink( RecentChange $cacheEntry ) {
116  $type = $cacheEntry->mAttribs['rc_type'];
117 
118  // New unpatrolled pages
119  if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
120  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
121  // Log entries
122  } elseif ( $type == RC_LOG ) {
123  $logType = $cacheEntry->mAttribs['rc_log_type'];
124 
125  if ( $logType ) {
126  $clink = $this->getLogLink( $logType );
127  } else {
128  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
129  $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
130  }
131  // Log entries (old format) and special pages
132  } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
133  wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
134  $clink = '';
135  // Edits
136  } else {
137  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
138  }
139 
140  return $clink;
141  }
142 
143  private function getLogLink( $logType ) {
144  $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
145  $logpage = new LogPage( $logType );
146  $logname = $logpage->getName()->text();
147 
148  $logLink = $this->context->msg( 'parentheses' )
149  ->rawParams(
150  $this->linkRenderer->makeKnownLink( $logtitle, $logname )
151  )->escaped();
152 
153  return $logLink;
154  }
155 
161  private function buildTimestamp( RecentChange $cacheEntry ) {
162  return $this->context->getLanguage()->userTime(
163  $cacheEntry->mAttribs['rc_timestamp'],
164  $this->context->getUser()
165  );
166  }
167 
173  private function buildCurQueryParams( RecentChange $recentChange ) {
174  return [
175  'curid' => $recentChange->mAttribs['rc_cur_id'],
176  'diff' => 0,
177  'oldid' => $recentChange->mAttribs['rc_this_oldid']
178  ];
179  }
180 
188  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
189  $queryParams = $this->buildCurQueryParams( $cacheEntry );
190  $curMessage = $this->getMessage( 'cur' );
191  $logTypes = [ RC_LOG ];
192 
193  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
194  $curLink = $curMessage;
195  } else {
196  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
197  $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
198  }
199 
200  return $curLink;
201  }
202 
208  private function buildDiffQueryParams( RecentChange $recentChange ) {
209  return [
210  'curid' => $recentChange->mAttribs['rc_cur_id'],
211  'diff' => $recentChange->mAttribs['rc_this_oldid'],
212  'oldid' => $recentChange->mAttribs['rc_last_oldid']
213  ];
214  }
215 
223  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
224  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
225  $diffMessage = $this->getMessage( 'diff' );
226  $logTypes = [ RC_NEW, RC_LOG ];
227 
228  if ( !$showDiffLinks ) {
229  $diffLink = $diffMessage;
230  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
231  $diffLink = $diffMessage;
232  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
233  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
234  $pageTitle = Title::newFromID( $rcCurId );
235  if ( $pageTitle === null ) {
236  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
237  return $diffMessage;
238  }
239  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
240  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
241  } else {
242  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
243  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
244  }
245 
246  return $diffLink;
247  }
248 
257  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
258  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
259  $lastMessage = $this->getMessage( 'last' );
260  $type = $cacheEntry->mAttribs['rc_type'];
261  $logTypes = [ RC_LOG ];
262 
263  // Make "last" link
264  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
265  $lastLink = $lastMessage;
266  } else {
267  $lastLink = $this->linkRenderer->makeKnownLink(
268  $cacheEntry->getTitle(),
269  new HtmlArmor( $lastMessage ),
270  [ 'class' => 'mw-changeslist-diff' ],
271  $this->buildDiffQueryParams( $cacheEntry )
272  );
273  }
274 
275  return $lastLink;
276  }
277 
283  private function getUserLink( RecentChange $cacheEntry ) {
284  if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
285  $userLink = ' <span class="history-deleted">' .
286  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
287  } else {
288  $userLink = Linker::userLink(
289  $cacheEntry->mAttribs['rc_user'],
290  $cacheEntry->mAttribs['rc_user_text'],
291  ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
292  );
293  }
294 
295  return $userLink;
296  }
297 
303  private function getMessage( $key ) {
304  return $this->messages[$key];
305  }
306 
307 }
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:48
RCCacheEntryFactory\showDiffLinks
showDiffLinks(RecentChange $cacheEntry, User $user)
Definition: RCCacheEntryFactory.php:106
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition: ExternalUserNames.php:145
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
RCCacheEntryFactory\$context
$context
Definition: RCCacheEntryFactory.php:27
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:28
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:892
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:69
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
Linker\userToolLinks
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:931
RC_LOG
const RC_LOG
Definition: Defines.php:144
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:82
RCCacheEntryFactory
Definition: RCCacheEntryFactory.php:24
RCCacheEntryFactory\getLogLink
getLogLink( $logType)
Definition: RCCacheEntryFactory.php:143
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1043
ChangesList\isDeleted
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
Definition: ChangesList.php:655
php
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
RCCacheEntryFactory\buildDiffQueryParams
buildDiffQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:208
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
RCCacheEntry\newFromParent
static newFromParent( $rc)
Definition: RCCacheEntry.php:38
RCCacheEntryFactory\buildCLink
buildCLink(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:115
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RCCacheEntryFactory\getUserLink
getUserLink(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:283
RCCacheEntryFactory\__construct
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
Definition: RCCacheEntryFactory.php:42
RCCacheEntryFactory\buildCurLink
buildCurLink(RecentChange $cacheEntry, $showDiffLinks, $counter)
Definition: RCCacheEntryFactory.php:188
RCCacheEntryFactory\buildDiffLink
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks, $counter)
Definition: RCCacheEntryFactory.php:223
RCCacheEntryFactory\buildTimestamp
buildTimestamp(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:161
RC_NEW
const RC_NEW
Definition: Defines.php:143
RCCacheEntryFactory\$messages
$messages
Definition: RCCacheEntryFactory.php:30
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
RCCacheEntryFactory\$linkRenderer
LinkRenderer $linkRenderer
Definition: RCCacheEntryFactory.php:35
RecentChange\getAttribute
getAttribute( $name)
Get an attribute value.
Definition: RecentChange.php:1078
RCCacheEntryFactory\buildCurQueryParams
buildCurQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:173
messages
passed in as a query string parameter to the various URLs constructed here(i.e. $prevlink) $ldel you ll need to handle error messages
Definition: hooks.txt:1290
RCCacheEntryFactory\newFromRecentChange
newFromRecentChange(RecentChange $baseRC, $watched)
Definition: RCCacheEntryFactory.php:56
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:146
ChangesList\isUnpatrolled
static isUnpatrolled( $rc, User $user)
Definition: ChangesList.php:772
RecentChange\getTitle
& getTitle()
Definition: RecentChange.php:331
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:457
RCCacheEntryFactory\buildLastLink
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Builds the link to the previous version.
Definition: RCCacheEntryFactory.php:257
Revision\DELETED_TEXT
const DELETED_TEXT
Definition: Revision.php:46
ChangesList\userCan
static userCan( $rc, $field, User $user=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: ChangesList.php:667
RCCacheEntryFactory\getMessage
getMessage( $key)
Definition: RCCacheEntryFactory.php:303
$type
$type
Definition: testCompression.php:48