MediaWiki master
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
47 private MapCacheLRU $userLinkCache;
48 private MapCacheLRU $toolLinkCache;
49
55 public function __construct(
56 IContextSource $context, $messages, LinkRenderer $linkRenderer
57 ) {
58 $this->context = $context;
59 $this->messages = $messages;
60 $this->linkRenderer = $linkRenderer;
61 $this->userLinkCache = new MapCacheLRU( 50 );
62 $this->toolLinkCache = new MapCacheLRU( 50 );
63 }
64
71 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
72 $user = $this->context->getUser();
73
74 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
75
76 // Should patrol-related stuff be shown?
77 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
78
79 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
80 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
81 $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
82
83 $cacheEntry->link = $this->buildCLink( $cacheEntry );
84 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
85
86 // Make "cur" and "diff" links. Do not use link(), it is too slow if
87 // called too many times (50% of CPU time on RecentChanges!).
88 $showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
89
90 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
91 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
92 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
93
94 // Make user links
95 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
96
97 if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
104 $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
105 $this->toolLinkCache->makeKey(
106 $cacheEntry->mAttribs['rc_user_text'],
107 $this->context->getUser()->getName(),
108 $this->context->getLanguage()->getCode()
109 ),
110 static fn () => Linker::userToolLinks(
111 $cacheEntry->mAttribs['rc_user'],
112 $cacheEntry->mAttribs['rc_user_text'],
113 // Should the contributions link be red if the user has no edits (using default)
114 false,
115 // Customisation flags (using default 0)
116 0,
117 // User edit count (using default )
118 null,
119 // do not wrap the message in parentheses
120 false
121 )
122 );
123 }
124
125 return $cacheEntry;
126 }
127
133 private function buildCLink( RCCacheEntry $cacheEntry ) {
134 $type = $cacheEntry->mAttribs['rc_type'];
135
136 // Log entries
137 if ( $type == RC_LOG ) {
138 $logType = $cacheEntry->mAttribs['rc_log_type'];
139
140 if ( $logType ) {
141 $clink = $this->getLogLink( $logType );
142 } else {
143 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
144 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
145 }
146 // Log entries (old format) and special pages
147 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
148 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
149 $clink = '';
150 // Edits and everything else
151 } else {
152 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
153 }
154
155 return $clink;
156 }
157
158 private function getLogLink( $logType ) {
159 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
160 $logpage = new LogPage( $logType );
161 $logname = $logpage->getName()->text();
162
163 $logLink = $this->context->msg( 'parentheses' )
164 ->rawParams(
165 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
166 )->escaped();
167
168 return $logLink;
169 }
170
176 private function buildTimestamp( RecentChange $cacheEntry ) {
177 return $this->context->getLanguage()->userTime(
178 $cacheEntry->mAttribs['rc_timestamp'],
179 $this->context->getUser()
180 );
181 }
182
188 private function buildCurQueryParams( RecentChange $recentChange ) {
189 return [
190 'curid' => $recentChange->mAttribs['rc_cur_id'],
191 'diff' => 0,
192 'oldid' => $recentChange->mAttribs['rc_this_oldid']
193 ];
194 }
195
202 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
203 $curMessage = $this->getMessage( 'cur' );
204 $logTypes = [ RC_LOG ];
205 if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
206 $showDiffLinks = false;
207 }
208
209 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
210 $curLink = $curMessage;
211 } else {
212 $queryParams = $this->buildCurQueryParams( $cacheEntry );
213 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
214 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
215 }
216
217 return $curLink;
218 }
219
225 private function buildDiffQueryParams( RecentChange $recentChange ) {
226 return [
227 'curid' => $recentChange->mAttribs['rc_cur_id'],
228 'diff' => $recentChange->mAttribs['rc_this_oldid'],
229 'oldid' => $recentChange->mAttribs['rc_last_oldid']
230 ];
231 }
232
239 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
240 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
241 $diffMessage = $this->getMessage( 'diff' );
242 $logTypes = [ RC_NEW, RC_LOG ];
243
244 if ( !$showDiffLinks ) {
245 $diffLink = $diffMessage;
246 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
247 $diffLink = $diffMessage;
248 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
249 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
250 $pageTitle = Title::newFromID( $rcCurId );
251 if ( $pageTitle === null ) {
252 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
253 return $diffMessage;
254 }
255 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
256 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
257 } else {
258 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
259 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
260 }
261
262 return $diffLink;
263 }
264
273 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
274 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
275 $lastMessage = $this->getMessage( 'last' );
276 $type = $cacheEntry->mAttribs['rc_type'];
277 $logTypes = [ RC_LOG ];
278
279 // Make "last" link
280 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
281 $lastLink = $lastMessage;
282 } else {
283 $lastLink = $this->linkRenderer->makeKnownLink(
284 $cacheEntry->getTitle(),
285 new HtmlArmor( $lastMessage ),
286 [ 'class' => 'mw-changeslist-diff' ],
287 $this->buildDiffQueryParams( $cacheEntry )
288 );
289 }
290
291 return $lastLink;
292 }
293
299 private function getUserLink( RecentChange $cacheEntry ) {
300 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
301 $deletedClass = 'history-deleted';
302 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
303 $deletedClass .= ' mw-history-suppressed';
304 }
305 $userLink = ' <span class="' . $deletedClass . '">' .
306 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
307 } else {
313 $userLink = $this->userLinkCache->getWithSetCallback(
314 $this->userLinkCache->makeKey(
315 $cacheEntry->mAttribs['rc_user_text'],
316 $this->context->getUser()->getName(),
317 $this->context->getLanguage()->getCode()
318 ),
319 static fn () => Linker::userLink(
320 $cacheEntry->mAttribs['rc_user'],
321 $cacheEntry->mAttribs['rc_user_text'],
322 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
323 )
324 );
325 }
326
327 return $userLink;
328 }
329
335 private function getMessage( $key ) {
336 return $this->messages[$key];
337 }
338
339}
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.