Update code formatting
[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 return $this->mDefaultQuery;
94 }
95
96 # protected function getOrderTypeMessages() {
97 # return array( 'abc' => 'special-categories-sort-abc',
98 # 'count' => 'special-categories-sort-count' );
99 # }
100
101 protected function getDefaultDirections() {
102 # return array( 'abc' => false, 'count' => true );
103 return false;
104 }
105
106 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
107 public function getBody() {
108 $batch = new LinkBatch;
109
110 $this->mResult->rewind();
111
112 foreach ( $this->mResult as $row ) {
113 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
114 }
115 $batch->execute();
116 $this->mResult->rewind();
117 return parent::getBody();
118 }
119
120 function formatRow( $result ) {
121 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
122 $titleText = Linker::link( $title, htmlspecialchars( $title->getText() ) );
123 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
124 return Xml::tags( 'li', null, $this->getLanguage()->specialList( $titleText, $count ) ) . "\n";
125 }
126
127 public function getStartForm( $from ) {
128 global $wgScript;
129
130 return Xml::tags(
131 'form',
132 array( 'method' => 'get', 'action' => $wgScript ),
133 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
134 Xml::fieldset(
135 $this->msg( 'categories' )->text(),
136 Xml::inputLabel(
137 $this->msg( 'categoriesfrom' )->text(),
138 'from', 'from', 20, $from ) .
139 ' ' .
140 Xml::submitButton( $this->msg( 'allpagessubmit' )->text()
141 )
142 )
143 );
144 }
145 }