Actually format search error Status objects nicely
[mediawiki.git] / includes / specials / SpecialCategories.php
blob734544d40825d64e4f1d7d4be48f6e2cc964d315
1 <?php
2 /**
3 * Implements Special:Categories
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * @ingroup SpecialPage
27 class SpecialCategories extends SpecialPage {
29 /**
30 * @var PageLinkRenderer
32 protected $linkRenderer = null;
34 public function __construct() {
35 parent::__construct( 'Categories' );
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.
42 /**
43 * Initialize or override the PageLinkRenderer SpecialCategories collaborates with.
44 * Useful mainly for testing.
46 * @todo: the pager should also be injected, and de-coupled from the rendering logic.
48 * @param PageLinkRenderer $linkRenderer
50 public function setPageLinkRenderer(
51 PageLinkRenderer $linkRenderer
52 ) {
53 $this->linkRenderer = $linkRenderer;
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.
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 );
68 public function execute( $par ) {
69 $this->initServices();
71 $this->setHeaders();
72 $this->outputHeader();
73 $this->getOutput()->allowClickjacking();
75 $from = $this->getRequest()->getText( 'from', $par );
77 $cap = new CategoryPager( $this->getContext(), $from, $this->linkRenderer );
78 $cap->doQuery();
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' )
91 protected function getGroupName() {
92 return 'pages';
96 /**
97 * TODO: Allow sorting by count. We need to have a unique index to do this
98 * properly.
100 * @ingroup SpecialPage Pager
102 class CategoryPager extends AlphabeticPager {
105 * @var PageLinkRenderer
107 protected $linkRenderer;
110 * @param IContextSource $context
111 * @param string $from
112 * @param PageLinkRenderer $linkRenderer
114 public function __construct( IContextSource $context, $from, PageLinkRenderer $linkRenderer
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 );
124 $this->linkRenderer = $linkRenderer;
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' ),
136 function getIndexField() {
137 # return array( 'abc' => 'cat_title', 'count' => 'cat_pages' );
138 return 'cat_title';
141 function getDefaultQuery() {
142 parent::getDefaultQuery();
143 unset( $this->mDefaultQuery['from'] );
145 return $this->mDefaultQuery;
148 # protected function getOrderTypeMessages() {
149 # return array( 'abc' => 'special-categories-sort-abc',
150 # 'count' => 'special-categories-sort-count' );
153 protected function getDefaultDirections() {
154 # return array( 'abc' => false, 'count' => true );
155 return false;
158 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
159 public function getBody() {
160 $batch = new LinkBatch;
162 $this->mResult->rewind();
164 foreach ( $this->mResult as $row ) {
165 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
167 $batch->execute();
168 $this->mResult->rewind();
170 return parent::getBody();
173 function formatRow( $result ) {
174 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
175 $text = $title->getText();
176 $link = $this->linkRenderer->renderHtmlLink( $title, $text );
178 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
179 return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
182 public function getStartForm( $from ) {
183 global $wgScript;
185 return Xml::tags(
186 'form',
187 array( 'method' => 'get', 'action' => $wgScript ),
188 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
189 Xml::fieldset(
190 $this->msg( 'categories' )->text(),
191 Xml::inputLabel(
192 $this->msg( 'categoriesfrom' )->text(),
193 'from', 'from', 20, $from ) .
194 ' ' .
195 Xml::submitButton( $this->msg( 'allpagessubmit' )->text()