Esempio n. 1
0
 /**
  * Perform the query on Document
  *
  * @param  string    $expression CSS selector or XPath query
  * @param  Document  $document   Document to query
  * @param  string    $type       The type of $expression
  * @return NodeList
  */
 public static function execute($expression, Document $document, $type = self::TYPE_XPATH)
 {
     // Expression check
     if ($type === static::TYPE_CSS) {
         $expression = static::cssToXpath($expression);
     }
     $xpath = new DOMXPath($document->getDomDocument());
     $xpathNamespaces = $document->getXpathNamespaces();
     foreach ($xpathNamespaces as $prefix => $namespaceUri) {
         $xpath->registerNamespace($prefix, $namespaceUri);
     }
     if ($xpathPhpfunctions = $document->getXpathPhpFunctions()) {
         $xpath->registerNamespace('php', 'http://php.net/xpath');
         $xpathPhpfunctions === true ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($xpathPhpfunctions);
     }
     $nodeList = $xpath->queryWithErrorException($expression);
     return new NodeList($nodeList);
 }
Esempio n. 2
0
 /**
  * Parse if the Redi xml snippet contains Redi urls.
  *
  * @param DOMDocument $xml Loaded xml document
  *
  * @return array Get back Redi direct link to sources containing title, URL and
  *               service_type
  */
 protected function parseRediOpenURLs($xml)
 {
     $retval = [];
     $xpath = new DOMXPath($xml);
     $ezbResultsNodesText = $xpath->query("//div[@class='t_ezb_result']/p");
     $ezbResultsNodesURL = $xpath->query("//div[@class='t_ezb_result']/p/span[@class='t_link']/a");
     if ($ezbResultsNodesText->length == $ezbResultsNodesURL->length) {
         for ($i = 0; $i < $ezbResultsNodesText->length; $i++) {
             $accessClass = 'unknown';
             $accessClassExpressions = ["denied" => "//div[@class='t_ezb_result'][" . ($i + 1) . "]/p/span[@class='t_ezb_red']", "limited" => "//div[@class='t_ezb_result'][" . ($i + 1) . "]/p/span[@class='t_ezb_yellow']", "open" => "//div[@class='t_ezb_result'][" . ($i + 1) . "]/p/span[@class='t_ezb_green']"];
             // $i+1 because XPath-element-counting starts with 1
             foreach ($accessClassExpressions as $key => $value) {
                 if ($xpath->evaluate("count({$value})") == 1) {
                     $accessClass = $key;
                 }
             }
             $itemInfo = '';
             $expression = "//div[@class='t_ezb_result'][" . ($i + 1) . "]/p/sup";
             if ($xpath->evaluate("count({$expression})") == 1) {
                 $itemInfo = $this->parseRediInfo($xml, $xpath->query($expression)->item(0)->textContent);
             }
             $retval[] = ['title' => $ezbResultsNodesText->item($i)->textContent, 'href' => $ezbResultsNodesURL->item($i)->attributes->getNamedItem("href")->textContent, 'access' => $accessClass, 'coverage' => $itemInfo, 'service_type' => 'getFullTxt'];
         }
     }
     return $retval;
 }
Esempio n. 3
0
 /**
  * Prepare node list
  *
  * @param  DOMDocument $document
  * @param  string|array $xpathQuery
  * @return array
  * @throws ErrorException If query cannot be executed
  */
 protected function getNodeList($document, $xpathQuery)
 {
     $xpath = new DOMXPath($document);
     foreach ($this->xpathNamespaces as $prefix => $namespaceUri) {
         $xpath->registerNamespace($prefix, $namespaceUri);
     }
     if ($this->xpathPhpFunctions) {
         $xpath->registerNamespace("php", "http://php.net/xpath");
         $this->xpathPhpFunctions === true ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($this->xpathPhpFunctions);
     }
     $xpathQuery = (string) $xpathQuery;
     $nodeList = $xpath->queryWithErrorException($xpathQuery);
     return $nodeList;
 }