-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBepressImportPlugin.inc.php
executable file
·270 lines (231 loc) · 8.24 KB
/
BepressImportPlugin.inc.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @file plugins/importexport/bepress/BepressImportPlugin.inc.php
*
* Copyright (c) 2017-2022 Simon Fraser University
* Copyright (c) 2017-2022 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class BepressImportPlugin
* @ingroup plugins_importexport_bepress
*
* @brief Bepress XML import plugin
*/
import('lib.pkp.classes.xml.PKPXMLParser');
import('lib.pkp.classes.plugins.ImportExportPlugin');
class BepressImportPlugin extends ImportExportPlugin {
/**
* Called as a plugin is registered to the registry
* @param string $category Name of category plugin was registered to
* @param int|null $mainContextId
* @return boolean True iff plugin initialized successfully; if false,
* the plugin will not be registered.
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path);
$this->addLocaleData();
return $success;
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
public function getName()
{
return 'BepressImportPlugin';
}
public function getDisplayName()
{
return __('plugins.importexport.bepress.displayName');
}
public function getDescription()
{
return __('plugins.importexport.bepress.description');
}
/**
* Execute import tasks using the command-line interface.
* @param $args array plugin options
*/
public function executeCLI($scriptName, &$args)
{
if (sizeof($args) != 5) {
$this->usage($scriptName);
exit();
}
$journalPath = array_shift($args);
$username = array_shift($args);
$editorUsername = array_shift($args);
$defaultEmail = array_shift($args);
$directoryName = rtrim(array_shift($args), '/');
if (!$journalPath || !$username || !$editorUsername || !$directoryName || !$defaultEmail) {
$this->usage($scriptName);
exit();
}
$journalDao = DAORegistry::getDAO('JournalDAO');
$journal = $journalDao->getByPath($journalPath);
if (!$journal) {
echo __('plugins.importexport.bepress.unknownJournal', array('journal' => $journalPath)) . "\n";
exit();
}
$userDao = DAORegistry::getDAO('UserDAO');
$user = $userDao->getByUsername($username);
if (!$user) {
echo __('plugins.importexport.bepress.unknownUser', array('username' => $username)) . "\n";
exit();
}
$editor = $userDao->getByUsername($editorUsername);
if (!$editor) {
echo __('plugins.importexport.bepress.unknownUser', array('username' => $editorUsername)) . "\n";
exit();
}
// FIXME: This attaches the associated user to the request and is a workaround for no users being present
// when running CLI tools. This assumes that given the username supplied should be used as the
// authenticated user. To revisit later.
Registry::set('user', $editor);
if (!file_exists($directoryName) && is_dir($directoryName) ) {
echo __('plugins.importexport.bepress.directoryDoesNotExist', array('directory' => $directoryName)) . "\n";
exit();
}
if (!filter_var($defaultEmail, FILTER_VALIDATE_EMAIL)){
echo __('plugins.importexport.bepress.unknownEmail', array('email' => $defaultEmail)). "\n";
exit();
}
$this->import('BepressImportDom');
echo __('plugins.importexport.bepress.importStart') . "\n";
// Import volumes from oldest to newest
$volumeHandle = opendir($directoryName);
while ($importVolumes[] = readdir($volumeHandle));
sort($importVolumes, SORT_NATURAL);
closedir($volumeHandle);
foreach ($importVolumes as $volumeName){
$volumePath = $directoryName . '/' . $volumeName;
if (!is_dir($volumePath) || preg_match('/^\./', $volumeName) || !$volumeName) continue;
// Import issues from oldest to newest
$importIssues = [];
$issueHandle = opendir($volumePath);
while ($importIssues[] = readdir($issueHandle));
sort($importIssues, SORT_NATURAL);
closedir($issueHandle);
$allIssueIds = [];
$curIssueId = 0;
foreach ($importIssues as $issueName){
$issuePath = $volumePath . '/' . $issueName;
if (!is_dir($issuePath) || preg_match('/^\./', $issueName) || !$issueName) continue;
// Import articles from oldest to newest
$importArticles = [];
$articleHandle = opendir($issuePath);
while ($importArticles[] = readdir($articleHandle));
sort($importArticles, SORT_NATURAL);
closedir($articleHandle);
$currSectionId = 0;
$allSectionIds = [];
foreach ($importArticles as $entry) {
$articlePath = $issuePath . '/' . $entry;
if (!is_dir($articlePath) || preg_match('/^\./', $entry) || !$entry) continue;
// Process all article files
$articleFileHandle = opendir($articlePath);
$importFiles = [];
while ($importFiles[] = readdir($articleFileHandle));
sort($importFiles, SORT_NATURAL);
closedir($articleFileHandle);
$xmlArticleFile = null;
$pdfArticleFiles = [];
foreach($importFiles as $importFile){
if (preg_match('/metadata\.xml$/', $importFile) && !$xmlArticleFile){
$xmlArticleFile = $articlePath . '/' . $importFile;
} elseif (preg_match('/fulltext(\.[a-z]{2}_[A-Z]{2})?\.pdf$/', $importFile)){
$pdfArticleFile = $articlePath . '/' . $importFile;
$pdfArticleFiles[] = $pdfArticleFile;
}
}
if (!$xmlArticleFile || empty($pdfArticleFiles)) continue;
if (is_file($xmlArticleFile)) {
$xmlArticle = $this->_getDocument($xmlArticleFile);
if ($xmlArticle) {
$number = null;
preg_match_all('/\d+/', basename(dirname(dirname($xmlArticleFile))), $number);
$shiftedArray = array_shift($number);
$number = array_shift($shiftedArray);
$volume = null;
preg_match_all('/\d+/', basename(dirname(dirname(dirname($xmlArticleFile)))), $volume);
$shiftedArray = array_shift($volume);
$volume = array_shift($shiftedArray);
$importDom = new BepressImportDom(
$journal,
$user,
$editor,
$xmlArticle,
$pdfArticleFiles,
$volume,
$number,
$defaultEmail
);
$returner = $importDom->importArticle();
unset($importDom);
if ($returner && is_array($returner)) {
$issue = $returner['issue'];
$section = $returner['section'];
$article = $returner['article'];
$issueId = $issue->getId();
$sectionId = $section->getId();
if ($curIssueId != $issueId) {
$allIssueIds[] = $issueId;
$curIssueId = $issueId;
$issueTitle = $issue->getIssueIdentification();
echo __('plugins.importexport.bepress.issueImport', array('title' => $issueTitle)) . "\n\n";
}
if ($currSectionId != $sectionId) {
$currSectionId = $sectionId;
$sectionTitle = $section->getLocalizedTitle();
echo __('plugins.importexport.bepress.sectionImport', array('title' => $sectionTitle)) . "\n\n";
}
if (!in_array($sectionId, $allSectionIds)) {
$allSectionIds[] = $sectionId;
}
$articleTitle = $article->getCurrentPublication()->getLocalizedTitle();
echo __('plugins.importexport.bepress.articleImported', array('title' => $articleTitle)) . "\n\n";
}
}
}
}
}
// Add default custom section ordering for TOC
$sectionDao = DAORegistry::getDAO('SectionDAO');
$numSections = 0;
// Add each section in order for articles
foreach ($allSectionIds as $curSectionId) {
$sectionDao->insertCustomSectionOrder($issueId, $curSectionId, ++$numSections);
}
}
// Setup default custom issue order
$issueDao = DAORegistry::getDAO('IssueDAO');
$issueDao->setDefaultCustomIssueOrders($journal->getId());
// Set latest imported issue as current issue
if (!empty($allIssueIds)) {
$lastIssueId = array_pop($allIssueIds);
$lastIssue = $issueDao->getById($lastIssueId);
$lastIssue->setCurrent(1);
$issueDao->updateObject($lastIssue);
}
echo __('plugins.importexport.bepress.importEnd');
exit();
}
/**
* Display the command-line usage information
*/
public function usage($scriptName)
{
echo __('plugins.importexport.bepress.cliUsage', array(
'scriptName' => $scriptName,
'pluginName' => $this->getName()
)) . "\n";
}
private function _getDocument($fileName)
{
$parser = new PKPXMLParser();
$returner = $parser->parse($fileName);
return $returner;
}
}