-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEmbed.php
More file actions
62 lines (50 loc) · 1.61 KB
/
SimpleEmbed.php
File metadata and controls
62 lines (50 loc) · 1.61 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
<?php
/**
*
* https://www.mediawiki.org/wiki/Extension:SimpleEmbed
*
* Example usage:
*
* <embed>{{PAGENAME}}</embed>
* <embed>http://localhost/mw/images/d/de/Sample.pdf</embed>;
* <embed>Sample.pdf</embed>
*
*/
$wgExtensionCredits['parserhook'][] = array(
'name' => 'SimpleEmbed',
# original 0.1 version from Kim Eik
'author' => 'Leonardo Martinho',
'version' => '0.2',
'url' => 'https://www.mediawiki.org/wiki/Extension:SimpleEmbed',
'description' => 'Allows for embedding files on a page',
);
$wgExtensionFunctions[] = 'registerEmbedHandler';
function registerEmbedHandler () {
$parser = \MediaWiki\MediaWikiServices::getInstance()->getParser();
$parser->setHook( 'embed', 'embed' );
}
function isValidUrl($url) {
return preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url);
}
function parseAsUrlOrWikiText($text, Parser $parser, PPFrame $frame) {
if (isValidUrl($text)){
return $text;
}
else{
$text = html_entity_decode($parser->recursiveTagParse($text, $frame));
if (!isValidUrl($text)) {
$file = \MediaWiki\MediaWikiServices::getInstance()->getRepoGroup()->findFile($text);
return $file ? $file->getFullUrl() : false;
}
return $text;
}
}
function embed ( $input, $argv, Parser $parser, PPFrame $frame ) {
$path = parseAsUrlOrWikiText($input, $parser, $frame);
if (!$path){
return "<span style=\"color: red;\">Invalid URI: $input</span>";
}
$width = isset($argv['width']) ? htmlspecialchars( $argv['width'] ) : '1000';
$height = isset($argv['height']) ? htmlspecialchars( $argv['height'] ) : '700';
return '<iframe src="'.$path.'" width="'.$width.'" height="'.$height.'"></iframe>';
}