MediaWiki  master
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
24 
26 
27  /* @var IContextSource */
28  private $context;
29 
30  /* @var string[] */
31  private $messages;
32 
36  private $linkRenderer;
37 
43  public function __construct(
45  ) {
46  $this->context = $context;
47  $this->messages = $messages;
48  $this->linkRenderer = $linkRenderer;
49  }
50 
57  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
58  $user = $this->context->getUser();
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 );
76  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
77  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
78 
79  // Make user links
80  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
81 
82  if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::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, RevisionRecord::DELETED_TEXT, $user );
108  }
109 
115  private function buildCLink( RCCacheEntry $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 
187  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
188  $queryParams = $this->buildCurQueryParams( $cacheEntry );
189  $curMessage = $this->getMessage( 'cur' );
190  $logTypes = [ RC_LOG ];
191 
192  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
193  $curLink = $curMessage;
194  } else {
195  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
196  $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
197  }
198 
199  return $curLink;
200  }
201 
207  private function buildDiffQueryParams( RecentChange $recentChange ) {
208  return [
209  'curid' => $recentChange->mAttribs['rc_cur_id'],
210  'diff' => $recentChange->mAttribs['rc_this_oldid'],
211  'oldid' => $recentChange->mAttribs['rc_last_oldid']
212  ];
213  }
214 
221  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
222  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
223  $diffMessage = $this->getMessage( 'diff' );
224  $logTypes = [ RC_NEW, RC_LOG ];
225 
226  if ( !$showDiffLinks ) {
227  $diffLink = $diffMessage;
228  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
229  $diffLink = $diffMessage;
230  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
231  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
232  $pageTitle = Title::newFromID( $rcCurId );
233  if ( $pageTitle === null ) {
234  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
235  return $diffMessage;
236  }
237  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
238  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
239  } else {
240  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
241  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
242  }
243 
244  return $diffLink;
245  }
246 
255  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
256  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
257  $lastMessage = $this->getMessage( 'last' );
258  $type = $cacheEntry->mAttribs['rc_type'];
259  $logTypes = [ RC_LOG ];
260 
261  // Make "last" link
262  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
263  $lastLink = $lastMessage;
264  } else {
265  $lastLink = $this->linkRenderer->makeKnownLink(
266  $cacheEntry->getTitle(),
267  new HtmlArmor( $lastMessage ),
268  [ 'class' => 'mw-changeslist-diff' ],
269  $this->buildDiffQueryParams( $cacheEntry )
270  );
271  }
272 
273  return $lastLink;
274  }
275 
281  private function getUserLink( RecentChange $cacheEntry ) {
282  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
283  $userLink = ' <span class="history-deleted">' .
284  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
285  } else {
286  $userLink = Linker::userLink(
287  $cacheEntry->mAttribs['rc_user'],
288  $cacheEntry->mAttribs['rc_user_text'],
289  ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
290  );
291  }
292 
293  return $userLink;
294  }
295 
301  private function getMessage( $key ) {
302  return $this->messages[$key];
303  }
304 
305 }
const RC_CATEGORIZE
Definition: Defines.php:126
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:465
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Builds the link to the previous version.
buildCLink(RCCacheEntry $cacheEntry)
newFromRecentChange(RecentChange $baseRC, $watched)
getAttribute( $name)
Get an attribute value.
buildDiffQueryParams(RecentChange $recentChange)
const NS_SPECIAL
Definition: Defines.php:49
Class to simplify the use of log pages.
Definition: LogPage.php:33
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
Class that generates HTML links for pages.
buildCurLink(RecentChange $cacheEntry, $showDiffLinks)
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
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:943
static newFromParent( $rc)
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:898
static isUnpatrolled( $rc, User $user)
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
buildTimestamp(RecentChange $cacheEntry)
showDiffLinks(RecentChange $cacheEntry, User $user)
getUserLink(RecentChange $cacheEntry)
static userCan( $rc, $field, User $user=null)
Determine if the current user is allowed to view a particular field of this revision, if it&#39;s marked as deleted.
const RC_NEW
Definition: Defines.php:123
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks)
static getLocal( $username)
Get local part of the user name.
const RC_LOG
Definition: Defines.php:124
buildCurQueryParams(RecentChange $recentChange)