summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/Browse/ValueFormatter.php
blob: 98a87e4886908c09e978c7e44d5cb04f65be11ee (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
<?php

namespace SMW\MediaWiki\Specials\Browse;

use SMW\ApplicationFactory;
use SMW\DataValueFactory;
use SMW\DataValues\PropertyValue;
use SMW\DataValues\ValueFormatters\DataValueFormatter;
use SMW\DIProperty;
use SMW\Localizer;
use SMWDataValue as DataValue;
use SMWInfolink as Infolink;

/**
 * @private
 *
 * This class should eventually be injected instead of relying on static methods,
 * for now this is the easiest way to unclutter the mammoth Browse class and
 * splitting up responsibilities.
 *
 * @license GNU GPL v2+
 * @since   2.5
 *
 * @author mwjames
 */
class ValueFormatter {

	/**
	 * @since 2.5
	 *
	 * @param DataValue $value
	 *
	 * @return string
	 */
	public static function getFormattedSubject( DataValue $dataValue ) {

		$extra = '';

		if ( $dataValue->getDataItem()->getNamespace() === SMW_NS_PROPERTY ) {

			$dv = DataValueFactory::getInstance()->newDataValueByItem(
				DIProperty::newFromUserLabel( $dataValue->getDataItem()->getDBKey() )
			);

			$label = $dv->getFormattedLabel( DataValueFormatter::WIKI_LONG );

			// Those with a formatted displayTitle
			// foaf:homepage&nbsp;<span style="font-size:small;">(Foaf:homepage)</span>
			if ( strpos( $label, '&nbsp;<span' ) !== false ) {
				list( $label, $extra ) = explode( '&nbsp;', $label );
				$extra = '&nbsp;' . $extra;
			}

			$dataValue->setCaption( $label );
		}

		return $dataValue->getLongHTMLText( smwfGetLinker() ) . $extra;
	}

	/**
	 * Displays a value, including all relevant links (browse and search by property)
	 *
	 * @since 2.5
	 *
	 * @param DataValue $value
	 * @param PropertyValue $property
	 * @param boolean $incoming
	 *
	 * @return string
	 */
	public static function getFormattedValue( DataValue $dataValue, PropertyValue $propertyValue, $incoming = false, $user = null ) {

		$linker = smwfGetLinker();
		$dataItem = $dataValue->getContextPage();

		// Allow the DV formatter to access a specific language code
		$dataValue->setOption(
			DataValue::OPT_CONTENT_LANGUAGE,
			Localizer::getInstance()->getPreferredContentLanguage( $dataItem )->getCode()
		);

		$dataValue->setOption(
			DataValue::OPT_USER_LANGUAGE,
			Localizer::getInstance()->getUserLanguage()->getCode()
		);

		$outputFormat = $dataValue->getOutputFormat();

		if ( $outputFormat === false ) {
			$outputFormat = 'LOCL';

			if ( Localizer::getInstance()->hasLocalTimeOffsetPreference( $user ) ) {
				$outputFormat .= '#TO';
			}
		}

		// Use LOCL formatting where appropriate (date)
		$dataValue->setOutputFormat( $outputFormat );

		// For a redirect, disable the DisplayTitle to show the original (aka source) page
		if ( $propertyValue->isValid() && $propertyValue->getDataItem()->getKey() == '_REDI' ) {
			$dataValue->setOption( 'smwgDVFeatures', ( $dataValue->getOption( 'smwgDVFeatures' ) & ~SMW_DV_WPV_DTITLE ) );
		}

		$html = $dataValue->getLongHTMLText( $linker );

		if ( $dataValue->getOption( DataValue::OPT_DISABLE_INFOLINKS, false ) === true ) {
			return $html;
		}

		$isCompactLink = $dataValue->getOption( DataValue::OPT_COMPACT_INFOLINKS, false );
		$noInfolinks = [ '_INST', '_SKEY' ];

		if ( in_array( $dataValue->getTypeID(), [ '_wpg', '_wpp', '__sob'] ) ) {
			$infolink = Infolink::newBrowsingLink( '+', $dataValue->getLongWikiText() );
			$infolink->setCompactLink( $isCompactLink );
			$html .= "&#160;" . $infolink->getHTML( $linker );
		} elseif ( $incoming && $propertyValue->isVisible() ) {
			$infolink = Infolink::newInversePropertySearchLink( '+', $dataValue->getTitle(), $propertyValue->getDataItem()->getLabel(), 'smwsearch' );
			$infolink->setCompactLink( $isCompactLink );
			$html .= "&#160;" . $infolink->getHTML( $linker );
		} elseif ( $dataValue->getProperty() instanceof DIProperty && !in_array( $dataValue->getProperty()->getKey(), $noInfolinks ) ) {
			$html .= $dataValue->getInfolinkText( SMW_OUTPUT_HTML, $linker );
		}

		return $html;
	}

	/**
	 * Figures out the label of the property to be used. For outgoing ones it is just
	 * the text, for incoming ones we try to figure out the inverse one if needed,
	 * either by looking for an explicitly stated one or by creating a default one.
	 *
	 * @since 2.5
	 *
	 * @param PropertyValue $property
	 * @param boolean $incoming
	 * @param boolean $showInverse
	 *
	 * @return string
	 */
	public static function getPropertyLabel( PropertyValue $propertyValue, $incoming = false, $showInverse = false ) {

		$proptext = null;

		$linker = smwfGetLinker();
		$property = $propertyValue->getDataItem();

		if ( $propertyValue->isVisible() ) {
			$propertyValue->setCaption( self::findPropertyLabel( $propertyValue, $incoming, $showInverse ) );
			$proptext = $propertyValue->getShortHTMLText( $linker ) . "\n";
		} elseif ( $property->getKey() == '_INST' ) {
			$proptext = $linker->specialLink( 'Categories', 'smw-category' );
		} elseif ( $property->getKey() == '_REDI' ) {
			$proptext = $linker->specialLink( 'Listredirects', 'isredirect' );
		}

		return $proptext;
	}

	private static function findPropertyLabel( PropertyValue $propertyValue, $incoming = false, $showInverse = false ) {

		$property = $propertyValue->getDataItem();
		$contextPage = $propertyValue->getContextPage();

		// Change caption for the incoming, Has query instance
		if ( $incoming && $property->getKey() === '_ASK' && strpos( $contextPage->getSubobjectName(), '_QUERY' ) === false ) {
			return self::addNonBreakingSpace( wfMessage( 'smw-query-reference-link-label' )->text() );
		}

		if ( !$incoming || !$showInverse ) {
			return self::addNonBreakingSpace( $propertyValue->getWikiValue() );
		}

		$inverseProperty = DataValueFactory::getInstance()->newPropertyValueByLabel( wfMessage( 'smw_inverse_label_property' )->text() );

		$dataItems = ApplicationFactory::getInstance()->getStore()->getPropertyValues(
			$property->getDiWikiPage(),
			$inverseProperty->getDataItem()
		);

		if ( $dataItems !== [] ) {
			$text = str_replace( '_', ' ', end( $dataItems )->getDBKey() );
		} else {
			$text = wfMessage( 'smw_inverse_label_default', $propertyValue->getWikiValue() )->text();
		}

		return self::addNonBreakingSpace( $text );
	}

	/**
	 * Replace the last two space characters with unbreakable spaces for beautification.
	 *
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return string
	 */
	public static function addNonBreakingSpace( $text ) {

		$nonBreakingSpace = html_entity_decode( '&#160;', ENT_NOQUOTES, 'UTF-8' );
		$text = preg_replace( '/[\s]/u', $nonBreakingSpace, $text, - 1, $count );

		if ( $count > 2) {
			return preg_replace( '/($nonBreakingSpace)/u', ' ', $text, max( 0, $count - 2 ) );
		}

		return $text;
	}

}