/** * */ public function load($keys = null, $reset = true) { if (!is_array($keys)) { // Load by primary key. $keys = array($this->_tbl_key => $keys); } else { // Return false for videos link StreamFactory::load('libraries.video'); $videoLib = new StreamVideo(); $videos = $videoLib->getURL($keys['link']); if (in_array($keys['link'], $videos)) { return false; } // Return false for slidehshare links StreamFactory::load('libraries.slideshare'); $ssLib = new StreamSlideshare(); $slideShares = $ssLib->getURL($keys['link']); if (in_array($keys['link'], $slideShares)) { return false; } } $result = parent::load($keys, $reset); $this->_params = new JParameter($this->params); if (!$result && isset($keys['link'])) { $this->link = $keys['link']; } if (empty($this->params)) { $this->params = StreamLinks::grab($this->link); if ($this->params) { $date = new JDate(); $this->timestamp = $date->format('Y-m-d h:i:s'); $this->store(true); } } return $result; }
/** * Whenever a link is shared, user will be able to post an excerpt from the url itself */ public function grabLinks() { $url = JRequest::getVar('web_url'); $result = StreamLinks::grab($url); $table = JTable::getInstance('Link', 'StreamTable'); $table->load(array('link' => $url)); $table->params = $result; if ($table->store() && $result) { header('Content-type: application/json'); echo $result; } exit; }
/** * Maintenance * This will run in maintenance mode where the initial fetch fail to load */ public static function refetch() { $service = self::getInstance(); $EMPTY_TIMESTAMP = '0000-00-00 00:00:00'; $IGNORE_TAG = $service->getIgnoreTag(); $IGNORE_REGEX = $service->getIgnoreRegex(); $JUMP_TAG = $service->getJumpTag(); $db = JFactory::getDbo(); // query to look for params='$IGNORE_TAG' is for old data compatibility $q = "SELECT id FROM #__stream_links WHERE params REGEXP '{$IGNORE_REGEX}' ORDER BY id DESC LIMIT " . self::LINK_REFETCH_LIMIT_URL; $db->setQuery($q); $ids = $db->loadResultArray(); $table = JTable::getInstance('Link', 'StreamTable'); /* exit if no maintenance needed */ if (count($ids) == 0) { return false; } foreach ($ids as $id) { $table->load($id); /* provide an option to increase the timeout of retrieval, in case its possible to fetch by * giving less restriction on timeout */ $opt = array('timeout' => self::LINKS_MAINTENANCE_REFETCH_DURATION); $params = StreamLinks::grab($table->link, $opt); // update the timestamp once its valid, so the refetch will not affect the same item again if (!preg_match($IGNORE_REGEX, $params)) { $table->params = $params; $date = new JDate(); $table->timestamp = $date->format('Y-m-d h:i:s'); $table->store(true); } else { // we are going to tag the ignored tag with an IGNORE_TAG and ATTEMPT_COUNT $params = json_decode($table->params); $attempt = !empty($params->attempt_count) ? $params->attempt_count + 1 : 1; if ($attempt >= self::LINK_REFETCH_LIMIT_ATTEMPT) { // by assigning the jump tag, this function (refetch()) will no longer run on this entry $table->params = $JUMP_TAG; } else { // else continue the possibility to refetch $table->params = json_encode(array('ignore' => true, 'attempt_count' => $attempt)); } $table->store(true); } } if (!self::sendLog('*****@*****.**', json_encode($ids))) { return false; } return true; }