public function testNamespaceValue() { $xml = XmlParser::getSimpleXml(file_get_contents('tests/fixtures/rue89.xml')); $this->assertNotFalse($xml); $namespaces = $xml->getNamespaces(true); $parser = new Rss20(''); $this->assertEquals('Blandine Grosjean', $parser->getNamespaceValue($xml->channel->item[0], $namespaces, 'creator')); $this->assertEquals('Pierre-Carl Langlais', $parser->getNamespaceValue($xml->channel->item[1], $namespaces, 'creator')); }
/** * Parse the OPML file * * @access public * @return array|false */ public function execute() { Logging::setMessage(get_called_class() . ': start importation'); $xml = XmlParser::getSimpleXml(trim($this->content)); if ($xml === false || $xml->getName() !== 'opml' || !isset($xml->body)) { Logging::setMessage(get_called_class() . ': OPML tag not found or malformed XML document'); return false; } $this->parseEntries($xml->body); Logging::setMessage(get_called_class() . ': ' . count($this->items) . ' subscriptions found'); return $this->items; }
public function testScanXmlWithDTD() { $xml = <<<XML <?xml version="1.0"?> <!DOCTYPE results [ <!ELEMENT results (result+)> <!ELEMENT result (#PCDATA)> ]> <results> <result>test</result> </results> XML; $result = XmlParser::getDomDocument($xml); $this->assertTrue($result instanceof DOMDocument); $this->assertTrue($result->validate()); }
/** * Strip useless tags * * @access public */ public function stripGarbage() { $dom = XmlParser::getDomDocument($this->content); if ($dom !== false) { $xpath = new DOMXPath($dom); foreach ($this->stripTags as $tag) { $nodes = $xpath->query('//' . $tag); if ($nodes !== false && $nodes->length > 0) { Logging::setMessage(get_called_class() . ' Strip tag: "' . $tag . '"'); foreach ($nodes as $node) { $node->parentNode->removeChild($node); } } } foreach ($this->stripAttributes as $attribute) { $nodes = $xpath->query('//*[contains(@class, "' . $attribute . '") or contains(@id, "' . $attribute . '")]'); if ($nodes !== false && $nodes->length > 0) { Logging::setMessage(get_called_class() . ' Strip attribute: "' . $attribute . '"'); foreach ($nodes as $node) { $node->parentNode->removeChild($node); } } } $this->content = $dom->saveXML($dom->documentElement); } }
/** * Discover the feed url inside a HTML document and download the feed * * @access public * @return boolean */ public function discover() { if (!$this->content) { return false; } Logging::setMessage(get_called_class() . ': Try to discover a subscription'); $dom = XmlParser::getHtmlDocument($this->content); $xpath = new DOMXPath($dom); $queries = array('//link[@type="application/rss+xml"]', '//link[@type="application/atom+xml"]'); foreach ($queries as $query) { $nodes = $xpath->query($query); if ($nodes->length !== 0) { $link = $nodes->item(0)->getAttribute('href'); if (!empty($link)) { // Relative links if (strpos($link, 'http') !== 0) { if ($link[0] === '/') { $link = substr($link, 1); } if ($this->url[strlen($this->url) - 1] !== '/') { $this->url .= '/'; } $link = $this->url . $link; } Logging::setMessage(get_called_class() . ': Find subscription link: ' . $link); $this->download($link); return true; } } } return false; }