Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / changes / RCCacheEntryFactory.php
blob2c5c8b128c9ee7f7c6338deba966c6ff39a4a2ad
1 <?php
2 /**
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
20 * @file
22 use MediaWiki\Linker\LinkRenderer;
24 class RCCacheEntryFactory {
26 /* @var IContextSource */
27 private $context;
29 /* @var string[] */
30 private $messages;
32 /**
33 * @var LinkRenderer
35 private $linkRenderer;
37 /**
38 * @param IContextSource $context
39 * @param string[] $messages
40 * @param LinkRenderer $linkRenderer
42 public function __construct(
43 IContextSource $context, $messages, LinkRenderer $linkRenderer
44 ) {
45 $this->context = $context;
46 $this->messages = $messages;
47 $this->linkRenderer = $linkRenderer;
50 /**
51 * @param RecentChange $baseRC
52 * @param bool $watched
54 * @return RCCacheEntry
56 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
57 $user = $this->context->getUser();
58 $counter = $baseRC->counter;
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, $counter );
76 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
77 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
79 // Make user links
80 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
82 if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
83 $cacheEntry->usertalklink = Linker::userToolLinks(
84 $cacheEntry->mAttribs['rc_user'],
85 $cacheEntry->mAttribs['rc_user_text']
89 return $cacheEntry;
92 /**
93 * @param RecentChange $cacheEntry
94 * @param User $user
96 * @return bool
98 private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
99 return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
103 * @param RecentChange $cacheEntry
105 * @return string
107 private function buildCLink( RecentChange $cacheEntry ) {
108 $type = $cacheEntry->mAttribs['rc_type'];
110 // New unpatrolled pages
111 if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
112 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
113 // Log entries
114 } elseif ( $type == RC_LOG ) {
115 $logType = $cacheEntry->mAttribs['rc_log_type'];
117 if ( $logType ) {
118 $clink = $this->getLogLink( $logType );
119 } else {
120 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
121 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
123 // Log entries (old format) and special pages
124 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
125 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
126 $clink = '';
127 // Edits
128 } else {
129 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
132 return $clink;
135 private function getLogLink( $logType ) {
136 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
137 $logpage = new LogPage( $logType );
138 $logname = $logpage->getName()->text();
140 $logLink = $this->context->msg( 'parentheses' )
141 ->rawParams(
142 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
143 )->escaped();
145 return $logLink;
149 * @param RecentChange $cacheEntry
151 * @return string
153 private function buildTimestamp( RecentChange $cacheEntry ) {
154 return $this->context->getLanguage()->userTime(
155 $cacheEntry->mAttribs['rc_timestamp'],
156 $this->context->getUser()
161 * @param RecentChange $recentChange
163 * @return array
165 private function buildCurQueryParams( RecentChange $recentChange ) {
166 return [
167 'curid' => $recentChange->mAttribs['rc_cur_id'],
168 'diff' => 0,
169 'oldid' => $recentChange->mAttribs['rc_this_oldid']
174 * @param RecentChange $cacheEntry
175 * @param bool $showDiffLinks
176 * @param int $counter
178 * @return string
180 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
181 $queryParams = $this->buildCurQueryParams( $cacheEntry );
182 $curMessage = $this->getMessage( 'cur' );
183 $logTypes = [ RC_LOG ];
185 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
186 $curLink = $curMessage;
187 } else {
188 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
189 $curLink = "<a href=\"$curUrl\">$curMessage</a>";
192 return $curLink;
196 * @param RecentChange $recentChange
198 * @return array
200 private function buildDiffQueryParams( RecentChange $recentChange ) {
201 return [
202 'curid' => $recentChange->mAttribs['rc_cur_id'],
203 'diff' => $recentChange->mAttribs['rc_this_oldid'],
204 'oldid' => $recentChange->mAttribs['rc_last_oldid']
209 * @param RecentChange $cacheEntry
210 * @param bool $showDiffLinks
211 * @param int $counter
213 * @return string
215 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
216 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
217 $diffMessage = $this->getMessage( 'diff' );
218 $logTypes = [ RC_NEW, RC_LOG ];
220 if ( !$showDiffLinks ) {
221 $diffLink = $diffMessage;
222 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
223 $diffLink = $diffMessage;
224 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
225 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
226 $pageTitle = Title::newFromID( $rcCurId );
227 if ( $pageTitle === null ) {
228 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
229 return $diffMessage;
231 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
232 $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>";
233 } else {
234 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
235 $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>";
238 return $diffLink;
242 * @param RecentChange $cacheEntry
243 * @param bool $showDiffLinks
245 * @return string
247 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
248 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
249 $lastMessage = $this->getMessage( 'last' );
250 $type = $cacheEntry->mAttribs['rc_type'];
251 $logTypes = [ RC_LOG ];
253 // Make "last" link
254 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
255 $lastLink = $lastMessage;
256 } else {
257 $lastLink = $this->linkRenderer->makeKnownLink(
258 $cacheEntry->getTitle(),
259 new HtmlArmor( $lastMessage ),
261 $this->buildDiffQueryParams( $cacheEntry )
265 return $lastLink;
269 * @param RecentChange $cacheEntry
271 * @return string
273 private function getUserLink( RecentChange $cacheEntry ) {
274 if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
275 $userLink = ' <span class="history-deleted">' .
276 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
277 } else {
278 $userLink = Linker::userLink(
279 $cacheEntry->mAttribs['rc_user'],
280 $cacheEntry->mAttribs['rc_user_text']
284 return $userLink;
288 * @param string $key
290 * @return string
292 private function getMessage( $key ) {
293 return $this->messages[$key];