Switch some HTMLForms in special pages to OOUI
[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 /**
30 * @var PageLinkRenderer
31 */
32 protected $linkRenderer = null;
33
34 public function __construct() {
35 parent::__construct( 'Categories' );
36
37 // Since we don't control the constructor parameters, we can't inject services that way.
38 // Instead, we initialize services in the execute() method, and allow them to be overridden
39 // using the initServices() method.
40 }
41
42 /**
43 * Initialize or override the PageLinkRenderer SpecialCategories collaborates with.
44 * Useful mainly for testing.
45 *
46 * @todo the pager should also be injected, and de-coupled from the rendering logic.
47 *
48 * @param PageLinkRenderer $linkRenderer
49 */
50 public function setPageLinkRenderer(
51 PageLinkRenderer $linkRenderer
52 ) {
53 $this->linkRenderer = $linkRenderer;
54 }
55
56 /**
57 * Initialize any services we'll need (unless it has already been provided via a setter).
58 * This allows for dependency injection even though we don't control object creation.
59 */
60 private function initServices() {
61 if ( !$this->linkRenderer ) {
62 $lang = $this->getContext()->getLanguage();
63 $titleFormatter = new MediaWikiTitleCodec( $lang, GenderCache::singleton() );
64 $this->linkRenderer = new MediaWikiPageLinkRenderer( $titleFormatter );
65 }
66 }
67
68 public function execute( $par ) {
69 $this->initServices();
70
71 $this->setHeaders();
72 $this->outputHeader();
73 $this->getOutput()->allowClickjacking();
74
75 $from = $this->getRequest()->getText( 'from', $par );
76
77 $cap = new CategoryPager( $this->getContext(), $from, $this->linkRenderer );
78 $cap->doQuery();
79
80 $this->getOutput()->addHTML(
81 Html::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) .
82 $this->msg( 'categoriespagetext', $cap->getNumRows() )->parseAsBlock() .
83 $cap->getStartForm( $from ) .
84 $cap->getNavigationBar() .
85 '<ul>' . $cap->getBody() . '</ul>' .
86 $cap->getNavigationBar() .
87 Html::closeElement( 'div' )
88 );
89 }
90
91 protected function getGroupName() {
92 return 'pages';
93 }
94 }
95
96 /**
97 * TODO: Allow sorting by count. We need to have a unique index to do this
98 * properly.
99 *
100 * @ingroup SpecialPage Pager
101 */
102 class CategoryPager extends AlphabeticPager {
103
104 /**
105 * @var PageLinkRenderer
106 */
107 protected $linkRenderer;
108
109 /**
110 * @param IContextSource $context
111 * @param string $from
112 * @param PageLinkRenderer $linkRenderer
113 */
114 public function __construct( IContextSource $context, $from, PageLinkRenderer $linkRenderer
115 ) {
116 parent::__construct( $context );
117 $from = str_replace( ' ', '_', $from );
118 if ( $from !== '' ) {
119 $from = Title::capitalize( $from, NS_CATEGORY );
120 $this->setOffset( $from );
121 $this->setIncludeOffset( true );
122 }
123
124 $this->linkRenderer = $linkRenderer;
125 }
126
127 function getQueryInfo() {
128 return array(
129 'tables' => array( 'category' ),
130 'fields' => array( 'cat_title', 'cat_pages' ),
131 'conds' => array( 'cat_pages > 0' ),
132 'options' => array( 'USE INDEX' => 'cat_title' ),
133 );
134 }
135
136 function getIndexField() {
137 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
138 return 'cat_title';
139 }
140
141 function getDefaultQuery() {
142 parent::getDefaultQuery();
143 unset( $this->mDefaultQuery['from'] );
144
145 return $this->mDefaultQuery;
146 }
147
148 # protected function getOrderTypeMessages() {
149 # return array( 'abc' => 'special-categories-sort-abc',
150 # 'count' => 'special-categories-sort-count' );
151 # }
152
153 protected function getDefaultDirections() {
154 # return array( 'abc' => false, 'count' => true );
155 return false;
156 }
157
158 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
159 public function getBody() {
160 $batch = new LinkBatch;
161
162 $this->mResult->rewind();
163
164 foreach ( $this->mResult as $row ) {
165 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
166 }
167 $batch->execute();
168 $this->mResult->rewind();
169
170 return parent::getBody();
171 }
172
173 function formatRow( $result ) {
174 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
175 $text = $title->getText();
176 $link = $this->linkRenderer->renderHtmlLink( $title, $text );
177
178 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
179 return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
180 }
181
182 public function getStartForm( $from ) {
183 return Xml::tags(
184 'form',
185 array( 'method' => 'get', 'action' => wfScript() ),
186 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
187 Xml::fieldset(
188 $this->msg( 'categories' )->text(),
189 Xml::inputLabel(
190 $this->msg( 'categoriesfrom' )->text(),
191 'from', 'from', 20, $from, array( 'class' => 'mw-ui-input-inline' ) ) .
192 ' ' .
193 Html::submitButton(
194 $this->msg( 'allpagessubmit' )->text(),
195 array(), array( 'mw-ui-progressive' )
196 )
197 )
198 );
199 }
200 }