MediaWiki  master
ProtectedPagesPager.php
Go to the documentation of this file.
1 <?php
25 
27 
28  public $mConds;
30 
33 
48  public function __construct(
49  $form,
50  $conds,
51  $type,
52  $level,
53  $namespace,
54  $sizetype,
55  $size,
56  $indefonly,
61  ) {
62  parent::__construct( $form->getContext(), $linkRenderer );
63  $this->mConds = $conds;
64  $this->type = $type ?: 'edit';
65  $this->level = $level;
66  $this->namespace = $namespace;
67  $this->sizetype = $sizetype;
68  $this->size = intval( $size );
69  $this->indefonly = (bool)$indefonly;
70  $this->cascadeonly = (bool)$cascadeonly;
71  $this->noredirect = (bool)$noredirect;
72  $this->linkBatchFactory = $linkBatchFactory ?? MediaWikiServices::getInstance()->getLinkBatchFactory();
73  }
74 
75  public function preprocessResults( $result ) {
76  # Do a link batch query
77  $lb = $this->linkBatchFactory->newLinkBatch();
78  $userids = [];
79 
80  foreach ( $result as $row ) {
81  $lb->add( $row->page_namespace, $row->page_title );
82  // field is nullable, maybe null on old protections
83  if ( $row->log_user !== null ) {
84  $userids[] = $row->log_user;
85  }
86  }
87 
88  // fill LinkBatch with user page and user talk
89  if ( count( $userids ) ) {
90  $userCache = UserCache::singleton();
91  $userCache->doQuery( $userids, [], __METHOD__ );
92  foreach ( $userids as $userid ) {
93  $name = $userCache->getProp( $userid, 'name' );
94  if ( $name !== false ) {
95  $lb->add( NS_USER, $name );
96  $lb->add( NS_USER_TALK, $name );
97  }
98  }
99  }
100 
101  $lb->execute();
102  }
103 
104  protected function getFieldNames() {
105  static $headers = null;
106 
107  if ( $headers == [] ) {
108  $headers = [
109  'log_timestamp' => 'protectedpages-timestamp',
110  'pr_page' => 'protectedpages-page',
111  'pr_expiry' => 'protectedpages-expiry',
112  'log_user' => 'protectedpages-performer',
113  'pr_params' => 'protectedpages-params',
114  'log_comment' => 'protectedpages-reason',
115  ];
116  foreach ( $headers as $key => $val ) {
117  $headers[$key] = $this->msg( $val )->text();
118  }
119  }
120 
121  return $headers;
122  }
123 
130  public function formatValue( $field, $value ) {
132  $row = $this->mCurrentRow;
133  $linkRenderer = $this->getLinkRenderer();
134 
135  switch ( $field ) {
136  case 'log_timestamp':
137  // when timestamp is null, this is a old protection row
138  if ( $value === null ) {
139  $formatted = Html::rawElement(
140  'span',
141  [ 'class' => 'mw-protectedpages-unknown' ],
142  $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
143  );
144  } else {
145  $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
146  $value, $this->getUser() ) );
147  }
148  break;
149 
150  case 'pr_page':
151  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
152  if ( !$title ) {
153  $formatted = Html::element(
154  'span',
155  [ 'class' => 'mw-invalidtitle' ],
157  $this->getContext(),
158  $row->page_namespace,
159  $row->page_title
160  )
161  );
162  } else {
163  $formatted = $linkRenderer->makeLink( $title );
164  }
165  if ( $row->page_len !== null ) {
166  $formatted .= $this->getLanguage()->getDirMark() .
167  ' ' . Html::rawElement(
168  'span',
169  [ 'class' => 'mw-protectedpages-length' ],
170  Linker::formatRevisionSize( $row->page_len )
171  );
172  }
173  break;
174 
175  case 'pr_expiry':
176  $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
177  $value, /* User preference timezone */true ) );
178  $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
179  if ( $title && MediaWikiServices::getInstance()
181  ->userHasRight( $this->getUser(), 'protect' )
182  ) {
183  $changeProtection = $linkRenderer->makeKnownLink(
184  $title,
185  $this->msg( 'protect_change' )->text(),
186  [],
187  [ 'action' => 'unprotect' ]
188  );
189  $formatted .= ' ' . Html::rawElement(
190  'span',
191  [ 'class' => 'mw-protectedpages-actions' ],
192  $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
193  );
194  }
195  break;
196 
197  case 'log_user':
198  // when timestamp is null, this is a old protection row
199  if ( $row->log_timestamp === null ) {
200  $formatted = Html::rawElement(
201  'span',
202  [ 'class' => 'mw-protectedpages-unknown' ],
203  $this->msg( 'protectedpages-unknown-performer' )->escaped()
204  );
205  } else {
206  $username = UserCache::singleton()->getProp( $value, 'name' );
208  $row->log_deleted,
210  $this->getUser()
211  ) ) {
212  if ( $username === false ) {
213  $formatted = htmlspecialchars( $value );
214  } else {
215  $formatted = Linker::userLink( $value, $username )
216  . Linker::userToolLinks( $value, $username );
217  }
218  } else {
219  $formatted = $this->msg( 'rev-deleted-user' )->escaped();
220  }
222  $formatted = '<span class="history-deleted">' . $formatted . '</span>';
223  }
224  }
225  break;
226 
227  case 'pr_params':
228  $params = [];
229  // Messages: restriction-level-sysop, restriction-level-autoconfirmed
230  $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
231  if ( $row->pr_cascade ) {
232  $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
233  }
234  $formatted = $this->getLanguage()->commaList( $params );
235  break;
236 
237  case 'log_comment':
238  // when timestamp is null, this is an old protection row
239  if ( $row->log_timestamp === null ) {
240  $formatted = Html::rawElement(
241  'span',
242  [ 'class' => 'mw-protectedpages-unknown' ],
243  $this->msg( 'protectedpages-unknown-reason' )->escaped()
244  );
245  } else {
247  $row->log_deleted,
249  $this->getUser()
250  ) ) {
251  $value = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
252  $formatted = Linker::formatComment( $value ?? '' );
253  } else {
254  $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
255  }
257  $formatted = '<span class="history-deleted">' . $formatted . '</span>';
258  }
259  }
260  break;
261 
262  default:
263  throw new MWException( "Unknown field '$field'" );
264  }
265 
266  return $formatted;
267  }
268 
269  public function getQueryInfo() {
270  $conds = $this->mConds;
271  $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
272  ' OR pr_expiry IS NULL';
273  $conds[] = 'page_id=pr_page';
274  $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
275 
276  if ( $this->sizetype == 'min' ) {
277  $conds[] = 'page_len>=' . $this->size;
278  } elseif ( $this->sizetype == 'max' ) {
279  $conds[] = 'page_len<=' . $this->size;
280  }
281 
282  if ( $this->indefonly ) {
283  $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
284  $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
285  }
286  if ( $this->cascadeonly ) {
287  $conds[] = 'pr_cascade = 1';
288  }
289  if ( $this->noredirect ) {
290  $conds[] = 'page_is_redirect = 0';
291  }
292 
293  if ( $this->level ) {
294  $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
295  }
296  if ( $this->namespace !== null ) {
297  $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
298  }
299 
300  $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
301  $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
302 
303  return [
304  'tables' => [
305  'page', 'page_restrictions', 'log_search',
306  'logparen' => [ 'logging' ] + $commentQuery['tables'] + $actorQuery['tables'],
307  ],
308  'fields' => [
309  'pr_id',
310  'page_namespace',
311  'page_title',
312  'page_len',
313  'pr_type',
314  'pr_level',
315  'pr_expiry',
316  'pr_cascade',
317  'log_timestamp',
318  'log_deleted',
319  ] + $commentQuery['fields'] + $actorQuery['fields'],
320  'conds' => $conds,
321  'join_conds' => [
322  'log_search' => [
323  'LEFT JOIN', [
324  'ls_field' => 'pr_id', 'ls_value = ' . $this->mDb->buildStringCast( 'pr_id' )
325  ]
326  ],
327  'logparen' => [
328  'LEFT JOIN', [
329  'ls_log_id = log_id'
330  ]
331  ]
332  ] + $commentQuery['joins'] + $actorQuery['joins']
333  ];
334  }
335 
336  protected function getTableClass() {
337  return parent::getTableClass() . ' mw-protectedpages';
338  }
339 
340  public function getIndexField() {
341  return 'pr_id';
342  }
343 
344  public function getDefaultSort() {
345  return 'pr_id';
346  }
347 
348  protected function isFieldSortable( $field ) {
349  // no index for sorting exists
350  return false;
351  }
352 }
ProtectedPagesPager\$level
$level
Definition: ProtectedPagesPager.php:29
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:45
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:896
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
ProtectedPagesPager\isFieldSortable
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.
Definition: ProtectedPagesPager.php:348
ProtectedPagesPager\formatValue
formatValue( $field, $value)
Definition: ProtectedPagesPager.php:130
ProtectedPagesPager\getIndexField
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
Definition: ProtectedPagesPager.php:340
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
Linker\userToolLinks
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:941
ProtectedPagesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: ProtectedPagesPager.php:269
ProtectedPagesPager\getDefaultSort
getDefaultSort()
The database field name used as a default sort order.
Definition: ProtectedPagesPager.php:344
ProtectedPagesPager\$type
$type
Definition: ProtectedPagesPager.php:29
ProtectedPagesPager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: ProtectedPagesPager.php:336
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
Linker\getInvalidTitleDescription
static getInvalidTitleDescription(IContextSource $context, $namespace, $title)
Get a message saying that an invalid title was encountered.
Definition: Linker.php:188
LogEventsList\userCanBitfield
static userCanBitfield( $bitfield, $field, User $user)
Determine if the current user is allowed to view a particular field of this log row,...
Definition: LogEventsList.php:540
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:134
ProtectedPagesPager\$namespace
$namespace
Definition: ProtectedPagesPager.php:29
ProtectedPagesPager\preprocessResults
preprocessResults( $result)
Pre-process results; useful for performing batch existence checks, etc.
Definition: ProtectedPagesPager.php:75
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:156
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
MWException
MediaWiki exception.
Definition: MWException.php:29
ProtectedPagesPager\$size
$size
Definition: ProtectedPagesPager.php:29
LogPage\DELETED_COMMENT
const DELETED_COMMENT
Definition: LogPage.php:39
ProtectedPagesPager\$cascadeonly
$cascadeonly
Definition: ProtectedPagesPager.php:29
getPermissionManager
getPermissionManager()
LogPage\DELETED_USER
const DELETED_USER
Definition: LogPage.php:40
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
TablePager
Table-based display with a user-selectable sort order Stable to extend.
Definition: TablePager.php:31
TablePager\$mCurrentRow
stdClass $mCurrentRow
Definition: TablePager.php:36
ProtectedPagesPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: ProtectedPagesPager.php:32
$title
$title
Definition: testCompression.php:38
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
ProtectedPagesPager\$indefonly
$indefonly
Definition: ProtectedPagesPager.php:29
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:617
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
Linker\formatRevisionSize
static formatRevisionSize( $size)
Definition: Linker.php:1650
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:976
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1199
ProtectedPagesPager\getFieldNames
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
Definition: ProtectedPagesPager.php:104
ProtectedPagesPager\$noredirect
$noredirect
Definition: ProtectedPagesPager.php:29
MediaWiki\Linker\LinkRenderer\makeKnownLink
makeKnownLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
Definition: LinkRenderer.php:225
UserCache\singleton
static singleton()
Definition: UserCache.php:48
LogEventsList\isDeleted
static isDeleted( $row, $field)
Definition: LogEventsList.php:580
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
NS_USER
const NS_USER
Definition: Defines.php:71
ProtectedPagesPager\$mConds
$mConds
Definition: ProtectedPagesPager.php:28
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
CommentStore\getStore
static getStore()
Definition: CommentStore.php:120
MediaWiki\Linker\LinkRenderer\makeLink
makeLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
Definition: LinkRenderer.php:160
ProtectedPagesPager\__construct
__construct( $form, $conds, $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory=null)
Definition: ProtectedPagesPager.php:48
ProtectedPagesPager
Definition: ProtectedPagesPager.php:26
ProtectedPagesPager\$sizetype
$sizetype
Definition: ProtectedPagesPager.php:29