Changeset 15861

Show
Ignore:
Timestamp:
07/15/08 23:55:39 (5 years ago)
Author:
sasezaki
Message:

lang/php/Scraper: abstactのgetValuesにて処理するように

Location:
lang/php/Scraper/library/Diggin
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/php/Scraper/library/Diggin/Scraper.php

    r15813 r15861  
    306306        $context = new Diggin_Scraper_Context($this->getStrategy($resource)); 
    307307        foreach (self::$_processes as $process) { 
    308             $values = self::$_strategy->getValue($context, $process); 
    309  
    310             if ($process->filters) { 
    311                 require_once 'Diggin/Scraper/Filter.php'; 
    312                 $values = Diggin_Scraper_Filter::run($values, $process->filters); 
    313             } 
    314    
     308            $values = self::$_strategy->getValues($context, $process); 
     309 
    315310            $this->results[$process->name] = $values; 
    316311        } 
  • lang/php/Scraper/library/Diggin/Scraper/Strategy/Abstract.php

    r14732 r15861  
    3939    } 
    4040     
    41     /** 
    42      *  
    43      */ 
    44     public function getData() 
    45     { 
    46         //if !is_readble($this->getBody)... 
    47          
    48         return $this->readData($this->getResponse()); 
    49     } 
    50      
    5141    public function scrapedData($process) 
    5242    {    
     
    6151    protected abstract function scrape($response, $process); 
    6252     
    63     protected abstract function getValue($context, $process); 
     53    protected abstract function getValue($values, $process); 
     54     
     55    protected abstract static function extract($values, $process); 
     56 
     57    public function getValues($context, $process) 
     58    { 
     59        if (!isset($process->type)) { 
     60            return $context->scrape($process); 
     61        } 
     62         
     63        if ($context instanceof Diggin_Scraper_Context) { 
     64            $values = $context->scrape($process); 
     65        } else { 
     66            $values = $this->extract($context, $process); 
     67        } 
     68                 
     69        if ($process->type instanceof scraper) { 
     70            foreach ($values as $count => $val) { 
     71                foreach ($process->type->processes as $proc) { 
     72                    $returns[$count][$proc->name] = $this->getValues($val, $proc); 
     73                } 
     74                 
     75                if (($process->arrayflag === false) && $count === 0) break; 
     76            } 
     77           return $returns; 
     78        } 
     79         
     80        $values = $this->getValue($values, $process); 
     81         
     82        if ($process->arrayflag === false && strtoupper($process->type) === 'RAW') { 
     83            $values = array_shift($values); 
     84        } elseif ($process->arrayflag === false) { 
     85            $values = (string) array_shift($values); 
     86        } 
     87         
     88        if ($process->filters) { 
     89            require_once 'Diggin/Scraper/Filter.php'; 
     90            $values = Diggin_Scraper_Filter::run($values, $process->filters); 
     91        } 
     92         
     93        return $values; 
     94    } 
    6495} 
  • lang/php/Scraper/library/Diggin/Scraper/Strategy/Selector.php

    r15814 r15861  
    1616 
    1717require_once 'Diggin/Scraper/Strategy/Abstract.php'; 
    18  
     18require_once dirname(__FILE__).'/Selector/sfDomCssSelector.class.php'; 
    1919class Diggin_Scraper_Strategy_Selector extends Diggin_Scraper_Strategy_Abstract  
    2020{     
     
    7171        $simplexml = $this->getAdapter()->readData($respose); 
    7272 
     73        return self::extract($simplexml, $process); 
     74    } 
     75     
     76    public static function extract($simplexml, $process) 
     77    { 
    7378        $dom = dom_import_simplexml($simplexml); 
    74  
    75         require_once dirname(__FILE__).'/Selector/sfDomCssSelector.class.php'; 
     79         
    7680        $selector = new sfDomCssSelector($dom); 
    7781         
     
    8387        return $results; 
    8488    } 
     89     
    8590 
    8691    /** 
     
    9196     * @return mixed 
    9297     */ 
    93     public function getValue($context, $process) 
    94     { 
    95          
    96         if (!isset($process->type)) { 
    97             return $context->scrape($process); 
    98         } 
    99          
    100         if ($context instanceof Diggin_Scraper_Context) { 
    101             $values = $context->scrape($process); 
    102         } else { 
    103             $dom = dom_import_simplexml($context);  
    104             $selector = new sfDomCssSelector($dom); 
    105              
    106             $values = array();        
    107             foreach ($selector->getElements($process->expression) as $result) { 
    108                 $values[] = simplexml_import_dom($result);  
    109             } 
    110         } 
    111  
    112         if ($process->type instanceof scraper) { 
    113             foreach ($values as $count => $val) { 
    114                 foreach ($process->type->processes as $proc) { 
    115                     $returns[$count][$proc->name] = $this->getValue($val, $proc); 
    116                 } 
    117             } 
    118              
    119             if($process->arrayflag === false) { 
    120                 $returns = array_shift($returns); 
    121             } 
    122              
    123            return $returns; 
    124         } 
    125          
     98    public function getValue($values, $process) 
     99    {         
    126100        //type 
    127101        if (strtoupper(($process->type)) === 'RAW'){ 
     
    161135        } 
    162136         
    163         if ($process->arrayflag === false && strtoupper($process->type) === 'RAW') { 
    164             $strings = array_shift($strings); 
    165         } elseif ($process->arrayflag === false) { 
    166             $strings = (string) array_shift($strings); 
    167         } 
    168  
    169137        return $strings; 
    170138    } 
  • lang/php/Scraper/library/Diggin/Scraper/Strategy/Xpath.php

    r15814 r15861  
    7272    { 
    7373        $simplexml = $this->getAdapter()->readData($respose); 
    74   
    75         $results = array();        
    76         foreach ($simplexml->xpath($process->expression) as $result) { 
     74         
     75        return self::extract($simplexml, $process); 
     76    } 
     77     
     78    public static function extract($values, $process) 
     79    { 
     80        $results = array(); 
     81        foreach ($values->xpath($process->expression) as $result) { 
    7782            $results[] = $result;  
    7883        } 
     
    8085        return $results; 
    8186    } 
    82      
     87 
    8388    /** 
    8489     * get value with DSL 
     
    8893     * @return mixed 
    8994     */ 
    90     public function getValue($context, $process) 
     95    public function getValue($values, $process) 
    9196    { 
    92         if (!isset($process->type)) { 
    93             return $context->scrape($process); 
    94         } 
    95          
    96         if ($context instanceof Diggin_Scraper_Context) { 
    97             $values = $context->scrape($process); 
    98         } else { 
    99              
    100             $values = array(); 
    101             foreach ($context->xpath($process->expression) as $result) { 
    102                 $values[] = $result;  
    103             } 
    104         } 
    105                  
    106         if ($process->type instanceof scraper) { 
    107             foreach ($values as $count => $val) { 
    108                 foreach ($process->type->processes as $proc) { 
    109                     $returns[$count][$proc->name] = $this->getValue($val, $proc); 
    110                 } 
    111             } 
    112              
    113             if($process->arrayflag === false) { 
    114                 $returns = array_shift($returns); 
    115             } 
    116            return $returns; 
    117         } 
    118  
    11997        //type 
    12098        if (strtoupper(($process->type)) === 'RAW') { 
    12199            $strings = $values; 
    122100        } elseif (strtoupper(($process->type)) === 'TEXT') { 
    123              
    124101            $strings = array(); 
    125102            foreach ($values as $value) { 
     
    153130            throw new Diggin_Scraper_Strategy_Exception("can not understand type :".$process->type); 
    154131        } 
    155  
    156         if ($process->arrayflag === false && strtoupper($process->type) === 'RAW') { 
    157             $strings = array_shift($strings); 
    158         } elseif ($process->arrayflag === false) { 
    159             $strings = (string) array_shift($strings); 
    160         } 
    161132         
    162133        return $strings; 
    163134    } 
     135 
    164136} 
    165137