MediaWiki  master
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
28 
30 
32  private $context;
33 
35  private $messages;
36 
40  private $linkRenderer;
41 
47  public function __construct(
48  IContextSource $context, $messages, LinkRenderer $linkRenderer
49  ) {
50  $this->context = $context;
51  $this->messages = $messages;
52  $this->linkRenderer = $linkRenderer;
53  }
54 
61  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
62  $user = $this->context->getUser();
63 
64  $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
65 
66  // Should patrol-related stuff be shown?
67  $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
68 
69  $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
70  $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
71  $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
72 
73  $cacheEntry->link = $this->buildCLink( $cacheEntry );
74  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
75 
76  // Make "cur" and "diff" links. Do not use link(), it is too slow if
77  // called too many times (50% of CPU time on RecentChanges!).
78  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
79 
80  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
81  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
82  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
83 
84  // Make user links
85  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
86 
87  if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
88  $cacheEntry->usertalklink = Linker::userToolLinks(
89  $cacheEntry->mAttribs['rc_user'],
90  $cacheEntry->mAttribs['rc_user_text'],
91  // Should the contributions link be red if the user has no edits (using default)
92  false,
93  // Customisation flags (using default 0)
94  0,
95  // User edit count (using default )
96  null,
97  // do not wrap the message in parentheses
98  false
99  );
100  }
101 
102  return $cacheEntry;
103  }
104 
111  private function showDiffLinks( RecentChange $cacheEntry, Authority $performer ) {
112  return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $performer );
113  }
114 
120  private function buildCLink( RCCacheEntry $cacheEntry ) {
121  $type = $cacheEntry->mAttribs['rc_type'];
122 
123  // Log entries
124  if ( $type == RC_LOG ) {
125  $logType = $cacheEntry->mAttribs['rc_log_type'];
126 
127  if ( $logType ) {
128  $clink = $this->getLogLink( $logType );
129  } else {
130  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
131  $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
132  }
133  // Log entries (old format) and special pages
134  } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
135  wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
136  $clink = '';
137  // Edits and everything else
138  } else {
139  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
140  }
141 
142  return $clink;
143  }
144 
145  private function getLogLink( $logType ) {
146  $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
147  $logpage = new LogPage( $logType );
148  $logname = $logpage->getName()->text();
149 
150  $logLink = $this->context->msg( 'parentheses' )
151  ->rawParams(
152  $this->linkRenderer->makeKnownLink( $logtitle, $logname )
153  )->escaped();
154 
155  return $logLink;
156  }
157 
163  private function buildTimestamp( RecentChange $cacheEntry ) {
164  return $this->context->getLanguage()->userTime(
165  $cacheEntry->mAttribs['rc_timestamp'],
166  $this->context->getUser()
167  );
168  }
169 
175  private function buildCurQueryParams( RecentChange $recentChange ) {
176  return [
177  'curid' => $recentChange->mAttribs['rc_cur_id'],
178  'diff' => 0,
179  'oldid' => $recentChange->mAttribs['rc_this_oldid']
180  ];
181  }
182 
189  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
190  $curMessage = $this->getMessage( 'cur' );
191  $logTypes = [ RC_LOG ];
192  if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
193  $showDiffLinks = false;
194  }
195 
196  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
197  $curLink = $curMessage;
198  } else {
199  $queryParams = $this->buildCurQueryParams( $cacheEntry );
200  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
201  $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
202  }
203 
204  return $curLink;
205  }
206 
212  private function buildDiffQueryParams( RecentChange $recentChange ) {
213  return [
214  'curid' => $recentChange->mAttribs['rc_cur_id'],
215  'diff' => $recentChange->mAttribs['rc_this_oldid'],
216  'oldid' => $recentChange->mAttribs['rc_last_oldid']
217  ];
218  }
219 
226  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
227  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
228  $diffMessage = $this->getMessage( 'diff' );
229  $logTypes = [ RC_NEW, RC_LOG ];
230 
231  if ( !$showDiffLinks ) {
232  $diffLink = $diffMessage;
233  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
234  $diffLink = $diffMessage;
235  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
236  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
237  $pageTitle = Title::newFromID( $rcCurId );
238  if ( $pageTitle === null ) {
239  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
240  return $diffMessage;
241  }
242  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
243  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
244  } else {
245  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
246  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
247  }
248 
249  return $diffLink;
250  }
251 
260  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
261  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
262  $lastMessage = $this->getMessage( 'last' );
263  $type = $cacheEntry->mAttribs['rc_type'];
264  $logTypes = [ RC_LOG ];
265 
266  // Make "last" link
267  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
268  $lastLink = $lastMessage;
269  } else {
270  $lastLink = $this->linkRenderer->makeKnownLink(
271  $cacheEntry->getTitle(),
272  new HtmlArmor( $lastMessage ),
273  [ 'class' => 'mw-changeslist-diff' ],
274  $this->buildDiffQueryParams( $cacheEntry )
275  );
276  }
277 
278  return $lastLink;
279  }
280 
286  private function getUserLink( RecentChange $cacheEntry ) {
287  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
288  $deletedClass = 'history-deleted';
289  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
290  $deletedClass .= ' mw-history-suppressed';
291  }
292  $userLink = ' <span class="' . $deletedClass . '">' .
293  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
294  } else {
295  $userLink = Linker::userLink(
296  $cacheEntry->mAttribs['rc_user'],
297  $cacheEntry->mAttribs['rc_user_text'],
298  ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] ),
299  [
300  'data-mw-revid' => $cacheEntry->mAttribs['rc_this_oldid']
301  ]
302  );
303  }
304 
305  return $userLink;
306  }
307 
313  private function getMessage( $key ) {
314  return $this->messages[$key];
315  }
316 
317 }
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,...
static getLocal( $username)
Get local part of the user name.
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
Class to simplify the use of log pages.
Definition: LogPage.php:41
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition: Linker.php:65
Page revision base class.
Represents a title within MediaWiki.
Definition: Title.php:82
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.
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,...
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