Completed
Push — master ( f835ba...dee201 )
by mw
47:53 queued 08:25
created

InTextAnnotationSanitizer::decodeSquareBracket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 2.5
8
 *
9
 * @author mwjames
10
 */
11
class InTextAnnotationSanitizer {
12
13
	/**
14
	 * @since 2.5
15
	 *
16
	 * @param string $text
17
	 *
18
	 * @return text
19
	 */
20
	public static function decodeSquareBracket( $text ) {
21
		return str_replace( array( '%5B', '%5D' ), array( '[', ']' ), $text );
22
	}
23
24
	/**
25
	 * @since 2.5
26
	 *
27
	 * @param string $text
28
	 *
29
	 * @return text
30
	 */
31
	public static function obscureAnnotation( $text ) {
32
		return preg_replace_callback(
33
			InTextAnnotationParser::getRegexpPattern( false ),
34
			function( array $matches ) {
35
				return str_replace( '[', '&#x005B;', $matches[0] );
36
			},
37
			self::decodeSquareBracket( $text )
38
		);
39
	}
40
41
	/**
42
	 * @since 2.5
43
	 *
44
	 * @param string $text
45
	 *
46
	 * @return text
47
	 */
48
	public static function removeAnnotation( $text ) {
49
		return preg_replace_callback(
50
			InTextAnnotationParser::getRegexpPattern( false ),
51
			'self::doRemoveAnnotation',
52
			self::decodeSquareBracket( $text )
53
		);
54
	}
55
56
	private static function doRemoveAnnotation( array $matches ) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
57
58
		$caption = false;
59
		$value = '';
60
61
		// #1453
62
		if ( $matches[0] === InTextAnnotationParser::OFF || $matches[0] === InTextAnnotationParser::ON ) {
63
			return false;
64
		}
65
66
		// Strict mode matching
67
		if ( array_key_exists( 1, $matches ) ) {
68
			if ( strpos( $matches[1], ':' ) !== false && isset( $matches[2] ) ) {
69
				list( $matches[1], $matches[2] ) = explode( '::', $matches[1] . '::' . $matches[2], 2 );
70
			}
71
		}
72
73
		if ( array_key_exists( 2, $matches ) ) {
74
75
			// #1747
76
			if ( strpos( $matches[1], '|' ) !== false ) {
77
				return $matches[0];
78
			}
79
80
			$parts = explode( '|', $matches[2] );
81
			$value = array_key_exists( 0, $parts ) ? $parts[0] : '';
82
			$caption = array_key_exists( 1, $parts ) ? $parts[1] : false;
83
		}
84
85
		// #...
86
		if ( $value === '@@@' ) {
87
			$value = '';
88
		}
89
90
		return $caption !== false ? $caption : $value;
91
	}
92
93
}
94