Esempio n. 1
0
 private static function executeQueryByType($partner_id, $report_type, $report_flavor, reportsInputFilter $input_filter, $page_size, $page_index, $order_by, $object_ids = null, $offset = null)
 {
     $start = microtime(true);
     try {
         $add_search_text = false;
         $has_object_ids = false;
         if ($input_filter instanceof endUserReportsInputFilter) {
             $has_object_ids = $input_filter->categories;
         }
         $has_object_ids = $has_object_ids || $object_ids;
         if (is_numeric($report_type)) {
             $file_path = myReportsSqlFileMgr::getSqlFilePath(self::$type_map[$report_type], self::$flavor_map[$report_flavor], $add_search_text, $has_object_ids ? true : false, $input_filter);
         } else {
             if (strpos($report_type, ".") === 0 || strpos($report_type, "/") === 0 || strpos($report_type, "http") === 0) {
                 throw new kCoreException("Will not search for invalid report_type [{$report_type}", kCoreException::INVALID_QUERY);
             }
             $file_path = dirname(__FILE__) . "/" . $report_type . ".sql";
         }
         $sql_raw_content = file_get_contents($file_path);
         if (!$sql_raw_content) {
             $pluginInstances = KalturaPluginManager::getPluginInstances('IKalturaReportProvider');
             foreach ($pluginInstances as $pluginInstance) {
                 $res = $pluginInstance->getReportResult($partner_id, $report_type, $report_flavor, $object_ids, $order_by);
                 if (!is_null($res)) {
                     return $res;
                 }
             }
             throw new kCoreException("Cannot find sql for [{$report_type}] [{$report_flavor}] at [{$file_path}]", kCoreException::QUERY_NOT_FOUND);
         }
         $entryFilter = new entryFilter();
         $entryFilter->setPartnerSearchScope($partner_id);
         $shouldSelectFromSearchEngine = false;
         $category_ids_clause = "1=1";
         if ($input_filter instanceof endUserReportsInputFilter) {
             if ($input_filter->playbackContext || $input_filter->ancestorPlaybackContext) {
                 $categoryFilter = new categoryFilter();
                 if ($input_filter->playbackContext && $input_filter->ancestorPlaybackContext) {
                     $categoryIds = category::CATEGORY_ID_THAT_DOES_NOT_EXIST;
                 } else {
                     if ($input_filter->playbackContext) {
                         $categoryFilter->set("_in_full_name", $input_filter->playbackContext);
                     }
                     if ($input_filter->ancestorPlaybackContext) {
                         $categoryFilter->set("_matchor_likex_full_name", $input_filter->ancestorPlaybackContext);
                     }
                     $c = KalturaCriteria::create(categoryPeer::OM_CLASS);
                     $categoryFilter->attachToCriteria($c);
                     $c->applyFilters();
                     $categoryIdsFromDB = $c->getFetchedIds();
                     if (count($categoryIdsFromDB)) {
                         $categoryIds = implode(",", $categoryIdsFromDB);
                     } else {
                         $categoryIds = category::CATEGORY_ID_THAT_DOES_NOT_EXIST;
                     }
                 }
                 $category_ids_clause = "ev.context_id in ( {$categoryIds} )";
             }
         }
         if ($input_filter->categories) {
             $entryFilter->set("_matchor_categories", $input_filter->categories);
             $shouldSelectFromSearchEngine = true;
         }
         if ($input_filter->keywords) {
             if ($input_filter->search_in_tags) {
                 $entryFilter->set("_free_text", $input_filter->keywords);
             } else {
                 $entryFilter->set("_like_admin_tags", $input_filter->keywords);
             }
             $shouldSelectFromSearchEngine = true;
         }
         $entryIdsFromDB = array();
         if ($shouldSelectFromSearchEngine) {
             $c = KalturaCriteria::create(entryPeer::OM_CLASS);
             $entryFilter->attachToCriteria($c);
             $c->applyFilters();
             $entryIdsFromDB = $c->getFetchedIds();
             if ($c->getRecordsCount() > count($entryIdsFromDB)) {
                 throw new kCoreException('Search is to general', kCoreException::SEARCH_TOO_GENERAL);
             }
             if (!count($entryIdsFromDB)) {
                 $entryIdsFromDB[] = entry::ENTRY_ID_THAT_DOES_NOT_EXIST;
             }
         }
         $obj_ids_clause = null;
         if ($object_ids) {
             //the object ids are not supposed to include single quotes - if they do have them - remove them
             $object_ids = str_replace("'", '', $object_ids);
             // quote all the objects with SINGLE-QUOTES
             $object_ids_str = "'" . str_replace(",", "','", $object_ids) . "'";
             if ($report_type == self::REPORT_TYPE_CONTENT_CONTRIBUTIONS) {
                 $obj_ids_clause = "en.entry_media_source_id in ( {$object_ids_str})";
             } else {
                 if ($report_type == self::REPORT_TYPE_TOP_SYNDICATION) {
                     $obj_ids_clause = "ev.domain_id in ( {$object_ids_str})";
                 } else {
                     if ($report_type == self::REPORT_TYPE_MAP_OVERLAY) {
                         $obj_ids_clause = "ev.country_id in ( {$object_ids_str})";
                     } else {
                         if ($report_type == self::REPORT_TYPE_ADMIN_CONSOLE) {
                             $obj_ids_clause = "dim_partner.partner_id in ( {$object_ids_str})";
                         } else {
                             if ($report_type == self::REPORT_TYPE_PARTNER_USAGE || $report_type == self::REPORT_TYPE_VAR_USAGE || $report_type == self::REPORT_TYPE_PEAK_STORAGE) {
                                 $obj_ids_clause = "partner_id in ({$object_ids_str})";
                             } else {
                                 if ($report_type == self::REPORT_TYPE_PLATFORMS) {
                                     $obj_ids_clause = "device in ({$object_ids_str})";
                                 } else {
                                     $objectIds = explode(',', $object_ids);
                                     $entryIds = "'" . implode("','", array_merge($objectIds, $entryIdsFromDB)) . "'";
                                     $obj_ids_clause = "ev.entry_id in ( {$entryIds} )";
                                 }
                             }
                         }
                     }
                 }
             }
         } elseif (count($entryIdsFromDB)) {
             $entryIds = "'" . implode("','", $entryIdsFromDB) . "'";
             $obj_ids_clause = "ev.entry_id in ( {$entryIds} )";
         }
         if ($input_filter instanceof endUserReportsInputFilter && $input_filter->userIds != null && ($report_type == self::REPORT_TYPE_USER_USAGE || $report_type == self::REPORT_TYPE_SPECIFIC_USER_USAGE)) {
             $userFilter = new kuserFilter();
             $userFilter->set("_in_puser_id", $input_filter->userIds);
             $c = KalturaCriteria::create(kuserPeer::OM_CLASS);
             $userFilter->attachToCriteria($c);
             $c->applyFilters();
             $userIdsFromDB = $c->getFetchedIds();
             if (count($userIdsFromDB)) {
                 $kuserIds = implode(",", $userIdsFromDB);
             } else {
                 $kuserIds = kuser::KUSER_ID_THAT_DOES_NOT_EXIST;
             }
             $obj_ids_clause = "u.kuser_id in ( {$kuserIds} )";
         }
         if (is_numeric($report_type)) {
             $order_by = self::getOrderBy(self::$type_map[$report_type], $order_by);
         }
         $query = self::getReplacedSql($sql_raw_content, $partner_id, $input_filter, $page_size, $page_index, $order_by, $obj_ids_clause, $category_ids_clause, $offset);
         if (is_numeric($report_type)) {
             $query_header = "/* -- " . self::$type_map[$report_type] . " " . self::$flavor_map[$report_flavor] . " -- */\n";
         } else {
             $query_header = "/* -- " . $report_type . " -- */\n";
         }
         KalturaLog::log("\n{$query_header}{$query}");
         $res = self::executeQuery($query);
         $end = microtime(true);
         KalturaLog::log("Query took [" . ($end - $start) . "]");
         return $res;
     } catch (Exception $ex) {
         KalturaLog::log($ex->getMessage());
         // TODO - write proeper error
         if ($ex->getCode() == kCoreException::SEARCH_TOO_GENERAL) {
         }
         throw $ex;
         throw new Exception("Error while processing report for [{$partner_id} , {$report_type} , {$report_flavor}]");
     }
 }
