This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPDFEmbed.hooks.php
107 lines (94 loc) · 2.71 KB
/
PDFEmbed.hooks.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
<?php
/**
* PDFEmbed
* PDFEmbed Hooks
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
class PDFEmbed {
/**
* 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) {
$parser->setHook('pdf', 'PDFEmbed::generateTag');
return true;
}
/**
* Generates the PDF object tag.
*
* @access public
* @param string Namespace prefixed article of the PDF file to display.
* @param array Arguments on the tag.
* @param object Parser object.
* @param object PPFrame object.
* @return string HTML
*/
static public function generateTag($file, $args = [], Parser $parser, PPFrame $frame) {
global $wgPdfEmbed, $wgRequest, $wgUser;
$parser->disableCache();
if (strstr($file, '{{{') !== false) {
$file = $parser->recursiveTagParse($file, $frame);
}
if ($wgRequest->getVal('action') == 'edit' || $wgRequest->getVal('action') == 'submit') {
$user = $wgUser;
} else {
$user = User::newFromName($parser->getRevisionUser());
}
if ($user === false) {
return self::error('embed_pdf_invalid_user');
}
if (!$user->isAllowed('embed_pdf')) {
return self::error('embed_pdf_no_permission');
}
if (empty($file) || !preg_match('#(.+?)\.pdf#is', $file)) {
return self::error('embed_pdf_blank_file');
}
$file = wfFindFile(Title::newFromText($file));
$width = (array_key_exists('width', $args) ? intval($args['width']) : intval($wgPdfEmbed['width']));
$height = (array_key_exists('height', $args) ? intval($args['height']) : intval($wgPdfEmbed['height']));
$page = (array_key_exists('page', $args) ? intval($args['page']) : 1);
if ($file !== false) {
return self::embed($file, $width, $height, $page);
} else {
return self::error('embed_pdf_invalid_file');
}
}
/**
* Returns a HTML object as string.
*
* @access private
* @param object File object.
* @param integer Width of the object.
* @param integer Height of the object.
* @return string HTML object.
*/
static private function embed(File $file, $width, $height, $page) {
return Html::rawElement(
'iframe',
[
'width' => $width,
'height' => $height,
'src' => $file->getFullUrl().'#page='.$page,
'style' => 'max-width: 100%;'
]
);
}
/**
* Returns a standard error message.
*
* @access private
* @param string Error message key to display.
* @return string HTML error message.
*/
static private function error($messageKey) {
return Xml::span(wfMessage($messageKey)->plain(), 'error');
}
}