forked from rotsee/LinkAttributes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkAttributes.body.php
112 lines (96 loc) · 2.24 KB
/
LinkAttributes.body.php
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
<?php
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkTarget;
class LinkAttributes {
private static $attrsAllowed=[
'rel',
'rev',
'charset ',
'type',
'hreflang',
'itemprop',
'itemscope',
'media',
'title',
'accesskey',
'target',
];
/**
* @param string|HtmlArmor &$text
* @param array &$attribs
*/
private static function doExtractAttributes ( &$text, &$attribs ) {
global $wgRequest;
if ( $wgRequest->getText( 'action' ) == 'edit' ) {
return false;
}
if ($text instanceof HtmlArmor) {
$_text = HtmlArmor::getHtml($text);
} else {
$_text = $text;
}
/* No user input */
if ( null === $_text )
return false;
/* Extract attributes, separated by | or ¦. /u is for unicode, to recognize the ¦.*/
$arr = preg_split( '/[|¦]/u', $_text);
$_text = array_shift( $arr );
if ($text instanceof HtmlArmor) {
$_text = new HtmlArmor($_text);
} else {
$text = $text;
}
foreach ( $arr as $a ) {
$pair = explode( '=', $a, 2);
/* Only go ahead if we have a aaa=bbb pattern, and aaa i an allowed attribute */
if ( isset( $pair[1] ) && in_array( $pair[0], self::$attrsAllowed ) ) {
/* Add to existing attribute, or create a new */
if ( isset( $attribs[$pair[0]] ) ) {
$attribs[$pair[0]] = $attribs[$pair[0]] . ' ' . $pair[1];
} else {
$attribs[$pair[0]] = $pair[1];
}
}
}
return true;
}
/**
* @link https://www.mediawiki.org/wiki/Manual:Hooks/LinkerMakeExternalLink
* @param string &$url
* @param string &$text
* @param string &$link
* @param string[] &$attribs
* @param string $linktype
* @return bool
*/
public static function ExternalLink (
&$url,
&$text,
&$link,
&$attribs
) {
self::doExtractAttributes ( $text, $attribs );
return true;
}
/**
* @link https://www.mediawiki.org/wiki/Manual:Hooks/HtmlPageLinkRendererEnd
* @param LinkRenderer $linkRenderer
* @param LinkTarget $target
* @param bool $isKnown
* @param string &$text
* @param string[] &$attribs
* @param string &$ret
* @return bool
*/
public static function InternalLink(
LinkRenderer $linkRenderer,
LinkTarget $target,
$isKnown,
&$text,
&$attribs,
&$ret
) {
static::doExtractAttributes( $text, $attribs );
return true;
}
}