Open Preprint 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  $submissionEnabled = $this->isObjectTypeEnabled('Publication', $context->getId());
36  $representationEnabled = $this->isObjectTypeEnabled('Representation', $context->getId());
37  if ($submissionEnabled || $representationEnabled) {
38  $publicationDao = DAORegistry::getDAO('PublicationDAO'); /* @var $publicationDao PublicationDAO */
39  $representationDao = Application::getRepresentationDAO();
40  $submissions = Services::get('submission')->getMany([
41  'contextId' => $context->getId(),
42  'status' => STATUS_PUBLISHED,
43  'count' => 5000, // large upper limit
44  ]);
45  foreach ($submissions as $submission) {
46  $publications = $submission->getData('publications');
47  if ($submissionEnabled) {
48  foreach ($publications as $publication) {
49  $publicationPubId = $publication->getStoredPubId($this->getPubIdType());
50  if (empty($publicationPubId)) {
51  $publicationPubId = $this->getPubId($publication);
52  $publicationDao->changePubId($publication->getId(), $this->getPubIdType(), $publicationPubId);
53  }
54  }
55  }
56  if ($representationEnabled) {
57  foreach ($publications as $publication) {
58  $representations = $representationDao->getByPublicationId($publication->getId(), $context->getId());
59  while ($representation = $representations->next()) {
60  $representationPubId = $representation->getStoredPubId($this->getPubIdType());
61  if (empty($representationPubId)) {
62  $representationPubId = $this->getPubId($representation);
63  $representationDao->changePubId($representation->getId(), $this->getPubIdType(), $representationPubId);
64  }
65  }
66  }
67  }
68  }
69  }
70  }
71  return new JSONMessage(true);
72  default:
73  return parent::manage($args, $request);
74  }
75  }
76 
77  //
78  // Protected template methods from PKPPlubIdPlugin
79  //
80 
84  function getPubId($pubObject) {
85  // Get the pub id type
86  $pubIdType = $this->getPubIdType();
87 
88  // If we already have an assigned pub id, use it.
89  $storedPubId = $pubObject->getStoredPubId($pubIdType);
90  if ($storedPubId) return $storedPubId;
91 
92  // Determine the type of the publishing object.
93  $pubObjectType = $this->getPubObjectType($pubObject);
94 
95  // Initialize variables for publication objects.
96  $submission = ($pubObjectType == 'Submission' ? $pubObject : null);
97  $representation = ($pubObjectType == 'Representation' ? $pubObject : null);
98  $submissionFile = ($pubObjectType == 'SubmissionFile' ? $pubObject : null);
99 
100  // Get the context id.
101  if ($pubObjectType === 'Representation') {
102  $publication = Services::get('publication')->get($pubObject->getData('publicationId'));
103  $submission = Services::get('submission')->get($publication->getData('submissionId'));
104  $contextId = $submission->getData('contextId');
105  } elseif ($pubObjectType === 'Publication') {
106  $submission = Services::get('submission')->get($pubObject->getData('submissionId'));
107  $publication = Services::get('publication')->get($pubObject->getId());
108  $contextId = $submission->getData('contextId');
109  } elseif ($pubObjectType === 'SubmissionFile') {
110  $submission = Services::get('submission')->get($pubObject->getData('submissionId'));
111  $contextId = $submission->getData('contextId');
112  }
113 
114 
115  // Check the context
116  $context = $this->getContext($contextId);
117  if (!$context) return null;
118  $contextId = $context->getId();
119 
120  // Check whether pub ids are enabled for the given object type.
121  $objectTypeEnabled = $this->isObjectTypeEnabled($pubObjectType, $contextId);
122  if (!$objectTypeEnabled) return null;
123 
124  // Retrieve the pub id prefix.
125  $pubIdPrefix = $this->getSetting($contextId, $this->getPrefixFieldName());
126  if (empty($pubIdPrefix)) return null;
127 
128  // Generate the pub id suffix.
129  $suffixFieldName = $this->getSuffixFieldName();
130  $suffixGenerationStrategy = $this->getSetting($contextId, $suffixFieldName);
131  switch ($suffixGenerationStrategy) {
132  case 'customId':
133  $pubIdSuffix = $pubObject->getData($suffixFieldName);
134  break;
135 
136  case 'pattern':
137  $suffixPatternsFieldNames = $this->getSuffixPatternsFieldNames();
138  $pubIdSuffix = $this->getSetting($contextId, $suffixPatternsFieldNames[$pubObjectType]);
139 
140  // %j - journal initials, remove special characters and uncapitalize
141  $pubIdSuffix = PKPString::regexp_replace('/%j/', PKPString::regexp_replace('/[^A-Za-z0-9]/', '', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale()))), $pubIdSuffix);
142 
143  // %x - custom identifier
144  if ($pubObject->getStoredPubId('publisher-id')) {
145  $pubIdSuffix = PKPString::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $pubIdSuffix);
146  }
147 
148  if ($submission) {
149  // %a - article id
150  $pubIdSuffix = PKPString::regexp_replace('/%a/', $submission->getId(), $pubIdSuffix);
151  }
152 
153  if ($publication) {
154  // %b - publication id
155  $pubIdSuffix = PKPString::regexp_replace('/%b/', $publication->getId(), $pubIdSuffix);
156  }
157 
158  if ($representation) {
159  // %g - galley id
160  $pubIdSuffix = PKPString::regexp_replace('/%g/', $representation->getId(), $pubIdSuffix);
161  }
162 
163  if ($submissionFile) {
164  // %f - file id
165  $pubIdSuffix = PKPString::regexp_replace('/%f/', $submissionFile->getFileId(), $pubIdSuffix);
166  }
167 
168  break;
169 
170  default:
171  $pubIdSuffix = PKPString::regexp_replace('/[^A-Za-z0-9]/', '', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale())));
172 
173  if ($submission) {
174  $pubIdSuffix .= '.' . $submission->getId();
175  }
176 
177  if ($representation) {
178  $pubIdSuffix .= '.g' . $representation->getId();
179  }
180 
181  if ($submissionFile) {
182  $pubIdSuffix .= '.f' . $submissionFile->getFileId();
183  }
184  }
185  if (empty($pubIdSuffix)) return null;
186 
187  // Costruct the pub id from prefix and suffix.
188  $pubId = $this->constructPubId($pubIdPrefix, $pubIdSuffix, $contextId);
189 
190  return $pubId;
191  }
192 
196  function versionPubId($pubObject) {
197  $pubObjectType = $this->getPubObjectType($pubObject);
198  $submission = Services::get('submission')->get($pubObject->getData('submissionId'));
199  $publication = Services::get('publication')->get($pubObject->getId());
200  $contextId = $submission->getData('contextId');
201 
202  // Check the context
203  $context = $this->getContext($contextId);
204  if (!$context) return null;
205  $contextId = $context->getId();
206 
207  // Check whether pub ids are enabled for the given object type.
208  $objectTypeEnabled = $this->isObjectTypeEnabled($pubObjectType, $contextId);
209  if (!$objectTypeEnabled) return null;
210 
211  // Retrieve the pub id prefix.
212  $pubIdPrefix = $this->getSetting($contextId, $this->getPrefixFieldName());
213  if (empty($pubIdPrefix)) return null;
214 
215  // Retrieve the pub id suffix.
216  $suffixPatternsFieldNames = $this->getSuffixPatternsFieldNames();
217 
218  $pubIdSuffix = $this->getSetting($contextId, $suffixPatternsFieldNames[$pubObjectType]);
219 
220  // %j - journal initials
221  $pubIdSuffix = PKPString::regexp_replace('/%j/', PKPString::regexp_replace('/[^A-Za-z0-9]/', '', PKPString::strtolower($context->getAcronym($context->getPrimaryLocale()))), $pubIdSuffix);
222 
223  // %x - custom identifier
224  if ($pubObject->getStoredPubId('publisher-id')) {
225  $pubIdSuffix = PKPString::regexp_replace('/%x/', $pubObject->getStoredPubId('publisher-id'), $pubIdSuffix);
226  }
227 
228  // %a - preprint id
229  if ($submission) {
230  $pubIdSuffix = PKPString::regexp_replace('/%a/', $submission->getId(), $pubIdSuffix);
231  }
232 
233  // %b - publication id
234  if ($publication) {
235  $pubIdSuffix = PKPString::regexp_replace('/%b/', $publication->getId(), $pubIdSuffix);
236  }
237 
238  if (empty($pubIdSuffix)) return null;
239 
240  // Costruct the pub id from prefix and suffix.
241  $pubId = $this->constructPubId($pubIdPrefix, $pubIdSuffix, $contextId);
242 
243  return $pubId;
244  }
245 
246 }
247 
248 
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
PubIdPlugin\versionPubId
versionPubId($pubObject)
Definition: PubIdPlugin.inc.php:196
Application\getRepresentationDAO
static getRepresentationDAO()
Definition: Application.inc.php:143
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)
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:84
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()
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49