summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Parser/LinksProcessor.php
blob: f6318e70a3821e82a7d7407d9de7dcce6d46a8a6 (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
<?php

namespace SMW\Parser;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class LinksProcessor {

	/**
	 * Internal state for switching SMW link annotations off/on during parsing
	 * ([[SMW::on]] and [[SMW:off]])
	 *
	 * @var boolean
	 */
	private $isAnnotation = true;

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

	/**
	 * Whether a strict interpretation (e.g [[property::value:partOfTheValue::alsoPartOfTheValue]])
	 * or a more loose interpretation (e.g. [[property1::property2::value]]) for
	 * annotations is expected.
	 *
	 * @since 2.3
	 *
	 * @param boolean $isStrictMode
	 */
	public function isStrictMode( $isStrictMode ) {
		$this->isStrictMode = (bool)$isStrictMode;
	}

	/**
	 * @since 2.5
	 *
	 * @return boolean
	 */
	public function isAnnotation() {
		return $this->isAnnotation;
	}

	/**
	 * $smwgLinksInValues (default = false) determines which regexp pattern
	 * is returned, either a more complex (lib PCRE may cause segfaults if text
	 * is long) or a simpler (no segfaults found for those, but no links
	 * in values) pattern.
	 *
	 * If enabled (SMW accepts inputs like [[property::Some [[link]] in value]]),
	 * this may lead to PHP crashes (!) when very long texts are
	 * used as values. This is due to limitations in the library PCRE that
	 * PHP uses for pattern matching.
	 *
	 * @since 1.9
	 *
	 * @param boolean $linksInValues
	 *
	 * @return string
	 */
	public static function getRegexpPattern( $linksInValues = false ) {

		if ( $linksInValues ) {
			return '/\[\[             # Beginning of the link
				(?:([^:][^]]*):[=:])+ # Property name (or a list of those)
				(                     # After that:
				  (?:[^|\[\]]         #   either normal text (without |, [ or ])
				  |\[\[[^]]*\]\]      #   or a [[link]]
				  |\[[^]]*\]          #   or an [external link]
				)*)                   # all this zero or more times
				(?:\|([^]]*))?        # Display text (like "text" in [[link|text]]), optional
				\]\]                  # End of link
				/xu';
		}

		return '/\[\[             # Beginning of the link
			(?:([^:][^]]*):[=:])+ # Property name (or a list of those)
			([^\[\]]*)            # content: anything but [, |, ]
			\]\]                  # End of link
			/xu';
	}

	/**
	 * A method that precedes the process method, it takes care of separating
	 * value and caption (instead of leaving this to a more complex regexp).
	 *
	 * @since 1.9
	 *
	 * @param array $semanticLink expects (linktext, properties, value|caption)
	 *
	 * @return string
	 */
	public function preprocess( array $semanticLink ) {

		$value = '';
		$caption = false;

		if ( array_key_exists( 2, $semanticLink ) ) {

			// #1747 avoid a mismatch on an annotation like [[Foo|Bar::Foobar]]
			// where the left part of :: is split and would contain "Foo|Bar"
			// hence this type is categorized as no value annotation
			if ( strpos( $semanticLink[1], '|' ) !== false ) {
				return $semanticLink[0];
			}

			$parts = explode( '|', $semanticLink[2] );

			if ( array_key_exists( 0, $parts ) ) {
				$value = $parts[0];
			}
			if ( array_key_exists( 1, $parts ) ) {
				$caption = $parts[1];
			}
		}

		if ( $caption !== false ) {
			return [ $semanticLink[0], $semanticLink[1], $value, $caption ];
		}

		return [ $semanticLink[0], $semanticLink[1], $value ];
	}

	/**
	 * Function strips out the semantic attributes from a wiki link.
	 *
	 * @since 1.9
	 *
	 * @param array $semanticLink expects (linktext, properties, value|caption)
	 *
	 * @return string
	 */
	public function process( array $semanticLink ) {

		$valueCaption = false;
		$property = '';
		$value = '';

		if ( array_key_exists( 1, $semanticLink ) ) {

			// Use case [[Foo::=Bar]] (:= being the legacy notation < 1.4) where
			// the regex splits it into `Foo:` and `Bar` loosing `=` from the value.
			// Restore the link to its previous form of `Foo::=Bar` and reapply
			// a simple split.
			if( strpos( $semanticLink[0], '::=' ) && substr( $semanticLink[1], -1 ) == ':' ) {
				list( $semanticLink[1], $semanticLink[2] ) = explode( '::', $semanticLink[1] . ':=' . $semanticLink[2], 2 );
			}

			// #1252 Strict mode being disabled for support of multi property
			// assignments (e.g. [[property1::property2::value]])

			// #1066 Strict mode is to check for colon(s) produced by something
			// like [[Foo::Bar::Foobar]], [[Foo:::0049 30 12345678]]
			// In case a colon appears (in what is expected to be a string without a colon)
			// then concatenate the string again and split for the first :: occurrence
			// only
			if ( $this->isStrictMode && strpos( $semanticLink[1], ':' ) !== false && isset( $semanticLink[2] ) ) {
				list( $semanticLink[1], $semanticLink[2] ) = explode( '::', $semanticLink[1] . '::' . $semanticLink[2], 2 );
			}

			$property = $semanticLink[1];
		}

		if ( array_key_exists( 2, $semanticLink ) ) {
			$value = $semanticLink[2];
		}

		$value = LinksEncoder::removeLinkObfuscation( $value );

		if ( $value === '' ) { // silently ignore empty values
			return '';
		}

		if ( $property == 'SMW' ) {
			return $this->setAnnotation( $value );
		}

		if ( array_key_exists( 3, $semanticLink ) ) {
			$valueCaption = $semanticLink[3];
		}

		// Extract annotations and create tooltip.
		$properties = preg_split( '/:[=:]/u', $property );

		return [ $properties, $value, $valueCaption ];
	}

	private function setAnnotation( $value ) {

		switch ( $value ) {
			case 'on':
				$this->isAnnotation = true;
				break;
			case 'off':
				$this->isAnnotation = false;
				break;
		}

		return '';
	}

}