Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a Google News Sitemap Generator #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/News.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Tackk\Cartographer;

class News
{
protected $name;
protected $language;
protected $access;
protected $genres;
protected $publicationDate;
protected $title;
protected $keywords;
protected $stockTickers;

function getName()
{
return $this->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;
}
}
91 changes: 91 additions & 0 deletions src/SitemapNews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Tackk\Cartographer;

use Tackk\Cartographer\News;
use DOMElement;

class SitemapNews extends AbstractSitemap
{
const SITEMAP_NEWS_NAMESPACE_URI = 'http://www.google.com/schemas/sitemap-news/0.9';

function __construct()
{
parent::__construct();
$this->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);
}
}
68 changes: 68 additions & 0 deletions tests/SitemapNewsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

class SitemapNewsTest extends PHPUnit_Framework_TestCase
{

public function testToString()
{
$expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>http://foo.com/1.html</loc>
<news:news>
<news:publication>
<news:name>Name1</news:name>
<news:language>en</news:language>
</news:publication>
<news:access>Subscription</news:access>
<news:genres>PressRelease, Blog</news:genres>
<news:publication_date>2016-09-06</news:publication_date>
<news:title>Title1</news:title>
<news:keywords>keyword11, keyword12</news:keywords>
<news:stock_tickers>NASDAQ:A, NASDAQ:B</news:stock_tickers>
</news:news>
</url>
<url>
<loc>http://foo.com/2.html</loc>
<news:news>
<news:publication>
<news:name>Name2</news:name>
<news:language>es</news:language>
</news:publication>
<news:access>Registration</news:access>
<news:genres>Satire, Opinion</news:genres>
<news:publication_date>2016-09-07</news:publication_date>
<news:title>Title2</news:title>
<news:keywords>keyword21, keyword22</news:keywords>
</news:news>
</url>
</urlset>

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());
}
}