MediaWiki master
CategoryPager.php
Go to the documentation of this file.
1<?php
7
8namespace MediaWiki\Specials\Pager;
9
10use MediaWiki\Context\IContextSource;
11use MediaWiki\Html\Html;
12use MediaWiki\HTMLForm\HTMLForm;
13use MediaWiki\Linker\LinkRenderer;
14use MediaWiki\Page\LinkBatchFactory;
15use MediaWiki\Pager\AlphabeticPager;
16use MediaWiki\Title\Title;
17use MediaWiki\Title\TitleValue;
18use Wikimedia\Rdbms\IConnectionProvider;
19
24
25 public function __construct(
26 IContextSource $context,
27 private readonly LinkBatchFactory $linkBatchFactory,
28 LinkRenderer $linkRenderer,
29 IConnectionProvider $dbProvider,
30 string $from,
31 ) {
32 // Set database before parent constructor to avoid setting it there
33 $this->mDb = $dbProvider->getReplicaDatabase();
34 parent::__construct( $context, $linkRenderer );
35 $from = str_replace( ' ', '_', $from );
36 if ( $from !== '' ) {
37 $from = Title::capitalize( $from, NS_CATEGORY );
38 $this->setOffset( $from );
39 $this->setIncludeOffset( true );
40 }
41 }
42
44 public function getQueryInfo() {
45 return [
46 'tables' => [ 'category' ],
47 'fields' => [ 'cat_title', 'cat_pages' ],
48 'options' => [ 'USE INDEX' => 'cat_title' ],
49 ];
50 }
51
53 public function getIndexField() {
54 return 'cat_title';
55 }
56
58 public function getDefaultQuery() {
59 parent::getDefaultQuery();
60 unset( $this->mDefaultQuery['from'] );
61
63 }
64
69 public function getBody() {
70 $batch = $this->linkBatchFactory->newLinkBatch();
71
72 $this->mResult->rewind();
73
74 foreach ( $this->mResult as $row ) {
75 $batch->add( NS_CATEGORY, $row->cat_title );
76 }
77 $batch->execute();
78 $this->mResult->rewind();
79
80 return parent::getBody();
81 }
82
84 public function formatRow( $result ) {
85 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
86 $text = $title->getText();
87 $link = $this->getLinkRenderer()->makeLink( $title, $text );
88
89 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
90 return Html::rawElement(
91 'li',
92 [ 'class' => 'mw-special-categories-list-item' ],
93 $this->getLanguage()->specialList( $link, $count )
94 ) . "\n";
95 }
96
101 public function getStartForm( $from ) {
102 $formDescriptor = [
103 'from' => [
104 'type' => 'title',
105 'namespace' => NS_CATEGORY,
106 'relative' => true,
107 'label-message' => 'categoriesfrom',
108 'name' => 'from',
109 'id' => 'from',
110 'size' => 20,
111 'default' => $from,
112 ],
113 ];
114
115 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
116 ->setSubmitTextMsg( 'categories-submit' )
117 ->setWrapperLegendMsg( 'categories' )
118 ->setId( 'mw-special-categories-list-form' )
119 ->setMethod( 'get' );
120 return $htmlForm->prepareForm()->getHTML( false );
121 }
122
123}
124
125// @codeCoverageIgnoreStart
130class_alias( CategoryPager::class, 'CategoryPager' );
131
133class_alias( CategoryPager::class, 'MediaWiki\\Pager\\CategoryPager' );
134// @codeCoverageIgnoreEnd
const NS_CATEGORY
Definition Defines.php:65
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage().
getContext()
Get the base IContextSource object.
Class that generates HTML for internal links.
Factory for LinkBatch objects to batch query page metadata.
IndexPager with an alphabetic list and a formatted navigation bar.
setOffset( $offset)
Set the offset from an other source than the request.
setIncludeOffset( $include)
Set whether a row matching exactly the offset should be also included in the result or not.
getQueryInfo()
Provides all parameters needed for the main paged query.It returns an associative array with the foll...
__construct(IContextSource $context, private readonly LinkBatchFactory $linkBatchFactory, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, string $from,)
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
getDefaultQuery()
Get an array of query parameters that should be put into self-links.By default, all parameters passed...
formatRow( $result)
Returns an HTML string representing the result row $row.Rows will be concatenated and returned by get...
getBody()
Override getBody to apply LinksBatch on resultset before actually outputting anything.
Represents the target of a wiki link.
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.
getReplicaDatabase(string|false $domain=false, $group=null)
Get connection to a replica database.