Merge "Avoid an infinite redirect in $wgSecureLogin handling"
[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
23 class RCCacheEntryFactory {
24
25 /* @var IContextSource */
26 private $context;
27
28 /* @var string[] */
29 private $messages;
30
31 /**
32 * @param IContextSource $context
33 * @param string[] $messages
34 */
35 public function __construct( IContextSource $context, $messages ) {
36 $this->context = $context;
37 $this->messages = $messages;
38 }
39
40 /**
41 * @param RecentChange $baseRC
42 * @param bool $watched
43 *
44 * @return RCCacheEntry
45 */
46 public function newFromRecentChange( RecentChange $baseRC, $watched ) {
47 $user = $this->context->getUser();
48 $counter = $baseRC->counter;
49
50 $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
51
52 // Should patrol-related stuff be shown?
53 $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
54
55 $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
56 $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
57
58 $cacheEntry->link = $this->buildCLink( $cacheEntry );
59 $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
60
61 // Make "cur" and "diff" links. Do not use link(), it is too slow if
62 // called too many times (50% of CPU time on RecentChanges!).
63 $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
64
65 $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
66 $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
67 $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
68
69 // Make user links
70 $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
71
72 if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
73 $cacheEntry->usertalklink = Linker::userToolLinks(
74 $cacheEntry->mAttribs['rc_user'],
75 $cacheEntry->mAttribs['rc_user_text']
76 );
77 }
78
79 return $cacheEntry;
80 }
81
82 /**
83 * @param RecentChange $cacheEntry
84 * @param User $user
85 *
86 * @return bool
87 */
88 private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
89 return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
90 }
91
92 /**
93 * @param RecentChange $cacheEntry
94 *
95 * @return string
96 */
97 private function buildCLink( RecentChange $cacheEntry ) {
98 $type = $cacheEntry->mAttribs['rc_type'];
99
100 // Page moves, very old style, not supported anymore
101 if ( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
102 $clink = '';
103 // New unpatrolled pages
104 } elseif ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
105 $clink = Linker::linkKnown( $cacheEntry->getTitle() );
106 // Log entries
107 } elseif ( $type == RC_LOG ) {
108 $logType = $cacheEntry->mAttribs['rc_log_type'];
109
110 if ( $logType ) {
111 $clink = $this->getLogLink( $logType );
112 } else {
113 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
114 $clink = Linker::link( $cacheEntry->getTitle() );
115 }
116 // Log entries (old format) and special pages
117 } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
118 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
119 $clink = '';
120 // Edits
121 } else {
122 $clink = Linker::linkKnown( $cacheEntry->getTitle() );
123 }
124
125 return $clink;
126 }
127
128 private function getLogLink( $logType ) {
129 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
130 $logpage = new LogPage( $logType );
131 $logname = $logpage->getName()->escaped();
132
133 $logLink = $this->context->msg( 'parentheses' )
134 ->rawParams( Linker::linkKnown( $logtitle, $logname ) )->escaped();
135
136 return $logLink;
137 }
138
139 /**
140 * @param RecentChange $cacheEntry
141 *
142 * @return string
143 */
144 private function buildTimestamp( RecentChange $cacheEntry ) {
145 return $this->context->getLanguage()->userTime(
146 $cacheEntry->mAttribs['rc_timestamp'],
147 $this->context->getUser()
148 );
149 }
150
151 /**
152 * @param RecentChange $recentChange
153 *
154 * @return array
155 */
156 private function buildCurQueryParams( RecentChange $recentChange ) {
157 return array(
158 'curid' => $recentChange->mAttribs['rc_cur_id'],
159 'diff' => 0,
160 'oldid' => $recentChange->mAttribs['rc_this_oldid']
161 );
162 }
163
164 /**
165 * @param RecentChange $cacheEntry
166 * @param bool $showDiffLinks
167 * @param int $counter
168 *
169 * @return string
170 */
171 private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
172 $queryParams = $this->buildCurQueryParams( $cacheEntry );
173 $curMessage = $this->getMessage( 'cur' );
174 $logTypes = array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT );
175
176 if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
177 $curLink = $curMessage;
178 } else {
179 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
180 $curLink = "<a href=\"$curUrl\" tabindex=\"$counter\">$curMessage</a>";
181 }
182
183 return $curLink;
184 }
185
186 /**
187 * @param RecentChange $recentChange
188 *
189 * @return array
190 */
191 private function buildDiffQueryParams( RecentChange $recentChange ) {
192 return array(
193 'curid' => $recentChange->mAttribs['rc_cur_id'],
194 'diff' => $recentChange->mAttribs['rc_this_oldid'],
195 'oldid' => $recentChange->mAttribs['rc_last_oldid']
196 );
197 }
198
199 /**
200 * @param RecentChange $cacheEntry
201 * @param bool $showDiffLinks
202 * @param int $counter
203 *
204 * @return string
205 */
206 private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
207 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
208 $diffMessage = $this->getMessage( 'diff' );
209 $logTypes = array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT );
210
211 if ( !$showDiffLinks ) {
212 $diffLink = $diffMessage;
213 } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
214 $diffLink = $diffMessage;
215 } else {
216 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
217 $diffLink = "<a href=\"$diffUrl\" tabindex=\"$counter\">$diffMessage</a>";
218 }
219
220 return $diffLink;
221 }
222
223 /**
224 * @param RecentChange $cacheEntry
225 * @param bool $showDiffLinks
226 *
227 * @return string
228 */
229 private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
230 $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
231 $lastMessage = $this->getMessage( 'last' );
232 $type = $cacheEntry->mAttribs['rc_type'];
233 $logTypes = array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT );
234
235 // Make "last" link
236 if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
237 $lastLink = $lastMessage;
238 } else {
239 $lastLink = Linker::linkKnown(
240 $cacheEntry->getTitle(),
241 $lastMessage,
242 array(),
243 $this->buildDiffQueryParams( $cacheEntry )
244 );
245 }
246
247 return $lastLink;
248 }
249
250 /**
251 * @param RecentChange $cacheEntry
252 *
253 * @return string
254 */
255 private function getUserLink( RecentChange $cacheEntry ) {
256 if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
257 $userLink = ' <span class="history-deleted">' .
258 $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
259 } else {
260 $userLink = Linker::userLink(
261 $cacheEntry->mAttribs['rc_user'],
262 $cacheEntry->mAttribs['rc_user_text']
263 );
264 }
265
266 return $userLink;
267 }
268
269 /**
270 * @param string $key
271 *
272 * @return string
273 */
274 private function getMessage( $key ) {
275 return $this->messages[$key];
276 }
277
278 }