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