MediaWiki REL1_35
RCCacheEntryFactory.php
Go to the documentation of this file.
1<?php
24
26
27 /* @var IContextSource */
28 private $context;
29
30 /* @var string[] */
31 private $messages;
32
37
43 public function __construct(
45 ) {
46 $this->context = $context;
47 $this->messages = $messages;
48 $this->linkRenderer = $linkRenderer;
49 }
50
57 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
58 $user = $this->context->getUser();
59
60 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
61
62 // Should patrol-related stuff be shown?
63 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
64
65 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
66 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
67 $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
68
69 $cacheEntry->link = $this->buildCLink( $cacheEntry );
70 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
71
72 // Make "cur" and "diff" links. Do not use link(), it is too slow if
73 // called too many times (50% of CPU time on RecentChanges!).
74 $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
75
76 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
77 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
78 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
79
80 // Make user links
81 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
82
83 if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
84 $cacheEntry->usertalklink = Linker::userToolLinks(
85 $cacheEntry->mAttribs['rc_user'],
86 $cacheEntry->mAttribs['rc_user_text'],
87 // Should the contributions link be red if the user has no edits (using default)
88 false,
89 // Customisation flags (using default 0)
90 0,
91 // User edit count (using default )
92 null,
93 // do not wrap the message in parentheses
94 false
95 );
96 }
97
98 return $cacheEntry;
99 }
100
107 private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
108 return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
109 }
110
116 private function buildCLink( RCCacheEntry $cacheEntry ) {
117 $type = $cacheEntry->mAttribs['rc_type'];
118
119 // New unpatrolled pages
120 if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
121 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
122 // Log entries
123 } elseif ( $type == RC_LOG ) {
124 $logType = $cacheEntry->mAttribs['rc_log_type'];
125
126 if ( $logType ) {
127 $clink = $this->getLogLink( $logType );
128 } else {
129 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
130 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
131 }
132 // Log entries (old format) and special pages
133 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
134 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
135 $clink = '';
136 // Edits
137 } else {
138 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
139 }
140
141 return $clink;
142 }
143
144 private function getLogLink( $logType ) {
145 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
146 $logpage = new LogPage( $logType );
147 $logname = $logpage->getName()->text();
148
149 $logLink = $this->context->msg( 'parentheses' )
150 ->rawParams(
151 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
152 )->escaped();
153
154 return $logLink;
155 }
156
162 private function buildTimestamp( RecentChange $cacheEntry ) {
163 return $this->context->getLanguage()->userTime(
164 $cacheEntry->mAttribs['rc_timestamp'],
165 $this->context->getUser()
166 );
167 }
168
174 private function buildCurQueryParams( RecentChange $recentChange ) {
175 return [
176 'curid' => $recentChange->mAttribs['rc_cur_id'],
177 'diff' => 0,
178 'oldid' => $recentChange->mAttribs['rc_this_oldid']
179 ];
180 }
181
188 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
189 $queryParams = $this->buildCurQueryParams( $cacheEntry );
190 $curMessage = $this->getMessage( 'cur' );
191 $logTypes = [ RC_LOG ];
192
193 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
194 $curLink = $curMessage;
195 } else {
196 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
197 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
198 }
199
200 return $curLink;
201 }
202
208 private function buildDiffQueryParams( RecentChange $recentChange ) {
209 return [
210 'curid' => $recentChange->mAttribs['rc_cur_id'],
211 'diff' => $recentChange->mAttribs['rc_this_oldid'],
212 'oldid' => $recentChange->mAttribs['rc_last_oldid']
213 ];
214 }
215
222 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
223 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
224 $diffMessage = $this->getMessage( 'diff' );
225 $logTypes = [ RC_NEW, RC_LOG ];
226
227 if ( !$showDiffLinks ) {
228 $diffLink = $diffMessage;
229 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
230 $diffLink = $diffMessage;
231 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
232 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
233 $pageTitle = Title::newFromID( $rcCurId );
234 if ( $pageTitle === null ) {
235 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
236 return $diffMessage;
237 }
238 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
239 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
240 } else {
241 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
242 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
243 }
244
245 return $diffLink;
246 }
247
256 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
257 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
258 $lastMessage = $this->getMessage( 'last' );
259 $type = $cacheEntry->mAttribs['rc_type'];
260 $logTypes = [ RC_LOG ];
261
262 // Make "last" link
263 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
264 $lastLink = $lastMessage;
265 } else {
266 $lastLink = $this->linkRenderer->makeKnownLink(
267 $cacheEntry->getTitle(),
268 new HtmlArmor( $lastMessage ),
269 [ 'class' => 'mw-changeslist-diff' ],
270 $this->buildDiffQueryParams( $cacheEntry )
271 );
272 }
273
274 return $lastLink;
275 }
276
282 private function getUserLink( RecentChange $cacheEntry ) {
283 if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
284 $userLink = ' <span class="history-deleted">' .
285 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
286 } else {
287 $userLink = Linker::userLink(
288 $cacheEntry->mAttribs['rc_user'],
289 $cacheEntry->mAttribs['rc_user_text'],
290 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
291 );
292 }
293
294 return $userLink;
295 }
296
302 private function getMessage( $key ) {
303 return $this->messages[$key];
304 }
305
306}
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
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:906
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition Linker.php:951
Class to simplify the use of log pages.
Definition LogPage.php:37
Class that generates HTML links for pages.
Page revision base class.
buildCLink(RCCacheEntry $cacheEntry)
buildCurQueryParams(RecentChange $recentChange)
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks)
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Builds the link to the previous version.
buildTimestamp(RecentChange $cacheEntry)
getUserLink(RecentChange $cacheEntry)
buildDiffQueryParams(RecentChange $recentChange)
showDiffLinks(RecentChange $cacheEntry, User $user)
buildCurLink(RecentChange $cacheEntry, $showDiffLinks)
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.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
const RC_NEW
Definition Defines.php:133
const NS_SPECIAL
Definition Defines.php:59
const RC_LOG
Definition Defines.php:134
const RC_CATEGORIZE
Definition Defines.php:136
Interface for objects which can provide a MediaWiki context on request.