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\Revision\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();
60 $cacheEntry = RCCacheEntry
::newFromParent( $baseRC );
62 // Should patrol-related stuff be shown?
63 $cacheEntry->unpatrolled
= ChangesList
::isUnpatrolled( $baseRC, $user );
65 $cacheEntry->watched
= $cacheEntry->mAttribs
['rc_type'] == RC_LOG ?
false : $watched;
66 $cacheEntry->numberofWatchingusers
= $baseRC->numberofWatchingusers
;
68 $cacheEntry->link
= $this->buildCLink( $cacheEntry );
69 $cacheEntry->timestamp
= $this->buildTimestamp( $cacheEntry );
71 // Make "cur" and "diff" links. Do not use link(), it is too slow if
72 // called too many times (50% of CPU time on RecentChanges!).
73 $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
75 $cacheEntry->difflink
= $this->buildDiffLink( $cacheEntry, $showDiffLinks );
76 $cacheEntry->curlink
= $this->buildCurLink( $cacheEntry, $showDiffLinks );
77 $cacheEntry->lastlink
= $this->buildLastLink( $cacheEntry, $showDiffLinks );
80 $cacheEntry->userlink
= $this->getUserLink( $cacheEntry );
82 if ( !ChangesList
::isDeleted( $cacheEntry, RevisionRecord
::DELETED_USER
) ) {
83 $cacheEntry->usertalklink
= Linker
::userToolLinks(
84 $cacheEntry->mAttribs
['rc_user'],
85 $cacheEntry->mAttribs
['rc_user_text'],
86 // Should the contributions link be red if the user has no edits (using default)
88 // Customisation flags (using default 0)
90 // User edit count (using default )
92 // do not wrap the message in parentheses
101 * @param RecentChange $cacheEntry
106 private function showDiffLinks( RecentChange
$cacheEntry, User
$user ) {
107 return ChangesList
::userCan( $cacheEntry, RevisionRecord
::DELETED_TEXT
, $user );
111 * @param RCCacheEntry $cacheEntry
115 private function buildCLink( RCCacheEntry
$cacheEntry ) {
116 $type = $cacheEntry->mAttribs
['rc_type'];
118 // New unpatrolled pages
119 if ( $cacheEntry->unpatrolled
&& $type == RC_NEW
) {
120 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
122 } elseif ( $type == RC_LOG
) {
123 $logType = $cacheEntry->mAttribs
['rc_log_type'];
126 $clink = $this->getLogLink( $logType );
128 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
129 $clink = $this->linkRenderer
->makeLink( $cacheEntry->getTitle() );
131 // Log entries (old format) and special pages
132 } elseif ( $cacheEntry->mAttribs
['rc_namespace'] == NS_SPECIAL
) {
133 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
137 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
143 private function getLogLink( $logType ) {
144 $logtitle = SpecialPage
::getTitleFor( 'Log', $logType );
145 $logpage = new LogPage( $logType );
146 $logname = $logpage->getName()->text();
148 $logLink = $this->context
->msg( 'parentheses' )
150 $this->linkRenderer
->makeKnownLink( $logtitle, $logname )
157 * @param RecentChange $cacheEntry
161 private function buildTimestamp( RecentChange
$cacheEntry ) {
162 return $this->context
->getLanguage()->userTime(
163 $cacheEntry->mAttribs
['rc_timestamp'],
164 $this->context
->getUser()
169 * @param RecentChange $recentChange
173 private function buildCurQueryParams( RecentChange
$recentChange ) {
175 'curid' => $recentChange->mAttribs
['rc_cur_id'],
177 'oldid' => $recentChange->mAttribs
['rc_this_oldid']
182 * @param RecentChange $cacheEntry
183 * @param bool $showDiffLinks
187 private function buildCurLink( RecentChange
$cacheEntry, $showDiffLinks ) {
188 $queryParams = $this->buildCurQueryParams( $cacheEntry );
189 $curMessage = $this->getMessage( 'cur' );
190 $logTypes = [ RC_LOG
];
192 if ( !$showDiffLinks ||
in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
193 $curLink = $curMessage;
195 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
196 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
203 * @param RecentChange $recentChange
207 private function buildDiffQueryParams( RecentChange
$recentChange ) {
209 'curid' => $recentChange->mAttribs
['rc_cur_id'],
210 'diff' => $recentChange->mAttribs
['rc_this_oldid'],
211 'oldid' => $recentChange->mAttribs
['rc_last_oldid']
216 * @param RecentChange $cacheEntry
217 * @param bool $showDiffLinks
221 private function buildDiffLink( RecentChange
$cacheEntry, $showDiffLinks ) {
222 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
223 $diffMessage = $this->getMessage( 'diff' );
224 $logTypes = [ RC_NEW
, RC_LOG
];
226 if ( !$showDiffLinks ) {
227 $diffLink = $diffMessage;
228 } elseif ( in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
229 $diffLink = $diffMessage;
230 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
231 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
232 $pageTitle = Title
::newFromID( $rcCurId );
233 if ( $pageTitle === null ) {
234 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
237 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
238 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
240 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
241 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
248 * Builds the link to the previous version
250 * @param RecentChange $cacheEntry
251 * @param bool $showDiffLinks
255 private function buildLastLink( RecentChange
$cacheEntry, $showDiffLinks ) {
256 $lastOldid = $cacheEntry->mAttribs
['rc_last_oldid'];
257 $lastMessage = $this->getMessage( 'last' );
258 $type = $cacheEntry->mAttribs
['rc_type'];
259 $logTypes = [ RC_LOG
];
262 if ( !$showDiffLinks ||
!$lastOldid ||
in_array( $type, $logTypes ) ) {
263 $lastLink = $lastMessage;
265 $lastLink = $this->linkRenderer
->makeKnownLink(
266 $cacheEntry->getTitle(),
267 new HtmlArmor( $lastMessage ),
268 [ 'class' => 'mw-changeslist-diff' ],
269 $this->buildDiffQueryParams( $cacheEntry )
277 * @param RecentChange $cacheEntry
281 private function getUserLink( RecentChange
$cacheEntry ) {
282 if ( ChangesList
::isDeleted( $cacheEntry, RevisionRecord
::DELETED_USER
) ) {
283 $userLink = ' <span class="history-deleted">' .
284 $this->context
->msg( 'rev-deleted-user' )->escaped() . '</span>';
286 $userLink = Linker
::userLink(
287 $cacheEntry->mAttribs
['rc_user'],
288 $cacheEntry->mAttribs
['rc_user_text'],
289 ExternalUserNames
::getLocal( $cacheEntry->mAttribs
['rc_user_text'] )
301 private function getMessage( $key ) {
302 return $this->messages
[$key];