Completed
Branch master (eb7c38)
by
unknown
25:25
created

CategoryPager::getDefaultDirections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 * @ingroup Pager
20
 */
21
22
/**
23
 * @ingroup Pager
24
 */
25
class CategoryPager extends AlphabeticPager {
26
27
	/**
28
	 * @var PageLinkRenderer
29
	 */
30
	protected $linkRenderer;
31
32
	/**
33
	 * @param IContextSource $context
34
	 * @param string $from
35
	 * @param PageLinkRenderer $linkRenderer
36
	 */
37
	public function __construct( IContextSource $context, $from, PageLinkRenderer $linkRenderer
38
	) {
39
		parent::__construct( $context );
40
		$from = str_replace( ' ', '_', $from );
41
		if ( $from !== '' ) {
42
			$from = Title::capitalize( $from, NS_CATEGORY );
43
			$this->setOffset( $from );
44
			$this->setIncludeOffset( true );
45
		}
46
47
		$this->linkRenderer = $linkRenderer;
48
	}
49
50 View Code Duplication
	function getQueryInfo() {
51
		return [
52
			'tables' => [ 'category' ],
53
			'fields' => [ 'cat_title', 'cat_pages' ],
54
			'conds' => [ 'cat_pages > 0' ],
55
			'options' => [ 'USE INDEX' => 'cat_title' ],
56
		];
57
	}
58
59
	function getIndexField() {
60
		return 'cat_title';
61
	}
62
63
	function getDefaultQuery() {
64
		parent::getDefaultQuery();
65
		unset( $this->mDefaultQuery['from'] );
66
67
		return $this->mDefaultQuery;
68
	}
69
70
	/* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
71
	public function getBody() {
72
		$batch = new LinkBatch;
73
74
		$this->mResult->rewind();
75
76
		foreach ( $this->mResult as $row ) {
77
			$batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
0 ignored issues
show
Bug introduced by
It seems like \Title::makeTitleSafe(NS...EGORY, $row->cat_title) can be null; however, addObj() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
78
		}
79
		$batch->execute();
80
		$this->mResult->rewind();
81
82
		return parent::getBody();
83
	}
84
85
	function formatRow( $result ) {
86
		$title = new TitleValue( NS_CATEGORY, $result->cat_title );
87
		$text = $title->getText();
88
		$link = $this->linkRenderer->renderHtmlLink( $title, $text );
89
90
		$count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
91
		return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
92
	}
93
94
	public function getStartForm( $from ) {
95
		return Xml::tags(
96
			'form',
97
			[ 'method' => 'get', 'action' => wfScript() ],
98
			Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
99
			Xml::fieldset(
100
				$this->msg( 'categories' )->text(),
101
				Xml::inputLabel(
102
					$this->msg( 'categoriesfrom' )->text(),
103
					'from', 'from', 20, $from, [ 'class' => 'mw-ui-input-inline' ] ) .
104
				' ' .
105
				Html::submitButton(
106
					$this->msg( 'categories-submit' )->text(),
107
					[], [ 'mw-ui-progressive' ]
108
				)
109
			)
110
		);
111
	}
112
}
113