MediaWiki master
RCCacheEntryFactory.php
Go to the documentation of this file.
1<?php
22
34
41
43 private $context;
44
46 private $messages;
47
51 private $linkRenderer;
52
53 private UserLinkRenderer $userLinkRenderer;
54
55 private MapCacheLRU $toolLinkCache;
56
63 public function __construct(
64 IContextSource $context,
65 $messages,
66 LinkRenderer $linkRenderer,
67 UserLinkRenderer $userLinkRenderer
68 ) {
69 $this->context = $context;
70 $this->messages = $messages;
71 $this->linkRenderer = $linkRenderer;
72 $this->userLinkRenderer = $userLinkRenderer;
73 $this->toolLinkCache = new MapCacheLRU( 50 );
74 }
75
82 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
83 $user = $this->context->getUser();
84
85 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
86
87 // Should patrol-related stuff be shown?
88 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
89
90 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
91 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
92 $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
93
94 $cacheEntry->link = $this->buildCLink( $cacheEntry );
95 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
96
97 // Make "cur" and "diff" links. Do not use link(), it is too slow if
98 // called too many times (50% of CPU time on RecentChanges!).
99 $showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
100
101 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
102 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
103 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
104
105 // Make user links
106 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
107
108 if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
115 $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
116 $this->toolLinkCache->makeKey(
117 $cacheEntry->mAttribs['rc_user_text'],
118 $this->context->getUser()->getName(),
119 $this->context->getLanguage()->getCode()
120 ),
121 static fn () => Linker::userToolLinks(
122 $cacheEntry->mAttribs['rc_user'],
123 $cacheEntry->mAttribs['rc_user_text'],
124 // Should the contributions link be red if the user has no edits (using default)
125 false,
126 // Customisation flags (using default 0)
127 0,
128 // User edit count (using default )
129 null,
130 // do not wrap the message in parentheses
131 false
132 )
133 );
134 }
135
136 return $cacheEntry;
137 }
138
144 private function buildCLink( RCCacheEntry $cacheEntry ) {
145 $type = $cacheEntry->mAttribs['rc_type'];
146
147 // Log entries
148 if ( $type == RC_LOG ) {
149 $logType = $cacheEntry->mAttribs['rc_log_type'];
150
151 if ( $logType ) {
152 $clink = $this->getLogLink( $logType );
153 } else {
154 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
155 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
156 }
157 // Log entries (old format) and special pages
158 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
159 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
160 $clink = '';
161 // Edits and everything else
162 } else {
163 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
164 }
165
166 return $clink;
167 }
168
169 private function getLogLink( string $logType ): string {
170 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
171 $logpage = new LogPage( $logType );
172 $logname = $logpage->getName()->text();
173
174 $logLink = $this->context->msg( 'parentheses' )
175 ->rawParams(
176 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
177 )->escaped();
178
179 return $logLink;
180 }
181
187 private function buildTimestamp( RecentChange $cacheEntry ) {
188 return $this->context->getLanguage()->userTime(
189 $cacheEntry->mAttribs['rc_timestamp'],
190 $this->context->getUser()
191 );
192 }
193
199 private function buildCurQueryParams( RecentChange $recentChange ) {
200 return [
201 'curid' => $recentChange->mAttribs['rc_cur_id'],
202 'diff' => 0,
203 'oldid' => $recentChange->mAttribs['rc_this_oldid']
204 ];
205 }
206
213 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
214 $curMessage = $this->getMessage( 'cur' );
215 $logTypes = [ RC_LOG ];
216 if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
217 $showDiffLinks = false;
218 }
219
220 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
221 $curLink = $curMessage;
222 } else {
223 $queryParams = $this->buildCurQueryParams( $cacheEntry );
224 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
225 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
226 }
227
228 return $curLink;
229 }
230
236 private function buildDiffQueryParams( RecentChange $recentChange ) {
237 return [
238 'curid' => $recentChange->mAttribs['rc_cur_id'],
239 'diff' => $recentChange->mAttribs['rc_this_oldid'],
240 'oldid' => $recentChange->mAttribs['rc_last_oldid']
241 ];
242 }
243
250 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
251 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
252 $diffMessage = $this->getMessage( 'diff' );
253 $logTypes = [ RC_NEW, RC_LOG ];
254
255 if ( !$showDiffLinks ) {
256 $diffLink = $diffMessage;
257 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
258 $diffLink = $diffMessage;
259 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
260 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
261 $pageTitle = Title::newFromID( $rcCurId );
262 if ( $pageTitle === null ) {
263 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
264 return $diffMessage;
265 }
266 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
267 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
268 } else {
269 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
270 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
271 }
272
273 return $diffLink;
274 }
275
284 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
285 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
286 $lastMessage = $this->getMessage( 'last' );
287 $type = $cacheEntry->mAttribs['rc_type'];
288 $logTypes = [ RC_LOG ];
289
290 // Make "last" link
291 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
292 $lastLink = $lastMessage;
293 } else {
294 $lastLink = $this->linkRenderer->makeKnownLink(
295 $cacheEntry->getTitle(),
296 new HtmlArmor( $lastMessage ),
297 [ 'class' => 'mw-changeslist-diff' ],
298 $this->buildDiffQueryParams( $cacheEntry )
299 );
300 }
301
302 return $lastLink;
303 }
304
310 private function getUserLink( RecentChange $cacheEntry ) {
311 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
312 $deletedClass = 'history-deleted';
313 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
314 $deletedClass .= ' mw-history-suppressed';
315 }
316 $userLink = ' <span class="' . $deletedClass . '">' .
317 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
318 } else {
319 return $this->userLinkRenderer->userLink(
320 $cacheEntry->getPerformerIdentity(),
321 $this->context,
322 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
323 );
324 }
325
326 return $userLink;
327 }
328
334 private function getMessage( $key ) {
335 return $this->messages[$key];
336 }
337
338}
339
341class_alias( RCCacheEntryFactory::class, 'RCCacheEntryFactory' );
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.
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition Linker.php:61
Service class that renders HTML for user-related links.
Class to simplify the use of log pages.
Definition LogPage.php:50
static isDeleted( $rc, $field)
Determine if said field of a revision is hidden.
static userCan( $rc, $field, ?Authority $performer=null)
Determine if the current user is allowed to view a particular field of this revision,...
static isUnpatrolled( $rc, User $user)
Create a RCCacheEntry from a RecentChange to use in EnhancedChangesList.
newFromRecentChange(RecentChange $baseRC, $watched)
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer, UserLinkRenderer $userLinkRenderer)
Utility class for creating and reading rows in the recentchanges table.
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.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:32
Store key-value entries in a size-limited in-memory LRU cache.
Interface for objects which can provide a MediaWiki context on request.