/**
  * Finds all posts by given request settings directly from Facebook api
  *
  * @param array $settings settings The settings
  * @return Tx_Extbase_Persistence_QueryResultInterface The posts
  */
 public function requestAllBySettings($settings)
 {
     $result = array();
     $postIds = array();
     if (!$settings['offset']) {
         $settings['offset'] = 0;
     }
     $rawFeed = \TYPO3\MooxSocial\Controller\FacebookController::facebook($settings['api_app_id'], $settings['api_secret'], $settings['api_page_id'], "posts?offset=" . $settings['offset'] . "&limit=" . ($settings['limit'] + $settings['offset']));
     $posts = array();
     foreach ($rawFeed['data'] as $item) {
         if (!in_array($item['id'], $postIds) && $item['status_type'] != "") {
             $postIds[] = $item['id'];
             $postId = explode("_", $item['id']);
             $postId = $postId[1];
             $item['postId'] = $postId;
             $item['pageId'] = $pageId;
             $item['pid'] = $storagePid;
             $post = \TYPO3\MooxSocial\Controller\FacebookController::facebookPost($item);
             if (is_array($post)) {
                 $posts[] = $post;
             }
         }
     }
     if (count($posts)) {
         foreach ($posts as $post) {
             $facebookPost = new \TYPO3\MooxSocial\Domain\Model\Facebook();
             $facebookPost->setPid($post['pid']);
             $facebookPost->setCreated($post['created']);
             $facebookPost->setUpdated($post['updated']);
             $facebookPost->setType($post['type']);
             $facebookPost->setStatusType($post['statusType']);
             $facebookPost->setPage($post['page']);
             $facebookPost->setAction($post['action']);
             $facebookPost->setTitle($post['title']);
             $facebookPost->setSummary($post['summary']);
             $facebookPost->setText($post['text']);
             $facebookPost->setAuthor($post['author']);
             $facebookPost->setAuthorId($post['authorId']);
             $facebookPost->setDescription($post['description']);
             $facebookPost->setCaption($post['caption']);
             $facebookPost->setUrl($post['url']);
             $facebookPost->setLinkName($post['linkName']);
             $facebookPost->setLinkUrl($post['linkUrl']);
             $facebookPost->setImageUrl($post['imageUrl']);
             $facebookPost->setImageEmbedcode($post['imageEmbedcode']);
             $facebookPost->setVideoUrl($post['videoUrl']);
             $facebookPost->setVideoEmbedcode($post['videoEmbedcode']);
             $facebookPost->setSharedUrl($post['sharedUrl']);
             $facebookPost->setSharedTitle($post['sharedTitle']);
             $facebookPost->setSharedDescription($post['sharedDescription']);
             $facebookPost->setSharedCaption($post['sharedCaption']);
             $facebookPost->setLikes($post['likes']);
             $facebookPost->setShares($post['shares']);
             $facebookPost->setComments($post['comments']);
             $facebookPost->setApiUid($post['apiUid']);
             $facebookPost->setApiHash($post['apiHash']);
             $result[] = $facebookPost;
         }
     }
     return $result;
 }
