MediaWiki 1.43.1
RCCacheEntryFactory.php
Go to the documentation of this file.
1<?php
28
35
37 private $context;
38
40 private $messages;
41
45 private $linkRenderer;
46
50 private MapCacheLRU $userLinkCache;
51
55 private MapCacheLRU $toolLinkCache;
56
62 public function __construct(
63 IContextSource $context, $messages, LinkRenderer $linkRenderer
64 ) {
65 $this->context = $context;
66 $this->messages = $messages;
67 $this->linkRenderer = $linkRenderer;
68 $this->userLinkCache = new MapCacheLRU( 50 );
69 $this->toolLinkCache = new MapCacheLRU( 50 );
70 }
71
78 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
79 $user = $this->context->getUser();
80
81 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
82
83 // Should patrol-related stuff be shown?
84 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
85
86 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
87 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
88 $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
89
90 $cacheEntry->link = $this->buildCLink( $cacheEntry );
91 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
92
93 // Make "cur" and "diff" links. Do not use link(), it is too slow if
94 // called too many times (50% of CPU time on RecentChanges!).
95 $showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
96
97 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
98 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
99 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
100
101 // Make user links
102 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
103
104 if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
111 $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
112 $this->toolLinkCache->makeKey(
113 $cacheEntry->mAttribs['rc_user_text'],
114 $this->context->getUser()->getName(),
115 $this->context->getLanguage()->getCode()
116 ),
117 static fn () => Linker::userToolLinks(
118 $cacheEntry->mAttribs['rc_user'],
119 $cacheEntry->mAttribs['rc_user_text'],
120 // Should the contributions link be red if the user has no edits (using default)
121 false,
122 // Customisation flags (using default 0)
123 0,
124 // User edit count (using default )
125 null,
126 // do not wrap the message in parentheses
127 false
128 )
129 );
130 }
131
132 return $cacheEntry;
133 }
134
140 private function buildCLink( RCCacheEntry $cacheEntry ) {
141 $type = $cacheEntry->mAttribs['rc_type'];
142
143 // Log entries
144 if ( $type == RC_LOG ) {
145 $logType = $cacheEntry->mAttribs['rc_log_type'];
146
147 if ( $logType ) {
148 $clink = $this->getLogLink( $logType );
149 } else {
150 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
151 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
152 }
153 // Log entries (old format) and special pages
154 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
155 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
156 $clink = '';
157 // Edits and everything else
158 } else {
159 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
160 }
161
162 return $clink;
163 }
164
165 private function getLogLink( $logType ) {
166 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
167 $logpage = new LogPage( $logType );
168 $logname = $logpage->getName()->text();
169
170 $logLink = $this->context->msg( 'parentheses' )
171 ->rawParams(
172 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
173 )->escaped();
174
175 return $logLink;
176 }
177
183 private function buildTimestamp( RecentChange $cacheEntry ) {
184 return $this->context->getLanguage()->userTime(
185 $cacheEntry->mAttribs['rc_timestamp'],
186 $this->context->getUser()
187 );
188 }
189
195 private function buildCurQueryParams( RecentChange $recentChange ) {
196 return [
197 'curid' => $recentChange->mAttribs['rc_cur_id'],
198 'diff' => 0,
199 'oldid' => $recentChange->mAttribs['rc_this_oldid']
200 ];
201 }
202
209 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
210 $curMessage = $this->getMessage( 'cur' );
211 $logTypes = [ RC_LOG ];
212 if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
213 $showDiffLinks = false;
214 }
215
216 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
217 $curLink = $curMessage;
218 } else {
219 $queryParams = $this->buildCurQueryParams( $cacheEntry );
220 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
221 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
222 }
223
224 return $curLink;
225 }
226
232 private function buildDiffQueryParams( RecentChange $recentChange ) {
233 return [
234 'curid' => $recentChange->mAttribs['rc_cur_id'],
235 'diff' => $recentChange->mAttribs['rc_this_oldid'],
236 'oldid' => $recentChange->mAttribs['rc_last_oldid']
237 ];
238 }
239
246 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
247 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
248 $diffMessage = $this->getMessage( 'diff' );
249 $logTypes = [ RC_NEW, RC_LOG ];
250
251 if ( !$showDiffLinks ) {
252 $diffLink = $diffMessage;
253 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
254 $diffLink = $diffMessage;
255 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
256 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
257 $pageTitle = Title::newFromID( $rcCurId );
258 if ( $pageTitle === null ) {
259 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
260 return $diffMessage;
261 }
262 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
263 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
264 } else {
265 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
266 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
267 }
268
269 return $diffLink;
270 }
271
280 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
281 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
282 $lastMessage = $this->getMessage( 'last' );
283 $type = $cacheEntry->mAttribs['rc_type'];
284 $logTypes = [ RC_LOG ];
285
286 // Make "last" link
287 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
288 $lastLink = $lastMessage;
289 } else {
290 $lastLink = $this->linkRenderer->makeKnownLink(
291 $cacheEntry->getTitle(),
292 new HtmlArmor( $lastMessage ),
293 [ 'class' => 'mw-changeslist-diff' ],
294 $this->buildDiffQueryParams( $cacheEntry )
295 );
296 }
297
298 return $lastLink;
299 }
300
306 private function getUserLink( RecentChange $cacheEntry ) {
307 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
308 $deletedClass = 'history-deleted';
309 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
310 $deletedClass .= ' mw-history-suppressed';
311 }
312 $userLink = ' <span class="' . $deletedClass . '">' .
313 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
314 } else {
320 $userLink = $this->userLinkCache->getWithSetCallback(
321 $this->userLinkCache->makeKey(
322 $cacheEntry->mAttribs['rc_user_text'],
323 $this->context->getUser()->getName(),
324 $this->context->getLanguage()->getCode()
325 ),
326 static fn () => Linker::userLink(
327 $cacheEntry->mAttribs['rc_user'],
328 $cacheEntry->mAttribs['rc_user_text'],
329 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
330 )
331 );
332 }
333
334 return $userLink;
335 }
336
342 private function getMessage( $key ) {
343 return $this->messages[$key];
344 }
345
346}
const RC_NEW
Definition Defines.php:118
const NS_SPECIAL
Definition Defines.php:54
const RC_LOG
Definition Defines.php:119
const RC_CATEGORIZE
Definition Defines.php:121
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:46
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:63
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.
Create a RCCacheEntry from a RecentChange to use in EnhancedChangesList.
newFromRecentChange(RecentChange $baseRC, $watched)
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
static newFromParent( $rc)
Utility class for creating and reading rows in the recentchanges table.
getAttribute( $name)
Get an attribute value.
Interface for objects which can provide a MediaWiki context on request.