]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/pagers/CategoryPager.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / specials / pagers / CategoryPager.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 use MediaWiki\Linker\LinkRenderer;
22
23 /**
24  * @ingroup Pager
25  */
26 class CategoryPager extends AlphabeticPager {
27
28         /**
29          * @var LinkRenderer
30          */
31         protected $linkRenderer;
32
33         /**
34          * @param IContextSource $context
35          * @param string $from
36          * @param LinkRenderer $linkRenderer
37          */
38         public function __construct( IContextSource $context, $from, LinkRenderer $linkRenderer
39         ) {
40                 parent::__construct( $context );
41                 $from = str_replace( ' ', '_', $from );
42                 if ( $from !== '' ) {
43                         $from = Title::capitalize( $from, NS_CATEGORY );
44                         $this->setOffset( $from );
45                         $this->setIncludeOffset( true );
46                 }
47
48                 $this->linkRenderer = $linkRenderer;
49         }
50
51         function getQueryInfo() {
52                 return [
53                         'tables' => [ 'category' ],
54                         'fields' => [ 'cat_title', 'cat_pages' ],
55                         'options' => [ 'USE INDEX' => 'cat_title' ],
56                 ];
57         }
58
59         function getIndexField() {
60                 return 'cat_title';
61         }
62
63         function getDefaultQuery() {
64                 parent::getDefaultQuery();
65                 unset( $this->mDefaultQuery['from'] );
66
67                 return $this->mDefaultQuery;
68         }
69
70         /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
71         public function getBody() {
72                 $batch = new LinkBatch;
73
74                 $this->mResult->rewind();
75
76                 foreach ( $this->mResult as $row ) {
77                         $batch->addObj( new TitleValue( NS_CATEGORY, $row->cat_title ) );
78                 }
79                 $batch->execute();
80                 $this->mResult->rewind();
81
82                 return parent::getBody();
83         }
84
85         function formatRow( $result ) {
86                 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
87                 $text = $title->getText();
88                 $link = $this->linkRenderer->makeLink( $title, $text );
89
90                 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
91                 return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
92         }
93
94         public function getStartForm( $from ) {
95                 $formDescriptor = [
96                         'from' => [
97                                 'type' => 'title',
98                                 'namespace' => NS_CATEGORY,
99                                 'relative' => true,
100                                 'label-message' => 'categoriesfrom',
101                                 'name' => 'from',
102                                 'id' => 'from',
103                                 'size' => 20,
104                                 'default' => $from,
105                         ],
106                 ];
107
108                 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
109                         ->setSubmitTextMsg( 'categories-submit' )
110                         ->setWrapperLegendMsg( 'categories' )
111                         ->setMethod( 'get' );
112                 return $htmlForm->prepareForm()->getHTML( false );
113         }
114
115 }