]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/specials/pagers/BlockListPager.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.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 use Wikimedia\Rdbms\ResultWrapper;
27
28 class BlockListPager extends TablePager {
29
30         protected $conds;
31         protected $page;
32
33         /**
34          * @param SpecialPage $page
35          * @param array $conds
36          */
37         function __construct( $page, $conds ) {
38                 $this->page = $page;
39                 $this->conds = $conds;
40                 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
41                 parent::__construct( $page->getContext() );
42         }
43
44         function getFieldNames() {
45                 static $headers = null;
46
47                 if ( $headers === null ) {
48                         $headers = [
49                                 'ipb_timestamp' => 'blocklist-timestamp',
50                                 'ipb_target' => 'blocklist-target',
51                                 'ipb_expiry' => 'blocklist-expiry',
52                                 'ipb_by' => 'blocklist-by',
53                                 'ipb_params' => 'blocklist-params',
54                                 'ipb_reason' => 'blocklist-reason',
55                         ];
56                         foreach ( $headers as $key => $val ) {
57                                 $headers[$key] = $this->msg( $val )->text();
58                         }
59                 }
60
61                 return $headers;
62         }
63
64         function formatValue( $name, $value ) {
65                 static $msg = null;
66                 if ( $msg === null ) {
67                         $keys = [
68                                 'anononlyblock',
69                                 'createaccountblock',
70                                 'noautoblockblock',
71                                 'emailblock',
72                                 'blocklist-nousertalk',
73                                 'unblocklink',
74                                 'change-blocklink',
75                         ];
76
77                         foreach ( $keys as $key ) {
78                                 $msg[$key] = $this->msg( $key )->text();
79                         }
80                 }
81
82                 /** @var object $row */
83                 $row = $this->mCurrentRow;
84
85                 $language = $this->getLanguage();
86
87                 $formatted = '';
88
89                 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
90
91                 switch ( $name ) {
92                         case 'ipb_timestamp':
93                                 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
94                                 break;
95
96                         case 'ipb_target':
97                                 if ( $row->ipb_auto ) {
98                                         $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
99                                 } else {
100                                         list( $target, $type ) = Block::parseTarget( $row->ipb_address );
101                                         switch ( $type ) {
102                                                 case Block::TYPE_USER:
103                                                 case Block::TYPE_IP:
104                                                         $formatted = Linker::userLink( $target->getId(), $target );
105                                                         $formatted .= Linker::userToolLinks(
106                                                                 $target->getId(),
107                                                                 $target,
108                                                                 false,
109                                                                 Linker::TOOL_LINKS_NOBLOCK
110                                                         );
111                                                         break;
112                                                 case Block::TYPE_RANGE:
113                                                         $formatted = htmlspecialchars( $target );
114                                         }
115                                 }
116                                 break;
117
118                         case 'ipb_expiry':
119                                 $formatted = htmlspecialchars( $language->formatExpiry(
120                                         $value,
121                                         /* User preference timezone */true
122                                 ) );
123                                 if ( $this->getUser()->isAllowed( 'block' ) ) {
124                                         if ( $row->ipb_auto ) {
125                                                 $links[] = $linkRenderer->makeKnownLink(
126                                                         SpecialPage::getTitleFor( 'Unblock' ),
127                                                         $msg['unblocklink'],
128                                                         [],
129                                                         [ 'wpTarget' => "#{$row->ipb_id}" ]
130                                                 );
131                                         } else {
132                                                 $links[] = $linkRenderer->makeKnownLink(
133                                                         SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
134                                                         $msg['unblocklink']
135                                                 );
136                                                 $links[] = $linkRenderer->makeKnownLink(
137                                                         SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
138                                                         $msg['change-blocklink']
139                                                 );
140                                         }
141                                         $formatted .= ' ' . Html::rawElement(
142                                                         'span',
143                                                         [ 'class' => 'mw-blocklist-actions' ],
144                                                         $this->msg( 'parentheses' )->rawParams(
145                                                                 $language->pipeList( $links ) )->escaped()
146                                                 );
147                                 }
148                                 if ( $value !== 'infinity' ) {
149                                         $timestamp = new MWTimestamp( $value );
150                                         $formatted .= '<br />' . $this->msg(
151                                                 'ipb-blocklist-duration-left',
152                                                 $language->formatDuration(
153                                                         $timestamp->getTimestamp() - time(),
154                                                         // reasonable output
155                                                         [
156                                                                 'minutes',
157                                                                 'hours',
158                                                                 'days',
159                                                                 'years',
160                                                         ]
161                                                 )
162                                         )->escaped();
163                                 }
164                                 break;
165
166                         case 'ipb_by':
167                                 if ( isset( $row->by_user_name ) ) {
168                                         $formatted = Linker::userLink( $value, $row->by_user_name );
169                                         $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
170                                 } else {
171                                         $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
172                                 }
173                                 break;
174
175                         case 'ipb_reason':
176                                 $value = CommentStore::newKey( 'ipb_reason' )->getComment( $row )->text;
177                                 $formatted = Linker::formatComment( $value );
178                                 break;
179
180                         case 'ipb_params':
181                                 $properties = [];
182                                 if ( $row->ipb_anon_only ) {
183                                         $properties[] = htmlspecialchars( $msg['anononlyblock'] );
184                                 }
185                                 if ( $row->ipb_create_account ) {
186                                         $properties[] = htmlspecialchars( $msg['createaccountblock'] );
187                                 }
188                                 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
189                                         $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
190                                 }
191
192                                 if ( $row->ipb_block_email ) {
193                                         $properties[] = htmlspecialchars( $msg['emailblock'] );
194                                 }
195
196                                 if ( !$row->ipb_allow_usertalk ) {
197                                         $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
198                                 }
199
200                                 $formatted = $language->commaList( $properties );
201                                 break;
202
203                         default:
204                                 $formatted = "Unable to format $name";
205                                 break;
206                 }
207
208                 return $formatted;
209         }
210
211         function getQueryInfo() {
212                 $commentQuery = CommentStore::newKey( 'ipb_reason' )->getJoin();
213
214                 $info = [
215                         'tables' => [ 'ipblocks', 'user' ] + $commentQuery['tables'],
216                         'fields' => [
217                                 'ipb_id',
218                                 'ipb_address',
219                                 'ipb_user',
220                                 'ipb_by',
221                                 'ipb_by_text',
222                                 'by_user_name' => 'user_name',
223                                 'ipb_timestamp',
224                                 'ipb_auto',
225                                 'ipb_anon_only',
226                                 'ipb_create_account',
227                                 'ipb_enable_autoblock',
228                                 'ipb_expiry',
229                                 'ipb_range_start',
230                                 'ipb_range_end',
231                                 'ipb_deleted',
232                                 'ipb_block_email',
233                                 'ipb_allow_usertalk',
234                         ] + $commentQuery['fields'],
235                         'conds' => $this->conds,
236                         'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id = ipb_by' ] ] + $commentQuery['joins']
237                 ];
238
239                 # Filter out any expired blocks
240                 $db = $this->getDatabase();
241                 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
242
243                 # Is the user allowed to see hidden blocks?
244                 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
245                         $info['conds']['ipb_deleted'] = 0;
246                 }
247
248                 return $info;
249         }
250
251         /**
252          * Get total number of autoblocks at any given time
253          *
254          * @return int Total number of unexpired active autoblocks
255          */
256         function getTotalAutoblocks() {
257                 $dbr = $this->getDatabase();
258                 $res = $dbr->selectField( 'ipblocks',
259                         [ 'COUNT(*) AS totalautoblocks' ],
260                         [
261                                 'ipb_auto' => '1',
262                                 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
263                         ]
264                 );
265                 if ( $res ) {
266                         return $res;
267                 }
268                 return 0; // We found nothing
269         }
270
271         protected function getTableClass() {
272                 return parent::getTableClass() . ' mw-blocklist';
273         }
274
275         function getIndexField() {
276                 return 'ipb_timestamp';
277         }
278
279         function getDefaultSort() {
280                 return 'ipb_timestamp';
281         }
282
283         function isFieldSortable( $name ) {
284                 return false;
285         }
286
287         /**
288          * Do a LinkBatch query to minimise database load when generating all these links
289          * @param ResultWrapper $result
290          */
291         function preprocessResults( $result ) {
292                 # Do a link batch query
293                 $lb = new LinkBatch;
294                 $lb->setCaller( __METHOD__ );
295
296                 foreach ( $result as $row ) {
297                         $lb->add( NS_USER, $row->ipb_address );
298                         $lb->add( NS_USER_TALK, $row->ipb_address );
299
300                         if ( isset( $row->by_user_name ) ) {
301                                 $lb->add( NS_USER, $row->by_user_name );
302                                 $lb->add( NS_USER_TALK, $row->by_user_name );
303                         }
304                 }
305
306                 $lb->execute();
307         }
308
309 }