From 96c585bebdacee5d08be61e4fff08d44cc574f3f Mon Sep 17 00:00:00 2001 From: jbarroso Date: Mon, 12 Sep 2016 09:41:11 +0200 Subject: [PATCH] Adding a Google News Sitemap Generator --- src/News.php | 103 ++++++++++++++++++++++++++++++++++++++ src/SitemapNews.php | 91 +++++++++++++++++++++++++++++++++ tests/SitemapNewsTest.php | 68 +++++++++++++++++++++++++ 3 files changed, 262 insertions(+) create mode 100644 src/News.php create mode 100644 src/SitemapNews.php create mode 100644 tests/SitemapNewsTest.php diff --git a/src/News.php b/src/News.php new file mode 100644 index 0000000..f946d1a --- /dev/null +++ b/src/News.php @@ -0,0 +1,103 @@ +name; + } + + function getLanguage() + { + return $this->language; + } + + function getAccess() + { + return $this->access; + } + + function getGenres() + { + return $this->genres; + } + + function getPublicationDate() + { + return $this->publicationDate; + } + + function getTitle() + { + return $this->title; + } + + function getKeywords() + { + return $this->keywords; + } + + function getStockTickers() + { + return $this->stockTickers; + } + + function setName($name) + { + $this->name = $name; + return $this; + } + + function setLanguage($language) + { + $this->language = $language; + return $this; + } + + function setAccess($access) + { + $this->access = $access; + return $this; + } + + function setGenres($genres) + { + $this->genres = $genres; + return $this; + } + + function setPublicationDate($publicationDate) + { + $this->publicationDate = $publicationDate; + return $this; + } + + function setTitle($title) + { + $this->title = $title; + return $this; + } + + function setKeywords($keywords) + { + $this->keywords = $keywords; + return $this; + } + + function setStockTickers($stockTickers) + { + $this->stockTickers = $stockTickers; + return $this; + } +} \ No newline at end of file diff --git a/src/SitemapNews.php b/src/SitemapNews.php new file mode 100644 index 0000000..1e4b55a --- /dev/null +++ b/src/SitemapNews.php @@ -0,0 +1,91 @@ +addSitemapNewsNS(); + } + + protected function addSitemapNewsNS() + { + $this->rootNode->setAttributeNS( + 'http://www.w3.org/2000/xmlns/', + 'xmlns:news', + self::SITEMAP_NEWS_NAMESPACE_URI); + } + + protected function getRootNodeName() + { + return 'urlset'; + } + + protected function getNodeName() + { + return 'url'; + } + + /** + * Adds a News element to the sitemapnews. + * @param string $loc + * @param News $news + * @return $this + */ + public function add($loc, News $news) + { + $loc = $this->escapeString($loc); + $node = $this->document->createElement($this->getNodeName()); + $this->rootNode->appendChild($node); + $node->appendChild(new DOMElement('loc', $loc)); + $node->appendChild($this->getNewsNode($news)); + $this->urlCount++; + return $this; + } + + protected function getNewsNode($news) + { + $node = $this->createNewsElement('news'); + $node->appendChild($this->getPublicationNode($news)); + $this->appendNewsElements($node, $news); + return $node; + } + + protected function getPublicationNode($news) + { + $node = $this->createNewsElement('publication'); + $this->appendNewsElementValue($node, 'name', $news->getName()); + $this->appendNewsElementValue($node, 'language', $news->getLanguage()); + return $node; + } + + protected function appendNewsElements($node, $news) + { + $this->appendNewsElementValue($node, 'access', $news->getAccess()); + $this->appendNewsElementValue($node, 'genres', $news->getGenres()); + $this->appendNewsElementValue($node, 'publication_date', $news->getPublicationDate()); + $this->appendNewsElementValue($node, 'title', $news->getTitle()); + $this->appendNewsElementValue($node, 'keywords', $news->getKeywords()); + $this->appendNewsElementValue($node, 'stock_tickers', $news->getStockTickers()); + } + + protected function appendNewsElementValue($node, $name, $value) + { + if ($value) { + $node->appendChild($this->createNewsElement($name, $value)); + } + } + + protected function createNewsElement($name, $value = NULL) + { + return $this->document->createElementNS( + self::SITEMAP_NEWS_NAMESPACE_URI, 'news:' . $name, $value); + } +} \ No newline at end of file diff --git a/tests/SitemapNewsTest.php b/tests/SitemapNewsTest.php new file mode 100644 index 0000000..2b5cd07 --- /dev/null +++ b/tests/SitemapNewsTest.php @@ -0,0 +1,68 @@ + + + + http://foo.com/1.html + + + Name1 + en + + Subscription + PressRelease, Blog + 2016-09-06 + Title1 + keyword11, keyword12 + NASDAQ:A, NASDAQ:B + + + + http://foo.com/2.html + + + Name2 + es + + Registration + Satire, Opinion + 2016-09-07 + Title2 + keyword21, keyword22 + + + + +XML; + $sitemapNews = new Tackk\Cartographer\SitemapNews(); + $news1 = new Tackk\Cartographer\News(); + $news1->setName('Name1') + ->setLanguage('en') + ->setAccess('Subscription') + ->setGenres('PressRelease, Blog') + ->setPublicationDate('2016-09-06') + ->setTitle('Title1') + ->setKeywords('keyword11, keyword12') + ->setStockTickers('NASDAQ:A, NASDAQ:B'); + + $news2 = new Tackk\Cartographer\News(); + $news2->setName('Name2') + ->setLanguage('es') + ->setAccess('Registration') + ->setGenres('Satire, Opinion') + ->setPublicationDate('2016-09-07') + ->setTitle('Title2') + ->setKeywords('keyword21, keyword22'); + + + $sitemapNews->add('http://foo.com/1.html', $news1); + $sitemapNews->add('http://foo.com/2.html', $news2); + $this->assertEquals($expected, $sitemapNews->toString()); + } +} \ No newline at end of file