| 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 getAdapter() |
|---|
| 37 | { |
|---|
| 38 | if (isset(self::$_adapter)) { |
|---|
| 39 | return self::$_adapter; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if (parent::$_adapter instanceof Diggin_Scraper_Adapter_Interface) { |
|---|
| 44 | if(self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig); |
|---|
| 45 | return parent::$_adapter; |
|---|
| 46 | } else { |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | require_once 'Diggin/Scraper/Adapter/Htmlscraping.php'; |
|---|
| 51 | self::$_adapter = new Diggin_Scraper_Adapter_Htmlscraping(); |
|---|
| 52 | if(self::$_adapterconfig) self::$_adapter->setConfig(self::$_adapterconfig); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | return self::$_adapter; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | public function scrape($respose, $process) |
|---|
| 66 | { |
|---|
| 67 | $simplexml = $this->getAdapter()->readData($respose); |
|---|
| 68 | |
|---|
| 69 | $results = array(); |
|---|
| 70 | foreach ($simplexml->xpath($process->expression) as $count => $result) { |
|---|
| 71 | $results[] = $result; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | return $results; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | public function getValue($context, $process) |
|---|
| 85 | { |
|---|
| 86 | if (!isset($process->type)) { |
|---|
| 87 | return $context->scrape($process); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | $values = $context->scrape($process); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | if (strtoupper(($process->type)) === 'TEXT') { |
|---|
| 94 | |
|---|
| 95 | $strings = array(); |
|---|
| 96 | foreach ($values as $value) { |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | array_push($strings, strip_tags((string) str_replace('&', '&', $value->asXML()))); |
|---|
| 100 | } |
|---|
| 101 | } elseif (strtoupper(($process->type)) === 'PLAIN') { |
|---|
| 102 | $strings = array(); |
|---|
| 103 | foreach ($values as $value) { |
|---|
| 104 | array_push($strings, (string) str_replace('&', '&', $value->asXML())); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | } elseif (strpos($process->type, '@') === 0) { |
|---|
| 108 | $strings = array(); |
|---|
| 109 | foreach ($values as $value) { |
|---|
| 110 | |
|---|
| 111 | if (($process->type == '@href' OR $process->type == '@src')){ |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | require_once 'Diggin/Uri/Http.php'; |
|---|
| 115 | array_push($strings, Diggin_Uri_Http::getAbsoluteUrl((string)$value[substr($process->type, 1)], self::$_adapterconfig['url'])); |
|---|
| 116 | } else { |
|---|
| 117 | array_push($strings, (string) $value[substr($process->type, 1)]); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | } else { |
|---|
| 122 | require_once 'Diggin/Scraper/Exception.php'; |
|---|
| 123 | throw new Diggin_Scraper_Exception("can't understand type"); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if ($process->filter) { |
|---|
| 128 | |
|---|
| 129 | require_once 'Diggin/Scraper/Filter.php'; |
|---|
| 130 | $filterObj = new Diggin_Scraper_Filter(new ArrayIterator($strings)); |
|---|
| 131 | |
|---|
| 132 | foreach ($filterObj as $value) { |
|---|
| 133 | $return[] = $value; |
|---|
| 134 | } |
|---|
| 135 | } else { |
|---|
| 136 | $return = $strings; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | if (count($return) === 1) { |
|---|
| 141 | $return = (string) array_shift($return); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | return $return; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | |
|---|