Esempio n. 2
0
 public function objectChanged(BaseObject $object, array $modifiedColumns)
 {
     if ($object instanceof entry && in_array(entryPeer::STATUS, $modifiedColumns) && ($object->getStatus() == entryStatus::READY || $object->getStatus() == entryStatus::ERROR_CONVERTING) && $object->getReplacedEntryId()) {
         kFlowHelper::handleEntryReplacement($object);
         return true;
     }
     if ($object instanceof UploadToken && in_array(UploadTokenPeer::STATUS, $modifiedColumns) && $object->getStatus() == UploadToken::UPLOAD_TOKEN_FULL_UPLOAD) {
         kFlowHelper::handleUploadFinished($object);
         return true;
     }
     if ($object instanceof BatchJob && $object->getJobType() == BatchJobType::BULKUPLOAD && $object->getStatus() == BatchJob::BATCHJOB_STATUS_ABORTED && in_array(BatchJobPeer::STATUS, $modifiedColumns) && in_array($object->getColumnsOldValue(BatchJobPeer::STATUS), BatchJobPeer::getClosedStatusList())) {
         $partner = $object->getPartner();
         if ($partner->getEnableBulkUploadNotificationsEmails()) {
             kFlowHelper::sendBulkUploadNotificationEmail($object, MailType::MAIL_TYPE_BULKUPLOAD_ABORTED, array($partner->getAdminName(), $object->getId(), kFlowHelper::createBulkUploadLogUrl($object)));
         }
         return true;
     }
     if ($object instanceof UserRole && in_array(UserRolePeer::PERMISSION_NAMES, $modifiedColumns)) {
         $filter = new kuserFilter();
         $filter->set('_eq_role_ids', $object->getId());
         kJobsManager::addIndexJob($object->getPartnerId(), IndexObjectType::USER, $filter, false);
         return true;
     }
     if ($object instanceof FileSync) {
         $c = new Criteria();
         $c->add(BatchJobLockPeer::OBJECT_ID, $object->getId());
         $c->add(BatchJobLockPeer::OBJECT_TYPE, BatchJobObjectType::FILE_SYNC);
         $c->add(BatchJobLockPeer::JOB_TYPE, BatchJobType::FILESYNC_IMPORT);
         $c->add(BatchJobLockPeer::STATUS, array(BatchJob::BATCHJOB_STATUS_RETRY, BatchJob::BATCHJOB_STATUS_PENDING), Criteria::IN);
         $fileSyncImportJobs = BatchJobLockPeer::doSelect($c);
         foreach ($fileSyncImportJobs as $fileSyncImportJob) {
             kJobsManager::abortDbBatchJob(BatchJobPeer::retrieveByPK($fileSyncImportJob->getId()));
         }
         return true;
     }
     if (!$object instanceof flavorAsset || !in_array(assetPeer::STATUS, $modifiedColumns)) {
         return true;
     }
     $entry = entryPeer::retrieveByPKNoFilter($object->getEntryId());
     KalturaLog::info("Asset id [" . $object->getId() . "] isOriginal [" . $object->getIsOriginal() . "] status [" . $object->getStatus() . "]");
     if ($object->getIsOriginal()) {
         return true;
     }
     if ($object->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_VALIDATING) {
         $postConvertAssetType = BatchJob::POSTCONVERT_ASSET_TYPE_FLAVOR;
         $offset = $entry->getThumbOffset();
         // entry getThumbOffset now takes the partner DefThumbOffset into consideration
         $syncKey = $object->getSyncKey(asset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET);
         $fileSync = kFileSyncUtils::getLocalFileSyncForKey($syncKey, false);
         if (!$fileSync) {
             return true;
         }
         $srcFileSyncLocalPath = kFileSyncUtils::getLocalFilePathForKey($syncKey);
         if ($srcFileSyncLocalPath) {
             kJobsManager::addPostConvertJob(null, $postConvertAssetType, $srcFileSyncLocalPath, $object->getId(), null, $entry->getCreateThumb(), $offset);
         }
     } elseif ($object->getStatus() == flavorAsset::FLAVOR_ASSET_STATUS_READY) {
         // If we get a ready flavor and the entry is in no content
         if ($entry->getStatus() == entryStatus::NO_CONTENT) {
             $entry->setStatus(entryStatus::PENDING);
             // we change the entry to pending
             $entry->save();
         }
     }
     return true;
 }