Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / changes / RCCacheEntryFactory.php
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 use MediaWiki\Linker\LinkRenderer;
23 use MediaWiki\Storage\RevisionRecord;
24
25 class RCCacheEntryFactory {
26
27 /* @var IContextSource */
28 private $context;
29
30 /* @var string[] */
31 private $messages;
32
33 /**
34 * @var LinkRenderer
35 */
36 private $linkRenderer;
37
38 /**
39 * @param IContextSource $context
40 * @param string[] $messages
41 * @param LinkRenderer $linkRenderer
42 */
43 public function __construct(
44 IContextSource $context, $messages, LinkRenderer $linkRenderer
45 ) {
46 $this->context = $context;
47 $this->messages = $messages;
48 $this->linkRenderer = $linkRenderer;
49 }
50
51 /**
52 * @param RecentChange $baseRC
53 * @param bool $watched
54 *
55 * @return RCCacheEntry
56 */
57 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
58 $user = $this->context->getUser();
59 $counter = $baseRC->counter;
60
61 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
62
63 // Should patrol-related stuff be shown?
64 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
65
66 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
67 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
68
69 $cacheEntry->link = $this->buildCLink( $cacheEntry );
70 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
71
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 );
75
76 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
77 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
78 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
79
80 // Make user links
81 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
82
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)
88 false,
89 // Customisation flags (using default 0)
90 0,
91 // User edit count (using default )
92 null,
93 // do not wrap the message in parentheses
94 false
95 );
96 }
97
98 return $cacheEntry;
99 }
100
101 /**
102 * @param RecentChange $cacheEntry
103 * @param User $user
104 *
105 * @return bool
106 */
107 private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
108 return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
109 }
110
111 /**
112 * @param RecentChange $cacheEntry
113 *
114 * @return string
115 */
116 private function buildCLink( RecentChange $cacheEntry ) {
117 $type = $cacheEntry->mAttribs['rc_type'];
118
119 // New unpatrolled pages
120 if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
121 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
122 // Log entries
123 } elseif ( $type == RC_LOG ) {
124 $logType = $cacheEntry->mAttribs['rc_log_type'];
125
126 if ( $logType ) {
127 $clink = $this->getLogLink( $logType );
128 } else {
129 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
130 $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
131 }
132 // Log entries (old format) and special pages
133 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
134 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
135 $clink = '';
136 // Edits
137 } else {
138 $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
139 }
140
141 return $clink;
142 }
143
144 private function getLogLink( $logType ) {
145 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
146 $logpage = new LogPage( $logType );
147 $logname = $logpage->getName()->text();
148
149 $logLink = $this->context->msg( 'parentheses' )
150 ->rawParams(
151 $this->linkRenderer->makeKnownLink( $logtitle, $logname )
152 )->escaped();
153
154 return $logLink;
155 }
156
157 /**
158 * @param RecentChange $cacheEntry
159 *
160 * @return string
161 */
162 private function buildTimestamp( RecentChange $cacheEntry ) {
163 return $this->context->getLanguage()->userTime(
164 $cacheEntry->mAttribs['rc_timestamp'],
165 $this->context->getUser()
166 );
167 }
168
169 /**
170 * @param RecentChange $recentChange
171 *
172 * @return array
173 */
174 private function buildCurQueryParams( RecentChange $recentChange ) {
175 return [
176 'curid' => $recentChange->mAttribs['rc_cur_id'],
177 'diff' => 0,
178 'oldid' => $recentChange->mAttribs['rc_this_oldid']
179 ];
180 }
181
182 /**
183 * @param RecentChange $cacheEntry
184 * @param bool $showDiffLinks
185 * @param int $counter
186 *
187 * @return string
188 */
189 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
190 $queryParams = $this->buildCurQueryParams( $cacheEntry );
191 $curMessage = $this->getMessage( 'cur' );
192 $logTypes = [ RC_LOG ];
193
194 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
195 $curLink = $curMessage;
196 } else {
197 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
198 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
199 }
200
201 return $curLink;
202 }
203
204 /**
205 * @param RecentChange $recentChange
206 *
207 * @return array
208 */
209 private function buildDiffQueryParams( RecentChange $recentChange ) {
210 return [
211 'curid' => $recentChange->mAttribs['rc_cur_id'],
212 'diff' => $recentChange->mAttribs['rc_this_oldid'],
213 'oldid' => $recentChange->mAttribs['rc_last_oldid']
214 ];
215 }
216
217 /**
218 * @param RecentChange $cacheEntry
219 * @param bool $showDiffLinks
220 * @param int $counter
221 *
222 * @return string
223 */
224 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
225 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
226 $diffMessage = $this->getMessage( 'diff' );
227 $logTypes = [ RC_NEW, RC_LOG ];
228
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 );
238 return $diffMessage;
239 }
240 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
241 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
242 } else {
243 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
244 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
245 }
246
247 return $diffLink;
248 }
249
250 /**
251 * Builds the link to the previous version
252 *
253 * @param RecentChange $cacheEntry
254 * @param bool $showDiffLinks
255 *
256 * @return string
257 */
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 ];
263
264 // Make "last" link
265 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
266 $lastLink = $lastMessage;
267 } else {
268 $lastLink = $this->linkRenderer->makeKnownLink(
269 $cacheEntry->getTitle(),
270 new HtmlArmor( $lastMessage ),
271 [ 'class' => 'mw-changeslist-diff' ],
272 $this->buildDiffQueryParams( $cacheEntry )
273 );
274 }
275
276 return $lastLink;
277 }
278
279 /**
280 * @param RecentChange $cacheEntry
281 *
282 * @return string
283 */
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>';
288 } else {
289 $userLink = Linker::userLink(
290 $cacheEntry->mAttribs['rc_user'],
291 $cacheEntry->mAttribs['rc_user_text'],
292 ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
293 );
294 }
295
296 return $userLink;
297 }
298
299 /**
300 * @param string $key
301 *
302 * @return string
303 */
304 private function getMessage( $key ) {
305 return $this->messages[$key];
306 }
307
308 }