Merge "Use canonical class name, remove unused globals."
[lhc/web/wiklou.git] / includes / specials / SpecialCategories.php
1 <?php
2 /**
3 * Implements Special:Categories
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * @ingroup SpecialPage
26 */
27 class SpecialCategories extends SpecialPage {
28
29 function __construct() {
30 parent::__construct( 'Categories' );
31 }
32
33 function execute( $par ) {
34 $this->setHeaders();
35 $this->outputHeader();
36 $this->getOutput()->allowClickjacking();
37
38 $from = $this->getRequest()->getText( 'from', $par );
39
40 $cap = new CategoryPager( $this->getContext(), $from );
41 $cap->doQuery();
42
43 $this->getOutput()->addHTML(
44 Html::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) .
45 $this->msg( 'categoriespagetext', $cap->getNumRows() )->parseAsBlock() .
46 $cap->getStartForm( $from ) .
47 $cap->getNavigationBar() .
48 '<ul>' . $cap->getBody() . '</ul>' .
49 $cap->getNavigationBar() .
50 Html::closeElement( 'div' )
51 );
52 }
53
54 protected function getGroupName() {
55 return 'pages';
56 }
57 }
58
59 /**
60 * TODO: Allow sorting by count. We need to have a unique index to do this
61 * properly.
62 *
63 * @ingroup SpecialPage Pager
64 */
65 class CategoryPager extends AlphabeticPager {
66 function __construct( IContextSource $context, $from ) {
67 parent::__construct( $context );
68 $from = str_replace( ' ', '_', $from );
69 if ( $from !== '' ) {
70 $from = Title::capitalize( $from, NS_CATEGORY );
71 $this->setOffset( $from );
72 $this->setIncludeOffset( true );
73 }
74 }
75
76 function getQueryInfo() {
77 return array(
78 'tables' => array( 'category' ),
79 'fields' => array( 'cat_title', 'cat_pages' ),
80 'conds' => array( 'cat_pages > 0' ),
81 'options' => array( 'USE INDEX' => 'cat_title' ),
82 );
83 }
84
85 function getIndexField() {
86 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
87 return 'cat_title';
88 }
89
90 function getDefaultQuery() {
91 parent::getDefaultQuery();
92 unset( $this->mDefaultQuery['from'] );
93
94 return $this->mDefaultQuery;
95 }
96
97 # protected function getOrderTypeMessages() {
98 # return array( 'abc' => 'special-categories-sort-abc',
99 # 'count' => 'special-categories-sort-count' );
100 # }
101
102 protected function getDefaultDirections() {
103 # return array( 'abc' => false, 'count' => true );
104 return false;
105 }
106
107 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
108 public function getBody() {
109 $batch = new LinkBatch;
110
111 $this->mResult->rewind();
112
113 foreach ( $this->mResult as $row ) {
114 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
115 }
116 $batch->execute();
117 $this->mResult->rewind();
118
119 return parent::getBody();
120 }
121
122 function formatRow( $result ) {
123 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
124 $titleText = Linker::link( $title, htmlspecialchars( $title->getText() ) );
125 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
126
127 return Xml::tags( 'li', null, $this->getLanguage()->specialList( $titleText, $count ) ) . "\n";
128 }
129
130 public function getStartForm( $from ) {
131 global $wgScript;
132
133 return Xml::tags(
134 'form',
135 array( 'method' => 'get', 'action' => $wgScript ),
136 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
137 Xml::fieldset(
138 $this->msg( 'categories' )->text(),
139 Xml::inputLabel(
140 $this->msg( 'categoriesfrom' )->text(),
141 'from', 'from', 20, $from ) .
142 ' ' .
143 Xml::submitButton( $this->msg( 'allpagessubmit' )->text()
144 )
145 )
146 );
147 }
148 }