MediaWiki  master
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
25 
27 
29  private $context;
30 
32  private $messages;
33 
37  private $linkRenderer;
38 
44  public function __construct(
46  ) {
47  $this->context = $context;
48  $this->messages = $messages;
49  $this->linkRenderer = $linkRenderer;
50  }
51 
58  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
59  $user = $this->context->getUser();
60 
61  $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
62 
63  // Should patrol-related stuff be shown?
64  $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
65 
66  $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
67  $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
68  $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
69 
70  $cacheEntry->link = $this->buildCLink( $cacheEntry );
71  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
72 
73  // Make "cur" and "diff" links. Do not use link(), it is too slow if
74  // called too many times (50% of CPU time on RecentChanges!).
75  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
76 
77  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
78  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
79  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
80 
81  // Make user links
82  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
83 
84  if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
85  $cacheEntry->usertalklink = Linker::userToolLinks(
86  $cacheEntry->mAttribs['rc_user'],
87  $cacheEntry->mAttribs['rc_user_text'],
88  // Should the contributions link be red if the user has no edits (using default)
89  false,
90  // Customisation flags (using default 0)
91  0,
92  // User edit count (using default )
93  null,
94  // do not wrap the message in parentheses
95  false
96  );
97  }
98 
99  return $cacheEntry;
100  }
101 
108  private function showDiffLinks( RecentChange $cacheEntry, Authority $performer ) {
109  return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $performer );
110  }
111 
117  private function buildCLink( RCCacheEntry $cacheEntry ) {
118  $type = $cacheEntry->mAttribs['rc_type'];
119 
120  // New unpatrolled pages
121  if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
122  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
123  // Log entries
124  } elseif ( $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
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  $queryParams = $this->buildCurQueryParams( $cacheEntry );
191  $curMessage = $this->getMessage( 'cur' );
192  $logTypes = [ RC_LOG ];
193 
194  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
195  $curLink = $curMessage;
196  } else {
197  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
198  $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
199  }
200 
201  return $curLink;
202  }
203 
209  private function buildDiffQueryParams( RecentChange $recentChange ) {
210  return [
211  'curid' => $recentChange->mAttribs['rc_cur_id'],
212  'diff' => $recentChange->mAttribs['rc_this_oldid'],
213  'oldid' => $recentChange->mAttribs['rc_last_oldid']
214  ];
215  }
216 
223  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
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, RevisionRecord::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 }
ExternalUserNames\getLocal
static getLocal( $username)
Get local part of the user name.
Definition: ExternalUserNames.php:159
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
RCCacheEntryFactory\buildDiffLink
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks)
Definition: RCCacheEntryFactory.php:223
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1068
RCCacheEntryFactory\buildCurLink
buildCurLink(RecentChange $cacheEntry, $showDiffLinks)
Definition: RCCacheEntryFactory.php:189
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:80
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
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:1113
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:107
RCCacheEntryFactory
Definition: RCCacheEntryFactory.php:26
RCCacheEntryFactory\getLogLink
getLogLink( $logType)
Definition: RCCacheEntryFactory.php:145
RCCacheEntryFactory\showDiffLinks
showDiffLinks(RecentChange $cacheEntry, Authority $performer)
Definition: RCCacheEntryFactory.php:108
RC_NEW
const RC_NEW
Definition: Defines.php:116
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:958
ChangesList\isDeleted
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
Definition: ChangesList.php:748
RecentChange\getTitle
getTitle()
Definition: RecentChange.php:329
RCCacheEntryFactory\buildDiffQueryParams
buildDiffQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:209
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
RC_LOG
const RC_LOG
Definition: Defines.php:117
RCCacheEntry\newFromParent
static newFromParent( $rc)
Definition: RCCacheEntry.php:47
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:38
RCCacheEntryFactory\getUserLink
getUserLink(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:283
RCCacheEntryFactory\__construct
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
Definition: RCCacheEntryFactory.php:44
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
RCCacheEntryFactory\buildCLink
buildCLink(RCCacheEntry $cacheEntry)
Definition: RCCacheEntryFactory.php:117
RCCacheEntryFactory\buildTimestamp
buildTimestamp(RecentChange $cacheEntry)
Definition: RCCacheEntryFactory.php:163
RCCacheEntryFactory\$context
IContextSource $context
Definition: RCCacheEntryFactory.php:29
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
RCCacheEntryFactory\$messages
string[] $messages
Definition: RCCacheEntryFactory.php:32
RCCacheEntryFactory\$linkRenderer
LinkRenderer $linkRenderer
Definition: RCCacheEntryFactory.php:37
RecentChange\getAttribute
getAttribute( $name)
Get an attribute value.
Definition: RecentChange.php:1167
RCCacheEntryFactory\buildCurQueryParams
buildCurQueryParams(RecentChange $recentChange)
Definition: RCCacheEntryFactory.php:175
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:119
RCCacheEntry
Definition: RCCacheEntry.php:21
RCCacheEntryFactory\newFromRecentChange
newFromRecentChange(RecentChange $baseRC, $watched)
Definition: RCCacheEntryFactory.php:58
ChangesList\isUnpatrolled
static isUnpatrolled( $rc, User $user)
Definition: ChangesList.php:878
ChangesList\userCan
static userCan( $rc, $field, Authority $performer=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: ChangesList.php:762
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:561
RCCacheEntryFactory\buildLastLink
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Builds the link to the previous version.
Definition: RCCacheEntryFactory.php:257
RCCacheEntryFactory\getMessage
getMessage( $key)
Definition: RCCacheEntryFactory.php:303
$type
$type
Definition: testCompression.php:52