Merge "API: Normalize input URL in ApiQueryExtLinksUsage"
[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
22 /**
23 * TODO: Allow sorting by count. We need to have a unique index to do this
24 * properly.
25 *
26 * @ingroup Pager
27 */
28 class CategoryPager extends AlphabeticPager {
29
30 /**
31 * @var PageLinkRenderer
32 */
33 protected $linkRenderer;
34
35 /**
36 * @param IContextSource $context
37 * @param string $from
38 * @param PageLinkRenderer $linkRenderer
39 */
40 public function __construct( IContextSource $context, $from, PageLinkRenderer $linkRenderer
41 ) {
42 parent::__construct( $context );
43 $from = str_replace( ' ', '_', $from );
44 if ( $from !== '' ) {
45 $from = Title::capitalize( $from, NS_CATEGORY );
46 $this->setOffset( $from );
47 $this->setIncludeOffset( true );
48 }
49
50 $this->linkRenderer = $linkRenderer;
51 }
52
53 function getQueryInfo() {
54 return [
55 'tables' => [ 'category' ],
56 'fields' => [ 'cat_title', 'cat_pages' ],
57 'conds' => [ 'cat_pages > 0' ],
58 'options' => [ 'USE INDEX' => 'cat_title' ],
59 ];
60 }
61
62 function getIndexField() {
63 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
64 return 'cat_title';
65 }
66
67 function getDefaultQuery() {
68 parent::getDefaultQuery();
69 unset( $this->mDefaultQuery['from'] );
70
71 return $this->mDefaultQuery;
72 }
73
74 # protected function getOrderTypeMessages() {
75 # return array( 'abc' => 'special-categories-sort-abc',
76 # 'count' => 'special-categories-sort-count' );
77 # }
78
79 protected function getDefaultDirections() {
80 # return array( 'abc' => false, 'count' => true );
81 return false;
82 }
83
84 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
85 public function getBody() {
86 $batch = new LinkBatch;
87
88 $this->mResult->rewind();
89
90 foreach ( $this->mResult as $row ) {
91 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
92 }
93 $batch->execute();
94 $this->mResult->rewind();
95
96 return parent::getBody();
97 }
98
99 function formatRow( $result ) {
100 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
101 $text = $title->getText();
102 $link = $this->linkRenderer->renderHtmlLink( $title, $text );
103
104 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
105 return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
106 }
107
108 public function getStartForm( $from ) {
109 return Xml::tags(
110 'form',
111 [ 'method' => 'get', 'action' => wfScript() ],
112 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
113 Xml::fieldset(
114 $this->msg( 'categories' )->text(),
115 Xml::inputLabel(
116 $this->msg( 'categoriesfrom' )->text(),
117 'from', 'from', 20, $from, [ 'class' => 'mw-ui-input-inline' ] ) .
118 ' ' .
119 Html::submitButton(
120 $this->msg( 'categories-submit' )->text(),
121 [], [ 'mw-ui-progressive' ]
122 )
123 )
124 );
125 }
126 }