Debian Codegraph @

includes/recentchanges/RCCacheEntryFactory.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Context\IContextSource;
22 use MediaWiki\Linker\Linker;
23 use MediaWiki\Linker\LinkRenderer;
24 use MediaWiki\Revision\RevisionRecord;
25 use MediaWiki\SpecialPage\SpecialPage;
26 use MediaWiki\Title\Title;
27 use MediaWiki\User\ExternalUserNames;
28
29 /**
30 * Create a RCCacheEntry from a RecentChange to use in EnhancedChangesList
31 *
32 * @ingroup RecentChanges
33 */
34 class RCCacheEntryFactory {
35
36 /** @var IContextSource */
37 private $context;
38
39 /** @var string[] */
40 private $messages;
41
42 /**
43 * @var LinkRenderer
44 */
45 private $linkRenderer;
46
47 /**
48 * @var MapCacheLRU
49 */
50 private MapCacheLRU $userLinkCache;
51
52 /**
53 * @var MapCacheLRU
54 */
55 private MapCacheLRU $toolLinkCache;
56
57 /**
58 * @param IContextSource $context
59 * @param string[] $messages
60 * @param LinkRenderer $linkRenderer
61 */
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
72 /**
73 * @param RecentChange $baseRC
74 * @param bool $watched
75 *
76 * @return RCCacheEntry
77 */
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 ) ) {
105 /**
106 * userToolLinks requires a lot of parser work to process multiple links that are
107 * rendered there, like contrib page, user talk etc. Often, active
108 * users will appear multiple times on same run of RecentChanges, and therefore it is
109 * unnecessary to process it for each RC record separately.
110 */
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
135 /**
136 * @param RCCacheEntry $cacheEntry
137 *
138 * @return string
139 */
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
178 /**
179 * @param RecentChange $cacheEntry
180 *
181 * @return string
182 */
183 private function buildTimestamp( RecentChange $cacheEntry ) {
184 return $this->context->getLanguage()->userTime(
185 $cacheEntry->mAttribs['rc_timestamp'],
186 $this->context->getUser()
187 );
188 }
189
190 /**
191 * @param RecentChange $recentChange
192 *
193 * @return array
194 */
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
203 /**
204 * @param RecentChange $cacheEntry
205 * @param bool $showDiffLinks
206 *
207 * @return string
208 */
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
227 /**
228 * @param RecentChange $recentChange
229 *
230 * @return array
231 */
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
240 /**
241 * @param RecentChange $cacheEntry
242 * @param bool $showDiffLinks
243 *
244 * @return string
245 */
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
272 /**
273 * Builds the link to the previous version
274 *
275 * @param RecentChange $cacheEntry
276 * @param bool $showDiffLinks
277 *
278 * @return string
279 */
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
301 /**
302 * @param RecentChange $cacheEntry
303 *
304 * @return string
305 */
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 {
315 /**
316 * UserLink requires parser to render which when run on thousands of records can add
317 * up to significant amount of processing time.
318 * @see RCCacheEntryFactory::newFromRecentChange
319 */
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
337 /**
338 * @param string $key
339 *
340 * @return string
341 */
342 private function getMessage( $key ) {
343 return $this->messages[$key];
344 }
345
346 }