/** * Save expanded version of all unexpanded URLs to data store, as well as intermediary short links. */ public function expandOriginalURLs($flickr_api_key = null) { $links_to_expand = $this->link_dao->getLinksToExpand($this->link_limit); $this->logger->logUserInfo(count($links_to_expand) . " links to expand. Please wait. Working...", __METHOD__ . ',' . __LINE__); $total_expanded = 0; $total_errors = 0; $has_expanded_flickr_link = false; foreach ($links_to_expand as $index => $link) { if (Utils::validateURL($link->url)) { $endless_loop_prevention_counter = 0; $this->logger->logInfo("Expanding " . ($total_expanded + 1) . " of " . count($links_to_expand) . " (" . $link->url . ")", __METHOD__ . ',' . __LINE__); //make sure shortened short links--like t.co--get fully expanded $fully_expanded = false; $short_link = $link->url; while (!$fully_expanded) { //begin Flickr thumbnail processing if (isset($flickr_api_key) && substr($short_link, 0, strlen('http://flic.kr/')) == 'http://flic.kr/') { self::expandFlickrThumbnail($flickr_api_key, $short_link, $link->url); $has_expanded_flickr_link = true; $fully_expanded = true; } //end Flickr thumbnail processing $expanded_url = URLExpander::expandURL($short_link, $link->url, $index, count($links_to_expand), $this->link_dao, $this->logger); if ($expanded_url == $short_link || $expanded_url == '' || $endless_loop_prevention_counter > self::EXPANSION_CAP) { $fully_expanded = true; } else { $this->short_link_dao->insert($link->id, $short_link); } $short_link = $expanded_url; $endless_loop_prevention_counter++; } if (!$has_expanded_flickr_link) { if ($expanded_url != '') { $image_src = URLProcessor::getImageSource($expanded_url); $this->link_dao->saveExpandedUrl($link->url, $expanded_url, '', $image_src); $total_expanded = $total_expanded + 1; } else { $this->logger->logError($link->url . " not a valid URL - relocates to nowhere", __METHOD__ . ',' . __LINE__); $this->link_dao->saveExpansionError($link->url, "Invalid URL - relocates to nowhere"); $total_errors = $total_errors + 1; } } } else { $total_errors = $total_errors + 1; $this->logger->logError($link->url . " not a valid URL", __METHOD__ . ',' . __LINE__); $this->link_dao->saveExpansionError($link->url, "Invalid URL"); } $has_expanded_flickr_link = false; } $this->logger->logUserSuccess($total_expanded . " URLs successfully expanded (" . $total_errors . " errors).", __METHOD__ . ',' . __LINE__); }
function testSaveExpandedUrl() { $ldao = new LinkDAO($this->db, $this->logger); $linkstoexpand = $ldao->getLinksToExpand(); $link = $linkstoexpand[0]; $ldao->saveExpandedUrl($link, "http://expandedurl.com"); $updatedlink = $ldao->getLinkByUrl($link); $this->assertEqual($updatedlink->expanded_url, "http://expandedurl.com"); $ldao->saveExpandedUrl($link, "http://expandedurl1.com", 'my title'); $updatedlink = $ldao->getLinkByUrl($link); $this->assertEqual($updatedlink->expanded_url, "http://expandedurl1.com"); $this->assertEqual($updatedlink->title, "my title"); $ldao->saveExpandedUrl($link, "http://expandedurl2.com", 'my title1', 1); $updatedlink = $ldao->getLinkByUrl($link); $this->assertEqual($updatedlink->expanded_url, "http://expandedurl2.com"); $this->assertEqual($updatedlink->title, "my title1"); $this->assertTrue($updatedlink->is_image); }
function expandurls_crawl() { global $THINKTANK_CFG; global $db; global $conn; $logger = new Logger($THINKTANK_CFG['log_location']); $ldao = new LinkDAO($db, $logger); $linkstoexpand = $ldao->getLinksToExpand(); $logger->logStatus(count($linkstoexpand) . " links to expand", "Expand URLs Plugin"); foreach ($linkstoexpand as $l) { $eurl = untinyurl($l, $logger, $ldao); if ($eurl != '') { $ldao->saveExpandedUrl($l, $eurl); } } $logger->logStatus("URL expansion complete for this run", "Expand URLs Plugin"); $logger->close(); # Close logging }