/** * Reindex PDF media by content for in-PDF search */ public static function reindex_pdfs($po_opts = null) { require_once __CA_LIB_DIR__ . "/core/Db.php"; require_once __CA_MODELS_DIR__ . "/ca_object_representations.php"; if (!caPDFMinerInstalled()) { CLIUtils::addError(_t("Can't reindex PDFs: PDFMiner is not installed.")); return false; } $o_db = new Db(); $t_rep = new ca_object_representations(); $t_rep->setMode(ACCESS_WRITE); $va_versions = array("original"); $va_kinds = ($vs_kinds = $po_opts->getOption("kinds")) ? explode(",", $vs_kinds) : array(); if (!is_array($va_kinds) || !sizeof($va_kinds)) { $va_kinds = array('all'); } $va_kinds = array_map('strtolower', $va_kinds); if (in_array('all', $va_kinds) || in_array('ca_object_representations', $va_kinds)) { if (!($vn_start = (int) $po_opts->getOption('start_id'))) { $vn_start = null; } if (!($vn_end = (int) $po_opts->getOption('end_id'))) { $vn_end = null; } if ($vn_id = (int) $po_opts->getOption('id')) { $vn_start = $vn_id; $vn_end = $vn_id; } $va_ids = array(); if ($vs_ids = (string) $po_opts->getOption('ids')) { if (sizeof($va_tmp = explode(",", $vs_ids))) { foreach ($va_tmp as $vn_id) { if ((int) $vn_id > 0) { $va_ids[] = (int) $vn_id; } } } } $vs_sql_where = null; $va_params = array(); if (sizeof($va_ids)) { $vs_sql_where = "WHERE representation_id IN (?)"; $va_params[] = $va_ids; } else { if ($vn_start > 0 && $vn_end > 0 && $vn_start <= $vn_end || $vn_start > 0 && $vn_end == null) { $vs_sql_where = "WHERE representation_id >= ?"; $va_params[] = $vn_start; if ($vn_end) { $vs_sql_where .= " AND representation_id <= ?"; $va_params[] = $vn_end; } } } if ($vs_sql_where) { $vs_sql_where .= " AND mimetype = 'application/pdf'"; } else { $vs_sql_where = " WHERE mimetype = 'application/pdf'"; } $qr_reps = $o_db->query("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM ca_object_representations \n\t\t\t\t\t{$vs_sql_where}\n\t\t\t\t\tORDER BY representation_id\n\t\t\t\t", $va_params); print CLIProgressBar::start($qr_reps->numRows(), _t('Reindexing PDF representations')); $vn_rep_table_num = $t_rep->tableNum(); while ($qr_reps->nextRow()) { $va_media_info = $qr_reps->getMediaInfo('media'); $vs_original_filename = $va_media_info['ORIGINAL_FILENAME']; print CLIProgressBar::next(1, _t("Reindexing PDF %1", $vs_original_filename ? $vs_original_filename . " (" . $qr_reps->get('representation_id') . ")" : $qr_reps->get('representation_id'))); $t_rep->load($qr_reps->get('representation_id')); $vn_rep_id = $t_rep->getPrimaryKey(); $m = new Media(); if ($m->read($vs_path = $t_rep->getMediaPath('media', 'original')) && is_array($va_locs = $m->getExtractedTextLocations())) { MediaContentLocationIndexer::clear($vn_rep_table_num, $vn_rep_id); foreach ($va_locs as $vs_content => $va_loc_list) { foreach ($va_loc_list as $va_loc) { MediaContentLocationIndexer::index($vn_rep_table_num, $vn_rep_id, $vs_content, $va_loc['p'], $va_loc['x1'], $va_loc['y1'], $va_loc['x2'], $va_loc['y2']); } } MediaContentLocationIndexer::write(); } else { //CLIUtils::addError(_t("[Warning] No content to reindex for PDF representation: %1", $vs_path)); } } print CLIProgressBar::finish(); } if (in_array('all', $va_kinds) || in_array('ca_attributes', $va_kinds)) { // get all Media elements $va_elements = ca_metadata_elements::getElementsAsList(false, null, null, true, false, true, array(16)); // 16=media $qr_c = $o_db->query("\n\t\t\t\t\tSELECT count(*) c \n\t\t\t\t\tFROM ca_attribute_values\n\t\t\t\t\tWHERE\n\t\t\t\t\t\telement_id in (?)\n\t\t\t\t", caExtractValuesFromArrayList($va_elements, 'element_id', array('preserveKeys' => false))); if ($qr_c->nextRow()) { $vn_count = $qr_c->get('c'); } else { $vn_count = 0; } $t_attr_val = new ca_attribute_values(); $vn_attr_table_num = $t_attr_val->tableNum(); print CLIProgressBar::start($vn_count, _t('Reindexing metadata attribute media')); foreach ($va_elements as $vs_element_code => $va_element_info) { $qr_vals = $o_db->query("SELECT value_id FROM ca_attribute_values WHERE element_id = ?", (int) $va_element_info['element_id']); $va_vals = $qr_vals->getAllFieldValues('value_id'); foreach ($va_vals as $vn_value_id) { $t_attr_val = new ca_attribute_values($vn_value_id); if ($t_attr_val->getPrimaryKey()) { $t_attr_val->setMode(ACCESS_WRITE); $t_attr_val->useBlobAsMediaField(true); $va_media_info = $t_attr_val->getMediaInfo('value_blob'); $vs_original_filename = $va_media_info['ORIGINAL_FILENAME']; if (!is_array($va_media_info) || $va_media_info['MIMETYPE'] !== 'application/pdf') { continue; } print CLIProgressBar::next(1, _t("Reindexing %1", $vs_original_filename ? $vs_original_filename . " ({$vn_value_id})" : $vn_value_id)); $m = new Media(); if ($m->read($vs_path = $t_attr_val->getMediaPath('value_blob', 'original')) && is_array($va_locs = $m->getExtractedTextLocations())) { MediaContentLocationIndexer::clear($vn_attr_table_num, $vn_attr_table_num); foreach ($va_locs as $vs_content => $va_loc_list) { foreach ($va_loc_list as $va_loc) { MediaContentLocationIndexer::index($vn_attr_table_num, $vn_value_id, $vs_content, $va_loc['p'], $va_loc['x1'], $va_loc['y1'], $va_loc['x2'], $va_loc['y2']); } } MediaContentLocationIndexer::write(); } else { //CLIUtils::addError(_t("[Warning] No content to reindex for PDF in metadata attribute: %1", $vs_path)); } } } } print CLIProgressBar::finish(); } return true; }
/** * Update annotation. If time code of annotation has changed media preview will be regenerated. * You can force the media preview to be regenerated whether the time code has changed or not * by passing the 'forcePreviewGeneration' option. * * @param array $pa_options An array of options: * forcePreviewGeneration = if set preview media will be regenerated whether time code has changed or not. Default is false. * @return bool True on success, false on failure */ public function update($pa_options = null) { $this->set('type_code', $vs_type = $this->getAnnotationType()); if (!$this->opo_annotations_properties->validate()) { $this->errors = $this->opo_annotations_properties->errors; return false; } $this->set('props', $this->opo_annotations_properties->getPropertyValues()); if (!$this->getAppConfig()->get('dont_generate_annotation_previews') && $this->getPrimaryKey() && ($this->changed('props') || isset($pa_options['forcePreviewGeneration']) && $pa_options['forcePreviewGeneration'])) { $vs_start = $this->getPropertyValue('startTimecode'); $vs_end = $this->getPropertyValue('endTimecode'); $va_data['start'] = $vs_start; $va_data['end'] = $vs_end; $t_rep = new ca_object_representations($this->get('representation_id')); if (($vs_file = $t_rep->getMediaPath('media', 'original')) && file_exists($vs_file)) { $o_media = new Media(); if ($o_media->read($vs_file)) { if ($o_media->writeClip($vs_file = tempnam(caGetTempDirPath(), 'annotationPreview'), $vs_start, $vs_end)) { $this->set('preview', $vs_file); } } } } $vn_rc = parent::update($pa_options); if (!$this->numErrors()) { $this->opo_annotations_properties = $this->loadProperties($vs_type); } if ($vs_file) { @unlink($vs_file); } return $vn_rc; }
/** * */ public function getClip() { $pn_id = $this->opo_request->getParameter('id', pInteger); $va_data = array(); $t_annotation = new ca_representation_annotations($pn_id); if ($t_annotation->getPrimaryKey()) { $vs_start = $t_annotation->getPropertyValue('startTimecode'); $vs_end = $t_annotation->getPropertyValue('endTimecode'); $va_data['start'] = $vs_start; $va_data['end'] = $vs_end; $t_rep = new ca_object_representations($t_annotation->get('representation_id')); $va_data['file'] = $vs_file = $t_rep->getMediaPath('media', 'original'); $o_media = new Media(); if ($o_media->read($vs_file)) { $o_media->writeClip($vs_file = tempnam('/tmp', 'meow'), $vs_start, $vs_end); } header("Content-type: audio/mpeg"); header("Content-length: " . @filesize($vs_file)); readfile($vs_file); return; } return $this->makeResponse($va_data, 500, "No such clip"); }
/** * Perform client services-related periodic tasks */ public function hookPeriodicTask(&$pa_params) { $t_log = new Eventlog(); $o_db = new Db(); if (!(bool) $this->opo_config->get('enable_client_services')) { return true; } // Find any orders with status PROCESSED_AWAITING_MEDIA_ACCESS and fetch media $qr_orders = $o_db->query("\n\t\t\t\tSELECT order_id\n\t\t\t\tFROM ca_commerce_orders\n\t\t\t\tWHERE\n\t\t\t\t\torder_status = 'PROCESSED_AWAITING_MEDIA_ACCESS'\n\t\t\t"); // // Set up HTTP client for REST calls // if ($this->opo_client_services_config->get('remote_media_base_url')) { $vs_base_url = $this->opo_client_services_config->get('remote_media_base_url'); $o_client = new RestClient($vs_base_url . "/service.php/iteminfo/ItemInfo/rest"); try { $o_res = $o_client->auth($this->opo_client_services_config->get('remote_media_username'), $this->opo_client_services_config->get('remote_media_password'))->get(); if (!$o_res->isSuccess()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not authenticate to remote system %1', $vs_base_url), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); $vb_download_errors = false; if ($t_order->getPrimaryKey() && sizeof($va_missing_media = $t_order->itemsMissingDownloadableMedia())) { $va_missing_media_representation_ids = $t_order->itemsMissingDownloadableMedia('original', array('returnRepresentationIDs' => true)); foreach ($va_missing_media as $vn_object_id => $va_representation_md5s) { foreach ($va_representation_md5s as $vn_i => $vs_representation_md5) { $o_xml = $o_client->getObjectRepresentationURLByMD5($vs_representation_md5, 'original')->get(); $vs_url = (string) $o_xml->getObjectRepresentationURLByMD5->original; if (!$vs_url) { continue; } // media no longer exists // fetch the file $t_rep = new ca_object_representations($va_missing_media_representation_ids[$vn_object_id][$vn_i]); if ($t_rep->getPrimaryKey() && ($vs_target_path = $t_rep->getMediaPath('media', 'original'))) { if ($r_source = fopen($vs_url, "rb")) { if ($r_target = fopen($vs_target_path, "wb")) { while (feof($r_source) === false) { fwrite($r_target, fread($r_source, 1024 * 8), 1024 * 8); } fclose($r_target); } else { $vb_download_errors = true; $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not open target path %1', $vs_target_path), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } fclose($r_source); } else { $vb_download_errors = true; $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not open download URL "%1"', $vs_url), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } // verify the file was downloaded correctly if (($vs_target_md5 = md5_file($vs_target_path)) !== $vs_representation_md5) { unlink($vs_target_path); $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Media file %1 failed to be downloaded from url "%2"; checksums differ: %3/%4', $vs_target_path, $vs_url, $vs_representation_md5, $vs_target_md5), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); $vb_download_errors = true; } } else { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Invalid representation_id "%1" or target path "%2"', $vn_representation_id, $vs_representation_md5, $vs_target_path), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); $vb_download_errors = true; } } } } if (!$vb_download_errors) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'PROCESSED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to PROCESSED from PROCESSED_AWAITING_MEDIA_ACCESS failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } } catch (Exception $e) { // noop } } // Find any orders with status PROCESSED_AWAITING_DIGITIZATION where all media are now present $qr_orders = $o_db->query("\n\t\t\t\tSELECT order_id\n\t\t\t\tFROM ca_commerce_orders\n\t\t\t\tWHERE\n\t\t\t\t\torder_status = 'PROCESSED_AWAITING_DIGITIZATION'\n\t\t\t"); while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); if ($t_order->getPrimaryKey() && !sizeof($t_order->itemsWithNoDownloadableMedia())) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'PROCESSED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to PROCESSED from PROCESSED_AWAITING_DIGITIZATION failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } // Find orders paid/shipped more than X days ago and mark them as "COMPLETED" $vn_days = (int) $this->opo_client_services_config->get('completed_order_age_threshold'); if ($vn_days > 1) { $vn_threshold = (int) (time() - $vn_days * 24 * 60 * 60); $qr_orders = $o_db->query("\n\t\t\t\t\tSELECT order_id\n\t\t\t\t\tFROM ca_commerce_orders\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t(order_status = 'PROCESSED') \n\t\t\t\t\t\tAND \n\t\t\t\t\t\t((payment_received_on > 0) AND (payment_received_on < ?))\n\t\t\t\t\t\tAND \n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t(shipping_date IS NULL AND shipped_on_date IS NULL)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t(shipped_on_date > 0) AND (shipped_on_date < ?)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t", $vn_threshold, $vn_threshold); while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); if ($t_order->getPrimaryKey()) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'COMPLETED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to COMPLETED from PROCESSED failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } } return true; }
/** * Download all media attached to specified object (not necessarily open for editing) * Includes all representation media attached to the specified object + any media attached to other * objects in the same object hierarchy as the specified object. */ public function DownloadMedia($pa_options = null) { list($vn_subject_id, $t_subject) = $this->_initView(); $pn_representation_id = $this->request->getParameter('representation_id', pInteger); $pn_value_id = $this->request->getParameter('value_id', pInteger); if ($pn_value_id) { return $this->DownloadAttributeFile(); } $ps_version = $this->request->getParameter('version', pString); if (!$vn_subject_id) { return; } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); if (!$ps_version) { $ps_version = 'original'; } $o_view->setVar('version', $ps_version); $va_ancestor_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyAncestors(null, array('idsOnly' => true, 'includeSelf' => true)) : array($vn_subject_id); if ($vn_parent_id = array_pop($va_ancestor_ids)) { $t_subject->load($vn_parent_id); array_unshift($va_ancestor_ids, $vn_parent_id); } $va_child_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyChildren(null, array('idsOnly' => true)) : array($vn_subject_id); foreach ($va_ancestor_ids as $vn_id) { array_unshift($va_child_ids, $vn_id); } $vn_c = 1; $va_file_names = array(); $va_file_paths = array(); foreach ($va_child_ids as $vn_child_id) { if (!$t_subject->load($vn_child_id)) { continue; } if ($t_subject->tableName() == 'ca_object_representations') { $va_reps = array($vn_child_id => array('representation_id' => $vn_child_id, 'info' => array($ps_version => $t_subject->getMediaInfo('media', $ps_version)))); } else { $va_reps = $t_subject->getRepresentations(array($ps_version)); } $vs_idno = $t_subject->get('idno'); foreach ($va_reps as $vn_representation_id => $va_rep) { if ($pn_representation_id && $pn_representation_id != $vn_representation_id) { continue; } $va_rep_info = $va_rep['info'][$ps_version]; $vs_idno_proc = preg_replace('![^A-Za-z0-9_\\-]+!', '_', $vs_idno); switch ($this->request->user->getPreference('downloaded_file_naming')) { case 'idno': $vs_file_name = $vs_idno_proc . '_' . $vn_c . '.' . $va_rep_info['EXTENSION']; break; case 'idno_and_version': $vs_file_name = $vs_idno_proc . '_' . $ps_version . '_' . $vn_c . '.' . $va_rep_info['EXTENSION']; break; case 'idno_and_rep_id_and_version': $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version . '.' . $va_rep_info['EXTENSION']; break; case 'original_name': default: if ($va_rep['info']['original_filename']) { $va_tmp = explode('.', $va_rep['info']['original_filename']); if (sizeof($va_tmp) > 1) { if (strlen($vs_ext = array_pop($va_tmp)) < 3) { $va_tmp[] = $vs_ext; } } $vs_file_name = join('_', $va_tmp); } else { $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version; } if (isset($va_file_names[$vs_file_name . '.' . $va_rep_info['EXTENSION']])) { $vs_file_name .= "_{$vn_c}"; } $vs_file_name .= '.' . $va_rep_info['EXTENSION']; break; } $va_file_names[$vs_file_name] = true; $o_view->setVar('version_download_name', $vs_file_name); // // Perform metadata embedding $t_rep = new ca_object_representations($va_rep['representation_id']); if (!($vs_path = caEmbedMediaMetadataIntoFile($t_rep->getMediaPath('media', $ps_version), $t_subject->tableName(), $t_subject->getPrimaryKey(), $t_subject->getTypeCode(), $t_rep->getPrimaryKey(), $t_rep->getTypeCode()))) { $vs_path = $t_rep->getMediaPath('media', $ps_version); } $va_file_paths[$vs_path] = $vs_file_name; $vn_c++; } } if (!($vn_limit = ini_get('max_execution_time'))) { $vn_limit = 30; } set_time_limit($vn_limit * 2); $o_zip = new ZipStream(); foreach ($va_file_paths as $vs_path => $vs_name) { $o_zip->addFile($vs_path, $vs_name); } $o_view->setVar('zip_stream', $o_zip); $o_view->setVar('archive_name', preg_replace('![^A-Za-z0-9\\.\\-]+!', '_', $t_subject->get('idno')) . '.zip'); $this->response->addContent($o_view->render('download_file_binary.php')); set_time_limit($vn_limit); }