Merge "(bug 19195) Make user IDs more readily available with the API"
[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
55 /**
56 * TODO: Allow sorting by count. We need to have a unique index to do this
57 * properly.
58 *
59 * @ingroup SpecialPage Pager
60 */
61 class CategoryPager extends AlphabeticPager {
62 function __construct( IContextSource $context, $from ) {
63 parent::__construct( $context );
64 $from = str_replace( ' ', '_', $from );
65 if( $from !== '' ) {
66 $from = Title::capitalize( $from, NS_CATEGORY );
67 $this->mOffset = $from;
68 }
69 }
70
71 function getQueryInfo() {
72 return array(
73 'tables' => array( 'category' ),
74 'fields' => array( 'cat_title','cat_pages' ),
75 'conds' => array( 'cat_pages > 0' ),
76 'options' => array( 'USE INDEX' => 'cat_title' ),
77 );
78 }
79
80 function getIndexField() {
81 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
82 return 'cat_title';
83 }
84
85 function getDefaultQuery() {
86 parent::getDefaultQuery();
87 unset( $this->mDefaultQuery['from'] );
88 return $this->mDefaultQuery;
89 }
90 # protected function getOrderTypeMessages() {
91 # return array( 'abc' => 'special-categories-sort-abc',
92 # 'count' => 'special-categories-sort-count' );
93 # }
94
95 protected function getDefaultDirections() {
96 # return array( 'abc' => false, 'count' => true );
97 return false;
98 }
99
100 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
101 public function getBody() {
102 $batch = new LinkBatch;
103
104 $this->mResult->rewind();
105
106 foreach ( $this->mResult as $row ) {
107 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
108 }
109 $batch->execute();
110 $this->mResult->rewind();
111 return parent::getBody();
112 }
113
114 function formatRow($result) {
115 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
116 $titleText = Linker::link( $title, htmlspecialchars( $title->getText() ) );
117 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
118 return Xml::tags( 'li', null, $this->getLanguage()->specialList( $titleText, $count ) ) . "\n";
119 }
120
121 public function getStartForm( $from ) {
122 global $wgScript;
123
124 return
125 Xml::tags( 'form', array( 'method' => 'get', 'action' => $wgScript ),
126 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
127 Xml::fieldset( $this->msg( 'categories' )->text(),
128 Xml::inputLabel( $this->msg( 'categoriesfrom' )->text(),
129 'from', 'from', 20, $from ) .
130 ' ' .
131 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) ) );
132 }
133 }