forked from pkp/googleScholar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleScholarPlugin.php
254 lines (228 loc) · 11.9 KB
/
GoogleScholarPlugin.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
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
249
250
251
252
253
254
<?php
/**
* @file plugins/generic/googleScholar/GoogleScholarPlugin.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2003-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class GoogleScholarPlugin
*
* @ingroup plugins_generic_googleScholar
*
* @brief Inject Google Scholar meta tags into submission views to facilitate indexing.
*/
namespace APP\plugins\generic\googleScholar;
use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
use APP\submission\Submission;
use APP\template\TemplateManager;
use PKP\citation\CitationDAO;
use PKP\core\PKPApplication;
use PKP\db\DAORegistry;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class GoogleScholarPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
Hook::add('ArticleHandler::view', $this->submissionView(...));
Hook::add('PreprintHandler::view', $this->submissionView(...));
}
return true;
}
return false;
}
/**
* Get the name of the settings file to be installed on new context
* creation.
*
* @return string
*/
public function getContextSpecificPluginSettingsFile()
{
return $this->getPluginPath() . '/settings.xml';
}
/**
* Inject Google Scholar metadata into submission landing page view
*
* @param string $hookName
* @param array $args
*
* @return boolean
*/
public function submissionView($hookName, $args)
{
$application = Application::get();
$applicationName = $application->getName();
/** @var Request */
$request = $args[0];
if ($applicationName == 'ojs2') {
$issue = $args[1];
$submission = $args[2];
$submissionPath = 'article';
}
if ($applicationName == 'ops') {
$submission = $args[1];
$submissionPath = 'preprint';
}
/** @var Submission $submission */
$requestArgs = $request->getRequestedArgs();
$context = $request->getContext();
// Only add Google Scholar metadata tags to the canonical URL for the latest version
// See discussion: https://github.com/pkp/pkp-lib/issues/4870
if (count($requestArgs) > 1 && $requestArgs[1] === 'version') {
return false;
}
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addHeader('googleScholarRevision', '<meta name="gs_meta_revision" content="1.1"/>');
// Context identification
if ($applicationName == 'ojs2') {
$templateMgr->addHeader('googleScholarJournalTitle', '<meta name="citation_journal_title" content="' . htmlspecialchars($context->getName($context->getPrimaryLocale())) . '"/>');
if (($abbreviation = $context->getData('abbreviation', $context->getPrimaryLocale())) || ($abbreviation = $context->getData('acronym', $context->getPrimaryLocale()))) {
$templateMgr->addHeader('googleScholarJournalAbbrev', '<meta name="citation_journal_abbrev" content="' . htmlspecialchars($abbreviation) . '"/>');
}
if (($issn = $context->getData('onlineIssn')) || ($issn = $context->getData('printIssn')) || ($issn = $context->getData('issn'))) {
$templateMgr->addHeader('googleScholarIssn', '<meta name="citation_issn" content="' . htmlspecialchars($issn) . '"/> ');
}
}
if ($applicationName == 'ops') {
$templateMgr->addHeader('googleScholarPublisher', '<meta name="citation_publisher" content="' . htmlspecialchars($context->getName($context->getPrimaryLocale())) . '"/>');
}
$publication = $submission->getCurrentPublication();
$publicationLocale = $publication->getData('locale');
$submissionBestId = $publication->getData('urlPath') ?? $submission->getId();
// Contributors
$authors = $publication->getData('authors');
foreach ($authors as $i => $author) {
$templateMgr->addHeader('googleScholarAuthor' . $i, '<meta name="citation_author" content="' . htmlspecialchars($author->getFullName(false, false, $publicationLocale)) . '"/>');
foreach($author->getLocalizedAffiliations($publicationLocale) as $affiliation) {
if (!empty($affiliation['name'])) {
$templateMgr->addHeader(
'googleScholarAuthor' . $i . 'Affiliation' . $affiliation['id'],
'<meta name="citation_author_institution" content="' . htmlspecialchars($affiliation['name']) . '"/>'
);
}
}
}
// Submission title
$templateMgr->addHeader('googleScholarTitle', '<meta name="citation_title" content="' . htmlspecialchars($publication->getLocalizedFullTitle($publicationLocale)) . '"/>');
$templateMgr->addHeader('googleScholarLanguage', '<meta name="citation_language" content="' . htmlspecialchars(substr($publicationLocale, 0, 2)) . '"/>');
// Submission publish date and issue information
if ($applicationName == 'ojs2') {
if (($datePublished = $publication->getData('datePublished')) && (!$issue || !$issue->getYear() || $issue->getYear() == date('Y', strtotime($datePublished)))) {
$templateMgr->addHeader('googleScholarDate', '<meta name="citation_date" content="' . date('Y/m/d', strtotime($datePublished)) . '"/>');
} elseif ($issue && $issue->getYear()) {
$templateMgr->addHeader('googleScholarDate', '<meta name="citation_date" content="' . htmlspecialchars($issue->getYear()) . '"/>');
} elseif ($issue && ($datePublished = $issue->getDatePublished())) {
$templateMgr->addHeader('googleScholarDate', '<meta name="citation_date" content="' . date('Y/m/d', strtotime($datePublished)) . '"/>');
}
if ($issue) {
if ($issue->getShowVolume()) {
$templateMgr->addHeader('googleScholarVolume', '<meta name="citation_volume" content="' . htmlspecialchars($issue->getVolume()) . '"/>');
}
if ($issue->getShowNumber()) {
$templateMgr->addHeader('googleScholarNumber', '<meta name="citation_issue" content="' . htmlspecialchars($issue->getNumber()) . '"/>');
}
}
if ($publication->getData('pages')) {
if ($startPage = $publication->getStartingPage()) {
$templateMgr->addHeader('googleScholarStartPage', '<meta name="citation_firstpage" content="' . htmlspecialchars($startPage) . '"/>');
}
if ($endPage = $publication->getEndingPage()) {
$templateMgr->addHeader('googleScholarEndPage', '<meta name="citation_lastpage" content="' . htmlspecialchars($endPage) . '"/>');
}
}
}
if ($applicationName == 'ops') {
if ($datePublished = $publication->getData('datePublished')) {
$templateMgr->addHeader('googleScholarDate', '<meta name="citation_online_date" content="' . date('Y/m/d', strtotime($datePublished)) . '"/>');
}
}
// DOI
if ($doi = $publication->getDoi()) {
$templateMgr->addHeader('googleScholarPublicationDOI', '<meta name="citation_doi" content="' . htmlspecialchars($doi) . '"/>');
}
// URN
foreach ((array) $templateMgr->getTemplateVars('pubIdPlugins') as $pubIdPlugin) {
if ($pubId = $publication->getStoredPubId($pubIdPlugin->getPubIdType())) {
$templateMgr->addHeader('googleScholarPubId' . $pubIdPlugin->getPubIdDisplayType(), '<meta name="citation_' . htmlspecialchars(strtolower($pubIdPlugin->getPubIdDisplayType())) . '" content="' . htmlspecialchars($pubId) . '"/>');
}
}
// Abstract URL
$templateMgr->addHeader('googleScholarHtmlUrl', '<meta name="citation_abstract_html_url" content="' . $request->getDispatcher()->url($request, PKPApplication::ROUTE_PAGE, null, $submissionPath, 'view', [$submissionBestId], urlLocaleForPage: '') . '"/>');
// Abstract
if ($abstract = $publication->getLocalizedData('abstract', $publicationLocale)) {
$templateMgr->addHeader('googleScholarAbstract', '<meta name="citation_abstract" xml:lang="' . htmlspecialchars(substr($publicationLocale, 0, 2)) . '" content="' . htmlspecialchars(strip_tags($abstract)) . '"/>');
}
// Subjects
if ($subjects = $publication->getData('subjects')) {
foreach ($subjects as $locale => $localeSubjects) {
foreach ($localeSubjects as $i => $subject) {
$templateMgr->addHeader('googleScholarSubject' . $i++, '<meta name="citation_keywords" xml:lang="' . htmlspecialchars(substr($locale, 0, 2)) . '" content="' . htmlspecialchars($subject) . '"/>');
}
}
}
// Keywords
if ($keywords = $publication->getData('keywords')) {
foreach ($keywords as $locale => $localeKeywords) {
foreach ($localeKeywords as $i => $keyword) {
$templateMgr->addHeader('googleScholarKeyword' . $i++, '<meta name="citation_keywords" xml:lang="' . htmlspecialchars(substr($locale, 0, 2)) . '" content="' . htmlspecialchars($keyword) . '"/>');
}
}
}
// Galley links
$galleys = $publication->getData('galleys');
foreach ($galleys as $i => $galley) {
$submissionFileId = $galley->getData('submissionFileId');
if ($submissionFileId && $submissionFile = Repo::submissionFile()->get($submissionFileId)) {
if ($submissionFile->getData('mimetype') == 'application/pdf') {
$templateMgr->addHeader('googleScholarPdfUrl' . $i++, '<meta name="citation_pdf_url" content="' . $request->getDispatcher()->url($request, PKPApplication::ROUTE_PAGE, null, $submissionPath, 'download', [$submissionBestId, $galley->getBestGalleyId()], urlLocaleForPage: '') . '"/>');
} elseif ($submissionFile->getData('mimetype') == 'text/html') {
$templateMgr->addHeader('googleScholarHtmlUrl' . $i++, '<meta name="citation_fulltext_html_url" content="' . $request->getDispatcher()->url($request, PKPApplication::ROUTE_PAGE, null, $submissionPath, 'view', [$submissionBestId, $galley->getBestGalleyId()], urlLocaleForPage: '') . '"/>');
}
}
}
// Citations
$outputReferences = [];
$citationDao = DAORegistry::getDAO('CitationDAO'); /** @var CitationDAO $citationDao */
$parsedCitations = $citationDao->getByPublicationId($publication->getId());
while ($citation = $parsedCitations->next()) {
$outputReferences[] = $citation->getRawCitation();
}
Hook::call('GoogleScholarPlugin::references', [&$outputReferences, $submission->getId()]);
foreach ($outputReferences as $i => $outputReference) {
$templateMgr->addHeader('googleScholarReference' . $i++, '<meta name="citation_reference" content="' . htmlspecialchars($outputReference) . '"/>');
}
return false;
}
/**
* Get the display name of this plugin
*
* @return string
*/
public function getDisplayName()
{
return __('plugins.generic.googleScholar.name');
}
/**
* Get the description of this plugin
*
* @return string
*/
public function getDescription()
{
return __('plugins.generic.googleScholar.description');
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\googleScholar\GoogleScholarPlugin', '\GoogleScholarPlugin');
}