From 73b056776b37e7a99fc7401556bb8c71b837c9f1 Mon Sep 17 00:00:00 2001
From: Venkatesh S <76198059+Venkatesh-KCET@users.noreply.github.com>
Date: Sat, 17 Jun 2023 09:40:09 +0530
Subject: [PATCH] News Sitemap
---
sitemap-index.xml | 6 ------
sitemap0.xml | 41 -----------------------------------------
src/sitemap.php | 46 ++++++++++++++++++++++++++++++++++++++++------
test.php | 8 ++++++++
4 files changed, 48 insertions(+), 53 deletions(-)
delete mode 100644 sitemap-index.xml
delete mode 100644 sitemap0.xml
diff --git a/sitemap-index.xml b/sitemap-index.xml
deleted file mode 100644
index 5301d4e..0000000
--- a/sitemap-index.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
- http://localhost/sitemap/sitemap0.xml
-
-
diff --git a/sitemap0.xml b/sitemap0.xml
deleted file mode 100644
index fa3b4b6..0000000
--- a/sitemap0.xml
+++ /dev/null
@@ -1,41 +0,0 @@
-
-
-
- https://pypi.org/search/?q=sitemap
- https://pypi.org/search/1.png
-
- https://pypi.org/search/1.png
-
- https://pypi.org/search/2.png
-
- https://pypi.org/search/2.png
-
-
- https://www.example.com/thumbs/123.jpg
- Grilling steaks for summer
- Alkis shows you how to get perfectly done steaks every time
- http://streamserver.example.com/video123.mp4
- https://www.example.com/videoplayer.php?video=123
- 600
- 2021-11-05T19:20:30+08:00
- 4.2
- 12345
- 2007-11-05T19:20:30+08:00
- yes
- IE GB US CA
- web tv
- 1.99
- yes
- GrillyMcGrillerson
- no
- outdoor
-
-
- https://www.example.com/thumbs/123.jpg
- Grilling steaks for summer
- Alkis shows you how to get perfectly done steaks every time
- http://streamserver.example.com/video123.mp4
- https://www.example.com/videoplayer.php?video=123
-
-
-
diff --git a/src/sitemap.php b/src/sitemap.php
index 3b45d17..7a63620 100644
--- a/src/sitemap.php
+++ b/src/sitemap.php
@@ -149,6 +149,23 @@ public function __construct($loc, $lastmod, $changefreq, $priority, $images, $vi
}
+ if(!empty($news)) {
+
+ if(!isset($news["publication"]["name"])) {
+ throw new Exception("Name Of The Publisher Is Required");
+ }
+ if(!isset($news["publication"]["language"])) {
+ throw new Exception("Language Of The Publisher Is Required");
+ }
+
+ if(!isset($news["publication_date"])) {
+ throw new Exception("publication_date Is Required");
+ }
+ if(!isset($news["title"])) {
+ throw new Exception("title Is Required");
+ }
+ $this->news = $news;
+ }
}
}
@@ -229,6 +246,9 @@ function write_sitemap($folderPath = "/", $limit = 50000) {
if($this->videoSitemap) {
$urlset->setAttribute('xmlns:video', 'http://www.google.com/schemas/sitemap-video/1.1');
}
+ if($this->newsSitemap) {
+ $urlset->setAttribute('xmlns:news', 'http://www.google.com/schemas/sitemap-news/0.9');
+ }
$xml->appendChild($urlset);
for ($j = $i; $j < $i + 50000; $j++) {
@@ -239,12 +259,26 @@ function write_sitemap($folderPath = "/", $limit = 50000) {
$url = $xml->createElement('url');
foreach ($this->urls[$j] as $tagName => $tagValue) {
if(!empty($tagValue)) {
- if($tagName == "images") {
- foreach($tagValue as $imageUrl) {
- // Create element for image URL
- $Imageloc = $xml->createElement('loc', $imageUrl);
- $url->appendChild($Imageloc);
-
+ if($tagName == "news") {
+ $newsData = $xml->createElement('news:news');
+
+ foreach ($tagValue as $key => $value) {
+ if ($key === 'publication' && is_array($value)) {
+ $publicationElement = $xml->createElement('news:publication');
+ foreach ($value as $publication_key => $publication_value) {
+ $subelement = $xml->createElement("news:{$publication_key}", $publication_value);
+ $publicationElement->appendChild($subelement);
+ }
+ $newsData->appendChild($publicationElement);
+ } else {
+ $element = $xml->createElement("news:{$key}", $value);
+ $newsData->appendChild($element);
+ }
+ }
+
+ $url->appendChild($newsData);
+ } else if($tagName == "images") {
+ foreach($tagValue as $imageUrl) {
// Create element with for the image URL
$image = $xml->createElement('image:image');
$imageLoc = $xml->createElement('image:loc', $imageUrl);
diff --git a/test.php b/test.php
index 878f3ba..654b51a 100644
--- a/test.php
+++ b/test.php
@@ -49,6 +49,14 @@
"content_loc" => "http://streamserver.example.com/video123.mp4",
"player_loc" => "https://www.example.com/videoplayer.php?video=123"
]
+ ],
+ 'news' => [
+ 'publication' => [
+ 'name' => 'The Example Times',
+ 'language' => 'en'
+ ],
+ 'publication_date' => '2008-12-23',
+ 'title' => 'Companies A, B in Merger Talks'
]
];
$sitemap->add_url($array);