Moved some of the functionality from mediawiki.legacy.prefs into mediawiki.specials...
[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 global $wgOut, $wgRequest;
35
36 $this->setHeaders();
37 $this->outputHeader();
38
39 $from = $wgRequest->getText( 'from', $par );
40
41 $cap = new CategoryPager( $from );
42 $cap->doQuery();
43
44 $wgOut->addHTML(
45 Html::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) .
46 wfMsgExt( 'categoriespagetext', array( 'parse' ), $cap->getNumRows() ) .
47 $cap->getStartForm( $from ) .
48 $cap->getNavigationBar() .
49 '<ul>' . $cap->getBody() . '</ul>' .
50 $cap->getNavigationBar() .
51 Html::closeElement( 'div' )
52 );
53 }
54 }
55
56 /**
57 * TODO: Allow sorting by count. We need to have a unique index to do this
58 * properly.
59 *
60 * @ingroup SpecialPage Pager
61 */
62 class CategoryPager extends AlphabeticPager {
63 function __construct( $from ) {
64 parent::__construct();
65 $from = str_replace( ' ', '_', $from );
66 if( $from !== '' ) {
67 $from = Title::capitalize( $from, NS_CATEGORY );
68 $this->mOffset = $from;
69 }
70 }
71
72 function getQueryInfo() {
73 return array(
74 'tables' => array( 'category' ),
75 'fields' => array( 'cat_title','cat_pages' ),
76 'conds' => array( 'cat_pages > 0' ),
77 'options' => array( 'USE INDEX' => 'cat_title' ),
78 );
79 }
80
81 function getIndexField() {
82 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
83 return 'cat_title';
84 }
85
86 function getDefaultQuery() {
87 parent::getDefaultQuery();
88 unset( $this->mDefaultQuery['from'] );
89 return $this->mDefaultQuery;
90 }
91 # protected function getOrderTypeMessages() {
92 # return array( 'abc' => 'special-categories-sort-abc',
93 # 'count' => 'special-categories-sort-count' );
94 # }
95
96 protected function getDefaultDirections() {
97 # return array( 'abc' => false, 'count' => true );
98 return false;
99 }
100
101 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
102 public function getBody() {
103 $batch = new LinkBatch;
104
105 $this->mResult->rewind();
106
107 while ( $row = $this->mResult->fetchObject() ) {
108 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
109 }
110 $batch->execute();
111 $this->mResult->rewind();
112 return parent::getBody();
113 }
114
115 function formatRow($result) {
116 global $wgLang;
117 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
118 $titleText = $this->getSkin()->link( $title, htmlspecialchars( $title->getText() ) );
119 $count = wfMsgExt( 'nmembers', array( 'parsemag', 'escape' ),
120 $wgLang->formatNum( $result->cat_pages ) );
121 return Xml::tags('li', null, "$titleText ($count)" ) . "\n";
122 }
123
124 public function getStartForm( $from ) {
125 global $wgScript;
126 $t = SpecialPage::getTitleFor( 'Categories' );
127
128 return
129 Xml::tags( 'form', array( 'method' => 'get', 'action' => $wgScript ),
130 Xml::hidden( 'title', $t->getPrefixedText() ) .
131 Xml::fieldset( wfMsg( 'categories' ),
132 Xml::inputLabel( wfMsg( 'categoriesfrom' ),
133 'from', 'from', 20, $from ) .
134 ' ' .
135 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) ) );
136 }
137 }