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