summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueFormatters/MonolingualTextValueFormatter.php
blob: 71702889dc6a1083170476d9a4bb130e01de3707 (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
<?php

namespace SMW\DataValues\ValueFormatters;

use RuntimeException;
use SMW\DataValueFactory;
use SMW\DataValues\MonolingualTextValue;
use SMW\DIProperty;
use SMW\Message;
use SMWDataValue as DataValue;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class MonolingualTextValueFormatter extends DataValueFormatter {

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function isFormatterFor( DataValue $dataValue ) {
		return $dataValue instanceof MonolingualTextValue;
	}

	/**
	 * @since 2.4
	 *
	 * {@inheritDoc}
	 */
	public function format( $type, $linker = null ) {

		if ( !$this->dataValue instanceof MonolingualTextValue ) {
			throw new RuntimeException( "The formatter is missing a valid MonolingualTextValue object" );
		}

		if (
			$this->dataValue->getCaption() !== false &&
			( $type === self::WIKI_SHORT || $type === self::HTML_SHORT ) ) {
			return $this->dataValue->getCaption();
		}

		return $this->getOutputText( $type, $linker );
	}

	protected function getOutputText( $type, $linker = null ) {

		if ( !$this->dataValue->isValid() ) {
			return ( ( $type == self::WIKI_SHORT ) || ( $type == self::HTML_SHORT ) ) ? '' : $this->dataValue->getErrorText();
		}

		// For the inverse case, return the subject that contains the reference
		// for Foo annotated with [[Bar::abc@en]] -> [[-Bar::Foo]]
		if ( $this->dataValue->getProperty() !== null && $this->dataValue->getProperty()->isInverse() ) {

			$dataItems = $this->dataValue->getDataItem()->getSemanticData()->getPropertyValues(
				new DIProperty(  $this->dataValue->getProperty()->getKey() )
			);

			$dataItem = reset( $dataItems );

			if ( !$dataItem ) {
				return '';
			}

			return $dataItem->getDBKey();
		}

		return $this->doFormatFinalOutputFor( $type, $linker );
	}

	private function doFormatFinalOutputFor( $type, $linker ) {

		$text = '';
		$languagecode = '';

		foreach ( $this->dataValue->getPropertyDataItems() as $property ) {

			// If we wanted to omit the language code display for some outputs then
			// this is the point to make it happen
			if ( ( $type == self::HTML_LONG || $type == self::WIKI_SHORT ) && $property->getKey() === '_LCODE' ) {
			//continue;
			}

			$dataItems = $this->dataValue->getDataItem()->getSemanticData()->getPropertyValues(
				$property
			);

			// Should not happen but just in case
			if ( !$dataItems === [] ) {
				$this->dataValue->addErrorMsg( 'smw-datavalue-monolingual-dataitem-missing' );
				continue;
			}

			$dataItem = reset( $dataItems );

			if ( $dataItem === false ) {
				continue;
			}

			$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
				$dataItem,
				$property
			);

			$result = $this->findValueOutputFor(
				$type,
				$dataValue,
				$linker
			);

			if ( $property->getKey() === '_LCODE' && $type !== self::VALUE ) {
				$languagecode = ' ' . Message::get( [ 'smw-datavalue-monolingual-lcode-parenthesis', $result ] );
			} elseif ( $property->getKey() === '_LCODE' && $type === self::VALUE ) {
				$languagecode = '@' . $result;
			} else {
				$text = $result;
			}
		}

		return $text . $languagecode;
	}

	private function findValueOutputFor( $type, $dataValue, $linker ) {
		switch ( $type ) {
			case self::VALUE:
				return $dataValue->getWikiValue();
			case self::WIKI_SHORT:
				return $dataValue->getShortWikiText( $linker );
			case self::HTML_SHORT:
				return $dataValue->getShortHTMLText( $linker );
			case self::WIKI_LONG:
				return $dataValue->getShortWikiText( $linker );
			case self::HTML_LONG:
				return $dataValue->getShortHTMLText( $linker );
		}
	}

}