Repository navigation
Expand file tree
/
Copy pathCharacterEscapes.hooks.php
More file actions
132 lines (117 loc) · 3.12 KB
/
Copy pathCharacterEscapes.hooks.php
File metadata and controls
132 lines (117 loc) · 3.12 KB
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
<?php
/**
* Character Escapes
* Character Escapes Hooks
*
* @author David M. Sledge
* @package Character Escapes
* @license GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
* @link http://www.mediawiki.org/wiki/Extension:Character_Escapes
*
**/
class CharacterEscapesHooks {
/**
* Available Parser Hook Tags
*
* @var array
*/
public static $tags = [
"esc" => "charEsc",
"unesc" => "charUnesc"
];
/**
* Sets up this extensions parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit(Parser &$parser) {
foreach (self::$tags as $hook => $method) {
$parser->setHook($hook, array(
__CLASS__,
$method
));
}
return true;
}
public static function unstrip($input, $args, $parser) {
$regex = "/\x07UNIQ[0-9a-fA-F]{1,16}-(" . implode('|', array_keys(self::$tags)) . ")-[0-9a-fA-F]{8}-QINU\x07/";
// find all the unique identifiers for the esc tag
preg_match_all($regex, $input, $strippedTags);
$unstrippedTags = array();
// unstrip each unique identifier
foreach ($strippedTags[0] as $strippedTag)
$unstrippedTags[$strippedTag] = $parser->mStripState->unstripGeneral($strippedTag);
// replace each unique identifier with the unstripped text
$input = strtr($input, $unstrippedTags);
return $input;
}
public static function charEsc($input, $args, $parser) {
$input = self::unstrip($input, $args, $parser);
return self::escChars($input);
}
// these character escapes are so the nested parser functions and tags are
// not called before the loops are performed. Many thanks to Gero Scholz
// (a Dynamic Page List 2 author) for the basic idea. This implementation
// is different in that it uses a convention similar to what is seen in
// many programming languages.
public static function escChars($text) {
// The following character escape sequences are used to avoid
// premature tag expansion and parser function execution.
//
// character sequence escape sequence
// {{ \o
// }} \c
// | \p
// < \l
// > \g
// newline \n
// \ \\
// prefix the pre-existing escape sequences with backslashes
$text = str_replace(array(
"\\",
"{{",
"}}",
"|",
"<",
">",
"\n"
), array(
"\\\\",
"\\o",
"\c",
"\\p",
"\\l",
"\\g",
"\\n"
), $text);
return $text;
}
public static function charUnesc($input, $args, $parser) {
$input = self::unstrip($input, $args, $parser);
return self::unescChars($input);
}
public static function unescChars($text) {
// since we're dealing with regular expressions and strings,
// the backslash character must be double escaped (4:1 ratio)
$text = preg_replace(array(
"/(?<!\\\\)((\\\\\\\\)*)\\\\o/",
"/(?<!\\\\)((\\\\\\\\)*)\\\\c/",
"/(?<!\\\\)((\\\\\\\\)*)\\\\p/",
"/(?<!\\\\)((\\\\\\\\)*)\\\\l/",
"/(?<!\\\\)((\\\\\\\\)*)\\\\g/",
"/(?<!\\\\)((\\\\\\\\)*)\\\\n/"
), array(
"$1{{",
"$1}}",
"$1|",
"$1<",
"$1>",
"$1\n"
), $text);
$text = str_replace("\\\\", "\\", $text);
return $text;
}
}
?>