Merge "SpecialBlock: Add confirmation dialog when removing blocks"
[mediawiki.git] / includes / specials / pagers / CategoryPager.php
blob5ae40cb1b77209c02d9dd7f38306b70bf446b3a0
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.
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.
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
18 * @file
19 * @ingroup Pager
22 namespace MediaWiki\Pager;
24 use MediaWiki\Cache\LinkBatchFactory;
25 use MediaWiki\Context\IContextSource;
26 use MediaWiki\Html\Html;
27 use MediaWiki\HTMLForm\HTMLForm;
28 use MediaWiki\Linker\LinkRenderer;
29 use MediaWiki\Title\Title;
30 use MediaWiki\Title\TitleValue;
31 use Wikimedia\Rdbms\IConnectionProvider;
33 /**
34 * @ingroup Pager
36 class CategoryPager extends AlphabeticPager {
38 private LinkBatchFactory $linkBatchFactory;
40 /**
41 * @param IContextSource $context
42 * @param LinkBatchFactory $linkBatchFactory
43 * @param LinkRenderer $linkRenderer
44 * @param IConnectionProvider $dbProvider
45 * @param string $from
47 public function __construct(
48 IContextSource $context,
49 LinkBatchFactory $linkBatchFactory,
50 LinkRenderer $linkRenderer,
51 IConnectionProvider $dbProvider,
52 $from
53 ) {
54 // Set database before parent constructor to avoid setting it there
55 $this->mDb = $dbProvider->getReplicaDatabase();
56 parent::__construct( $context, $linkRenderer );
57 $this->linkBatchFactory = $linkBatchFactory;
58 $from = str_replace( ' ', '_', $from );
59 if ( $from !== '' ) {
60 $from = Title::capitalize( $from, NS_CATEGORY );
61 $this->setOffset( $from );
62 $this->setIncludeOffset( true );
66 public function getQueryInfo() {
67 return [
68 'tables' => [ 'category' ],
69 'fields' => [ 'cat_title', 'cat_pages' ],
70 'options' => [ 'USE INDEX' => 'cat_title' ],
74 public function getIndexField() {
75 return 'cat_title';
78 public function getDefaultQuery() {
79 parent::getDefaultQuery();
80 unset( $this->mDefaultQuery['from'] );
82 return $this->mDefaultQuery;
85 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
86 public function getBody() {
87 $batch = $this->linkBatchFactory->newLinkBatch();
89 $this->mResult->rewind();
91 foreach ( $this->mResult as $row ) {
92 $batch->add( NS_CATEGORY, $row->cat_title );
94 $batch->execute();
95 $this->mResult->rewind();
97 return parent::getBody();
100 public function formatRow( $result ) {
101 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
102 $text = $title->getText();
103 $link = $this->getLinkRenderer()->makeLink( $title, $text );
105 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
106 return Html::rawElement( 'li', [], $this->getLanguage()->specialList( $link, $count ) ) . "\n";
109 public function getStartForm( $from ) {
110 $formDescriptor = [
111 'from' => [
112 'type' => 'title',
113 'namespace' => NS_CATEGORY,
114 'relative' => true,
115 'label-message' => 'categoriesfrom',
116 'name' => 'from',
117 'id' => 'from',
118 'size' => 20,
119 'default' => $from,
123 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
124 ->setSubmitTextMsg( 'categories-submit' )
125 ->setWrapperLegendMsg( 'categories' )
126 ->setMethod( 'get' );
127 return $htmlForm->prepareForm()->getHTML( false );
133 * Retain the old class name for backwards compatibility.
134 * @deprecated since 1.41
136 class_alias( CategoryPager::class, 'CategoryPager' );