45dced882902985e7a415dc2409900294636c43a
[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 $type
40 * @param $level
41 * @param $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 $formatted = Linker::formatComment( $value !== null ? $value : '' );
240 } else {
241 $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
242 }
243 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
244 $formatted = '<span class="history-deleted">' . $formatted . '</span>';
245 }
246 }
247 break;
248
249 default:
250 throw new MWException( "Unknown field '$field'" );
251 }
252
253 return $formatted;
254 }
255
256 function getQueryInfo() {
257 $conds = $this->mConds;
258 $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
259 ' OR pr_expiry IS NULL';
260 $conds[] = 'page_id=pr_page';
261 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
262
263 if ( $this->sizetype == 'min' ) {
264 $conds[] = 'page_len>=' . $this->size;
265 } elseif ( $this->sizetype == 'max' ) {
266 $conds[] = 'page_len<=' . $this->size;
267 }
268
269 if ( $this->indefonly ) {
270 $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
271 $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
272 }
273 if ( $this->cascadeonly ) {
274 $conds[] = 'pr_cascade = 1';
275 }
276 if ( $this->noredirect ) {
277 $conds[] = 'page_is_redirect = 0';
278 }
279
280 if ( $this->level ) {
281 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
282 }
283 if ( !is_null( $this->namespace ) ) {
284 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
285 }
286
287 return [
288 'tables' => [ 'page', 'page_restrictions', 'log_search', 'logging' ],
289 'fields' => [
290 'pr_id',
291 'page_namespace',
292 'page_title',
293 'page_len',
294 'pr_type',
295 'pr_level',
296 'pr_expiry',
297 'pr_cascade',
298 'log_timestamp',
299 'log_user',
300 'log_comment',
301 'log_deleted',
302 ],
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 ]
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 }