-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInlineHtmlGalleyPlugin.php
More file actions
248 lines (234 loc) · 7.32 KB
/
InlineHtmlGalleyPlugin.php
File metadata and controls
248 lines (234 loc) · 7.32 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* @file plugins/generic/inlineHtmlGalley/InlineHtmlGalleyPlugin.inc.php
*
* Copyright (c) University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v2 or later. For full terms see the file docs/COPYING.
*
* @class InlineHtmlGalleyPlugin
* @ingroup plugins_generic_inlineHtmlGalley
*
* @brief Class for InlineHtmlGalley plugin
*/
namespace APP\plugins\generic\inlineHtmlGalley;
use PKP\plugins\PluginRegistry;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\core\JSONMessage;
use PKP\plugins\Hook;
use PKP\core\Core;
use Exception;
use DOMDocument;
use DOMXPath;
use APP\plugins\generic\htmlArticleGalley\HtmlArticleGalleyPlugin;
use APP\plugins\generic\inlineHtmlGalley\InlineHtmlGalleyBlockPlugin;
use APP\plugins\generic\inlineHtmlGalley\InlineHtmlGalleySidebarBlockPlugin;
use APP\plugins\generic\inlineHtmlGalley\InlineHtmlGalleySettingsForm;
use APP\template\TemplateManager;
include "InlineHtmlGalleySidebarBlockPlugin.php";
class InlineHtmlGalleyPlugin extends HtmlArticleGalleyPlugin {
/**
* @see Plugin::register()
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!$success) return false;
if ($success && $this->getEnabled()) {
// Load this plugin as a block plugin as well
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyAuthorsSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyKeywordsSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyDoiSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyCoverImageSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyDetailsSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyPublishedDateSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyHowToCiteSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyLicenseSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyReferencesSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
PluginRegistry::register(
'blocks',
new InlineHtmlGalleyGalleysSidebarBlockPlugin($this->getName(), $this->getPluginPath()),
$this->getPluginPath()
);
Hook::add('ArticleHandler::view', array($this, 'articleViewCallback'), HOOK_SEQUENCE_LATE);
Hook::add('TemplateResource::getFilename', array($this, '_overridePluginTemplates'), HOOK_SEQUENCE_CORE);
}
return true;
}
/**
* Get the display name of this plugin.
* @return String
*/
function getDisplayName() {
return __('plugins.generic.inlineHtmlGalley.displayName');
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return __('plugins.generic.inlineHtmlGalley.description');
}
/**
* Present the article wrapper page.
* @param string $hookName
* @param array $args
*/
function articleViewCallback($hookName, $args) {
if ($hookName == "ArticleHandler::view") {
$request =& $args[0];
$issue =& $args[1];
$article =& $args[2];
$publication = $args[3];
$galleys = $article->getGalleys();
if (!$galleys) return false;
foreach ($galleys as $galley) {
if ($galley->getFileType() == 'text/html') {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
'issue' => $issue,
'article' => $article,
'publication' => $publication,
'galley' => $galley,
'orcidIcon' => $this->getOrcidIcon()
));
$inlineHtmlGalley = $this->_getHTMLContents($request, $galley);
$inlineHtmlGalleyBody = $this->_extractBodyContents($inlineHtmlGalley, $request->getContext()->getId());
$templateMgr->assign('inlineHtmlGalley', $inlineHtmlGalleyBody);
$templateMgr->display($this->getTemplateResource('displayInline.tpl'));
return true;
}
}
}
return false;
}
/**
* Return string containing the contents of the HTML body
* @param $html string
* @param $contextId int
* @return string
*/
function _extractBodyContents($html, $contextId) {
$bodyContent = '';
try {
if (!function_exists('libxml_use_internal_errors') || !class_exists('DOMDocument') || !class_exists('DOMXPath')) {
throw new Exception('Missing libxml/dom requirements');
}
$errorsEnabled = libxml_use_internal_errors();
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = $this->getSetting($contextId, 'xpath');
if (empty($xpath)) {
$tags = $dom->getElementsByTagName('body');
foreach ($tags as $body) {
foreach ($body->childNodes as $child) {
$bodyContent .= $dom->saveHTML($child);
}
break;
}
} else {
$domXpath = new DOMXPath($dom);
$tags = $domXpath->query($xpath);
foreach ($tags as $tag) {
$bodyContent .= $dom->saveHTML($tag);
}
}
libxml_use_internal_errors($errorsEnabled);
} catch (Exception $e) {
$html = preg_replace('/.*<body[^>]*>/isA', '', $html);
$html = preg_replace('/<\/body\s*>.*$/isD', '', $html);
$bodyContent = $html;
}
return $bodyContent;
}
/**
* Return a string of the ORCiD SVG icon
*
* @return string
*/
function getOrcidIcon() {
$path = Core::getBaseDir() . '/' . $this->getPluginPath() . '/templates/images/orcid.svg';
return file_exists($path) ? file_get_contents($path) : '';
}
/**
* @copydoc Plugin::manage()
*/
function manage($args, $request) {
if ($request->getUserVar('verb') == 'settings') {
$settingsForm = new InlineHtmlGalleySettingsForm($this, $request->getContext()->getId());
if ($request->getUserVar('save')) {
$settingsForm->readInputData();
if ($settingsForm->validate()) {
$settingsForm->execute();
return new JSONMessage(true);
}
} else {
$settingsForm->initData();
}
return new JSONMessage(true, $settingsForm->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $verb) {
$router = $request->getRouter();
return array_merge(
$this->getEnabled()?array(
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array('verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic')),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
):array(),
parent::getActions($request, $verb)
);
}
}