summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Page/Page.php
blob: 5e682193e11105d872314f1ed32bcac331006c15 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php

namespace SMW\Page;

use Article;
use SMW\DIWikiPage;
use SMW\Options;
use SMWOutputs as Outputs;

/**
 * Abstract subclass of MediaWiki's Article that handles the common tasks of
 * article pages for Concept and Property pages. This is mainly parameter
 * handling and some very basic output control.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author Nikolas Iwan
 * @author Markus Krötzsch
 * @author Jeroen De Dauw
 */
abstract class Page extends Article {

	/**
	 * Limit for results per page.
	 *
	 * @var integer
	 */
	protected $limit;

	/**
	 * Start string: print $limit results from here.
	 *
	 * @var string
	 */
	protected $from;

	/**
	 * End string: print $limit results strictly before this article.
	 *
	 * @var string
	 */
	protected $until;

	/**
	 * Cache for the current skin, obtained from $wgUser.
	 *
	 * @var Skin
	 */
	protected $skin;

	/**
	 * @var Options
	 */
	private $options;

	/**
	 * Overwrite Article::view to add additional HTML to the output.
	 *
	 * @see Article::view
	 */
	public function view() {

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

		if ( !$this->getOption( 'smwgSemanticsEnabled' ) ) {
			$outputPage->setPageTitle( $this->getTitle()->getPrefixedText() );
			$outputPage->addHTML( wfMessage( 'smw-semantics-not-enabled' )->text() );
			return;
		}

		if ( ( $redirectTargetURL = $this->getRedirectTargetURL() ) !== false ) {
			$outputPage->redirect( $redirectTargetURL );
		}

		$this->initParameters();

		// Copied from CategoryPage
		$user = $this->getContext()->getUser();
		$request = $this->getContext()->getRequest();

		$diff = $request->getVal( 'diff' );
		$diffOnly = $request->getBool( 'diffonly', $user->getOption( 'diffonly' ) );

		if ( !isset( $diff ) || !$diffOnly ) {
			// MW 1.25+
			if ( method_exists( $outputPage, 'setIndicators' ) && ( $indicators = $this->getTopIndicators() ) !== '' ) {
				$outputPage->setIndicators( $indicators );
			}

			$outputPage->addHTML( $this->initHtml() );
			$outputPage->addHTML( $this->beforeView() );
		}

		if ( $this->isLockedView() === false ) {
			parent::view();
		}

		if ( !isset( $diff ) || !$diffOnly ) {
			$this->showList();
		}

		$outputPage->addHTML( $this->afterHtml() );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 *
	 * @return mixed
	 */
	public function getOption( $key ) {

		if ( $this->options === null ) {
			$this->options = new Options();
		}

		return $this->options->safeGet( $key, false );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function setOption( $key, $value ) {

		if ( $this->options === null ) {
			$this->options = new Options();
		}

		return $this->options->set( $key, $value );
	}

	/**
	 * @since 3.0
	 *
	 * @return string|boolean
	 */
	protected function getRedirectTargetURL() {
		return false;
	}

	/**
	 * @since 2.4
	 *
	 * @return string
	 */
	protected function getTopIndicators() {
		return '';
	}

	/**
	 * @since 3.0
	 *
	 * @return string
	 */
	protected function initHtml() {
		return $this->getIntroductoryText();
	}

	/**
	 * @since 2.4
	 *
	 * @return string
	 */
	protected function getIntroductoryText() {
		return '';
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	protected function isLockedView() {
		return false;
	}

	/**
	 * Main method for adding all additional HTML to the output stream.
	 */
	protected function showList() {

		$outputPage = $this->getContext()->getOutput();
		$request = $this->getContext()->getRequest();

		$this->from = $request->getVal( 'from', '' );
		$this->until = $request->getVal( 'until', '' );

		$outputPage->addHTML( $this->getHtml() );

		Outputs::commitToOutputPage( $outputPage );
	}

	/**
	 * Initialise some parameters that might be changed by subclasses
	 * (e.g. $limit). Method can be overwritten in this case.
	 * If the method returns false, nothing will be printed besides
	 * the original article.
	 *
	 * @return true
	 */
	protected function initParameters() {
		$this->limit = 20;
	}

	/**
	 * Returns HTML to be displayed before the article text.
	 *
	 * @return string
	 */
	protected function beforeView() {
		return '';
	}

	/**
	 * Returns HTML to be displayed after the list display.
	 *
	 * @return string
	 */
	protected function afterHtml() {
		return '';
	}

	/**
	 * Returns the HTML which is added to $wgOut after the article text.
	 *
	 * @return string
	 */
	protected abstract function getHtml();

	/**
	 * Like Article's getTitle(), but returning a suitable SMWDIWikiPage.
	 *
	 * @since 1.6
	 *
	 * @return SMWDIWikiPage
	 */
	protected function getDataItem() {
		return DIWikiPage::newFromTitle( $this->getTitle() );
	}

}