Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.54% covered (success)
91.54%
119 / 130
75.00% covered (warning)
75.00%
9 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
RCCacheEntryFactory
91.54% covered (success)
91.54%
119 / 130
75.00% covered (warning)
75.00%
9 / 12
29.51
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 newFromRecentChange
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
3
 buildCLink
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
4.25
 getLogLink
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 buildTimestamp
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 buildCurQueryParams
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 buildCurLink
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 buildDiffQueryParams
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 buildDiffLink
61.11% covered (warning)
61.11%
11 / 18
0.00% covered (danger)
0.00%
0 / 1
6.47
 buildLastLink
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getUserLink
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
3.00
 getMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Creates a RCCacheEntry from a RecentChange to use in EnhancedChangesList
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\Context\IContextSource;
24use MediaWiki\Linker\Linker;
25use MediaWiki\Linker\LinkRenderer;
26use MediaWiki\Revision\RevisionRecord;
27use MediaWiki\SpecialPage\SpecialPage;
28use MediaWiki\Title\Title;
29use MediaWiki\User\ExternalUserNames;
30
31class RCCacheEntryFactory {
32
33    /** @var IContextSource */
34    private $context;
35
36    /** @var string[] */
37    private $messages;
38
39    /**
40     * @var LinkRenderer
41     */
42    private $linkRenderer;
43
44    /**
45     * @var MapCacheLRU
46     */
47    private MapCacheLRU $userLinkCache;
48
49    /**
50     * @var MapCacheLRU
51     */
52    private MapCacheLRU $toolLinkCache;
53
54    /**
55     * @param IContextSource $context
56     * @param string[] $messages
57     * @param LinkRenderer $linkRenderer
58     */
59    public function __construct(
60        IContextSource $context, $messages, LinkRenderer $linkRenderer
61    ) {
62        $this->context = $context;
63        $this->messages = $messages;
64        $this->linkRenderer = $linkRenderer;
65        $this->userLinkCache = new MapCacheLRU( 50 );
66        $this->toolLinkCache = new MapCacheLRU( 50 );
67    }
68
69    /**
70     * @param RecentChange $baseRC
71     * @param bool $watched
72     *
73     * @return RCCacheEntry
74     */
75    public function newFromRecentChange( RecentChange $baseRC, $watched ) {
76        $user = $this->context->getUser();
77
78        $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
79
80        // Should patrol-related stuff be shown?
81        $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
82
83        $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
84        $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
85        $cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry;
86
87        $cacheEntry->link = $this->buildCLink( $cacheEntry );
88        $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
89
90        // Make "cur" and "diff" links.  Do not use link(), it is too slow if
91        // called too many times (50% of CPU time on RecentChanges!).
92        $showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
93
94        $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
95        $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
96        $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
97
98        // Make user links
99        $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
100
101        if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
102            /**
103             * userToolLinks requires a lot of parser work to process multiple links that are
104             * rendered there, like contrib page, user talk etc. Often, active
105             * users will appear multiple times on same run of RecentChanges, and therefore it is
106             * unnecessary to process it for each RC record separately.
107             */
108            $cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback(
109                $this->toolLinkCache->makeKey(
110                    $cacheEntry->mAttribs['rc_user_text'],
111                    $this->context->getUser()->getName(),
112                    $this->context->getLanguage()->getCode()
113                ),
114                static fn () => Linker::userToolLinks(
115                    $cacheEntry->mAttribs['rc_user'],
116                    $cacheEntry->mAttribs['rc_user_text'],
117                    // Should the contributions link be red if the user has no edits (using default)
118                    false,
119                    // Customisation flags (using default 0)
120                    0,
121                    // User edit count (using default )
122                    null,
123                    // do not wrap the message in parentheses
124                    false
125                )
126            );
127        }
128
129        return $cacheEntry;
130    }
131
132    /**
133     * @param RCCacheEntry $cacheEntry
134     *
135     * @return string
136     */
137    private function buildCLink( RCCacheEntry $cacheEntry ) {
138        $type = $cacheEntry->mAttribs['rc_type'];
139
140        // Log entries
141        if ( $type == RC_LOG ) {
142            $logType = $cacheEntry->mAttribs['rc_log_type'];
143
144            if ( $logType ) {
145                $clink = $this->getLogLink( $logType );
146            } else {
147                wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
148                $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
149            }
150        // Log entries (old format) and special pages
151        } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
152            wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
153            $clink = '';
154        // Edits and everything else
155        } else {
156            $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
157        }
158
159        return $clink;
160    }
161
162    private function getLogLink( $logType ) {
163        $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
164        $logpage = new LogPage( $logType );
165        $logname = $logpage->getName()->text();
166
167        $logLink = $this->context->msg( 'parentheses' )
168            ->rawParams(
169                $this->linkRenderer->makeKnownLink( $logtitle, $logname )
170            )->escaped();
171
172        return $logLink;
173    }
174
175    /**
176     * @param RecentChange $cacheEntry
177     *
178     * @return string
179     */
180    private function buildTimestamp( RecentChange $cacheEntry ) {
181        return $this->context->getLanguage()->userTime(
182            $cacheEntry->mAttribs['rc_timestamp'],
183            $this->context->getUser()
184        );
185    }
186
187    /**
188     * @param RecentChange $recentChange
189     *
190     * @return array
191     */
192    private function buildCurQueryParams( RecentChange $recentChange ) {
193        return [
194            'curid' => $recentChange->mAttribs['rc_cur_id'],
195            'diff' => 0,
196            'oldid' => $recentChange->mAttribs['rc_this_oldid']
197        ];
198    }
199
200    /**
201     * @param RecentChange $cacheEntry
202     * @param bool $showDiffLinks
203     *
204     * @return string
205     */
206    private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
207        $curMessage = $this->getMessage( 'cur' );
208        $logTypes = [ RC_LOG ];
209        if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) {
210            $showDiffLinks = false;
211        }
212
213        if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
214            $curLink = $curMessage;
215        } else {
216            $queryParams = $this->buildCurQueryParams( $cacheEntry );
217            $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
218            $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
219        }
220
221        return $curLink;
222    }
223
224    /**
225     * @param RecentChange $recentChange
226     *
227     * @return array
228     */
229    private function buildDiffQueryParams( RecentChange $recentChange ) {
230        return [
231            'curid' => $recentChange->mAttribs['rc_cur_id'],
232            'diff' => $recentChange->mAttribs['rc_this_oldid'],
233            'oldid' => $recentChange->mAttribs['rc_last_oldid']
234        ];
235    }
236
237    /**
238     * @param RecentChange $cacheEntry
239     * @param bool $showDiffLinks
240     *
241     * @return string
242     */
243    private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
244        $queryParams = $this->buildDiffQueryParams( $cacheEntry );
245        $diffMessage = $this->getMessage( 'diff' );
246        $logTypes = [ RC_NEW, RC_LOG ];
247
248        if ( !$showDiffLinks ) {
249            $diffLink = $diffMessage;
250        } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
251            $diffLink = $diffMessage;
252        } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
253            $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
254            $pageTitle = Title::newFromID( $rcCurId );
255            if ( $pageTitle === null ) {
256                wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
257                return $diffMessage;
258            }
259            $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
260            $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
261        } else {
262            $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
263            $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
264        }
265
266        return $diffLink;
267    }
268
269    /**
270     * Builds the link to the previous version
271     *
272     * @param RecentChange $cacheEntry
273     * @param bool $showDiffLinks
274     *
275     * @return string
276     */
277    private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
278        $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
279        $lastMessage = $this->getMessage( 'last' );
280        $type = $cacheEntry->mAttribs['rc_type'];
281        $logTypes = [ RC_LOG ];
282
283        // Make "last" link
284        if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
285            $lastLink = $lastMessage;
286        } else {
287            $lastLink = $this->linkRenderer->makeKnownLink(
288                $cacheEntry->getTitle(),
289                new HtmlArmor( $lastMessage ),
290                [ 'class' => 'mw-changeslist-diff' ],
291                $this->buildDiffQueryParams( $cacheEntry )
292            );
293        }
294
295        return $lastLink;
296    }
297
298    /**
299     * @param RecentChange $cacheEntry
300     *
301     * @return string
302     */
303    private function getUserLink( RecentChange $cacheEntry ) {
304        if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
305            $deletedClass = 'history-deleted';
306            if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) {
307                $deletedClass .= ' mw-history-suppressed';
308            }
309            $userLink = ' <span class="' . $deletedClass . '">' .
310                $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
311        } else {
312            /**
313             * UserLink requires parser to render which when run on thousands of records can add
314             * up to significant amount of processing time.
315             * @see RCCacheEntryFactory::newFromRecentChange
316             */
317            $userLink = $this->userLinkCache->getWithSetCallback(
318                $this->userLinkCache->makeKey(
319                    $cacheEntry->mAttribs['rc_user_text'],
320                    $this->context->getUser()->getName(),
321                    $this->context->getLanguage()->getCode()
322                ),
323                static fn () => Linker::userLink(
324                    $cacheEntry->mAttribs['rc_user'],
325                    $cacheEntry->mAttribs['rc_user_text'],
326                    ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
327                )
328            );
329        }
330
331        return $userLink;
332    }
333
334    /**
335     * @param string $key
336     *
337     * @return string
338     */
339    private function getMessage( $key ) {
340        return $this->messages[$key];
341    }
342
343}