MediaWiki  master
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
24 
26 
28  private $context;
29 
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  $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
68 
69  $cacheEntry->link = $this->buildCLink( $cacheEntry );
70  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
71 
72  // Make "cur" and "diff" links. Do not use link(), it is too slow if
73  // called too many times (50% of CPU time on RecentChanges!).
74  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
75 
76  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
77  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
78  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
79 
80  // Make user links
81  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
82 
83  if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
84  $cacheEntry->usertalklink = Linker::userToolLinks(
85  $cacheEntry->mAttribs['rc_user'],
86  $cacheEntry->mAttribs['rc_user_text'],
87  // Should the contributions link be red if the user has no edits (using default)
88  false,
89  // Customisation flags (using default 0)
90  0,
91  // User edit count (using default )
92  null,
93  // do not wrap the message in parentheses
94  false
95  );
96  }
97 
98  return $cacheEntry;
99  }
100 
107  private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
108  return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
109  }
110 
116  private function buildCLink( RCCacheEntry $cacheEntry ) {
117  $type = $cacheEntry->mAttribs['rc_type'];
118 
119  // New unpatrolled pages
120  if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
121  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
122  // Log entries
123  } elseif ( $type == RC_LOG ) {
124  $logType = $cacheEntry->mAttribs['rc_log_type'];
125 
126  if ( $logType ) {
127  $clink = $this->getLogLink( $logType );
128  } else {
129  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
130  $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
131  }
132  // Log entries (old format) and special pages
133  } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
134  wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
135  $clink = '';
136  // Edits
137  } else {
138  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
139  }
140 
141  return $clink;
142  }
143 
144  private function getLogLink( $logType ) {
145  $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
146  $logpage = new LogPage( $logType );
147  $logname = $logpage->getName()->text();
148 
149  $logLink = $this->context->msg( 'parentheses' )
150  ->rawParams(
151  $this->linkRenderer->makeKnownLink( $logtitle, $logname )
152  )->escaped();
153 
154  return $logLink;
155  }
156 
162  private function buildTimestamp( RecentChange $cacheEntry ) {
163  return $this->context->getLanguage()->userTime(
164  $cacheEntry->mAttribs['rc_timestamp'],
165  $this->context->getUser()
166  );
167  }
168 
174  private function buildCurQueryParams( RecentChange $recentChange ) {
175  return [
176  'curid' => $recentChange->mAttribs['rc_cur_id'],
177  'diff' => 0,
178  'oldid' => $recentChange->mAttribs['rc_this_oldid']
179  ];
180  }
181 
188  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
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 
222  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
223  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
224  $diffMessage = $this->getMessage( 'diff' );
225  $logTypes = [ RC_NEW, RC_LOG ];
226 
227  if ( !$showDiffLinks ) {
228  $diffLink = $diffMessage;
229  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
230  $diffLink = $diffMessage;
231  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
232  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
233  $pageTitle = Title::newFromID( $rcCurId );
234  if ( $pageTitle === null ) {
235  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
236  return $diffMessage;
237  }
238  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
239  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
240  } else {
241  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
242  $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
243  }
244 
245  return $diffLink;
246  }
247 
256  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
257  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
258  $lastMessage = $this->getMessage( 'last' );
259  $type = $cacheEntry->mAttribs['rc_type'];
260  $logTypes = [ RC_LOG ];
261 
262  // Make "last" link
263  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
264  $lastLink = $lastMessage;
265  } else {
266  $lastLink = $this->linkRenderer->makeKnownLink(
267  $cacheEntry->getTitle(),
268  new HtmlArmor( $lastMessage ),
269  [ 'class' => 'mw-changeslist-diff' ],
270  $this->buildDiffQueryParams( $cacheEntry )
271  );
272  }
273 
274  return $lastLink;
275  }
276 
282  private function getUserLink( RecentChange $cacheEntry ) {
283  if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
284  $userLink = ' <span class="history-deleted">' .
285  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
286  } else {
287  $userLink = Linker::userLink(
288  $cacheEntry->mAttribs['rc_user'],
289  $cacheEntry->mAttribs['rc_user_text'],
290  ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
291  );
292  }
293 
294  return $userLink;
295  }
296 
302  private function getMessage( $key ) {
303  return $this->messages[$key];
304  }
305 
306 }
RCCacheEntryFactory\showDiffLinks
showDiffLinks(RecentChange $cacheEntry, User $user)
Definition: RCCacheEntryFactory.php:107
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition: ExternalUserNames.php:147
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
RCCacheEntryFactory\buildDiffLink
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks)
Definition: RCCacheEntryFactory.php:222
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:896
RCCacheEntryFactory\buildCurLink
buildCurLink(RecentChange $cacheEntry, $showDiffLinks)
Definition: RCCacheEntryFactory.php:188
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:73
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:941
RC_LOG
const RC_LOG
Definition: Defines.php:133
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:92
RCCacheEntryFactory
Definition: RCCacheEntryFactory.php:25
RCCacheEntryFactory\getLogLink
getLogLink( $logType)
Definition: RCCacheEntryFactory.php:144
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:989
ChangesList\isDeleted
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
Definition: ChangesList.php:734
RCCacheEntryFactory\buildDiffQueryParams
buildDiffQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:208
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:58
RCCacheEntry\newFromParent
static newFromParent( $rc)
Definition: RCCacheEntry.php:47
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
RCCacheEntryFactory\getUserLink
getUserLink(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:282
RCCacheEntryFactory\__construct
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
Definition: RCCacheEntryFactory.php:43
RCCacheEntryFactory\buildCLink
buildCLink(RCCacheEntry $cacheEntry)
Definition: RCCacheEntryFactory.php:116
RCCacheEntryFactory\buildTimestamp
buildTimestamp(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:162
RC_NEW
const RC_NEW
Definition: Defines.php:132
RCCacheEntryFactory\$context
IContextSource $context
Definition: RCCacheEntryFactory.php:28
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
RCCacheEntryFactory\$messages
string[] $messages
Definition: RCCacheEntryFactory.php:31
RCCacheEntryFactory\$linkRenderer
LinkRenderer $linkRenderer
Definition: RCCacheEntryFactory.php:36
RecentChange\getAttribute
getAttribute( $name)
Get an attribute value.
Definition: RecentChange.php:1058
RCCacheEntryFactory\buildCurQueryParams
buildCurQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:174
RCCacheEntry
Definition: RCCacheEntry.php:21
RCCacheEntryFactory\newFromRecentChange
newFromRecentChange(RecentChange $baseRC, $watched)
Definition: RCCacheEntryFactory.php:57
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:135
ChangesList\isUnpatrolled
static isUnpatrolled( $rc, User $user)
Definition: ChangesList.php:866
RecentChange\getTitle
& getTitle()
Definition: RecentChange.php:297
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:472
RCCacheEntryFactory\buildLastLink
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Builds the link to the previous version.
Definition: RCCacheEntryFactory.php:256
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:747
RCCacheEntryFactory\getMessage
getMessage( $key)
Definition: RCCacheEntryFactory.php:302
$type
$type
Definition: testCompression.php:52