3 * Creates a RCCacheEntry from a RecentChange to use in EnhancedChangesList
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.
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.
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
22 use MediaWiki\Linker\LinkRenderer
;
23 use MediaWiki\Storage\RevisionRecord
;
25 class RCCacheEntryFactory
{
27 /* @var IContextSource */
36 private $linkRenderer;
39 * @param IContextSource $context
40 * @param string[] $messages
41 * @param LinkRenderer $linkRenderer
43 public function __construct(
44 IContextSource
$context, $messages, LinkRenderer
$linkRenderer
46 $this->context
= $context;
47 $this->messages
= $messages;
48 $this->linkRenderer
= $linkRenderer;
52 * @param RecentChange $baseRC
53 * @param bool $watched
55 * @return RCCacheEntry
57 public function newFromRecentChange( RecentChange
$baseRC, $watched ) {
58 $user = $this->context
->getUser();
59 $counter = $baseRC->counter
;
61 $cacheEntry = RCCacheEntry
::newFromParent( $baseRC );
63 // Should patrol-related stuff be shown?
64 $cacheEntry->unpatrolled
= ChangesList
::isUnpatrolled( $baseRC, $user );
66 $cacheEntry->watched
= $cacheEntry->mAttribs
['rc_type'] == RC_LOG ?
false : $watched;
67 $cacheEntry->numberofWatchingusers
= $baseRC->numberofWatchingusers
;
69 $cacheEntry->link
= $this->buildCLink( $cacheEntry );
70 $cacheEntry->timestamp
= $this->buildTimestamp( $cacheEntry );
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 );
76 $cacheEntry->difflink
= $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
77 $cacheEntry->curlink
= $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
78 $cacheEntry->lastlink
= $this->buildLastLink( $cacheEntry, $showDiffLinks );
81 $cacheEntry->userlink
= $this->getUserLink( $cacheEntry );
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)
89 // Customisation flags (using default 0)
91 // User edit count (using default )
93 // do not wrap the message in parentheses
102 * @param RecentChange $cacheEntry
107 private function showDiffLinks( RecentChange
$cacheEntry, User
$user ) {
108 return ChangesList
::userCan( $cacheEntry, RevisionRecord
::DELETED_TEXT
, $user );
112 * @param RecentChange $cacheEntry
116 private function buildCLink( RecentChange
$cacheEntry ) {
117 $type = $cacheEntry->mAttribs
['rc_type'];
119 // New unpatrolled pages
120 if ( $cacheEntry->unpatrolled
&& $type == RC_NEW
) {
121 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
123 } elseif ( $type == RC_LOG
) {
124 $logType = $cacheEntry->mAttribs
['rc_log_type'];
127 $clink = $this->getLogLink( $logType );
129 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
130 $clink = $this->linkRenderer
->makeLink( $cacheEntry->getTitle() );
132 // Log entries (old format) and special pages
133 } elseif ( $cacheEntry->mAttribs
['rc_namespace'] == NS_SPECIAL
) {
134 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
138 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
144 private function getLogLink( $logType ) {
145 $logtitle = SpecialPage
::getTitleFor( 'Log', $logType );
146 $logpage = new LogPage( $logType );
147 $logname = $logpage->getName()->text();
149 $logLink = $this->context
->msg( 'parentheses' )
151 $this->linkRenderer
->makeKnownLink( $logtitle, $logname )
158 * @param RecentChange $cacheEntry
162 private function buildTimestamp( RecentChange
$cacheEntry ) {
163 return $this->context
->getLanguage()->userTime(
164 $cacheEntry->mAttribs
['rc_timestamp'],
165 $this->context
->getUser()
170 * @param RecentChange $recentChange
174 private function buildCurQueryParams( RecentChange
$recentChange ) {
176 'curid' => $recentChange->mAttribs
['rc_cur_id'],
178 'oldid' => $recentChange->mAttribs
['rc_this_oldid']
183 * @param RecentChange $cacheEntry
184 * @param bool $showDiffLinks
185 * @param int $counter
189 private function buildCurLink( RecentChange
$cacheEntry, $showDiffLinks, $counter ) {
190 $queryParams = $this->buildCurQueryParams( $cacheEntry );
191 $curMessage = $this->getMessage( 'cur' );
192 $logTypes = [ RC_LOG
];
194 if ( !$showDiffLinks ||
in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
195 $curLink = $curMessage;
197 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
198 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
205 * @param RecentChange $recentChange
209 private function buildDiffQueryParams( RecentChange
$recentChange ) {
211 'curid' => $recentChange->mAttribs
['rc_cur_id'],
212 'diff' => $recentChange->mAttribs
['rc_this_oldid'],
213 'oldid' => $recentChange->mAttribs
['rc_last_oldid']
218 * @param RecentChange $cacheEntry
219 * @param bool $showDiffLinks
220 * @param int $counter
224 private function buildDiffLink( RecentChange
$cacheEntry, $showDiffLinks, $counter ) {
225 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
226 $diffMessage = $this->getMessage( 'diff' );
227 $logTypes = [ RC_NEW
, RC_LOG
];
229 if ( !$showDiffLinks ) {
230 $diffLink = $diffMessage;
231 } elseif ( in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
232 $diffLink = $diffMessage;
233 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
234 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
235 $pageTitle = Title
::newFromID( $rcCurId );
236 if ( $pageTitle === null ) {
237 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
240 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
241 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
243 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
244 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
251 * Builds the link to the previous version
253 * @param RecentChange $cacheEntry
254 * @param bool $showDiffLinks
258 private function buildLastLink( RecentChange
$cacheEntry, $showDiffLinks ) {
259 $lastOldid = $cacheEntry->mAttribs
['rc_last_oldid'];
260 $lastMessage = $this->getMessage( 'last' );
261 $type = $cacheEntry->mAttribs
['rc_type'];
262 $logTypes = [ RC_LOG
];
265 if ( !$showDiffLinks ||
!$lastOldid ||
in_array( $type, $logTypes ) ) {
266 $lastLink = $lastMessage;
268 $lastLink = $this->linkRenderer
->makeKnownLink(
269 $cacheEntry->getTitle(),
270 new HtmlArmor( $lastMessage ),
271 [ 'class' => 'mw-changeslist-diff' ],
272 $this->buildDiffQueryParams( $cacheEntry )
280 * @param RecentChange $cacheEntry
284 private function getUserLink( RecentChange
$cacheEntry ) {
285 if ( ChangesList
::isDeleted( $cacheEntry, RevisionRecord
::DELETED_USER
) ) {
286 $userLink = ' <span class="history-deleted">' .
287 $this->context
->msg( 'rev-deleted-user' )->escaped() . '</span>';
289 $userLink = Linker
::userLink(
290 $cacheEntry->mAttribs
['rc_user'],
291 $cacheEntry->mAttribs
['rc_user_text'],
292 ExternalUserNames
::getLocal( $cacheEntry->mAttribs
['rc_user_text'] )
304 private function getMessage( $key ) {
305 return $this->messages
[$key];