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