seek_quarry
[ class tree: seek_quarry ] [ index: seek_quarry ] [ all elements ]

Source for file odp_rdf_bundle_iterator.php

Documentation is available at odp_rdf_bundle_iterator.php

  1. <?php
  2. /**
  3.  *  SeekQuarry/Yioop --
  4.  *  Open Source Pure PHP Search Engine, Crawler, and Indexer
  5.  *
  6.  *  Copyright (C) 2009 - 2013  Chris Pollett chris@pollett.org
  7.  *
  8.  *  LICENSE:
  9.  *
  10.  *  This program is free software: you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation, either version 3 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  *
  23.  *  END LICENSE
  24.  *
  25.  * @author Chris Pollett chris@pollett.org
  26.  * @package seek_quarry
  27.  * @subpackage iterator
  28.  * @license http://www.gnu.org/licenses/ GPL3
  29.  * @link http://www.seekquarry.com/
  30.  * @copyright 2009 - 2013
  31.  * @filesource
  32.  */
  33.  
  34. if(!defined('BASE_DIR')) {echo "BAD REQUEST"exit();}
  35.  
  36. /**
  37.  *Loads base class for iterating
  38.  */
  39. require_once BASE_DIR.
  40.     '/lib/archive_bundle_iterators/text_archive_bundle_iterator.php';
  41.  
  42. /**
  43.  * Used to iterate through the records of a collection of one or more open
  44.  * directory RDF files stored in a WebArchiveBundle folder. Open Directory
  45.  * file can be found at http://rdf.dmoz.org/ .  Iteration would be
  46.  * for the purpose making an index of these records
  47.  *
  48.  * @author Chris Pollett
  49.  * @package seek_quarry
  50.  * @subpackage iterator
  51.  * @see WebArchiveBundle
  52.  */
  53.     implements CrawlConstants
  54. {
  55.     /**
  56.      *  Associative array containing global properties like base url of the
  57.      *  current open odp rdf file
  58.      *  @var array 
  59.      */
  60.     var $header;
  61.  
  62.  
  63.     /**
  64.      * How many bytes to read into buffer from gzip stream in one go
  65.      * @var int 
  66.      */
  67.     const BLOCK_SIZE = 1024;
  68.  
  69.     /**
  70.      * Creates an open directory rdf archive iterator with the given parameters.
  71.      *
  72.      * @param string $iterate_timestamp timestamp of the arc archive bundle to
  73.      *       iterate  over the pages of
  74.      * @param string $iterate_dir folder of files to iterate over
  75.      * @param string $result_timestamp timestamp of the arc archive bundle
  76.      *       results are being stored in
  77.      * @param string $result_dir where to write last position checkpoints to
  78.      */
  79.     function __construct($iterate_timestamp$iterate_dir,
  80.             $result_timestamp$result_dir)
  81.     {
  82.         $ini array'compression' => 'gzip',
  83.             'file_extension' => 'gz',
  84.             'encoding' => 'UTF-8',
  85.             'start_delimiter' => '@Topic|ExternalPage@',
  86.             'end_delimiter' => '@/Topic|/ExternalPage@');
  87.         parent::__construct($iterate_timestamp$iterate_dir,
  88.             $result_timestamp$result_dir$ini);
  89.         $this->header['base_address'"http://www.dmoz.org/";
  90.         $url_parts @parse_url($this->header['base_address']);
  91.         $this->header['ip_address'gethostbyname($url_parts['host']);
  92.     }
  93.  
  94.     /**
  95.      * Estimates the important of the site according to the weighting of
  96.      * the particular archive iterator
  97.      * @param $site an associative array containing info about a web page
  98.      * @return int a 4-bit number based on the topic path of the odp entry
  99.      *       (@see processTopic @see processExternalPage)
  100.      */
  101.     function weight(&$site)
  102.     {
  103.         return min($site[self::WEIGHT]15);
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Gets the text content of the first dom node satisfying the
  109.      * xpath expression $path in the dom document $dom
  110.      *
  111.      * @param object $dom DOMDocument to get the text from
  112.      * @param $path xpath expression to find node with text
  113.      *
  114.      * @return string text content of the given node if it exists
  115.      */
  116.     function getTextContent($dom$path)
  117.     {
  118.         $xpath new DOMXPath($dom);
  119.         $objects $xpath->evaluate($path);
  120.         if($objects  && is_object($objects&& $objects->item(0!= NULL{
  121.             return $objects->item(0)->textContent;
  122.         }
  123.         return "";
  124.     }
  125.  
  126.     /**
  127.      * Gets the value of the attribute $attribute for each dom node
  128.      * satisfying the xpath expression $path in the dom document $dom
  129.      *
  130.      * @param object $dom DOMDocument to get the text from
  131.      * @param $path xpath expression to find node with text
  132.      * @param string $attribute name of the attribute to get the values for
  133.      *
  134.      * @return array of values of the given attribute
  135.      */
  136.     function getAttributeValueAll($dom$path$attribute)
  137.     {
  138.         $values array();
  139.         $xpath new DOMXPath($dom);
  140.         $objects $xpath->evaluate($path);
  141.         if($objects  && is_object($objects)) {
  142.             foreach($objects as $object{
  143.                 $value $object->getAttribute($attribute);
  144.                 if($value{
  145.                     $values[$value;
  146.                 }
  147.             }
  148.         }
  149.         return $values;
  150.     }
  151.  
  152.     /**
  153.      * Gets the value of the attribute $attribute of the first dom node
  154.      * satisfying the xpath expression $path in the dom document $dom
  155.      *
  156.      * @param object $dom DOMDocument to get the text from
  157.      * @param $path xpath expression to find node with text
  158.      * @param string $attribute name of the attribute to get the value for
  159.      *
  160.      * @return string value of the given attribute
  161.      */
  162.     function getAttributeValue($dom$path,  $attribute)
  163.     {
  164.         $xpath new DOMXPath($dom);
  165.         $objects $xpath->evaluate($path);
  166.         if($objects  && is_object($objects&& $objects->item(0!= NULL{
  167.             return $objects->item(0)->getAttribute($attribute);
  168.         }
  169.         return "";
  170.     }
  171.  
  172.     /**
  173.      * Gets the next doc from the iterator
  174.      * @param bool $no_process do not do any processing on page data
  175.      * @return array associative array for doc or string if no_process true
  176.      */
  177.     function nextPage($no_process false)
  178.     {
  179.         if(!$this->checkFileHandle()) return NULL;
  180.         $tag_data $this->getNextTagsData(
  181.             array("Topic","ExternalPage"));
  182.         if(!$tag_data{
  183.             return false;
  184.         }
  185.         list($page_info$tag$tag_data;
  186.         if($no_processreturn $page_info}
  187.         $page_info str_replace("r:id","id"$page_info);
  188.         $page_info str_replace("r:resource","resource"$page_info);
  189.         $page_info str_replace("d:Title","Title"$page_info);
  190.         $page_info str_replace("d:Description","Description"$page_info);
  191.         $dom new DOMDocument();
  192.         $dom->loadXML($page_info);
  193.         $processMethod "process".$tag;
  194.         $site[self::IP_ADDRESSESarray($this->header['ip_address']);
  195.         $site[self::MODIFIEDtime();
  196.         $site[self::TIMESTAMPtime();
  197.         $site[self::TYPE"text/html";
  198.         $site[self::HEADER"odp_rdf_bundle_iterator extractor";
  199.         $site[self::HTTP_CODE200;
  200.         $site[self::ENCODING"UTF-8";
  201.         $site[self::SERVER"unknown";
  202.         $site[self::SERVER_VERSION"unknown";
  203.         $site[self::OPERATING_SYSTEM"unknown";
  204.         $this->$processMethod($dom$site);
  205.  
  206.         $site[self::HASHFetchUrl::computePageHash($site[self::PAGE]);
  207.  
  208.         return $site;
  209.     }
  210.  
  211.     /**
  212.      *  Computes an HTML page for a Topic tag parsed from the ODP RDF
  213.      *  document
  214.      *
  215.      *  @param object $dom document object for one Topic tag tag
  216.      *  @param array &$site a reference to an array of header and page info
  217.      *       for an html page
  218.      */
  219.     function processTopic($dom&$site)
  220.     {
  221.         $topic_path $this->getAttributeValue($dom"/Topic""id");
  222.         $site[self::URL$this->header['base_address'].$topic_path;
  223.  
  224.         $site[self::WEIGHTmax(15 substr_count($topic_path"/")1);
  225.         $title str_replace("/"" "$topic_path);
  226.         $links $this->computeTopicLinks($topic_path);
  227.  
  228.         $topic_link1 $this->getAttributeValue($dom"/Topic/link1",
  229.             "resource");
  230.         if($topic_link1{
  231.             $links[$topic_link1$topic_link1." - ".$title;
  232.         }
  233.  
  234.         $topic_links $this->getAttributeValueAll($dom"/Topic/link",
  235.             "resource");
  236.         if($topic_links != NULL{
  237.             foreach($topic_links as $topic_link{
  238.                 $links[$topic_link$topic_link." - ".$title;
  239.             }
  240.         }
  241.         $site[self::PAGE"<html>\n".
  242.             "<head><title>$title</title></head>\n"
  243.             ."<body><h1>$title</h1>\n";
  244.         $site[self::PAGE.= $this->linksToHtml($links);
  245.         $site[self::PAGE.= "</body></html>";
  246.  
  247.     }
  248.  
  249.     /**
  250.      *  Computes an HTML page for an ExternalPage tag parsed from the ODP RDF
  251.      *  document
  252.      *
  253.      *  @param object $dom document object for one Topic tag tag
  254.      *  @param array &$site a reference to an array of header and page info
  255.      *       for an html page
  256.      */
  257.     function processExternalPage($dom&$site)
  258.     {
  259.         $site[self::URL$this->getAttributeValue($dom,
  260.             "/ExternalPage""about");
  261.  
  262.         $topic_path $this->getTextContent($dom"/ExternalPage/topic");
  263.         $site[self::WEIGHTmax(14 substr_count($topic_path"/")1);
  264.  
  265.         $links $this->computeTopicLinks($topic_path);
  266.         $title $this->getTextContent($dom"/ExternalPage/Title");
  267.         $title "$title - ".str_replace("/"" "$topic_path);
  268.         $description $this->getTextContent(
  269.             $dom"/ExternalPage/Description");
  270.  
  271.         $site[self::PAGE"<html>\n".
  272.             "<head><title>$title</title></head>\n"
  273.             ."<body><h1>$title</h1>\n";
  274.         $site[self::PAGE.= $this->linksToHtml($links);
  275.         $site[self::PAGE.= "<div>$description</div></body></html>";
  276.     }
  277.  
  278.     /**
  279.      *  Computes links for prefix topics of an ODP topic path
  280.      *
  281.      *  @param string $topic_path to compute links for
  282.      *  @return array url => text pairs for each prefix of path
  283.      */
  284.     function computeTopicLinks($topic_path)
  285.     {
  286.         $links array();
  287.         $topic_parts explode("/"$topic_path);
  288.         $path "";
  289.  
  290.         foreach($topic_parts as $part){
  291.             $path .= "/$part";
  292.             $links[$this->header['base_address'].$path$part;
  293.         }
  294.         return $links;
  295.     }
  296.  
  297.     /**
  298.      *  Makes an unordered HTML list out of an associative array of
  299.      *  url => link_text pairs.
  300.      *
  301.      *  @param array $links url=>link_text pairs
  302.      *  @return string containing html for unorderlisted list of links
  303.      */
  304.     function linksToHtml($links)
  305.     {
  306.         $html "";
  307.         if(count($links0{
  308.             $html .= "<ul>\n";
  309.             foreach($links as $url => $text{
  310.                 $html .= '<li><a href="'.
  311.                     $url.'">'.$text.'</a></li>';
  312.             }
  313.             $html .= "</ul>\n";
  314.         }
  315.         return $html;
  316.     }
  317.  
  318. }
  319. ?>

Documentation generated by phpDocumentor 1.4.3