| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | require_once 'Diggin/Scraper/Strategy/Abstract.php'; |
|---|
| 18 | |
|---|
| 19 | class Diggin_Scraper_Strategy_Xpath 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 | |
|---|
| 49 | if (parent::$_adapter instanceof Diggin_Scraper_Adapter_Interface) { |
|---|
| 50 | if (self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig); |
|---|
| 51 | return parent::$_adapter; |
|---|
| 52 | } else { |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | require_once 'Diggin/Scraper/Adapter/Htmlscraping.php'; |
|---|
| 57 | self::$_adapter = new Diggin_Scraper_Adapter_Htmlscraping(); |
|---|
| 58 | if(self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | return self::$_adapter; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | public function scrape($respose, $process) |
|---|
| 72 | { |
|---|
| 73 | $simplexml = $this->getAdapter()->readData($respose); |
|---|
| 74 | |
|---|
| 75 | return self::extract($simplexml, $process); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public static function extract($values, $process) |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 81 | set_error_handler( |
|---|
| 82 | create_function('$errno, $errstr', |
|---|
| 83 | 'if($errno) require_once "Diggin/Scraper/Strategy/Xpath/Exception.php"; |
|---|
| 84 | throw new Diggin_Scraper_Strategy_Xpath_Exception($errstr, $errno);' |
|---|
| 85 | ) |
|---|
| 86 | ); |
|---|
| 87 | $results = (array) $values->xpath($process->expression); |
|---|
| 88 | restore_error_handler(); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | return $results; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | public function getValue($values, $process) |
|---|
| 111 | { |
|---|
| 112 | |
|---|
| 113 | if (strtoupper(($process->type)) === 'RAW') { |
|---|
| 114 | $strings = $values; |
|---|
| 115 | } elseif (strtoupper(($process->type)) === 'TEXT') { |
|---|
| 116 | $strings = array(); |
|---|
| 117 | foreach ($values as $value) { |
|---|
| 118 | $value = strip_tags((string) str_replace('&', '&', $value->asXML())); |
|---|
| 119 | $value = str_replace(array(chr(10), chr(13)), '', $value); |
|---|
| 120 | array_push($strings, $value); |
|---|
| 121 | } |
|---|
| 122 | } elseif (strtoupper(($process->type)) === 'PLAIN') { |
|---|
| 123 | $strings = array(); |
|---|
| 124 | foreach ($values as $value) { |
|---|
| 125 | array_push($strings, (string) str_replace('&', '&', $value->asXML())); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | } elseif (strpos($process->type, '@') === 0) { |
|---|
| 129 | $strings = array(); |
|---|
| 130 | require_once 'Diggin/Scraper/Adapter/Htmlscraping.php'; |
|---|
| 131 | foreach ($values as $value) { |
|---|
| 132 | if (($process->type == '@href' OR $process->type == '@src')) { |
|---|
| 133 | array_push($strings, Diggin_Scraper_Adapter_Htmlscraping::getAbsoluteUrl((string)$value[substr($process->type, 1)], self::$_adapterconfig['url'])); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | } else { |
|---|
| 138 | array_push($strings, (string) $value[substr($process->type, 1)]); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | } else { |
|---|
| 143 | require_once 'Diggin/Scraper/Strategy/Exception.php'; |
|---|
| 144 | throw new Diggin_Scraper_Strategy_Exception("Unknown value type :".$process->type); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | return $strings; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|