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 = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $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
137 private function buildCLink( RCCacheEntry $cacheEntry ) {
138 $type = $cacheEntry->mAttribs['rc_type'];
139
140 // Log entries
141 if ( $type == RC_LOG ) {
142 $logType = $cacheEntry->mAttribs['rc_log_type'];
143
144 if ( $logType ) {
145 $clink = $this->getLogLink( $logType );
146 } else {
147 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
148 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
149 }
150 // Log entries (old format) and special pages
151 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
152 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
153 $clink = '';
154 // Edits and everything else
155 } else {
156 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
157 }
158
159 return $clink;
160 }
161
162 private function getLogLink( $logType ) {
163 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
164 $logpage = new LogPage( $logType );
165 $logname = $logpage->getName()->text();
166
167 $logLink = $this->context->msg( 'parentheses' )
168 ->rawParams(
169 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
170 )->escaped();
171
172 return $logLink;
173 }
174
180 private function buildTimestamp( RecentChange $cacheEntry ) {
181 return $this->context->getLanguage()->userTime(
182 $cacheEntry->mAttribs['rc_timestamp'],
183 $this->context->getUser()
184 );
185 }
186
192 private function buildCurQueryParams( RecentChange $recentChange ) {
193 return [
194 'curid' => $recentChange->mAttribs['rc_cur_id'],
195 'diff' => 0,
196 'oldid' => $recentChange->mAttribs['rc_this_oldid']
197 ];
198 }
199
206 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
207 $curMessage = $this->getMessage( 'cur' );
208 $logTypes = [ RC_LOG ];
209 if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
210 $showDiffLinks = false;
211 }
212
213 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
214 $curLink = $curMessage;
215 } else {
216 $queryParams = $this->buildCurQueryParams( $cacheEntry );
217 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
218 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
219 }
220
221 return $curLink;
222 }
223
229 private function buildDiffQueryParams( RecentChange $recentChange ) {
230 return [
231 'curid' => $recentChange->mAttribs['rc_cur_id'],
232 'diff' => $recentChange->mAttribs['rc_this_oldid'],
233 'oldid' => $recentChange->mAttribs['rc_last_oldid']
234 ];
235 }
236
243 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
244 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
245 $diffMessage = $this->getMessage( 'diff' );
246 $logTypes = [ RC_NEW, RC_LOG ];
247
248 if ( !$showDiffLinks ) {
249 $diffLink = $diffMessage;
250 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
251 $diffLink = $diffMessage;
252 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
253 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
254 $pageTitle = Title::newFromID( $rcCurId );
255 if ( $pageTitle === null ) {
256 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
257 return $diffMessage;
258 }
259 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
260 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
261 } else {
262 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
263 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
264 }
265
266 return $diffLink;
267 }
268
277 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
278 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
279 $lastMessage = $this->getMessage( 'last' );
280 $type = $cacheEntry->mAttribs['rc_type'];
281 $logTypes = [ RC_LOG ];
282
283 // Make "last" link
284 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
285 $lastLink = $lastMessage;
286 } else {
287 $lastLink = $this->linkRenderer->makeKnownLink(
288 $cacheEntry->getTitle(),
289 new HtmlArmor( $lastMessage ),
290 [ 'class' => 'mw-changeslist-diff' ],
291 $this->buildDiffQueryParams( $cacheEntry )
292 );
293 }
294
295 return $lastLink;
296 }
297
303 private function getUserLink( RecentChange $cacheEntry ) {
304 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
305 $deletedClass = 'history-deleted';
306 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
307 $deletedClass .= ' mw-history-suppressed';
308 }
309 $userLink = ' <span class="' . $deletedClass . '">' .
310 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
311 } else {
317 $userLink = $this->userLinkCache->getWithSetCallback(
318 $this->userLinkCache->makeKey(
319 $cacheEntry->mAttribs['rc_user_text'],
320 $this->context->getUser()->getName(),
321 $this->context->getLanguage()->getCode()
322 ),
323 static fn () => Linker::userLink(
324 $cacheEntry->mAttribs['rc_user'],
325 $cacheEntry->mAttribs['rc_user_text'],
326 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
327 )
328 );
329 }
330
331 return $userLink;
332 }
333
339 private function getMessage( $key ) {
340 return $this->messages[$key];
341 }
342
343}
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.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
Class to simplify the use of log pages.
Definition LogPage.php:44
Store key-value entries in a size-limited in-memory LRU cache.
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.
Represents a title within MediaWiki.
Definition Title.php:78
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.