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