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