-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alink_body.php
executable file
·181 lines (140 loc) · 4 KB
/
Alink_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
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
<?php
class Alink {
private static $attrs_ref = array( "href", "rel", "target", "class", "id", "content", "name", "itemprop" );
private static $attrs_img_ref = array( "src", "rel", "title", "alt", "class", "id", "content", "itemprop" );
private static $attrs_like = array( "data-" );
private static $protocols = array( "https://", "http://", "ftp://" );
/**
* @param $parser Parser
* @param $frame PPFrame
* @param $args array
* @return string
*/
public static function process_alink( &$parser, $frame, $args ) {
$attrs = array();
$text = "";
// Default urlencode
$urlencode = true;
// Process page
$noprefix = false;
foreach ( $args as $arg ) {
$arg_clean = trim( $frame->expand( $arg ) );
$arg_proc = explode( "=", $arg_clean, 2 );
if ( count( $arg_proc ) == 1 ){
// If arg urlencode -> trigger
if ( trim( $arg_proc[0] ) == "nourlencode" ) {
$urlencode = false;
} elseif ( trim( $arg_proc[0] ) == "noprefix" ) {
$noprefix = true;
} else {
$text = trim( $arg_proc[0] );
}
} else {
if ( in_array( trim( $arg_proc[0] ), self::$attrs_ref ) ) {
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] );
}
foreach ( self::$attrs_like as $attr_like ) {
if ( strpos( trim( $arg_proc[0] ), $attr_like ) === 0 ) {
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] );
}
}
}
}
// Code for dealing with internal - external
$external = 0;
if ( isset( $attrs["href"] ) ) {
foreach ( self::$protocols as $protocol ) {
$detect = strpos( $attrs["href"], $protocol );
if ( is_int( $detect ) ) {
$external = 1;
}
}
}
// Saving link for modifications
$link = null;
if ( isset( $attrs["href"] ) ) {
$link = $attrs["href"];
if ( $external == 0 ) {
#Handling internal links
$anchor_detect = strpos( $attrs["href"], "#" );
if ( ! is_int( $anchor_detect ) || $anchor_detect != 0 ) {
global $wgArticlePath;
$page = $attrs["href"];
$page = str_replace( " ", "_", $page );
$attrs["href"] = $wgArticlePath;
if ( $urlencode ) {
$page = urlencode( $page );
}
$attrs["href"] = str_replace( "$1", $page, $attrs["href"] );
}
}
}
// If no text, use href
if ( $text == "" && ! is_null( $link ) ) {
$text = $link;
// No prefixed title
if ( $noprefix ) {
$title = Title::newFromText( $link );
if ( $title ) {
$text = $title->getText();
}
}
}
$tag = Html::element(
'a',
$attrs,
$text
);
return $parser->insertStripItem( $tag, $parser->mStripState );
}
/**
* @param $parser Parser
* @param $frame PPFrame
* @param $args array
* @return string
*/
public static function process_aimg( &$parser, $frame, $args ) {
// TODO: More control of img sources should be allowed
$attrs = array();
foreach ( $args as $arg ) {
$arg_clean = trim( $frame->expand( $arg ) );
$arg_proc = explode( "=", $arg_clean, 2 );
if ( count( $arg_proc ) == 1 ){
} else {
if ( in_array( trim( $arg_proc[0] ), self::$attrs_img_ref ) ) {
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] );
}
foreach ( self::$attrs_like as $attr_like ) {
if ( strpos( $arg_proc[0], $attr_like ) == 0 ) {
$attrs[ trim( $arg_proc[0] ) ] = trim( $arg_proc[1] );
}
}
}
}
// Code for dealing with internal - external
$external = 0;
if ( isset( $attrs["src"] ) ) {
foreach ( self::$protocols as $protocol ) {
$detect = strpos( $attrs["src"], $protocol );
if ( is_int( $detect ) ) {
$external = 1;
}
}
}
if ( $external == 0 ) {
if ( isset( $attrs["src"] ) ) {
$page = $attrs["src"];
$page = str_replace( " ", "_", $page );
$file = wfFindFile( $page );
if ( $file && $file->exists() ) {
$attrs["src"] = $file->getUrl();
}
}
}
$tag = Html::element(
'img',
$attrs
);
return $parser->insertStripItem( $tag, $parser->mStripState );
}
}