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

namespace SMW\DataValues;

use SMW\ApplicationFactory;
use SMW\DIProperty;
use SMW\Message;
use SMW\Parser\InTextAnnotationParser;
use SMW\PropertySpecificationLookup;
use SMWDataValue as DataValue;
use SMWDIBlob as DIBlob;
use SMWInfolink as Infolink;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class InfoLinksProvider {

	/**
	 * @var DataValue
	 */
	private $dataValue;

	/**
	 * @var PropertySpecificationLookup
	 */
	private $propertySpecificationLookup;

	/**
	 * @var Infolink[]
	 */
	protected $infoLinks = [];

	/**
	 * Used to control the addition of the standard search link.
	 * @var boolean
	 */
	private $hasSearchLink;

	/**
	 * Used to control service link creation.
	 * @var boolean
	 */
	private $hasServiceLinks;

	/**
	 * @var boolean
	 */
	private $enabledServiceLinks = true;

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

	/**
	 * @var boolean|array
	 */
	private $serviceLinkParameters = false;

	/**
	 * @var []
	 */
	private $browseLinks = [ '__sob' ];

	/**
	 * @since 2.4
	 *
	 * @param DataValue $dataValue
	 * @param PropertySpecificationLookup $propertySpecificationLookup
	 */
	public function __construct( DataValue $dataValue, PropertySpecificationLookup $propertySpecificationLookup ) {
		$this->dataValue = $dataValue;
		$this->propertySpecificationLookup = $propertySpecificationLookup;
	}

	/**
	 * @since 2.4
	 */
	public function init() {
		$this->infoLinks = [];
		$this->hasSearchLink = false;
		$this->hasServiceLinks = false;
		$this->enabledServiceLinks = true;
		$this->serviceLinkParameters = false;
		$this->compactLink = false;
	}

	/**
	 * @since 2.4
	 */
	public function disableServiceLinks() {
		$this->enabledServiceLinks = false;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $compactLink
	 */
	public function setCompactLink( $compactLink ) {
		$this->compactLink = (bool)$compactLink;
	}

	/**
	 * Adds a single SMWInfolink object to the infoLinks array.
	 *
	 * @since 2.4
	 *
	 * @param Infolink $link
	 */
	public function addInfolink( Infolink $infoLink ) {
		$this->infoLinks[] = $infoLink;
	}

	/**
	 * @since 2.4
	 *
	 * @param array|false $serviceLinkParameters
	 */
	public function setServiceLinkParameters( $serviceLinkParameters ) {
		$this->serviceLinkParameters = $serviceLinkParameters;
	}

	/**
	 * Return an array of SMWLink objects that provide additional resources
	 * for the given value. Captions can contain some HTML markup which is
	 * admissible for wiki text, but no more. Result might have no entries
	 * but is always an array.
	 *
	 * @since 2.4
	 */
	public function createInfoLinks() {

		if ( $this->infoLinks !== [] ) {
			return $this->infoLinks;
		}

		if ( !$this->dataValue->isValid() ) {
			return [];
		}

		// Avoid any localization when generating the value
		$this->dataValue->setOutputFormat( '' );

		$value = $this->dataValue->getWikiValue();
		$property = $this->dataValue->getProperty();

		// InTextAnnotationParser will detect :: therefore avoid link
		// breakage by encoding the string
		if ( strpos( $value, '::' ) !== false && !InTextAnnotationParser::hasMarker( $value ) ) {
			$value = str_replace( ':', '-3A', $value );
		}

		if ( in_array( $this->dataValue->getTypeID(), $this->browseLinks ) ) {
			$infoLink = Infolink::newBrowsingLink( '+', $this->dataValue->getLongWikiText() );
			$infoLink->setCompactLink( $this->compactLink );
		} elseif ( $property !== null ) {
			$infoLink = Infolink::newPropertySearchLink( '+', $property->getLabel(), $value );
			$infoLink->setCompactLink( $this->compactLink );
		}

		$this->infoLinks[] = $infoLink;
		$this->hasSearchLink = $this->infoLinks !== [];

		 // add further service links
		if ( !$this->hasServiceLinks && $this->enabledServiceLinks ) {
			$this->addServiceLinks();
		}

		return $this->infoLinks;
	}

	/**
	 * Return text serialisation of info links. Ensures more uniform layout
	 * throughout wiki (Factbox, Property pages, ...).
	 *
	 * @param integer $outputformat Element of the SMW_OUTPUT_ enum
	 * @param Linker|null $linker
	 *
	 * @return string
	 */
	public function getInfolinkText( $outputformat, $linker = null ) {

		$result = '';
		$first = true;
		$extralinks = [];

		foreach ( $this->dataValue->getInfolinks() as $link ) {

			if ( $outputformat === SMW_OUTPUT_WIKI ) {
				$text = $link->getWikiText();
			} else {
				$text = $link->getHTML( $linker );
			}

			// the comment is needed to prevent MediaWiki from linking
			// URL-strings together with the nbsps!
			if ( $first ) {
				$result .= ( $outputformat === SMW_OUTPUT_WIKI ? '<!-- -->  ' : '&#160;&#160;' ) . $text;
				$first = false;
			} else {
				$extralinks[] = $text;
			}
		}

		if ( $extralinks !== [] ) {
			$result .= smwfEncodeMessages( $extralinks, 'service', '', false );
		}

		// #1453 SMW::on/off will break any potential link therefore just don't even try
		return !InTextAnnotationParser::hasMarker( $result ) ? $result : '';
	}

	/**
	 * Servicelinks are special kinds of infolinks that are created from
	 * current parameters and in-wiki specification of URL templates. This
	 * method adds the current property's servicelinks found in the
	 * messages. The number and content of the parameters is depending on
	 * the datatype, and the service link message is usually crafted with a
	 * particular datatype in mind.
	 */
	public function addServiceLinks() {

		if ( $this->hasServiceLinks ) {
			return;
		}

		$dataItem = null;

		if ( $this->dataValue->getProperty() !== null ) {
			$dataItem = $this->dataValue->getProperty()->getDiWikiPage();
		}

		// No property known, or not associated with a page!
		if ( $dataItem === null ) {
			return;
		}

		$args = $this->serviceLinkParameters;

		if ( $args === false ) {
			return; // no services supported
		}

		// add a 0 element as placeholder
		array_unshift( $args, '' );

		$servicelinks = $this->propertySpecificationLookup->getSpecification(
			$dataItem,
			new DIProperty( '_SERV' )
		);

		foreach ( $servicelinks as $servicelink ) {
			$this->makeLink( $servicelink, $args );
		}

		$this->hasServiceLinks = true;
	}

	private function makeLink( $dataItem, $args ) {

		if ( !( $dataItem instanceof DIBlob ) ) {
			return;
		}

		 // messages distinguish ' ' from '_'
		$args[0] = 'smw_service_' . str_replace( ' ', '_', $dataItem->getString() );
		$text = Message::get( $args, Message::TEXT, Message::CONTENT_LANGUAGE );
		$links = preg_split( "/[\n][\s]?/u", $text );

		foreach ( $links as $link ) {
			$linkdat = explode( '|', $link, 2 );

			if ( count( $linkdat ) == 2 ) {
				$this->addInfolink( Infolink::newExternalLink( $linkdat[0], trim( $linkdat[1] ) ) );
			}
		}
	}

}