示例#1
0
文件: Batch.php 项目: burtay/bdApi
 public function actionPostIndex()
 {
     $input = file_get_contents('php://input');
     $batchJobs = @json_decode($input, true);
     if (empty($batchJobs)) {
         return $this->responseError(new XenForo_Phrase('bdapi_slash_batch_requires_json'), 400);
     }
     $jobsOutput = array();
     foreach ($batchJobs as $batchJob) {
         if (empty($batchJob['uri'])) {
             continue;
         }
         if (empty($batchJob['id'])) {
             $number = 0;
             do {
                 if ($number == 0) {
                     $id = $batchJob['uri'];
                 } else {
                     $id = sprintf('%s_%d', $batchJob['uri'], $number);
                 }
                 $number++;
             } while (isset($jobsOutput[$id]));
         } else {
             $id = $batchJob['id'];
         }
         if (empty($batchJob['method'])) {
             $method = 'GET';
         } else {
             $method = strtoupper($batchJob['method']);
         }
         if (empty($batchJob['params']) or !is_array($batchJob['params'])) {
             $params = array();
         } else {
             $params = $batchJob['params'];
         }
         $params = array_merge($this->_extractUriParams($batchJob['uri']), $params);
         $jobsOutput[$id] = bdApi_Data_Helper_Batch::doJob($method, $batchJob['uri'], $params);
     }
     $data = array('jobs' => $jobsOutput);
     return $this->responseData('bdApi_ViewApi_Batch_Index', $data);
 }
示例#2
0
 public function prepareApiContentDataForSearch(array $preparedResults)
 {
     $clientContentIds = array();
     $threadIds = array();
     $postIds = array();
     $profilePostIds = array();
     $data = array();
     foreach ($preparedResults as $key => $preparedResult) {
         switch ($preparedResult['content_type']) {
             case 'api_client_content':
                 $clientContentIds[$preparedResult['content_id']] = $key;
                 break;
             case 'thread':
                 $threadIds[$preparedResult['content_id']] = $key;
                 break;
             case 'post':
                 $postIds[$preparedResult['content_id']] = $key;
                 break;
             case 'profile_post':
                 $profilePostIds[$preparedResult['content_id']] = $key;
                 break;
         }
     }
     if (!empty($clientContentIds)) {
         /** @var bdApi_Model_ClientContent $clientContentModel */
         $clientContentModel = $this->getModelFromCache('bdApi_Model_ClientContent');
         $clientContents = $clientContentModel->getClientContents(array('client_content_id' => array_keys($clientContentIds)));
         foreach ($clientContents as $clientContentId => $clientContent) {
             if (!isset($clientContentIds[$clientContentId])) {
                 // key not found?!
                 continue;
             }
             $key = $clientContentIds[$clientContentId];
             $data[$key] = array_merge($preparedResults[$key], array_intersect_key($clientContent, array_flip(array('title', 'body', 'date', 'link', 'user_id'))));
         }
     }
     if (!empty($threadIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['thread_ids'] = implode(',', array_keys($threadIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'threads', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['threads'])) {
             foreach ($dataJob['_job_response']->params['threads'] as $thread) {
                 if (empty($thread['thread_id']) || !isset($threadIds[$thread['thread_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $threadIds[$thread['thread_id']];
                 $data[$key] = array_merge($preparedResults[$key], $thread);
             }
         }
     }
     if (!empty($postIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['post_ids'] = implode(',', array_keys($postIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'posts', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['posts'])) {
             foreach ($dataJob['_job_response']->params['posts'] as $post) {
                 if (empty($post['post_id']) || !isset($postIds[$post['post_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $postIds[$post['post_id']];
                 if (!empty($post['thread']['first_post'])) {
                     // the found post is a reply
                     $data[$key] = array_merge($preparedResults[$key], $post);
                 } else {
                     // the found post is a first post, return as thread instead
                     $thread = $post['thread'];
                     unset($post['thread']);
                     $thread['first_post'] = $post;
                     $data[$key] = array_merge(array('content_type' => 'thread', 'content_id' => $thread['thread_id']), $thread);
                 }
             }
         }
     }
     if (!empty($profilePostIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['profile_post_ids'] = implode(',', array_keys($profilePostIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'profile-posts', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['profile_posts'])) {
             foreach ($dataJob['_job_response']->params['profile_posts'] as $profilePost) {
                 if (empty($profilePost['profile_post_id']) || !isset($profilePostIds[$profilePost['profile_post_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $profilePostIds[$profilePost['profile_post_id']];
                 $data[$key] = array_merge($preparedResults[$key], $profilePost);
             }
         }
     }
     ksort($data);
     return $data;
 }
示例#3
0
文件: Search.php 项目: burtay/bdApi
 public function prepareApiContentDataForSearch(array $preparedResults)
 {
     $threadIds = array();
     $postIds = array();
     $profilePostIds = array();
     $data = array();
     foreach ($preparedResults as $key => $preparedResult) {
         switch ($preparedResult['content_type']) {
             case 'thread':
                 $threadIds[$preparedResult['content_id']] = $key;
                 break;
             case 'post':
                 $postIds[$preparedResult['content_id']] = $key;
                 break;
             case 'profile_post':
                 $profilePostIds[$preparedResult['content_id']] = $key;
                 break;
         }
     }
     if (!empty($threadIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['thread_ids'] = implode(',', array_keys($threadIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'threads', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['threads'])) {
             foreach ($dataJob['_job_response']->params['threads'] as $thread) {
                 if (empty($thread['thread_id']) || !isset($threadIds[$thread['thread_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $threadIds[$thread['thread_id']];
                 $data[$key] = array_merge($preparedResults[$key], $thread);
             }
         }
     }
     if (!empty($postIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['post_ids'] = implode(',', array_keys($postIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'posts', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['posts'])) {
             foreach ($dataJob['_job_response']->params['posts'] as $post) {
                 if (empty($post['post_id']) || !isset($postIds[$post['post_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $postIds[$post['post_id']];
                 if (!empty($post['thread']['first_post'])) {
                     // the found post is a reply
                     $data[$key] = array_merge($preparedResults[$key], $post);
                 } else {
                     // the found post is a first post, return as thread instead
                     $thread = $post['thread'];
                     unset($post['thread']);
                     $thread['first_post'] = $post;
                     $data[$key] = array_merge(array('content_type' => 'thread', 'content_id' => $thread['thread_id']), $thread);
                 }
             }
         }
     }
     if (!empty($profilePostIds)) {
         // fetch the first few thread data as a bonus
         $dataJobParams = array();
         $dataJobParams['profile_post_ids'] = implode(',', array_keys($profilePostIds));
         $dataJob = bdApi_Data_Helper_Batch::doJob('GET', 'profile-posts', $dataJobParams);
         if (isset($dataJob['_job_response']) && !empty($dataJob['_job_response']->params['profile_posts'])) {
             foreach ($dataJob['_job_response']->params['profile_posts'] as $profilePost) {
                 if (empty($profilePost['profile_post_id']) || !isset($profilePostIds[$profilePost['profile_post_id']])) {
                     // key not found?!
                     continue;
                 }
                 $key = $profilePostIds[$profilePost['profile_post_id']];
                 $data[$key] = array_merge($preparedResults[$key], $profilePost);
             }
         }
     }
     ksort($data);
     return $data;
 }