Skip to content

Commit

Permalink
Fixes for Patching Sitemap files
Browse files Browse the repository at this point in the history
Fixes for #1050
Includes:
1) fix for patching mobile sitemap
2) fix for create all sitemap files running after everything is patched fine
3) items not getting deleted when they should (like when post put into draft mode)
  • Loading branch information
eSilverStrike committed Feb 7, 2022
1 parent d6e898c commit 03d1f6e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 52 deletions.
81 changes: 54 additions & 27 deletions plugins/xmlsitemap/functions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function XMLSMAP_update($type = null, $include = null, $exclude = null)
function plugin_itemsaved_xmlsitemap($id, $type, $old_id, $sub_type)
{
global $_XMLSMAP_CONF;

// Make sure type is setup in the arrays and $id is set
if (empty($type) || !in_array($type, $_XMLSMAP_CONF['types']) || empty($id)) {
return;
Expand All @@ -437,35 +437,45 @@ function plugin_itemsaved_xmlsitemap($id, $type, $old_id, $sub_type)
$mobile_sitemap_file,
$news_sitemap_file
);
$newItem = PLG_getItemInfo($type, $id, 'url,date-modified,date-created,title', 1);

if (!empty($newItem)) {
$sitemap->beginUpdate();

if (!empty($old_id) && ($id !== $old_id)) {
// Item updated

// PLG_getItemInfo doesn't work here, since the item has already been deleted from database
$oldUrl = PLG_idToURL($type, $sub_type, $old_id);
$sitemap->deleteItem($type, $oldUrl);
$sitemap->addItem(
$type, $id, $newItem[0], $newItem[1], $newItem[2], $newItem[3],
@$_XMLSMAP_CONF['priorities'][$type], @$_XMLSMAP_CONF['frequencies'][$type]
);
} else {
// Item created
$sitemap->addItem(
$type, $id, $newItem[0], $newItem[1], $newItem[2], $newItem[3],
@$_XMLSMAP_CONF['priorities'][$type], @$_XMLSMAP_CONF['frequencies'][$type]
);

// News Sitemap settings
if (isset($_XMLSMAP_CONF['news_sitemap_file']) && !empty($_XMLSMAP_CONF['news_sitemap_file'])) {
// Article topics to include
if (isset($_XMLSMAP_CONF['news_sitemap_topics']) && $_XMLSMAP_CONF['news_sitemap_topics']) {
$sitemap->setNewsTopics($_XMLSMAP_CONF['news_sitemap_topics']);
}
// Max article age in seconds
if (isset($_XMLSMAP_CONF['news_sitemap_age']) && $_XMLSMAP_CONF['news_sitemap_age']) {
$sitemap->setNewsAge($_XMLSMAP_CONF['news_sitemap_age']);
}
}

$newItem = PLG_getItemInfo($type, $id, 'url,date-modified,date-created,title', 1);

$result = $sitemap->endUpdate();
}
$sitemap->beginUpdate();

if (!empty($old_id) && ($id !== $old_id)) {
// Item updated
$Url = PLG_idToURL($type, $sub_type, $old_id);
} else {
$Url = PLG_idToURL($type, $sub_type, $id);
}
// Always need to delete as item may exist already in sitemap but needs to be changed
$sitemap->deleteItem($type, $Url);

if (!empty($newItem)) {
// Item created
$sitemap->addItem(
$type, $id, $newItem[0], $newItem[1], $newItem[2], $newItem[3],
@$_XMLSMAP_CONF['priorities'][$type], @$_XMLSMAP_CONF['frequencies'][$type]
);
}

$result = $sitemap->endUpdate();

if (!$result) {
// Tried to update sitemap files partially, but failed. Let's use the old way
XMLSMAP_update($type);
// Tried to update sitemap files partially, but failed. Let's use the old way
XMLSMAP_update($type);
}
}

Expand Down Expand Up @@ -504,11 +514,28 @@ function plugin_itemdeleted_xmlsitemap($id, $type, $sub_type)
$mobileSitemapFile = isset($_XMLSMAP_CONF['mobile_sitemap_file']) ? $_XMLSMAP_CONF['mobile_sitemap_file'] : '';
$newsSitemapFile = isset($_XMLSMAP_CONF['news_sitemap_file']) ? $_XMLSMAP_CONF['news_sitemap_file'] : '';
$sitemap->setFileNames($sitemapFile, $mobileSitemapFile, $newsSitemapFile);

// News Sitemap settings
if (isset($_XMLSMAP_CONF['news_sitemap_file']) && !empty($_XMLSMAP_CONF['news_sitemap_file'])) {
// Article topics to include
if (isset($_XMLSMAP_CONF['news_sitemap_topics']) && $_XMLSMAP_CONF['news_sitemap_topics']) {
$sitemap->setNewsTopics($_XMLSMAP_CONF['news_sitemap_topics']);
}
// Max article age in seconds
if (isset($_XMLSMAP_CONF['news_sitemap_age']) && $_XMLSMAP_CONF['news_sitemap_age']) {
$sitemap->setNewsAge($_XMLSMAP_CONF['news_sitemap_age']);
}
}

$sitemap->beginUpdate();

// PLG_getItemInfo doesn't work here, since the item has already been deleted from database
$oldUrl = PLG_idToURL($type, $sub_type, $id);
$sitemap->deleteItem($type, $oldUrl);

$result = $sitemap->endUpdate();

if (!$sitemap->deleteItem($type, $oldUrl)) {
if (!$result) {
XMLSMAP_update($type);
}
}
Expand Down
60 changes: 35 additions & 25 deletions plugins/xmlsitemap/xmlsitemap.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,14 @@ protected function write($filename, $sitemap)
/**
* Format an item
*
* @param int $type sitemap type: one of XMLSitemap::TYPE_REGULAR or XMLSitemap::TYPE_MOBILE
* @param string $url
* @param int $lastModified
* @param float $priority
* @param string $frequency
* @return string
*/
protected function formatItem($url, $lastModified = null, $priority = null, $frequency = null)
protected function formatItem($type, $url, $lastModified = null, $priority = null, $frequency = null)
{
static $timezone = null;

Expand Down Expand Up @@ -537,6 +538,11 @@ protected function formatItem($url, $lastModified = null, $priority = null, $fre
$retval .= ' <changefreq>' . $frequency
. '</changefreq>' . self::LB;
}

// Mobile Sitemap
if ($type == self::TYPE_MOBILE) {
$retval .= ' <mobile:mobile />' . self::LB;
}

$retval .= ' </url>' . self::LB;

Expand Down Expand Up @@ -571,7 +577,7 @@ protected function formatEntryForNewsSitemap(array $entry, $site_lang_id, $multi
if ($timezone === null) {
$timezone = $this->getTimezoneStr();
}

// Check URL, date is under max age, and appropriate topics
if (empty($entry['url']) || ((int) $entry['date-created'] < time() - $this->getNewsAge())) {
// <loc> element is mandatory for the sitemap. So, when no url is provided, or the entry is too old,
Expand Down Expand Up @@ -647,7 +653,7 @@ public function create()

// Prepend the homepage (feature #997)
if (isset($_XMLSMAP_CONF['include_homepage']) && $_XMLSMAP_CONF['include_homepage']) {
$sitemap .= $this->formatItem($_CONF['site_url']);
$sitemap .= $this->formatItem(self::TYPE_REGULAR, $_CONF['site_url']);
}

foreach ($types as $type) {
Expand Down Expand Up @@ -685,7 +691,7 @@ public function create()
? $entry['priority']
: $this->getPriority($type);

$sitemap .= $this->formatItem($url, $lastModified, $priority, $frequency);
$sitemap .= $this->formatItem(self::TYPE_REGULAR, $url, $lastModified, $priority, $frequency);
$numEntries++;
}
}
Expand Down Expand Up @@ -724,6 +730,8 @@ public function create()
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',
$sitemap
);

// Convert regular sitemap now to mobile
$sitemap = str_replace(
' </url>',
' <mobile:mobile />' . self::LB . ' </url>',
Expand Down Expand Up @@ -883,34 +891,34 @@ public function deleteItem($type, $url)
protected function isValidNewsItem(array $entry)
{
if ($entry['type'] === 'article') {
return false;
//return false;
}

$what = 'url,date-created,id,title';
$uid = 1; // anonymous user
$options = [];

// Retrieve complete topic list including inherited ones
$topicList = '';
$newsTopics = $this->getNewsTopics();

if (!empty($newsTopics)) {
foreach ($newsTopics as $tid) {
$tids = TOPIC_getChildList($tid, $uid);

if (!empty($tids)) {
if (!empty($topicList)) {
$topicList = $topicList . "," . $tids;
} else {
$topicList = $tids;
}
}
}
}
// Retrieve complete topic list including inherited ones
$topic_list = '';
$newstopics = $this->getNewsTopics();

if (!empty($topicList)) {
$options['filter']['topic-ids'] = $topicList;
}
if (!empty($newstopics)) {
foreach ($newstopics as $tid) {
$tids = TOPIC_getChildList($tid, $uid);
if (!empty($tids)) {
if (!empty($topic_list)) {
$topic_list = $topic_list . "," . $tids;
} else {
$topic_list = $tids;
}
}
}
}
if (!empty($topic_list)) {
$options['filter']['topic-ids'] = $topic_list;
}

// Figure out max age
if ($this->getNewsAge() > 0) {
Expand Down Expand Up @@ -958,7 +966,7 @@ protected function patchFile($type, $path, array $items)
$formattedItem = '';

if (($type === self::TYPE_REGULAR) || ($type == self::TYPE_MOBILE)) {
$formattedItem = $this->formatItem($data['url'], $data['date-modified'], $data['priority'], $data['frequency']);
$formattedItem = $this->formatItem($type, $data['url'], $data['date-modified'], $data['priority'], $data['frequency']);
} else {
// News sitemap
if ($this->isValidNewsItem($data)) {
Expand All @@ -981,6 +989,7 @@ protected function patchFile($type, $path, array $items)
}

// Delete an existing item
// Note may not even exist in sitemap so let $updated = true anyways even if not found and removed
$target = ' <url>' . self::LB . ' <loc>' . $this->normalizeURL($data['url']) . '</loc>' . self::LB;
$pos = strpos($sitemap, $target);

Expand All @@ -990,9 +999,10 @@ protected function patchFile($type, $path, array $items)
if ($pos2 !== false) {
$sitemap = substr($sitemap, 0, $pos)
. substr($sitemap, $pos2 + strlen('</url>' . self::LB));
$updated = true;
}
}

$updated = true;
}
}

Expand Down

0 comments on commit 03d1f6e

Please sign in to comment.