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