Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / includes / specials / pagers / ProtectedPagesPager.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 use MediaWiki\Linker\LinkRenderer;
23
24 class ProtectedPagesPager extends TablePager {
25
26 public $mForm, $mConds;
27 private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
28
29 /**
30 * @var LinkRenderer
31 */
32 private $linkRenderer;
33
34 /**
35 * @param SpecialProtectedpages $form
36 * @param array $conds
37 * @param string $type
38 * @param string $level
39 * @param int $namespace
40 * @param string $sizetype
41 * @param int $size
42 * @param bool $indefonly
43 * @param bool $cascadeonly
44 * @param bool $noredirect
45 * @param LinkRenderer $linkRenderer
46 */
47 function __construct( $form, $conds = [], $type, $level, $namespace,
48 $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false,
49 LinkRenderer $linkRenderer
50 ) {
51 $this->mForm = $form;
52 $this->mConds = $conds;
53 $this->type = ( $type ) ? $type : 'edit';
54 $this->level = $level;
55 $this->namespace = $namespace;
56 $this->sizetype = $sizetype;
57 $this->size = intval( $size );
58 $this->indefonly = (bool)$indefonly;
59 $this->cascadeonly = (bool)$cascadeonly;
60 $this->noredirect = (bool)$noredirect;
61 $this->linkRenderer = $linkRenderer;
62 parent::__construct( $form->getContext() );
63 }
64
65 function preprocessResults( $result ) {
66 # Do a link batch query
67 $lb = new LinkBatch;
68 $userids = [];
69
70 foreach ( $result as $row ) {
71 $lb->add( $row->page_namespace, $row->page_title );
72 // field is nullable, maybe null on old protections
73 if ( $row->log_user !== null ) {
74 $userids[] = $row->log_user;
75 }
76 }
77
78 // fill LinkBatch with user page and user talk
79 if ( count( $userids ) ) {
80 $userCache = UserCache::singleton();
81 $userCache->doQuery( $userids, [], __METHOD__ );
82 foreach ( $userids as $userid ) {
83 $name = $userCache->getProp( $userid, 'name' );
84 if ( $name !== false ) {
85 $lb->add( NS_USER, $name );
86 $lb->add( NS_USER_TALK, $name );
87 }
88 }
89 }
90
91 $lb->execute();
92 }
93
94 function getFieldNames() {
95 static $headers = null;
96
97 if ( $headers == [] ) {
98 $headers = [
99 'log_timestamp' => 'protectedpages-timestamp',
100 'pr_page' => 'protectedpages-page',
101 'pr_expiry' => 'protectedpages-expiry',
102 'log_user' => 'protectedpages-performer',
103 'pr_params' => 'protectedpages-params',
104 'log_comment' => 'protectedpages-reason',
105 ];
106 foreach ( $headers as $key => $val ) {
107 $headers[$key] = $this->msg( $val )->text();
108 }
109 }
110
111 return $headers;
112 }
113
114 /**
115 * @param string $field
116 * @param string $value
117 * @return string HTML
118 * @throws MWException
119 */
120 function formatValue( $field, $value ) {
121 /** @var object $row */
122 $row = $this->mCurrentRow;
123
124 switch ( $field ) {
125 case 'log_timestamp':
126 // when timestamp is null, this is a old protection row
127 if ( $value === null ) {
128 $formatted = Html::rawElement(
129 'span',
130 [ 'class' => 'mw-protectedpages-unknown' ],
131 $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
132 );
133 } else {
134 $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
135 $value, $this->getUser() ) );
136 }
137 break;
138
139 case 'pr_page':
140 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
141 if ( !$title ) {
142 $formatted = Html::element(
143 'span',
144 [ 'class' => 'mw-invalidtitle' ],
145 Linker::getInvalidTitleDescription(
146 $this->getContext(),
147 $row->page_namespace,
148 $row->page_title
149 )
150 );
151 } else {
152 $formatted = $this->linkRenderer->makeLink( $title );
153 }
154 if ( !is_null( $row->page_len ) ) {
155 $formatted .= $this->getLanguage()->getDirMark() .
156 ' ' . Html::rawElement(
157 'span',
158 [ 'class' => 'mw-protectedpages-length' ],
159 Linker::formatRevisionSize( $row->page_len )
160 );
161 }
162 break;
163
164 case 'pr_expiry':
165 $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
166 $value, /* User preference timezone */true ) );
167 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
168 if ( $this->getUser()->isAllowed( 'protect' ) && $title ) {
169 $changeProtection = $this->linkRenderer->makeKnownLink(
170 $title,
171 $this->msg( 'protect_change' )->text(),
172 [],
173 [ 'action' => 'unprotect' ]
174 );
175 $formatted .= ' ' . Html::rawElement(
176 'span',
177 [ 'class' => 'mw-protectedpages-actions' ],
178 $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
179 );
180 }
181 break;
182
183 case 'log_user':
184 // when timestamp is null, this is a old protection row
185 if ( $row->log_timestamp === null ) {
186 $formatted = Html::rawElement(
187 'span',
188 [ 'class' => 'mw-protectedpages-unknown' ],
189 $this->msg( 'protectedpages-unknown-performer' )->escaped()
190 );
191 } else {
192 $username = UserCache::singleton()->getProp( $value, 'name' );
193 if ( LogEventsList::userCanBitfield(
194 $row->log_deleted,
195 LogPage::DELETED_USER,
196 $this->getUser()
197 ) ) {
198 if ( $username === false ) {
199 $formatted = htmlspecialchars( $value );
200 } else {
201 $formatted = Linker::userLink( $value, $username )
202 . Linker::userToolLinks( $value, $username );
203 }
204 } else {
205 $formatted = $this->msg( 'rev-deleted-user' )->escaped();
206 }
207 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
208 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
209 }
210 }
211 break;
212
213 case 'pr_params':
214 $params = [];
215 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
216 $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
217 if ( $row->pr_cascade ) {
218 $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
219 }
220 $formatted = $this->getLanguage()->commaList( $params );
221 break;
222
223 case 'log_comment':
224 // when timestamp is null, this is an old protection row
225 if ( $row->log_timestamp === null ) {
226 $formatted = Html::rawElement(
227 'span',
228 [ 'class' => 'mw-protectedpages-unknown' ],
229 $this->msg( 'protectedpages-unknown-reason' )->escaped()
230 );
231 } else {
232 if ( LogEventsList::userCanBitfield(
233 $row->log_deleted,
234 LogPage::DELETED_COMMENT,
235 $this->getUser()
236 ) ) {
237 $value = CommentStore::newKey( 'log_comment' )->getComment( $row )->text;
238 $formatted = Linker::formatComment( $value !== null ? $value : '' );
239 } else {
240 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
241 }
242 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
243 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
244 }
245 }
246 break;
247
248 default:
249 throw new MWException( "Unknown field '$field'" );
250 }
251
252 return $formatted;
253 }
254
255 function getQueryInfo() {
256 $conds = $this->mConds;
257 $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
258 ' OR pr_expiry IS NULL';
259 $conds[] = 'page_id=pr_page';
260 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
261
262 if ( $this->sizetype == 'min' ) {
263 $conds[] = 'page_len>=' . $this->size;
264 } elseif ( $this->sizetype == 'max' ) {
265 $conds[] = 'page_len<=' . $this->size;
266 }
267
268 if ( $this->indefonly ) {
269 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
270 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
271 }
272 if ( $this->cascadeonly ) {
273 $conds[] = 'pr_cascade = 1';
274 }
275 if ( $this->noredirect ) {
276 $conds[] = 'page_is_redirect = 0';
277 }
278
279 if ( $this->level ) {
280 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
281 }
282 if ( !is_null( $this->namespace ) ) {
283 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
284 }
285
286 $commentQuery = CommentStore::newKey( 'log_comment' )->getJoin();
287
288 return [
289 'tables' => [ 'page', 'page_restrictions', 'log_search', 'logging' ] + $commentQuery['tables'],
290 'fields' => [
291 'pr_id',
292 'page_namespace',
293 'page_title',
294 'page_len',
295 'pr_type',
296 'pr_level',
297 'pr_expiry',
298 'pr_cascade',
299 'log_timestamp',
300 'log_user',
301 'log_deleted',
302 ] + $commentQuery['fields'],
303 'conds' => $conds,
304 'join_conds' => [
305 'log_search' => [
306 'LEFT JOIN', [
307 'ls_field' => 'pr_id', 'ls_value = ' . $this->mDb->buildStringCast( 'pr_id' )
308 ]
309 ],
310 'logging' => [
311 'LEFT JOIN', [
312 'ls_log_id = log_id'
313 ]
314 ]
315 ] + $commentQuery['joins']
316 ];
317 }
318
319 protected function getTableClass() {
320 return parent::getTableClass() . ' mw-protectedpages';
321 }
322
323 function getIndexField() {
324 return 'pr_id';
325 }
326
327 function getDefaultSort() {
328 return 'pr_id';
329 }
330
331 function isFieldSortable( $field ) {
332 // no index for sorting exists
333 return false;
334 }
335 }