MediaWiki master
RCCacheEntryFactory.php
Go to the documentation of this file.
1<?php
8
20
27
29 private $context;
30
32 private $messages;
33
37 private $linkRenderer;
38
39 private UserLinkRenderer $userLinkRenderer;
40
41 private MapCacheLRU $toolLinkCache;
42
49 public function __construct(
50 IContextSource $context,
51 $messages,
52 LinkRenderer $linkRenderer,
53 UserLinkRenderer $userLinkRenderer
54 ) {
55 $this->context = $context;
56 $this->messages = $messages;
57 $this->linkRenderer = $linkRenderer;
58 $this->userLinkRenderer = $userLinkRenderer;
59 $this->toolLinkCache = new MapCacheLRU( 50 );
60 }
61
68 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
69 $user = $this->context->getUser();
70
71 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
72
73 // Should patrol-related stuff be shown?
74 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
75
76 $cacheEntry->watched = $cacheEntry->mAttribs['rc_source'] == RecentChange::SRC_LOG ? false : $watched;
77 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
78 $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
79
80 $cacheEntry->link = $this->buildCLink( $cacheEntry );
81 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
82
83 // Make "cur" and "diff" links. Do not use link(), it is too slow if
84 // called too many times (50% of CPU time on RecentChanges!).
85 $showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
86
87 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
88 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
89 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
90
91 // Make user links
92 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
93
94 if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
101 $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
102 $this->toolLinkCache->makeKey(
103 $cacheEntry->mAttribs['rc_user_text'],
104 $this->context->getUser()->getName(),
105 $this->context->getLanguage()->getCode()
106 ),
107 static fn () => Linker::userToolLinks(
108 $cacheEntry->mAttribs['rc_user'],
109 $cacheEntry->mAttribs['rc_user_text'],
110 // Should the contributions link be red if the user has no edits (using default)
111 false,
112 // Customisation flags (using default 0)
113 0,
114 // User edit count (using default )
115 null,
116 // do not wrap the message in parentheses
117 false
118 )
119 );
120 }
121
122 return $cacheEntry;
123 }
124
130 private function buildCLink( RCCacheEntry $cacheEntry ) {
131 $source = $cacheEntry->mAttribs['rc_source'];
132
133 // Log entries
135 $logType = $cacheEntry->mAttribs['rc_log_type'];
136
137 if ( $logType ) {
138 $clink = $this->getLogLink( $logType );
139 } else {
140 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
141 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
142 }
143 // Log entries (old format) and special pages
144 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
145 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
146 $clink = '';
147 // Edits and everything else
148 } else {
149 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
150 }
151
152 return $clink;
153 }
154
155 private function getLogLink( string $logType ): string {
156 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
157 $logpage = new LogPage( $logType );
158 $logname = $logpage->getName()->text();
159
160 $logLink = $this->context->msg( 'parentheses' )
161 ->rawParams(
162 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
163 )->escaped();
164
165 return $logLink;
166 }
167
173 private function buildTimestamp( RecentChange $cacheEntry ) {
174 return $this->context->getLanguage()->userTime(
175 $cacheEntry->mAttribs['rc_timestamp'],
176 $this->context->getUser()
177 );
178 }
179
185 private function buildCurQueryParams( RecentChange $recentChange ) {
186 return [
187 'curid' => $recentChange->mAttribs['rc_cur_id'],
188 'diff' => 0,
189 'oldid' => $recentChange->mAttribs['rc_this_oldid']
190 ];
191 }
192
199 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
200 $curMessage = $this->getMessage( 'cur' );
201 $logTypes = [ RecentChange::SRC_LOG ];
202 if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
203 $showDiffLinks = false;
204 }
205
206 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_source'], $logTypes ) ) {
207 $curLink = $curMessage;
208 } else {
209 $queryParams = $this->buildCurQueryParams( $cacheEntry );
210 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
211 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
212 }
213
214 return $curLink;
215 }
216
222 private function buildDiffQueryParams( RecentChange $recentChange ) {
223 return [
224 'curid' => $recentChange->mAttribs['rc_cur_id'],
225 'diff' => $recentChange->mAttribs['rc_this_oldid'],
226 'oldid' => $recentChange->mAttribs['rc_last_oldid']
227 ];
228 }
229
236 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
237 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
238 $diffMessage = $this->getMessage( 'diff' );
240
241 if ( !$showDiffLinks ) {
242 $diffLink = $diffMessage;
243 } elseif ( in_array( $cacheEntry->mAttribs['rc_source'], $logTypes ) ) {
244 $diffLink = $diffMessage;
245 } elseif ( $cacheEntry->getAttribute( 'rc_source' ) == RecentChange::SRC_CATEGORIZE ) {
246 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
247 $pageTitle = Title::newFromID( $rcCurId );
248 if ( $pageTitle === null ) {
249 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
250 return $diffMessage;
251 }
252 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
253 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
254 } else {
255 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
256 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
257 }
258
259 return $diffLink;
260 }
261
270 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
271 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
272 $lastMessage = $this->getMessage( 'last' );
273 $source = $cacheEntry->mAttribs['rc_source'];
274 $logTypes = [ RecentChange::SRC_LOG ];
275
276 // Make "last" link
277 if ( !$showDiffLinks || !$lastOldid || in_array( $source, $logTypes ) ) {
278 $lastLink = $lastMessage;
279 } else {
280 $lastLink = $this->linkRenderer->makeKnownLink(
281 $cacheEntry->getTitle(),
282 new HtmlArmor( $lastMessage ),
283 [ 'class' => 'mw-changeslist-diff' ],
284 $this->buildDiffQueryParams( $cacheEntry )
285 );
286 }
287
288 return $lastLink;
289 }
290
296 private function getUserLink( RecentChange $cacheEntry ) {
297 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
298 $deletedClass = 'history-deleted';
299 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
300 $deletedClass .= ' mw-history-suppressed';
301 }
302 $userLink = ' <span class="' . $deletedClass . '">' .
303 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
304 } else {
305 return $this->linkRenderer->makeUserLink(
306 $cacheEntry->getPerformerIdentity(),
307 $this->context,
308 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
309 );
310 }
311
312 return $userLink;
313 }
314
320 private function getMessage( $key ) {
321 return $this->messages[$key];
322 }
323
324}
325
327class_alias( RCCacheEntryFactory::class, 'RCCacheEntryFactory' );
const NS_SPECIAL
Definition Defines.php:40
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:47
Service class that renders HTML for user-related links.
Class to simplify the use of log pages.
Definition LogPage.php:35
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:70
Class to parse and build external user names.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:18
Store key-value entries in a size-limited in-memory LRU cache.
Interface for objects which can provide a MediaWiki context on request.
$source