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

namespace SMW\Parser;

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

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 * @param InTextAnnotationParser $parser
	 *
	 * @return text
	 */
	public static function findAndEncodeLinks( $text, InTextAnnotationParser $parser ) {

		// #2193
		// Use &#x005B; instead of &#91; to distinguish it from the MW's Sanitizer
		// who uses the same decode sequence and avoid issues when removing links
		// after obfuscation

		// #2671
		// Use &#x005D; instead of &#93; (]) since CiteExtension use the later as
		// encoding for the ref brackets

		// Filter simple [ ... ] from [[ ... ]] links and ensure to find the correct
		// start and end in case of [[Foo::[[Bar]]]] or [[Foo::[http://example.org/foo]]]
		$text = str_replace(
			[ '[', ']', '&#x005B;&#x005B;', '&#x005D;&#x005D;&#x005D;&#x005D;', '&#x005D;&#x005D;&#x005D;', '&#x005D;&#x005D;' ],
			[ '&#x005B;', '&#x005D;', '[[', ']]]]', '&#x005D;]]', ']]' ],
			$text
		);

		// Deep nesting is NOT supported as in [[Foo::[[abc]] [[Bar::123[[abc]] ]] ]]
		return self::matchAndReplace( $text, $parser );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public static function removeLinkObfuscation( $text ) {

		$from = [ '&#x005B;', '&#x005D;', '&#124;' ];
		$to = [ '[', ']', '|' ];

		return str_replace( $from, $to,	$text );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public static function encodeLinks( $text ) {
		return str_replace(
			[ '[', ']', '|' ],
			[ '&#x005B;', '&#x005D;', '&#124;' ],
			$text
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public static function decodeSquareBracket( $text ) {
		return str_replace( [ '%5B', '%5D' ], [ '[', ']' ], $text );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public static function obfuscateAnnotation( $text ) {
		return preg_replace_callback(
			LinksProcessor::getRegexpPattern( false ),
			function( array $matches ) {
				return str_replace( '[', '&#91;', $matches[0] );
			},
			self::decodeSquareBracket( $text )
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param string $text
	 *
	 * @return text
	 */
	public static function removeAnnotation( $text ) {

		if ( strpos( $text, '::' ) === false && strpos( $text, ':=' ) === false ) {
			return $text;
		}

		return preg_replace_callback(
			LinksProcessor::getRegexpPattern( false ),
			'self::doRemoveAnnotation',
			self::decodeSquareBracket( $text )
		);
	}

	private static function doRemoveAnnotation( array $matches ) {

		$caption = false;
		$value = '';

		// #1453
		if ( $matches[0] === InTextAnnotationParser::OFF || $matches[0] === InTextAnnotationParser::ON ) {
			return false;
		}

		// Strict mode matching
		if ( array_key_exists( 1, $matches ) ) {
			if ( strpos( $matches[1], ':' ) !== false && isset( $matches[2] ) ) {
				list( $matches[1], $matches[2] ) = explode( '::', $matches[1] . '::' . $matches[2], 2 );
			}
		}

		if ( array_key_exists( 2, $matches ) ) {

			// #1747
			if ( strpos( $matches[1], '|' ) !== false ) {
				return $matches[0];
			}

			$parts = explode( '|', $matches[2] );
			$value = array_key_exists( 0, $parts ) ? $parts[0] : '';
			$caption = array_key_exists( 1, $parts ) ? $parts[1] : false;
		}

		// #1855
		if ( $value === '@@@' ) {
			$value = '';
		}

		return $caption !== false ? $caption : $value;
	}

	private static function matchAndReplace( $text, $parser ) {

		/**
		 * @see http://blog.angeloff.name/post/2012/08/05/php-recursive-patterns/
		 *
		 * \[{2}         # find the first opening '[['.
		 *   (?:         # start a new group, this is so '|' below does not apply/affect the opening '['.
		 *     [^\[\]]+  # skip ahead happily if no '[' or ']'.
		 *     |         #   ...otherwise...
		 *     (?R)      # we may be at the start of a new group, repeat whole pattern.
		 *     )
		 *   *           # nesting can be many levels deep.
		 * \]{2}         # finally, expect a balanced closing ']]'
		 */
		preg_match_all("/\[{2}(?:[^\[\]]+|(?R))*\]{2}/is", $text, $matches );
		$isOffAnnotation = false;

		// At this point we distinguish between a normal [[Foo::bar]] annotation
		// and a compound construct such as [[Foo::[[Foobar::Bar]] ]] and
		// [[Foo::[http://example.org/foo foo] [[Foo::123|Bar]] ]].
		//
		// Only the compound is being processed and matched as we require to
		// identify the boundaries of the enclosing annotation
		foreach ( $matches[0] as $match ) {

			// Ignore simple links like `[[:Property:Has URL|Has URL]]` but
			// do parse `[[Has description::[[foo]][[:bar]]]]` (:= legacy notation)
			if ( strpos( $match, '[[' ) !== false && strpos( $match, '::' ) === false && strpos( $match, ':=' ) === false ) {
				continue;
			}

			// Remember whether the text contains OFF/ON marker (added by
			// recursive parser, template, embedded result printer)
			if ( $isOffAnnotation === false ) {
				$isOffAnnotation = $match === InTextAnnotationParser::OFF;
			}

			$annotationOpenNum = substr_count( $match, '[[' );

			// Only engage if the match contains more than one [[ :: ]] pair
			if ( $annotationOpenNum > 1 ) {
				$replace = self::replace( $match, $parser, $isOffAnnotation );
				$text = str_replace( $match, $replace, $text );
			}
		}

		return $text;
	}

	private static function replace( $match, $parser, $isOffAnnotation = false ) {

		// Remove the Leading and last square bracket to avoid distortion
		// during the annotation parsing
		$match = substr( substr( $match, 2 ), 0, -2 );

		// Restore OFF/ON for the recursive processing
		if ( $isOffAnnotation === true ) {
			$match = InTextAnnotationParser::OFF . $match . InTextAnnotationParser::ON;
		}

		// Only match annotations of style [[...::...]] during a recursive
		// obfuscation process, any other processing is being done by the
		// InTextAnnotation parser hereafter
		//
		// [[Foo::Bar]] annotation therefore run a pattern match and
		// obfuscate the returning [[, |, ]] result
		$replace = self::encodeLinks( preg_replace_callback(
			LinksProcessor::getRegexpPattern( false ),
			[ $parser, 'preprocess' ],
			$match
		) );

		// Restore the square brackets
		return '[[' . $replace . ']]';
	}

}