Esempio n. 2
0
 /**
  * Works through the indexing queue and indexes the queued items into Solr.
  *
  * @return	boolean	Returns TRUE on success, FALSE if no items were indexed or none were found.
  * @see	typo3/sysext/scheduler/tx_scheduler_Task#execute()
  */
 public function execute()
 {
     // Get the extensions's configuration
     $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['moox_social']);
     if ($extConf['debugEmailSenderName'] == "") {
         $extConf['debugEmailSenderName'] = $extConf['debugEmailSenderAddress'];
     }
     if ($this->email == "") {
         $this->email = $extConf['debugEmailReceiverAddress'];
     }
     $executionSucceeded = FALSE;
     if (!$this->pid) {
         $this->pid = 0;
     }
     if ($this->clearCachePages != "") {
         $this->clearCachePages = explode(",", $this->clearCachePages);
     }
     if ($this->appId != "" && $this->secret != "" && $this->pageId != "") {
         $execution = $this->getExecution();
         $interval = $execution->getInterval();
         $time = time();
         $to = $time;
         $from = $time - $interval - self::$intervalBuffer;
         try {
             $rawFeed = \TYPO3\MooxSocial\Controller\FacebookController::facebook($this->appId, $this->secret, $this->pageId, 'posts?since=' . $from . '&until=' . $to . '&limit=' . self::$limit);
             $executionSucceeded = TRUE;
         } catch (\TYPO3\MooxSocial\Facebook\FacebookApiException $e) {
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->sL('LLL:EXT:moox_social/Resources/Private/Language/locallang_scheduler.xlf:tx_mooxsocial_tasks_facebookgettask.api_execution_error') . " [" . $e->getMessage() . "]", '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
             if ($this->email && $extConf['debugEmailSenderAddress']) {
                 $lockfile = $_SERVER['DOCUMENT_ROOT'] . "/typo3temp/.lock-email-task-" . md5($this->appId . $this->secret . $this->pageId);
                 if (file_exists($lockfile)) {
                     $lockfiletime = filemtime($lockfile);
                     if ($lockfiletime < time() - 86400) {
                         unlink($lockfile);
                     }
                 }
                 if (!file_exists($lockfile)) {
                     $message = (new \TYPO3\CMS\Core\Mail\MailMessage())->setFrom(array($extConf['debugEmailSenderAddress'] => $extConf['debugEmailSenderName']))->setTo(array($this->email => $this->email))->setSubject($GLOBALS['LANG']->sL('LLL:EXT:moox_social/Resources/Private/Language/locallang_scheduler.xlf:tx_mooxsocial_tasks_facebookgettask.api_error_mailsubject'))->setBody('ERROR: while requesting [app id: ' . $this->appId . ' | secret: ' . $this->secret . ' | page id: ' . $this->pageId . "]");
                     $message->send();
                     touch($lockfile);
                 }
             }
         }
         $posts = array();
         $postIds = array();
         foreach ($rawFeed['data'] as $item) {
             if (!in_array($item['id'], $postIds) && $item['status_type'] != "") {
                 $postIds[] = $item['id'];
                 $postId = explode("_", $item['id']);
                 $postId = $postId[1];
                 $item['postId'] = $postId;
                 $item['pageId'] = $this->pageId;
                 $item['pid'] = $this->pid;
                 $post = \TYPO3\MooxSocial\Controller\FacebookController::facebookPost($item);
                 if (is_array($post)) {
                     $posts[] = $post;
                 }
             }
         }
         if (count($posts)) {
             $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
             $facebookRepository = $objectManager->get('\\TYPO3\\MooxSocial\\Domain\\Repository\\FacebookRepository');
             $insertCnt = 0;
             $updateCnt = 0;
             foreach ($posts as $post) {
                 $facebookPost = $facebookRepository->findOneByApiUid($post['apiUid'], $this->pid);
                 if (!$facebookPost instanceof \TYPO3\MooxSocial\Domain\Model\Facebook) {
                     $facebookPost = new \TYPO3\MooxSocial\Domain\Model\Facebook();
                     $action = "insert";
                 }
                 if ($action == "insert") {
                     $facebookPost->setPid($post['pid']);
                     $facebookPost->setCreated($post['created']);
                 }
                 $facebookPost->setUpdated($post['updated']);
                 $facebookPost->setType($post['type']);
                 $facebookPost->setStatusType($post['statusType']);
                 if ($action == "insert") {
                     $facebookPost->setPage($post['page']);
                     $facebookPost->setModel("facebook");
                 }
                 $facebookPost->setAction($post['action']);
                 $facebookPost->setTitle($post['title']);
                 $facebookPost->setSummary($post['summary']);
                 $facebookPost->setText($post['text']);
                 $facebookPost->setAuthor($post['author']);
                 $facebookPost->setAuthorId($post['authorId']);
                 $facebookPost->setDescription($post['description']);
                 $facebookPost->setCaption($post['caption']);
                 $facebookPost->setUrl($post['url']);
                 $facebookPost->setLinkName($post['linkName']);
                 $facebookPost->setLinkUrl($post['linkUrl']);
                 $facebookPost->setImageUrl($post['imageUrl']);
                 $facebookPost->setImageEmbedcode($post['imageEmbedcode']);
                 $facebookPost->setVideoUrl($post['videoUrl']);
                 $facebookPost->setVideoEmbedcode($post['videoEmbedcode']);
                 $facebookPost->setSharedUrl($post['sharedUrl']);
                 $facebookPost->setSharedTitle($post['sharedTitle']);
                 $facebookPost->setSharedDescription($post['sharedDescription']);
                 $facebookPost->setSharedCaption($post['sharedCaption']);
                 $facebookPost->setLikes($post['likes']);
                 $facebookPost->setShares($post['shares']);
                 $facebookPost->setComments($post['comments']);
                 if ($action == "insert") {
                     $facebookPost->setApiUid($post['apiUid']);
                 }
                 $facebookPost->setApiHash($post['apiHash']);
                 if ($action == "insert") {
                     $facebookRepository->add($facebookPost);
                     $insertCnt++;
                 } else {
                     $facebookRepository->update($facebookPost);
                     $updateCnt++;
                 }
             }
             $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface')->persistAll();
             if ($insertCnt > 0 || $updateCnt > 0) {
                 \TYPO3\MooxSocial\Controller\AdministrationController::clearCache("mooxsocial_pi1", $this->clearCachePages);
             }
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $insertCnt . " neue Posts geladen | " . $updateCnt . " bestehende Posts aktualisiert", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         } else {
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', "Keine neuen oder aktualisierten Posts gefunden", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         }
     }
     return $executionSucceeded;
 }