Open Journal Systems  3.3.0
MarkupBatchGatewayPlugin.inc.php
1 <?php
2 
17 import('lib.pkp.classes.plugins.GatewayPlugin');
18 
21  protected $_parentPluginName = null;
23  protected $_markupConversionHelper = null;
25  protected $_batchConversionHelper = null;
27  protected $_user = null;
29  protected $_plugin = null;
31  protected $_otsWrapper = null;
32 
37  public function __construct($parentPluginName) {
38  parent::__construct();
39  $this->_parentPluginName = $parentPluginName;
40  $this->_plugin = PluginRegistry::getPlugin('generic', $parentPluginName);
41 
42  // initialize batch conversion helper
43  $this->import('classes.MarkupBatchConversionHelper');
44  $this->_batchConversionHelper = new MarkupBatchConversionHelper();
45  }
46 
52  protected function initMarkupConversionHelper($request, $journal) {
53  $this->import('classes.MarkupConversionHelper');
55  $this->_plugin,
56  $journal,
57  $this->_user
58  );
59  $this->_markupConversionHelper = new MarkupConversionHelper(
60  $this->_plugin,
61  $this->_otsWrapper,
62  $this->_user
63  );
64  }
65 
72  public function getName() {
73  return 'MarkupBatchGatewayPlugin';
74  }
75 
81  public function getDisplayName() {
82  return __('plugins.generic.markup.batch.displayName');
83  }
84 
90  public function getDescription() {
91  return __('plugins.generic.markup.batch.description');
92  }
93 
99  public function &getMarkupPlugin() {
100  return $this->_plugin;
101  }
102 
108  public function getPluginPath() {
109  $plugin =& $this->getMarkupPlugin();
110  return $plugin->getPluginPath();
111  }
112 
117  public function getSeq() {
118  return parent::getSeq() + 1;
119  }
120 
128  public function fetch($args, $request) {
129  // set custom error handler
130  set_error_handler(array($this->_batchConversionHelper,'errorHandler'), E_ERROR );
131 
132  // skip if conversion is already running
133  if ($this->_batchConversionHelper->isRunning()) {
134  return;
135  }
136 
137  // Parse keys and values from arguments
138  $keys = array();
139  $values = array();
140  foreach ($args as $index => $arg) {
141  if ($arg == 'true') $arg = true;
142  if ($arg == 'false') $arg = false;
143 
144  if ($index % 2 == 0) {
145  $keys[] = $arg;
146  } else {
147  $values[] = $arg;
148  }
149  }
150  $args = array_combine($keys, $values);
151 
152  // find user object
153  $userDao = DAORegistry::getDAO('UserDAO');
154  $userId = isset($args['userId']) ? (int) $args['userId'] : false;
155  if (!$userId) {
156  fatalError(__('plugins.generic.markup.archive.noUserID'));
157  exit;
158  }
159  $this->_user = $userDao->getById($userId);
160 
161  $journal = $request->getJournal();
162  // initialize markup conversion helper
163  $this->initMarkupConversionHelper($request, $journal);
164  // get list of submissions
165  if (empty($_POST)) {
166  return;
167  }
168 
169  // access key
170  $accessKey = isset($args['accessKey']) ? $args['accessKey'] : null;
171  if (empty($accessKey)) {
172  fatalError(__('plugins.generic.markup.archive.noAccessKey'));
173  exit;
174  }
175 
176  // validate access key
177  if (!$this->_user || !MarkupConversionHelper::validateAccessToken($this->_user, $accessKey)) {
178  fatalError(__('plugins.generic.markup.archive.noAccessKey'));
179  exit;
180  }
181 
182  $submissions = $_POST;
183 
184  $pid = getmypid();
185  $submissionCount = count($submissions);
186  $data = array(
187  'pid' => $pid,
188  'submissionCount' => $submissionCount,
189  'processedCount' => 0,
190  );
191 
192  // create outfile
193  $this->_batchConversionHelper->createOutFile($data);
194 
195  // find current user's group
196  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
197  $userGroups = $userGroupDao->getByUserId($this->_user->getId(), $journal->getId());
198  $userGroup = $userGroups->next();
199 
200  // batch conversion
201  $processedCount = 0;
202  $tmpZipFile = null;
203  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
204  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO');
205  $cancellationToken = sha1(time());
206  foreach ($submissions as $submissionId => $submissionFileId) {
207  $data = array(
208  'pid' => $pid,
209  'cancellationToken' => $cancellationToken,
210  'submissionCount' => $submissionCount,
211  'processedCount' => ++$processedCount,
212  'submissionId' => $submissionId,
213  'conversionStatus' => '',
214  'otsJobId' => ''
215  );
216 
217  // load submission file
218  $submissionFile = $submissionFileDao->getLatestRevision($submissionFileId);
219  if (!$submissionFile) {
220  continue;
221  }
222 
223  // load submission
224  $submission = $submissionDao->getById($submissionFile->getSubmissionId());
225 
226  try {
228  $journal,
229  $this->_user,
230  $submissionFileId
231  );
232  $data['jobInfoId'] = $jobInfoId;
233  $this->_batchConversionHelper->updateOutFile($data);
234 
235  $jobId = $this->_markupConversionHelper->triggerConversion(
236  $request->getJournal(),
237  $submissionFile,
238  $submissionFile->getFileStage(),
239  'galley-generate',
240  $jobInfoId
241  );
242  $data['otsJobId'] = $jobId;
243 
244  // status callback closure
245  $batchConversionHelper = $this->_batchConversionHelper;
246  $user = $this->_user;
247  $plugin = $this->_plugin;
248  $statusCallbackFn = function($jobStatus) use ($data, $batchConversionHelper, $request, $user, $plugin) {
250  $plugin,
251  $request->getJournal(),
252  $user
253  );
254  $data['conversionStatus'] = $wrapper->statusCodeToLabel($jobStatus);
255  $batchConversionHelper->updateOutFile($data);
256  };
257 
258  $tmpZipFile = $this->_markupConversionHelper->retrieveConversionJobArchive(
259  $submissionFile,
260  $jobId,
261  $statusCallbackFn
262  );
263  if (($tmpZipFile == false) || !file_exists($tmpZipFile)) {
264  throw new Exception(__('plugins.generic.markup.archive-download-failure'));
265  }
266 
267  $extractionPath = null;
268  if (($extractionPath = $this->_markupConversionHelper->unzipArchive($tmpZipFile)) === false) {
269  throw new Exception(__('plugins.generic.markup.archive-extract-failure'));
270  }
271 
272  $fileName = "document" . '__' . date('Y-m-d_h:i:s');
273  $this->_markupConversionHelper->handleArchiveExtractionAfterGalleyGenerate(
274  $extractionPath,
275  $journal,
276  $submission,
277  $submissionFile,
278  $fileName
279  );
280  }
281  catch (Exception $e) {
282  error_log('EXCEPTION!!! ' . $e->getMessage());
283  $statusCallbackFn($e->getMessage());
284  // In case of exception pause few seconds so that user gets a chance to
285  // see the error before we carry on
286  sleep(5);
287  }
288  }
289 
290  $this->_batchConversionHelper->deleteOutFile();
291  }
292 }
MarkupConversionHelper\validateAccessToken
static validateAccessToken($user, $accessKey)
Definition: MarkupConversionHelper.inc.php:655
MarkupBatchGatewayPlugin\getMarkupPlugin
& getMarkupPlugin()
Definition: MarkupBatchGatewayPlugin.inc.php:117
MarkupBatchGatewayPlugin\fetch
fetch($args, $request)
Definition: MarkupBatchGatewayPlugin.inc.php:146
MarkupConversionHelper\createConversionJobInfo
static createConversionJobInfo($journal, $user, $fileId)
Definition: MarkupConversionHelper.inc.php:563
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
MarkupBatchConversionHelper
Definition: MarkupBatchConversionHelper.inc.php:17
MarkupBatchGatewayPlugin\$_parentPluginName
$_parentPluginName
Definition: MarkupBatchGatewayPlugin.inc.php:24
MarkupConversionHelper
Markup conversion Helper class.
Definition: MarkupConversionHelper.inc.php:17
Plugin\__construct
__construct()
Definition: Plugin.inc.php:73
MarkupBatchGatewayPlugin\$_otsWrapper
$_otsWrapper
Definition: MarkupBatchGatewayPlugin.inc.php:49
MarkupBatchGatewayPlugin\initMarkupConversionHelper
initMarkupConversionHelper($request, $journal)
Definition: MarkupBatchGatewayPlugin.inc.php:70
MarkupBatchGatewayPlugin\getName
getName()
Definition: MarkupBatchGatewayPlugin.inc.php:90
MarkupConversionHelper\getOTSWrapperInstance
static getOTSWrapperInstance($plugin, $journal, $userObject, $reuseCached=true)
Definition: MarkupConversionHelper.inc.php:590
MarkupBatchGatewayPlugin\$_batchConversionHelper
$_batchConversionHelper
Definition: MarkupBatchGatewayPlugin.inc.php:34
MarkupBatchGatewayPlugin\getPluginPath
getPluginPath()
Definition: MarkupBatchGatewayPlugin.inc.php:126
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
MarkupBatchGatewayPlugin\getDisplayName
getDisplayName()
Definition: MarkupBatchGatewayPlugin.inc.php:99
MarkupBatchGatewayPlugin\$_markupConversionHelper
$_markupConversionHelper
Definition: MarkupBatchGatewayPlugin.inc.php:29
MarkupBatchGatewayPlugin
Batch conversion Helper class.
Definition: MarkupBatchGatewayPlugin.inc.php:19
MarkupBatchGatewayPlugin\$_plugin
$_plugin
Definition: MarkupBatchGatewayPlugin.inc.php:44
Plugin\$request
$request
Definition: Plugin.inc.php:68
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
MarkupBatchGatewayPlugin\getDescription
getDescription()
Definition: MarkupBatchGatewayPlugin.inc.php:108
MarkupBatchGatewayPlugin\getSeq
getSeq()
Definition: MarkupBatchGatewayPlugin.inc.php:135
MarkupBatchGatewayPlugin\$_user
$_user
Definition: MarkupBatchGatewayPlugin.inc.php:39
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
MarkupBatchGatewayPlugin\__construct
__construct($parentPluginName)
Definition: MarkupBatchGatewayPlugin.inc.php:55
GatewayPlugin
Abstract class for gateway plugins.
Definition: GatewayPlugin.inc.php:18