MediaWiki  master
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
30 
32 
34  private $context;
35 
37  private $messages;
38 
42  private $linkRenderer;
43 
47  private MapCacheLRU $userLinkCache;
48 
52  private MapCacheLRU $toolLinkCache;
53 
59  public function __construct(
60  IContextSource $context, $messages, LinkRenderer $linkRenderer
61  ) {
62  $this->context = $context;
63  $this->messages = $messages;
64  $this->linkRenderer = $linkRenderer;
65  $this->userLinkCache = new MapCacheLRU( 50 );
66  $this->toolLinkCache = new MapCacheLRU( 50 );
67  }
68 
75  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
76  $user = $this->context->getUser();
77 
78  $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
79 
80  // Should patrol-related stuff be shown?
81  $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
82 
83  $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
84  $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
85  $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
86 
87  $cacheEntry->link = $this->buildCLink( $cacheEntry );
88  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
89 
90  // Make "cur" and "diff" links. Do not use link(), it is too slow if
91  // called too many times (50% of CPU time on RecentChanges!).
92  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
93 
94  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
95  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
96  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
97 
98  // Make user links
99  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
100 
101  if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
108  $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
109  $this->toolLinkCache->makeKey(
110  $cacheEntry->mAttribs['rc_user_text'],
111  $this->context->getUser()->getName(),
112  $this->context->getLanguage()->getCode()
113  ),
114  static fn() => Linker::userToolLinks(
115  $cacheEntry->mAttribs['rc_user'],
116  $cacheEntry->mAttribs['rc_user_text'],
117  // Should the contributions link be red if the user has no edits (using default)
118  false,
119  // Customisation flags (using default 0)
120  0,
121  // User edit count (using default )
122  null,
123  // do not wrap the message in parentheses
124  false
125  )
126  );
127  }
128 
129  return $cacheEntry;
130  }
131 
138  private function showDiffLinks( RecentChange $cacheEntry, Authority $performer ) {
139  return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $performer );
140  }
141 
147  private function buildCLink( RCCacheEntry $cacheEntry ) {
148  $type = $cacheEntry->mAttribs['rc_type'];
149 
150  // Log entries
151  if ( $type == RC_LOG ) {
152  $logType = $cacheEntry->mAttribs['rc_log_type'];
153 
154  if ( $logType ) {
155  $clink = $this->getLogLink( $logType );
156  } else {
157  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
158  $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
159  }
160  // Log entries (old format) and special pages
161  } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
162  wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
163  $clink = '';
164  // Edits and everything else
165  } else {
166  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
167  }
168 
169  return $clink;
170  }
171 
172  private function getLogLink( $logType ) {
173  $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
174  $logpage = new LogPage( $logType );
175  $logname = $logpage->getName()->text();
176 
177  $logLink = $this->context->msg( 'parentheses' )
178  ->rawParams(
179  $this->linkRenderer->makeKnownLink( $logtitle, $logname )
180  )->escaped();
181 
182  return $logLink;
183  }
184 
190  private function buildTimestamp( RecentChange $cacheEntry ) {
191  return $this->context->getLanguage()->userTime(
192  $cacheEntry->mAttribs['rc_timestamp'],
193  $this->context->getUser()
194  );
195  }
196 
202  private function buildCurQueryParams( RecentChange $recentChange ) {
203  return [
204  'curid' => $recentChange->mAttribs['rc_cur_id'],
205  'diff' => 0,
206  'oldid' => $recentChange->mAttribs['rc_this_oldid']
207  ];
208  }
209 
216  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
217  $curMessage = $this->getMessage( 'cur' );
218  $logTypes = [ RC_LOG ];
219  if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
220  $showDiffLinks = false;
221  }
222 
223  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
224  $curLink = $curMessage;
225  } else {
226  $queryParams = $this->buildCurQueryParams( $cacheEntry );
227  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
228  $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
229  }
230 
231  return $curLink;
232  }
233 
239  private function buildDiffQueryParams( RecentChange $recentChange ) {
240  return [
241  'curid' => $recentChange->mAttribs['rc_cur_id'],
242  'diff' => $recentChange->mAttribs['rc_this_oldid'],
243  'oldid' => $recentChange->mAttribs['rc_last_oldid']
244  ];
245  }
246 
253  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
254  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
255  $diffMessage = $this->getMessage( 'diff' );
256  $logTypes = [ RC_NEW, RC_LOG ];
257 
258  if ( !$showDiffLinks ) {
259  $diffLink = $diffMessage;
260  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
261  $diffLink = $diffMessage;
262  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
263  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
264  $pageTitle = Title::newFromID( $rcCurId );
265  if ( $pageTitle === null ) {
266  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
267  return $diffMessage;
268  }
269  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
270  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
271  } else {
272  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
273  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
274  }
275 
276  return $diffLink;
277  }
278 
287  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
288  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
289  $lastMessage = $this->getMessage( 'last' );
290  $type = $cacheEntry->mAttribs['rc_type'];
291  $logTypes = [ RC_LOG ];
292 
293  // Make "last" link
294  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
295  $lastLink = $lastMessage;
296  } else {
297  $lastLink = $this->linkRenderer->makeKnownLink(
298  $cacheEntry->getTitle(),
299  new HtmlArmor( $lastMessage ),
300  [ 'class' => 'mw-changeslist-diff' ],
301  $this->buildDiffQueryParams( $cacheEntry )
302  );
303  }
304 
305  return $lastLink;
306  }
307 
313  private function getUserLink( RecentChange $cacheEntry ) {
314  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
315  $deletedClass = 'history-deleted';
316  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
317  $deletedClass .= ' mw-history-suppressed';
318  }
319  $userLink = ' <span class="' . $deletedClass . '">' .
320  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
321  } else {
327  $userLink = $this->userLinkCache->getWithSetCallback(
328  $this->userLinkCache->makeKey(
329  $cacheEntry->mAttribs['rc_user_text'],
330  $this->context->getUser()->getName(),
331  $this->context->getLanguage()->getCode()
332  ),
333  static fn() => Linker::userLink(
334  $cacheEntry->mAttribs['rc_user'],
335  $cacheEntry->mAttribs['rc_user_text'],
336  ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
337  )
338  );
339  }
340 
341  return $userLink;
342  }
343 
349  private function getMessage( $key ) {
350  return $this->messages[$key];
351  }
352 
353 }
const RC_NEW
Definition: Defines.php:117
const NS_SPECIAL
Definition: Defines.php:53
const RC_LOG
Definition: Defines.php:118
const RC_CATEGORIZE
Definition: Defines.php:120
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
static isUnpatrolled( $rc, User $user)
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
static userCan( $rc, $field, Authority $performer=null)
Determine if the current user is allowed to view a particular field of this revision,...
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
Class to simplify the use of log pages.
Definition: LogPage.php:43
Store key-value entries in a size-limited in-memory LRU cache.
Definition: MapCacheLRU.php:34
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition: Linker.php:65
Page revision base class.
Parent class for all special pages.
Definition: SpecialPage.php:66
Represents a title within MediaWiki.
Definition: Title.php:76
Class to parse and build external user names.
newFromRecentChange(RecentChange $baseRC, $watched)
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
static newFromParent( $rc)
Utility class for creating new RC entries.
getAttribute( $name)
Get an attribute value.
Interface for objects which can provide a MediaWiki context on request.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37