338cd7066608bbf15428fefbaf17b3e4054a4e52
[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 private $conds = array( 'cat_pages > 0' );
63
64 function __construct( IContextSource $context, $from ) {
65 parent::__construct( $context );
66 $from = str_replace( ' ', '_', $from );
67 if( $from !== '' ) {
68 $from = Title::capitalize( $from, NS_CATEGORY );
69 $dbr = wfGetDB( DB_SLAVE );
70 $this->conds[] = 'cat_title >= ' . $dbr->addQuotes( $from );
71 $this->setOffset( '' );
72 }
73 }
74
75 function getQueryInfo() {
76 return array(
77 'tables' => array( 'category' ),
78 'fields' => array( 'cat_title','cat_pages' ),
79 'conds' => $this->conds,
80 'options' => array( 'USE INDEX' => 'cat_title' ),
81 );
82 }
83
84 function getIndexField() {
85 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
86 return 'cat_title';
87 }
88
89 function getDefaultQuery() {
90 parent::getDefaultQuery();
91 unset( $this->mDefaultQuery['from'] );
92 return $this->mDefaultQuery;
93 }
94 # protected function getOrderTypeMessages() {
95 # return array( 'abc' => 'special-categories-sort-abc',
96 # 'count' => 'special-categories-sort-count' );
97 # }
98
99 protected function getDefaultDirections() {
100 # return array( 'abc' => false, 'count' => true );
101 return false;
102 }
103
104 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
105 public function getBody() {
106 $batch = new LinkBatch;
107
108 $this->mResult->rewind();
109
110 foreach ( $this->mResult as $row ) {
111 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
112 }
113 $batch->execute();
114 $this->mResult->rewind();
115 return parent::getBody();
116 }
117
118 function formatRow($result) {
119 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
120 $titleText = Linker::link( $title, htmlspecialchars( $title->getText() ) );
121 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
122 return Xml::tags( 'li', null, $this->getLanguage()->specialList( $titleText, $count ) ) . "\n";
123 }
124
125 public function getStartForm( $from ) {
126 global $wgScript;
127
128 return
129 Xml::tags( 'form', array( 'method' => 'get', 'action' => $wgScript ),
130 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
131 Xml::fieldset( $this->msg( 'categories' )->text(),
132 Xml::inputLabel( $this->msg( 'categoriesfrom' )->text(),
133 'from', 'from', 20, $from ) .
134 ' ' .
135 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) ) );
136 }
137 }