root/lang/php/Scraper/library/Diggin/Scraper/Strategy/Selector.php @ 9514

Revision 9514, 4.6 kB (checked in by sasezaki, 5 years ago)

lang/php/Scraper: 取得結果は、resultsプロパティに変更。absoluteUrl部の変更ほか

Line 
1<?php
2/**
3 * Diggin - Library Of PHP
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license.
8 * It is also available through the world-wide-web at this URL:
9 * http://framework.zend.com/license/new-bsd
10 *
11 * @category   Diggin
12 * @package    Diggin_Scraper
13 * @copyright  2006-2008 sasezaki (http://diggin.musicrider.com)
14 * @license    http://framework.zend.com/license/new-bsd     New BSD License
15 */
16
17require_once 'Diggin/Scraper/Strategy/Abstract.php';
18
19class Diggin_Scraper_Strategy_Selector extends Diggin_Scraper_Strategy_Abstract
20{
21    protected static $_adapter = null;
22    protected static $_adapterconfig = null;
23
24    public function __destruct()
25    {
26       self::$_adapter = null;
27       self::$_adapterconfig = null;
28       parent::$_adapter = null;
29    }
30   
31    public function setAdapter(Diggin_Scraper_Adapter_Interface $adapter)
32    {
33        self::$_adapter = $adapter;
34    }
35   
36    public function setAdapterConfig($config)
37    {
38        self::$_adapterconfig = $config;
39    }
40   
41    public function getAdapter()
42    {
43        if (isset(self::$_adapter)) {
44            if(self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig);
45            return self::$_adapter;
46        }
47        //コンストラクタで設定されてた時用
48        if (parent::$_adapter instanceof Diggin_Scraper_Adapter_Interface) {
49            return parent::$_adapter;
50        } else {
51            /**
52             * @see Diggin_Scraper_Adapter
53             */
54            require_once 'Diggin/Scraper/Adapter/Htmlscraping.php';
55            self::$_adapter = new Diggin_Scraper_Adapter_Htmlscraping();           
56            if(self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig);
57        }
58
59        return self::$_adapter;
60    }
61
62    /**
63     * scraping with CssSelector
64     *
65     * @param Zend_Http_Response $respose
66     * @param string $process
67     * @return
68     */
69    public function scrape($respose, $process)
70    {
71        $simplexml = $this->getAdapter()->readData($respose);
72 
73        $dom = dom_import_simplexml($simplexml);
74
75        require_once dirname(__FILE__).'/Selector/sfDomCssSelector.class.php';
76        $selector = new sfDomCssSelector($dom);
77       
78        $results = array();       
79        foreach ($selector->getElements($process->expression) as $result) {
80            $results[] = simplexml_import_dom($result);
81        }
82       
83        return $results;
84    }
85
86    /**
87     * get value with DSL
88     *
89     * @param Diggin_Scraper_Context
90     * @param Diggin_Scraper_Process
91     * @return array
92     */
93    public function getValue($context, $process)
94    {
95        if (!isset($process->type)) {
96            return $context->scrape($process);
97        }
98       
99        $values = $context->scrape($process);
100
101        //type
102        if (strtoupper(($process->type)) === 'TEXT') {
103           
104            $strings = array();
105            foreach ($values as $value) {
106                //@todo strict
107                //array_push($strings, (string) $value);
108                array_push($strings, strip_tags((string) str_replace('&amp;', '&', $value->asXML())));
109            }
110        } elseif (strtoupper(($process->type)) === 'PLAIN') {
111            $strings = array();
112            foreach ($values as $value) {
113                array_push($strings, (string) str_replace('&amp;', '&', $value->asXML()));
114            }
115               
116        } elseif (strpos($process->type, '@') === 0) {
117            $strings = array();
118            foreach ($values as $value) {
119                //@todo
120                if (($process->type == '@href' OR $process->type == '@src')
121                    && method_exists($this->getAdapter(), 'getAbsoluteUrl')) {
122                    array_push($strings, $this->getAdapter()->getAbsoluteUrl((string)$value[substr($process->type, 1)], self::$_adapterconfig['url']));
123                    //require_once 'Diggin/Uri/Http.php';
124                    //array_push($strings, Diggin_Uri_Http::getAbsoluteUrl((string)$value[substr($process->type, 1)], self::$_adapterconfig['url']));
125                } else {
126                    array_push($strings, (string) $value[substr($process->type, 1)]);
127                }
128            }
129           
130        } else {
131            require_once 'Diggin/Scraper/Exception.php';
132            throw new Diggin_Scraper_Exception("can't understand type");
133        }
134       
135        //スクレイプ該当が1件だったときに、
136        if (count($strings) === 1) {
137            $strings = (string) array_shift($strings);
138        }
139
140        return $strings;
141    }
142}
Note: See TracBrowser for help on using the browser.