Open Journal Systems  3.3.0
PubIdPlugin.inc.php
1 <?php
2 
16 import('lib.pkp.classes.plugins.PKPPubIdPlugin');
17 
18 abstract class PubIdPlugin extends PKPPubIdPlugin {
19 
23  function manage($args, $request) {
24  $user = $request->getUser();
25  $router = $request->getRouter();
26  $context = $router->getContext($request);
27 
28  $notificationManager = new NotificationManager();
29  switch ($request->getUserVar('verb')) {
30  case 'assignPubIds':
31  if (!$request->checkCSRF()) return new JSONMessage(false);
32  $suffixFieldName = $this->getSuffixFieldName();
33  $suffixGenerationStrategy = $this->getSetting($context->getId(), $suffixFieldName);
34  if ($suffixGenerationStrategy != 'customId') {
35  $issueEnabled = $this->isObjectTypeEnabled('Issue', $context->getId());
36  $submissionEnabled = $this->isObjectTypeEnabled('Publication', $context->getId());
37  $representationEnabled = $this->isObjectTypeEnabled('Representation', $context->getId());
38  if ($issueEnabled) {
39  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
40  $issues = $issueDao->getPublishedIssues($context->getId());
41  while ($issue = $issues->next()) {
42  $issuePubId = $issue->getStoredPubId($this->getPubIdType());
43  if (empty($issuePubId)) {
44  $issuePubId = $this->getPubId($issue);
45  $issueDao->changePubId($issue->getId(), $this->getPubIdType(), $issuePubId);
46  }
47  }
48  }
49  if ($submissionEnabled || $representationEnabled) {
50  $publicationDao = DAORegistry::getDAO('PublicationDAO'); /* @var $publicationDao PublicationDAO */
51  $representationDao = Application::getRepresentationDAO();
52  $submissions = Services::get('submission')->getMany([
53  'contextId' => $context->getId(),
54  'status' => STATUS_PUBLISHED,
55  'count' => 5000, // large upper limit
56  ]);
57  foreach ($submissions as $submission) {
58  $publications = $submission->getData('publications');
59  if ($submissionEnabled) {
60  foreach ($publications as $publication) {
61  $publicationPubId = $publication->getStoredPubId($this->getPubIdType());
62  if (empty($publicationPubId)) {
63  $publicationPubId = $this->getPubId($publication);
64  $publicationDao->changePubId($publication->getId(), $this->getPubIdType(), $publicationPubId);
65  }
66  }
67  }
68  if ($representationEnabled) {
69  foreach ($publications as $publication) {
70  $representations = $representationDao->getByPublicationId($publication->getId(), $context->getId());
71  while ($representation = $representations->next()) {
72  $representationPubId = $representation->getStoredPubId($this->getPubIdType());
73  if (empty($representationPubId)) {
74  $representationPubId = $this->getPubId($representation);
75  $representationDao->changePubId($representation->getId(), $this->getPubIdType(), $representationPubId);
76  }
77  }
78  }
79  }
80  }
81  }
82  }
83  return new JSONMessage(true);
84  default:
85  return parent::manage($args, $request);
86  }
87  }
88 
89  //
90  // Protected template methods from PKPPlubIdPlugin
91  //
95  function getPubObjectTypes() {
96  $pubObjectTypes = parent::getPubObjectTypes();
97  array_push($pubObjectTypes, 'Issue');
98  return $pubObjectTypes;
99  }
100 
104  function checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId) {
105  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
106  foreach ($this->getPubObjectTypes() as $type) {
107  if ($type === 'Issue') {
108  $excludeTypeId = $type === $pubObjectType ? $excludeId : null;
109  if ($issueDao->pubIdExists($type, $pubId, $excludeTypeId, $contextId)) {
110  return false;
111  }
112  }
113  }
114 
115  return parent::checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId);
116  }
117 
121  function getPubId($pubObject) {
122  // Get the pub id type
123  $pubIdType = $this->getPubIdType();
124 
125  // If we already have an assigned pub id, use it.
126  $storedPubId = $pubObject->getStoredPubId($pubIdType);
127  if ($storedPubId) return $storedPubId;
128 
129  // Determine the type of the publishing object.
130  $pubObjectType = $this->getPubObjectType($pubObject);
131 
132  // Initialize variables for publication objects.
133  $issue = ($pubObjectType == 'Issue' ? $pubObject : null);
134  $submission = ($pubObjectType == 'Submission' ? $pubObject : null);
135  $representation = ($pubObjectType == 'Representation' ? $pubObject : null);
136  $submissionFile = ($pubObjectType == 'SubmissionFile' ? $pubObject : null);
137 
138  // Get the context id.
139  if ($pubObjectType === 'Issue') {
140  $contextId = $pubObject->getJournalId();
141  } elseif ($pubObjectType === 'Representation') {
142  $publication = Services::get('publication')->get($pubObject->getData('publicationId'));
143  $submission = Services::get('submission')->get($publication->getData('submissionId'));
144  $contextId = $submission->getData('contextId');
145  } elseif (in_array($pubObjectType, ['Publication', 'SubmissionFile'])) {
146  $submission = Services::get('submission')->get($pubObject->getData('submissionId'));
147  $contextId = $submission->getData('contextId');
148  }
149 
150  // Check the context
151  $context = $this->getContext($contextId);
152  if (!$context) return null;
153  $contextId = $context->getId();
154 
155  // Check whether pub ids are enabled for the given object type.
156  $objectTypeEnabled = $this->isObjectTypeEnabled($pubObjectType, $contextId);
157  if (!$objectTypeEnabled) return null;
158 
159  // Retrieve the issue.
160  if (!is_a($pubObject, 'Issue')) {
161  assert(!is_null($submission));
162  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
163  $issue = $issueDao->getBySubmissionId($submission->getId(), $contextId);
164  }
165  if ($issue && $contextId != $issue->getJournalId()) return null;
166 
167  // Retrieve the pub id prefix.
168  $pubIdPrefix = $this->getSetting($contextId, $this->getPrefixFieldName());
169  if (empty($pubIdPrefix)) return null;
170 
171  // Generate the pub id suffix.
172  $suffixFieldName = $this->getSuffixFieldName();
173  $suffixGenerationStrategy = $this->getSetting($contextId, $suffixFieldName);
174  switch ($suffixGenerationStrategy) {
175  case 'customId':
176  $pubIdSuffix = $pubObject->getData($suffixFieldName);
177  break;
178 
179  case 'pattern':
180  $suffixPatternsFieldNames = $this->getSuffixPatternsFieldNames();
181  $pubIdSuffix = $this->getSetting($contextId, $suffixPatternsFieldNames[$pubObjectType]);
182 
183  // %j - journal initials, remove special characters and uncapitalize
184  $pubIdSuffix = PKPString::regexp_replace('/%j/', PKPString::regexp_replace('/[^A-Za-z0-9]/', '', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale()))), $pubIdSuffix);
185 
186  // %x - custom identifier
187  if ($pubObject->getStoredPubId('publisher-id')) {
188  $pubIdSuffix = PKPString::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $pubIdSuffix);
189  }
190 
191  if ($issue) {
192  // %v - volume number
193  $pubIdSuffix = PKPString::regexp_replace('/%v/', $issue->getVolume(), $pubIdSuffix);
194  // %i - issue number
195  $pubIdSuffix = PKPString::regexp_replace('/%i/', $issue->getNumber(), $pubIdSuffix);
196  // %Y - year
197  $pubIdSuffix = PKPString::regexp_replace('/%Y/', $issue->getYear(), $pubIdSuffix);
198  }
199 
200  if ($submission) {
201  // %a - article id
202  $pubIdSuffix = PKPString::regexp_replace('/%a/', $submission->getId(), $pubIdSuffix);
203  // %p - page number
204  if ($submission->getPages()) {
205  $pubIdSuffix = PKPString::regexp_replace('/%p/', $submission->getPages(), $pubIdSuffix);
206  }
207  }
208 
209  if ($representation) {
210  // %g - galley id
211  $pubIdSuffix = PKPString::regexp_replace('/%g/', $representation->getId(), $pubIdSuffix);
212  }
213 
214  if ($submissionFile) {
215  // %f - file id
216  $pubIdSuffix = PKPString::regexp_replace('/%f/', $submissionFile->getFileId(), $pubIdSuffix);
217  }
218 
219  break;
220 
221  default:
222  $pubIdSuffix = PKPString::regexp_replace('/[^A-Za-z0-9]/', '', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale())));
223 
224  if ($issue) {
225  $pubIdSuffix .= '.v' . $issue->getVolume() . 'i' . $issue->getNumber();
226  } else {
227  $pubIdSuffix .= '.v%vi%i';
228  }
229 
230  if ($submission) {
231  $pubIdSuffix .= '.' . $submission->getId();
232  }
233 
234  if ($representation) {
235  $pubIdSuffix .= '.g' . $representation->getId();
236  }
237 
238  if ($submissionFile) {
239  $pubIdSuffix .= '.f' . $submissionFile->getFileId();
240  }
241  }
242  if (empty($pubIdSuffix)) return null;
243 
244  // Costruct the pub id from prefix and suffix.
245  $pubId = $this->constructPubId($pubIdPrefix, $pubIdSuffix, $contextId);
246 
247  return $pubId;
248  }
249 
250  //
251  // Public API
252  //
257  function clearIssueObjectsPubIds($issue) {
258  $submissionPubIdEnabled = $this->isObjectTypeEnabled('Submission', $issue->getJournalId());
259  $representationPubIdEnabled = $this->isObjectTypeEnabled('Representation', $issue->getJournalId());
260  $filePubIdEnabled = $this->isObjectTypeEnabled('SubmissionFile', $issue->getJournalId());
261  if (!$submissionPubIdEnabled && !$representationPubIdEnabled && !$filePubIdEnabled) return false;
262 
263  $pubIdType = $this->getPubIdType();
264  import('lib.pkp.classes.submission.SubmissionFile'); // SUBMISSION_FILE_... constants
265 
266  $submissionIds = Services::get('submission')->getIds([
267  'contextId' => $issue->getJournalId(),
268  'issueIds' => $issue->getId(),
269  ]);
270  $publicationDao = DAORegistry::getDAO('PublicationDAO'); /* @var $publicationDao PublicationDAO */
271  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
272  foreach ($submissionIds as $submissionId) {
273  $submission = Services::get('submission')->get($submissionId);
274  if ($submissionPubIdEnabled) { // Does this option have to be enabled here for?
275  foreach ((array) $submission->getData('publications') as $publication) {
276  $publicationDao->deletePubId($publication->getId(), $pubIdType);
277  }
278  }
279  if ($representationPubIdEnabled || $filePubIdEnabled) { // Does this option have to be enabled here for?
280  foreach ((array) $submission->getData('publications') as $publication) {
281  $representations = Application::getRepresentationDAO()->getByPublicationId($publication->getId(), $submission->getData('contextId'));
282  while ($representation = $representations->next()) {
283  if ($representationPubIdEnabled) { // Does this option have to be enabled here for?
284  Application::getRepresentationDAO()->deletePubId($representation->getId(), $pubIdType);
285  }
286  if ($filePubIdEnabled) { // Does this option have to be enabled here for?
287  $articleProofFiles = $submissionFileDao->getAllRevisionsByAssocId(ASSOC_TYPE_REPRESENTATION, $representation->getId(), SUBMISSION_FILE_PROOF);
288  foreach ($articleProofFiles as $articleProofFile) {
289  $submissionFileDao->deletePubId($articleProofFile->getFileId(), $pubIdType);
290  }
291  }
292  }
293  unset($representations);
294  }
295  }
296  }
297  }
298 
302  function getDAOs() {
303  return array_merge(parent::getDAOs(), array(DAORegistry::getDAO('IssueDAO')));
304  }
305 }
306 
PubIdPlugin\getDAOs
getDAOs()
Definition: PubIdPlugin.inc.php:302
PKPString\regexp_replace
static regexp_replace($pattern, $replacement, $subject, $limit=-1)
Definition: PKPString.inc.php:279
PKPPubIdPlugin\getContext
getContext($contextId)
Definition: PKPPubIdPlugin.inc.php:437
Application\getRepresentationDAO
static getRepresentationDAO()
Definition: Application.inc.php:162
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PubIdPlugin\manage
manage($args, $request)
Definition: PubIdPlugin.inc.php:23
PubIdPlugin
Public identifiers plugins common functions.
Definition: PubIdPlugin.inc.php:18
PKPPubIdPlugin\getSuffixFieldName
getSuffixFieldName()
PKPPubIdPlugin\isObjectTypeEnabled
isObjectTypeEnabled($pubObjectType, $contextId)
PubIdPlugin\checkDuplicate
checkDuplicate($pubId, $pubObjectType, $excludeId, $contextId)
Definition: PubIdPlugin.inc.php:104
PubIdPlugin\getPubObjectTypes
getPubObjectTypes()
Definition: PubIdPlugin.inc.php:95
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
Plugin\getSetting
getSetting($contextId, $name)
Definition: Plugin.inc.php:473
PKPPubIdPlugin\getPrefixFieldName
getPrefixFieldName()
PubIdPlugin\getPubId
getPubId($pubObject)
Definition: PubIdPlugin.inc.php:121
PKPPubIdPlugin
Abstract class for public identifiers plugins.
Definition: PKPPubIdPlugin.inc.php:18
PKPPubIdPlugin\getPubIdType
getPubIdType()
PKPString\strtolower
static strtolower($string)
Definition: PKPString.inc.php:169
Plugin\$request
$request
Definition: Plugin.inc.php:68
NotificationManager
Definition: NotificationManager.inc.php:19
PKPPubIdPlugin\getPubObjectType
getPubObjectType($pubObject)
Definition: PKPPubIdPlugin.inc.php:370
PKPPubIdPlugin\constructPubId
constructPubId($pubIdPrefix, $pubIdSuffix, $contextId)
PKPPubIdPlugin\getSuffixPatternsFieldNames
getSuffixPatternsFieldNames()
PubIdPlugin\clearIssueObjectsPubIds
clearIssueObjectsPubIds($issue)
Definition: PubIdPlugin.inc.php:257
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49