summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/specials/SpecialConcepts.php
blob: 9e5bea44a2f77d14963743180ef16c5ced766543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

namespace SMW;

use Html;
use SMW\Page\ListPager;
use SMW\Page\ListBuilder;
use SMW\SQLStore\SQLStore;
use SMW\Utils\HtmlTabs;
use SMW\ApplicationFactory;
use SMW\MediaWiki\Collator;

/**
 * Special page that lists available concepts
 *
 * @license GNU GPL v2+
 * @since   1.9
 *
 * @author mwjames
 */
class SpecialConcepts extends \SpecialPage {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @see SpecialPage::__construct
	 */
	public function __construct() {
		parent::__construct( 'Concepts' );
	}

	/**
	 * @see SpecialPage::execute
	 */
	public function execute( $param ) {

		$this->setHeaders();
		$out = $this->getOutput();
		$out->addModuleStyles( 'ext.smw.page.styles' );

		$limit = $this->getRequest()->getVal( 'limit', 50 );
		$offset = $this->getRequest()->getVal( 'offset', 0 );

		$this->store = ApplicationFactory::getInstance()->getStore();

		$diWikiPages = $this->fetchFromTable( $limit, $offset );
		$html = $this->getHtml( $diWikiPages, $limit, $offset );

		$this->addHelpLink( wfMessage( 'smw-helplink-concepts' )->escaped(), true );

		$out->setPageTitle( $this->msg( 'concepts' )->text() );
		$out->addHTML( $html );
	}

	/**
	 * @since 1.9
	 *
	 * @param integer $limit
	 * @param integer $offset
	 *
	 * @return DIWikiPage[]
	 */
	public function fetchFromTable( $limit, $offset ) {

		$connection = $this->store->getConnection( 'mw.db' );
		$results = [];

		$fields = [
			'smw_id',
			'smw_title'
		];

		$conditions = [
			'smw_namespace' => SMW_NS_CONCEPT,
			'smw_iw' => '',
			'smw_subobject' => '',
			'smw_proptable_hash IS NOT NULL',
			'concept_features > 0'
		];

		$options = [
			'LIMIT' => $limit + 1,
			'OFFSET' => $offset,
		];

		$res = $connection->select(
			[
				$connection->tableName( SQLStore::ID_TABLE ),
				$connection->tableName( SQLStore::CONCEPT_TABLE )
			],
			$fields,
			$conditions,
			__METHOD__,
			$options,
			[
				$connection->tableName( SQLStore::ID_TABLE ) => [ 'INNER JOIN', [ 'smw_id=s_id' ] ]
			]
		);

		foreach ( $res as $row ) {
			$results[] = new DIWikiPage( $row->smw_title, SMW_NS_CONCEPT );
		}

		return $results;
	}

	/**
	 * @since 1.9
	 *
	 * @param DIWikiPage[] $dataItems
	 * @param integer $limit
	 * @param integer $offset
	 *
	 * @return string
	 */
	public function getHtml( $dataItems, $limit, $offset ) {

		if ( $this->store === null ) {
			$this->store = ApplicationFactory::getInstance()->getStore();
		}

		$count = count( $dataItems );
		$resultNumber = min( $limit, $count );

		if ( $resultNumber == 0 ) {
			$key = 'smw-special-concept-empty';
		} else {
			$key = 'smw-special-concept-count';
		}

		$listBuilder = new ListBuilder(
			$this->store,
			Collator::singleton()
		);

		$htmlTabs = new HtmlTabs();
		$htmlTabs->setGroup( 'concept' );

		$html = Html::rawElement(
				'div',
				[ 'id' => 'mw-pages'],
			Html::rawElement(
				'div',
				[ 'class' => 'smw-page-navigation' ],
				ListPager::pagination( $this->getPageTitle(), $limit, $offset, $count )
			) . Html::element(
				'div',
				[ 'class' => $key, 'style' => 'margin-top:10px;margin-bottom:10px;' ],
				$this->msg( $key, $resultNumber )->parse()
			) . $listBuilder->getColumnList( $dataItems )
		);

		$htmlTabs->tab( 'smw-concept-list', $this->msg( 'smw-concept-tab-list' ) );
		$htmlTabs->content( 'smw-concept-list', $html );

		$html = $htmlTabs->buildHTML(
			[ 'class' => 'smw-concept clearfix' ]
		);

		return Html::rawElement(
			'p',
			[ 'class' => 'smw-special-concept-docu plainlinks' ],
			$this->msg( 'smw-special-concept-docu' )->parse()
		) . $html;
	}

	/**
	 * @see SpecialPage::getGroupName
	 */
	protected function getGroupName() {
		return 'pages';
	}

}