Permalink
Browse files

SyndicatedPost, SyndicatedPostXPathQuery / hive off XPath-like query …

…functionality into a separate class, SyndicatedPost is too big, blah blah.
  • Loading branch information...
Rad Geek
Rad Geek committed Apr 20, 2016
1 parent aeec143 commit bc169712e54dbb86651a5935ddd98aefd9b738a9
Showing with 267 additions and 166 deletions.
  1. +10 −166 syndicatedpost.class.php
  2. +257 −0 syndicatedpostxpathquery.class.php
View
@@ -1,6 +1,7 @@
<?php
require_once(dirname(__FILE__).'/feedtime.class.php');
require_once(dirname(__FILE__).'/syndicatedpostterm.class.php');
+require_once(dirname(__FILE__).'/syndicatedpostxpathquery.class.php');
/**
* class SyndicatedPost: FeedWordPress uses to manage the conversion of
@@ -290,97 +291,18 @@ function substitution_function ($name) {
* elements or attributes
*/
function query ($path) {
- $urlHash = array();
+ $xq = new SyndicatedPostXPathQuery(array("path" => $path));
- // Allow {url} notation for namespaces. URLs will contain : and /, so...
- preg_match_all('/{([^}]+)}/', $path, $match, PREG_SET_ORDER);
- foreach ($match as $ref) :
- $urlHash[md5($ref[1])] = $ref[1];
- endforeach;
-
- foreach ($urlHash as $hash => $url) :
- $path = str_replace('{'.$url.'}', '{#'.$hash.'}', $path);
- endforeach;
+ /*DBG*/ //echo "DATA: ".MyPHP::val($this->entry->data);
- $path = explode('/', $path);
- foreach ($path as $index => $node) :
- if (preg_match('/{#([^}]+)}/', $node, $ref)) :
- if (isset($urlHash[$ref[1]])) :
- $path[$index] = str_replace(
- '{#'.$ref[1].'}',
- '{'.$urlHash[$ref[1]].'}',
- $node
- );
- endif;
- endif;
- endforeach;
-
- // Start out with a get_item_tags query.
- $node = '';
- while (strlen($node)==0 and !is_null($node)) :
- $node = array_shift($path);
- endwhile;
-
- switch ($node) :
- case 'feed' :
- case 'channel' :
- $node = array_shift($path);
- $data = $this->get_feed_root_element();
- $data = array_merge($data, $this->get_feed_channel_elements());
- break;
- case 'item' :
- $node = array_shift($path);
- default :
- $data = array($this->entry->data);
- $method = NULL;
- endswitch;
-
- while (!is_null($node)) :
- if (strlen($node) > 0) :
- $matches = array();
-
- list($axis, $element) = $this->xpath_name_and_axis($node);
-
- foreach ($data as $datum) :
- if (!is_string($datum) and isset($datum[$axis])) :
- foreach ($datum[$axis] as $ns => $elements) :
- if (isset($elements[$element])) :
- // Potential match.
- // Check namespace.
- if (is_string($elements[$element])) : // Attribute
- $addenda = array($elements[$element]);
- $contexts = array($datum);
- else : // Element
- $addenda = $elements[$element];
- $contexts = $elements[$element];
- endif;
-
- foreach ($addenda as $index => $addendum) :
- $context = $contexts[$index];
-
- $namespaces = $this->xpath_possible_namespaces($node, $context);
- if (in_array($ns, $namespaces)) :
- $matches[] = $addendum;
- endif;
- endforeach;
- endif;
- endforeach;
- endif;
- endforeach;
-
- $data = $matches;
- endif;
- $node = array_shift($path);
- endwhile;
+ $matches = $xq->match(array(
+ "type" => $this->link->simplepie->get_type(),
+ "xmlns" => $this->xmlns,
+ "entry" => $this->entry->data,
+ "feed" => $this->get_feed_root_element(),
+ "channel" => $this->get_feed_channel_elements(),
+ ));
- $matches = array();
- foreach ($data as $datum) :
- if (is_string($datum)) :
- $matches[] = $datum;
- elseif (isset($datum['data'])) :
- $matches[] = $datum['data'];
- endif;
- endforeach;
return $matches;
} /* SyndicatedPost::query() */
@@ -412,84 +334,6 @@ function get_feed_channel_elements () {
return $matches;
} /* SyndicatedPost::get_feed_channel_elements() */
- function xpath_default_namespace () {
- // Get the default namespace.
- $type = $this->link->simplepie->get_type();
- if ($type & SIMPLEPIE_TYPE_ATOM_10) :
- $defaultNS = SIMPLEPIE_NAMESPACE_ATOM_10;
- elseif ($type & SIMPLEPIE_TYPE_ATOM_03) :
- $defaultNS = SIMPLEPIE_NAMESPACE_ATOM_03;
- elseif ($type & SIMPLEPIE_TYPE_RSS_090) :
- $defaultNS = SIMPLEPIE_NAMESPACE_RSS_090;
- elseif ($type & SIMPLEPIE_TYPE_RSS_10) :
- $defaultNS = SIMPLEPIE_NAMESPACE_RSS_10;
- elseif ($type & SIMPLEPIE_TYPE_RSS_20) :
- $defaultNS = SIMPLEPIE_NAMESPACE_RSS_20;
- else :
- $defaultNS = SIMPLEPIE_NAMESPACE_RSS_20;
- endif;
- return $defaultNS;
- } /* SyndicatedPost::xpath_default_namespace() */
-
- function xpath_name_and_axis ($node) {
- $ns = NULL; $element = NULL;
-
- if (substr($node, 0, 1)=='@') :
- $axis = 'attribs'; $node = substr($node, 1);
- else :
- $axis = 'child';
- endif;
-
- if (preg_match('/^{([^}]*)}(.*)$/', $node, $ref)) :
- $element = $ref[2];
- elseif (strpos($node, ':') !== FALSE) :
- list($xmlns, $element) = explode(':', $node, 2);
- else :
- $element = $node;
- endif;
- return array($axis, $element);
- } /* SyndicatedPost::xpath_local_name () */
-
- function xpath_possible_namespaces ($node, $datum = array()) {
- $ns = NULL; $element = NULL;
-
- if (substr($node, 0, 1)=='@') :
- $attr = '@'; $node = substr($node, 1);
- else :
- $attr = '';
- endif;
-
- if (preg_match('/^{([^}]*)}(.*)$/', $node, $ref)) :
- $ns = array($ref[1]);
- elseif (strpos($node, ':') !== FALSE) :
- list($xmlns, $element) = explode(':', $node, 2);
-
- if (isset($this->xmlns['reverse'][$xmlns])) :
- $ns = $this->xmlns['reverse'][$xmlns];
- else :
- $ns = array($xmlns);
- endif;
-
- // Fucking SimplePie. For attributes in default xmlns.
- $defaultNS = $this->xpath_default_namespace();
- if (isset($this->xmlns['forward'][$defaultNS])
- and ($xmlns==$this->xmlns['forward'][$defaultNS])) :
- $ns[] = '';
- endif;
-
- if (isset($datum['xmlns'])) :
- if (isset($datum['xmlns'][$xmlns])) :
- $ns[] = $datum['xmlns'][$xmlns];
- endif;
- endif;
- else :
- // Often in SimplePie, the default namespace gets stored
- // as an empty string rather than a URL.
- $ns = array($this->xpath_default_namespace(), '');
- endif;
- return array_unique($ns);
- } /* SyndicatedPost::xpath_possible_namespaces() */
-
function get_categories ($params = array()) {
return $this->entry->get_categories();
}
Oops, something went wrong.

0 comments on commit bc16971

Please sign in to comment.