summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Factbox/Factbox.php
blob: feae3999b122436d9fb83bb137c1bca3cff727eb (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php

namespace SMW\Factbox;

use Html;
use Sanitizer;
use Title;
use SMW\ApplicationFactory;
use SMW\DataValueFactory;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\Localizer;
use SMW\Message;
use SMW\ParserData;
use SMW\Profiler;
use SMW\SemanticData;
use SMW\Store;
use SMW\Utils\HtmlDivTable;
use SMW\Utils\HtmlTabs;
use SMWInfolink;
use SMWSemanticData;

/**
 * Class handling the "Factbox" content rendering
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class Factbox {

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

	/**
	 * @var ParserData
	 */
	private $parserData;

	/**
	 * @var ApplicationFactory
	 */
	private $applicationFactory;

	/**
	 * @var DataValueFactory
	 */
	private $dataValueFactory;

	/**
	 * @var integer
	 */
	private $featureSet = 0;

	/**
	 * @var boolean
	 */
	protected $isVisible = false;

	/**
	 * @var string
	 */
	protected $content = null;

	/**
	 * @var boolean
	 */
	private $previewFlag = false;

	/**
	 * @since 1.9
	 *
	 * @param Store $store
	 * @param ParserData $parserData
	 */
	public function __construct( Store $store, ParserData $parserData ) {
		$this->store = $store;
		$this->parserData = $parserData;
		$this->applicationFactory = ApplicationFactory::getInstance();
		$this->dataValueFactory = DataValueFactory::getInstance();
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $featureSet
	 */
	public function setFeatureSet( $featureSet ) {
		$this->featureSet = $featureSet;
	}

	/**
	 * @note contains information about wpPreview
	 *
	 * @since 2.1
	 *
	 * @param boolean $previewFlag
	 */
	public function setPreviewFlag( $previewFlag ) {
		$this->previewFlag = $previewFlag;
	}

	/**
	 * Builds content suitable for rendering a Factbox and
	 * updating the ParserOutput accordingly
	 *
	 * @since 1.9
	 *
	 * @return Factbox
	 */
	public function doBuild() {

		$this->content = $this->fetchContent( $this->getMagicWords() );

		if ( $this->content !== '' ) {
			$this->parserData->getOutput()->addModules( $this->getModules() );
			$this->parserData->pushSemanticDataToParserOutput();
			$this->isVisible = true;
		}

		return $this;
	}

	/**
	 * Returns Title object
	 *
	 * @since 1.9
	 *
	 * @return string|null
	 */
	public function getTitle() {
		return $this->parserData->getTitle();
	}

	/**
	 * Returns content
	 *
	 * @since 1.9
	 *
	 * @return string|null
	 */
	public function getContent() {
		return $this->content;
	}

	/**
	 * Returns if content is visible
	 *
	 * @since 1.9
	 *
	 * @return boolean
	 */
	public function isVisible() {
		return $this->isVisible;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $rendered
	 * @param string $derived
	 *
	 * @return string
	 */
	public static function tabs( $rendered, $derived = '' ) {

		$htmlTabs = new HtmlTabs();
		$htmlTabs->setActiveTab( 'facts-rendered' );
		$htmlTabs->tab(
			'facts-rendered',
			Message::get( 'smw-factbox-facts' , Message::TEXT, Message::USER_LANGUAGE ),
			[
				'title' => Message::get( 'smw-factbox-facts-help' , Message::TEXT, Message::USER_LANGUAGE )
			]
		);

		$htmlTabs->content( 'facts-rendered', $rendered );

		$htmlTabs->tab(
			'facts-derived',
			Message::get( 'smw-factbox-derived' , Message::TEXT, Message::USER_LANGUAGE ),
			[
				'hide' => $derived === '' ? true : false
			]
		);

		$htmlTabs->content( 'facts-derived', $derived );

		return $htmlTabs->buildHTML(
			[
				'class' => 'smw-factbox'
			]
		);
	}

	/**
	 * Returns magic words attached to the ParserOutput object
	 *
	 * @since 1.9
	 *
	 * @return string|null
	 */
	protected function getMagicWords() {

		$settings = $this->applicationFactory->getSettings();
		$parserOutput = $this->parserData->getOutput();

		// Prior MW 1.21 mSMWMagicWords is used (see SMW\ParserTextProcessor)
		if ( method_exists( $parserOutput, 'getExtensionData' ) ) {
			$smwMagicWords = $parserOutput->getExtensionData( 'smwmagicwords' );
			$mws = $smwMagicWords === null ? [] : $smwMagicWords;
		} else {
			// @codeCoverageIgnoreStart
			$mws = isset( $parserOutput->mSMWMagicWords ) ? $parserOutput->mSMWMagicWords : [];
			// @codeCoverageIgnoreEnd
		}

		if ( in_array( 'SMW_SHOWFACTBOX', $mws ) ) {
			$showfactbox = SMW_FACTBOX_NONEMPTY;
		} elseif ( in_array( 'SMW_NOFACTBOX', $mws ) ) {
			$showfactbox = SMW_FACTBOX_HIDDEN;
		} elseif ( $this->previewFlag ) {
			$showfactbox = $settings->get( 'smwgShowFactboxEdit' );
		} else {
			$showfactbox = $settings->get( 'smwgShowFactbox' );
		}

		return $showfactbox;
	}

	/**
	 * Returns required resource modules
	 *
	 * @since 1.9
	 *
	 * @return array
	 */
	protected function getModules() {
		return [
			'ext.smw.style',
			'ext.smw.table.styles'
		];
	}

	/**
	 * Returns content found for a given ParserOutput object and if the required
	 * custom data was not available then semantic data are retrieved from
	 * the store for a given subject.
	 *
	 * The method checks whether the given setting of $showfactbox requires
	 * displaying the given data at all.
	 *
	 * @since 1.9
	 *
	 * @return integer $showFactbox
	 *
	 * @return string|null
	 */
	protected function fetchContent( $showFactbox = SMW_FACTBOX_NONEMPTY ) {

		if ( $showFactbox === SMW_FACTBOX_HIDDEN ) {
			return '';
		}

		$semanticData = $this->parserData->getSemanticData();

		if ( $semanticData === null || $semanticData->stubObject || $this->isEmpty( $semanticData ) ) {
			$semanticData = $this->store->getSemanticData( $this->parserData->getSubject() );
		}

		if ( $showFactbox === SMW_FACTBOX_SPECIAL && !$semanticData->hasVisibleSpecialProperties() ) {
			// show only if there are special properties
			return '';
		} elseif ( $showFactbox === SMW_FACTBOX_NONEMPTY && !$semanticData->hasVisibleProperties() ) {
			// show only if non-empty
			return '';
		}

		return $this->createTable( $semanticData );
	}

	/**
	 * Returns a formatted factbox table
	 *
	 * @since 1.9
	 *
	 * @param SMWSemanticData $semanticData
	 *
	 * @return string|null
	 */
	protected function createTable( SemanticData $semanticData ) {

		$html = '';

		// Hook deprecated with SMW 1.9 and will vanish with SMW 1.11
		\Hooks::run( 'smwShowFactbox', [ &$html, $semanticData ] );

		// Hook since 1.9
		if ( \Hooks::run( 'SMW::Factbox::BeforeContentGeneration', [ &$html, $semanticData ] ) ) {

			$header = $this->createHeader( $semanticData->getSubject() );
			$rows = $this->createRows( $semanticData );

			$html .= Html::rawElement(
				'div',
				[
					'class' => 'smwfact',
					'style' => 'display:block;'
				],
				$header . HtmlDivTable::table(
					$rows,
					[
						'class' => 'smwfacttable'
					]
				)
			);
		}

		return $html;
	}

	private function createHeader( DIWikiPage $subject ) {

		$dataValue = $this->dataValueFactory->newDataValueByItem( $subject, null );

		$browselink = SMWInfolink::newBrowsingLink(
			$dataValue->getPreferredCaption(),
			$dataValue->getWikiValue(),
			''
		);

		$header = Html::rawElement(
			'div',
			[ 'class' => 'smwfactboxhead' ],
			Message::get( [ 'smw-factbox-head', $browselink->getWikiText() ], Message::TEXT, Message::USER_LANGUAGE )
		);

		$rdflink = SMWInfolink::newInternalLink(
			Message::get( 'smw_viewasrdf', Message::TEXT, Message::USER_LANGUAGE ),
			Localizer::getInstance()->getNamespaceTextById( NS_SPECIAL ) . ':ExportRDF/' . $dataValue->getWikiValue(),
			'rdflink'
		);

		$header .= Html::rawElement(
			'div',
			[ 'class' => 'smwrdflink' ],
			$rdflink->getWikiText()
		);

		return $header;
	}

	private function createRows( SemanticData $semanticData ) {

		$rows = '';
		$attributes = [];

		$comma = Message::get(
			'comma-separator',
			Message::ESCAPED,
			Message::USER_LANGUAGE
		);

		$and = Message::get(
			'and',
			Message::ESCAPED,
			Message::USER_LANGUAGE
		);

		foreach ( $semanticData->getProperties() as $property ) {

			if ( $property->getKey() === '_SOBJ' && !$this->hasFeature( SMW_FACTBOX_DISPLAY_SUBOBJECT ) ) {
				continue;
			}

			$propertyDv = $this->dataValueFactory->newDataValueByItem( $property, null );
			$row = '';

			if ( !$property->isShown() ) {
				// showing this is not desired, hide
				continue;
			} elseif ( $property->isUserDefined() ) {
				$propertyDv->setCaption( $propertyDv->getWikiValue() );
				$attributes['property'] = [ 'class' => 'smwpropname' ];
				$attributes['values'] = [ 'class' => 'smwprops' ];
			} elseif ( $propertyDv->isVisible() ) {
				// Predefined property
				$attributes['property'] = [ 'class' => 'smwspecname' ];
				$attributes['values'] = [ 'class' => 'smwspecs' ];
			} else {
				// predefined, internal property
				// @codeCoverageIgnoreStart
				continue;
				// @codeCoverageIgnoreEnd
			}

			$list = [];
			$html = '';

			foreach ( $semanticData->getPropertyValues( $property ) as $dataItem ) {

				$dataValue = $this->dataValueFactory->newDataValueByItem( $dataItem, $property );

				$outputFormat = $dataValue->getOutputFormat();
				$dataValue->setOutputFormat( $outputFormat ? $outputFormat : 'LOCL' );

				$dataValue->setOption( $dataValue::OPT_DISABLE_SERVICELINKS, true );

				if ( $dataValue->isValid() ) {
					$list[] = $dataValue->getLongWikiText( true ) . $dataValue->getInfolinkText( SMW_OUTPUT_WIKI );
				}
			}

			if ( $list !== [] ) {
				$last = array_pop( $list );

				if ( $list === [] ) {
					$html = $last;
				} else {
					$html = implode( $comma, $list ) . '&nbsp;' . $and . '&nbsp;' . $last;
				}
			}

			$row .= HtmlDivTable::cell(
				$propertyDv->getShortWikiText( true ),
				$attributes['property']
			);

			$row .= HtmlDivTable::cell(
				$html,
				$attributes['values']
			);

			$rows .= HtmlDivTable::row(
				$row
			);
		}

		return $rows;
	}

	private function isEmpty( SemanticData $semanticData ) {

		// MW's internal Parser does iterate the ParserOutput object several times
		// which can leave a '_SKEY' property while in fact the container is empty.
		$semanticData->removeProperty(
			new DIProperty( '_SKEY' )
		);

		return $semanticData->isEmpty();
	}


	private function hasFeature( $feature ) {
		return ( (int)$this->featureSet & $feature ) != 0;
	}

}