Merge "MessageCache invalidation improvements"
[lhc/web/wiklou.git] / includes / specials / pagers / BlockListPager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\MediaWikiServices;
26
27 class BlockListPager extends TablePager {
28
29 protected $conds;
30 protected $page;
31
32 /**
33 * @param SpecialPage $page
34 * @param array $conds
35 */
36 function __construct( $page, $conds ) {
37 $this->page = $page;
38 $this->conds = $conds;
39 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
40 parent::__construct( $page->getContext() );
41 }
42
43 function getFieldNames() {
44 static $headers = null;
45
46 if ( $headers === null ) {
47 $headers = [
48 'ipb_timestamp' => 'blocklist-timestamp',
49 'ipb_target' => 'blocklist-target',
50 'ipb_expiry' => 'blocklist-expiry',
51 'ipb_by' => 'blocklist-by',
52 'ipb_params' => 'blocklist-params',
53 'ipb_reason' => 'blocklist-reason',
54 ];
55 foreach ( $headers as $key => $val ) {
56 $headers[$key] = $this->msg( $val )->text();
57 }
58 }
59
60 return $headers;
61 }
62
63 function formatValue( $name, $value ) {
64 static $msg = null;
65 if ( $msg === null ) {
66 $keys = [
67 'anononlyblock',
68 'createaccountblock',
69 'noautoblockblock',
70 'emailblock',
71 'blocklist-nousertalk',
72 'unblocklink',
73 'change-blocklink',
74 ];
75
76 foreach ( $keys as $key ) {
77 $msg[$key] = $this->msg( $key )->text();
78 }
79 }
80
81 /** @var $row object */
82 $row = $this->mCurrentRow;
83
84 $language = $this->getLanguage();
85
86 $formatted = '';
87
88 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
89
90 switch ( $name ) {
91 case 'ipb_timestamp':
92 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
93 break;
94
95 case 'ipb_target':
96 if ( $row->ipb_auto ) {
97 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
98 } else {
99 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
100 switch ( $type ) {
101 case Block::TYPE_USER:
102 case Block::TYPE_IP:
103 $formatted = Linker::userLink( $target->getId(), $target );
104 $formatted .= Linker::userToolLinks(
105 $target->getId(),
106 $target,
107 false,
108 Linker::TOOL_LINKS_NOBLOCK
109 );
110 break;
111 case Block::TYPE_RANGE:
112 $formatted = htmlspecialchars( $target );
113 }
114 }
115 break;
116
117 case 'ipb_expiry':
118 $formatted = htmlspecialchars( $language->formatExpiry(
119 $value,
120 /* User preference timezone */true
121 ) );
122 if ( $this->getUser()->isAllowed( 'block' ) ) {
123 if ( $row->ipb_auto ) {
124 $links[] = $linkRenderer->makeKnownLink(
125 SpecialPage::getTitleFor( 'Unblock' ),
126 $msg['unblocklink'],
127 [],
128 [ 'wpTarget' => "#{$row->ipb_id}" ]
129 );
130 } else {
131 $links[] = $linkRenderer->makeKnownLink(
132 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
133 $msg['unblocklink']
134 );
135 $links[] = $linkRenderer->makeKnownLink(
136 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
137 $msg['change-blocklink']
138 );
139 }
140 $formatted .= ' ' . Html::rawElement(
141 'span',
142 [ 'class' => 'mw-blocklist-actions' ],
143 $this->msg( 'parentheses' )->rawParams(
144 $language->pipeList( $links ) )->escaped()
145 );
146 }
147 if ( $value !== 'infinity' ) {
148 $timestamp = new MWTimestamp( $value );
149 $formatted .= '<br />' . $this->msg(
150 'ipb-blocklist-duration-left',
151 $language->formatDuration(
152 $timestamp->getTimestamp() - time(),
153 // reasonable output
154 [
155 'minutes',
156 'hours',
157 'days',
158 'years',
159 ]
160 )
161 )->escaped();
162 }
163 break;
164
165 case 'ipb_by':
166 if ( isset( $row->by_user_name ) ) {
167 $formatted = Linker::userLink( $value, $row->by_user_name );
168 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
169 } else {
170 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
171 }
172 break;
173
174 case 'ipb_reason':
175 $formatted = Linker::formatComment( $value );
176 break;
177
178 case 'ipb_params':
179 $properties = [];
180 if ( $row->ipb_anon_only ) {
181 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
182 }
183 if ( $row->ipb_create_account ) {
184 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
185 }
186 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
187 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
188 }
189
190 if ( $row->ipb_block_email ) {
191 $properties[] = htmlspecialchars( $msg['emailblock'] );
192 }
193
194 if ( !$row->ipb_allow_usertalk ) {
195 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
196 }
197
198 $formatted = $language->commaList( $properties );
199 break;
200
201 default:
202 $formatted = "Unable to format $name";
203 break;
204 }
205
206 return $formatted;
207 }
208
209 function getQueryInfo() {
210 $info = [
211 'tables' => [ 'ipblocks', 'user' ],
212 'fields' => [
213 'ipb_id',
214 'ipb_address',
215 'ipb_user',
216 'ipb_by',
217 'ipb_by_text',
218 'by_user_name' => 'user_name',
219 'ipb_reason',
220 'ipb_timestamp',
221 'ipb_auto',
222 'ipb_anon_only',
223 'ipb_create_account',
224 'ipb_enable_autoblock',
225 'ipb_expiry',
226 'ipb_range_start',
227 'ipb_range_end',
228 'ipb_deleted',
229 'ipb_block_email',
230 'ipb_allow_usertalk',
231 ],
232 'conds' => $this->conds,
233 'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ]
234 ];
235
236 # Filter out any expired blocks
237 $db = $this->getDatabase();
238 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
239
240 # Is the user allowed to see hidden blocks?
241 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
242 $info['conds']['ipb_deleted'] = 0;
243 }
244
245 return $info;
246 }
247
248 protected function getTableClass() {
249 return parent::getTableClass() . ' mw-blocklist';
250 }
251
252 function getIndexField() {
253 return 'ipb_timestamp';
254 }
255
256 function getDefaultSort() {
257 return 'ipb_timestamp';
258 }
259
260 function isFieldSortable( $name ) {
261 return false;
262 }
263
264 /**
265 * Do a LinkBatch query to minimise database load when generating all these links
266 * @param ResultWrapper $result
267 */
268 function preprocessResults( $result ) {
269 # Do a link batch query
270 $lb = new LinkBatch;
271 $lb->setCaller( __METHOD__ );
272
273 foreach ( $result as $row ) {
274 $lb->add( NS_USER, $row->ipb_address );
275 $lb->add( NS_USER_TALK, $row->ipb_address );
276
277 if ( isset( $row->by_user_name ) ) {
278 $lb->add( NS_USER, $row->by_user_name );
279 $lb->add( NS_USER_TALK, $row->by_user_name );
280 }
281 }
282
283 $lb->execute();
284 }
285
286 }