Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Pages: [1] 2

Author Topic: Sitemap *.xml.gz for google webmaster tools  (Read 6163 times)

someone

  • Newbie
  • *
  • Karma: 2
  • Posts: 7
Sitemap *.xml.gz for google webmaster tools
« on: May 05, 2009, 11:24:03 AM »

Hi,
I wrote simple piece of code based on sitemap in snews. Modification will will allow for faster indexing of your site done by Google. Always when somebody visit the sitemap page on your website, the xml sitemap will be created again and script send ping to Google report that your page has been updated. If you are logged in as admin you'll see information about the notification at the end of sitemap page.

Add this:
Code: [Select]
// SITEMAP XML AND GZIP
// funkcja przygotowywujaca tag xml
function maketag($url,$changefreq,$priority) {
  $tag="\t<url>\n\t\t<loc>".$url."</loc>\n\t\t<changefreq>".$changefreq."</changefreq>
  \t\t<priority>".$priority."</priority>\n\t</url>\n";
  return $tag;
  }
 
function sitemapgz() {
  //naglowek
  DEFINE('NL',"\n");
  $xml='<?xml version="1.0" encoding="UTF-8"?>'.NL;
  $xml.='<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">'.NL;
  //stale strony
$link = _SITE;
  $xml.=maketag($link,'daily','1');
$xml.=maketag($link.'archive/','daily','0.8');

$query = "SELECT title,seftitle
FROM "._PRE.'articles'."
WHERE position = 3
AND published = 1
AND visible = 'YES'
ORDER BY artorder ASC, date, id";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
    $xml.=maketag($link.$r['seftitle'],'weekly','0.7');
}
$xml.=maketag($link.'contact/','monthly','0.3');
$xml.=maketag($link.'sitemap/','daily','0.8');
  //artykuly
$art_query = 'SELECT title, seftitle, date
FROM '._PRE.'articles'.'
WHERE position = 1
AND published = 1
AND visible = \'YES\'';
$cat_query = 'SELECT id, name, seftitle, description, subcat
FROM '._PRE.'categories'.'
WHERE published = \'YES\'
AND subcat = 0
ORDER BY catorder,id';
$cat_result = mysql_query($cat_query);
if (mysql_num_rows($cat_result) == 0) {
///
} else {
while ($c = mysql_fetch_array($cat_result)) {
$category_title = $c['seftitle'];
$xml.=maketag($link.$category_title,'weekly','0.8');
$catid = $c['id'];
$query = $art_query.' AND category = '.$catid.' ORDER BY id DESC';
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
echo '<ul>';
}
while ($r = mysql_fetch_array($result)) {
        $xml.=maketag($link.$category_title.'/'.$r['seftitle'],'weekly','0.8');
}
if (mysql_num_rows($result) > 0) {
///
}
$subcat_result = mysql_query('SELECT id, name, seftitle, description, subcat
FROM '._PRE.'categories'.'
WHERE published = \'YES\'
AND subcat = '.$c['id'].'
ORDER BY catorder ASC');
if (mysql_num_rows($subcat_result) > 0) {
      ///
}
while ($s = mysql_fetch_array($subcat_result)) {
$subcat_title = $s['seftitle'];
$subcat_name = $s['name'];
$xml.=maketag($link.$category_title.'/'.$subcat_title,'weekly','0.8');
$subcatid = $s['id'];
$query = $art_query.' AND category = '.$subcatid.' ORDER BY id DESC';
$artresult = mysql_query($query);
if (mysql_num_rows($artresult) > 0) {
        ///
}
while ($r = mysql_fetch_array($artresult)) {
          $xml.=maketag($link.$category_title.'/'.$subcat_title.'/'.$r['seftitle'],'weekly','0.8');
}
if (mysql_num_rows($artresult) > 0) {
//
}
///
}
if (mysql_num_rows($subcat_result) > 0) {
///
}
///
}
///
}
$xml.='</urlset>'.NL;

//zpis
$sitemapfile=fopen("sitemap.xml",'w');
  fwrite($sitemapfile,$xml);
  fclose($sitemapfile);
//kompresja
$sitemapgfile=fopen("sitemap.xml.gz",'w');
  $gzfile=gzopen("sitemap.xml.gz", 'w');
  gzwrite($gzfile, $xml);
  gzclose ($gzfile);
  fclose($sitemapgfile);
  //ping do google
  $handcurl = curl_init();
  $url='http://www.google.com/webmasters/sitemaps/ping?sitemap='.$link.'sitemap.xml.gz';
  curl_setopt($handcurl, CURLOPT_URL, $url);
  curl_setopt($handcurl, CURLOPT_RETURNTRANSFER, 1);
  $answer = curl_exec($handcurl);
  echo _ADMIN ? $answer : '';
  curl_close($handcurl);
}

Before:
Code: [Select]
// SITEMAP
 function sitemap() {
...

And add call function:
Code: [Select]
sitemapgz();

At end of function sitemap()
« Last Edit: May 05, 2009, 11:27:18 AM by someone »
Logged

roie

  • Newbie
  • *
  • Karma: 1
  • Posts: 32
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #1 on: May 14, 2009, 09:09:36 PM »

This mod works great, many thanks for this one, much appreciated!
« Last Edit: May 14, 2009, 09:33:23 PM by roie »
Logged

toolman

  • Newbie
  • *
  • Karma: 2
  • Posts: 31
    • Magazyn kodów - największa baza tipsów, kodów i solucji do gier
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #2 on: May 20, 2009, 08:13:00 PM »

This mod works great!

Dzięki :P

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #3 on: May 28, 2009, 08:38:40 PM »

Seems to work perfectly. Thanks someone. +1 karma.
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #4 on: May 29, 2009, 08:21:08 AM »

Yes it's working very well but,
I got an ERROR 500 when calling the HTML sitemap since,
there's a conflict with the sitemap.xml file,
and the sitemap/ page.
Gotta fix it.

1 karma for you too.

roie

  • Newbie
  • *
  • Karma: 1
  • Posts: 32
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #5 on: July 01, 2009, 08:44:37 PM »

Found another major problem (for me):
This mod generates plenty <UL> tags before the DOCTYPE decalation. This can't be good for Google or for template driven sites (may break the layout).
I just removed the echo '<ul>'; so you don't get the ping notice...
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #6 on: July 02, 2009, 11:32:44 AM »

Found another major problem (for me):
This mod generates plenty <UL> tags before the DOCTYPE decalation. This can't be good for Google or for template driven sites (may break the layout).
I just removed the echo '<ul>'; so you don't get the ping notice...
I don't get at all what you're sayin pal. This xml sitemap really rocks. There's no <ul> at all.

roie

  • Newbie
  • *
  • Karma: 1
  • Posts: 32
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #7 on: July 02, 2009, 08:13:27 PM »

It is an excellent mod, i agree, but you can clearly see the echo '<ul>'; in the code above. and this generates multiple ul tags at the top of your HTML...
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #8 on: July 03, 2009, 12:01:01 PM »

Yeap you're right: there's this <ul> echo in the code above. I was looking at the xml outputed code where (in my case) there's no <ul> at all. ???
If the feed seems not to be valid for the feed validator from the w3.org, however Google and other search engines recognize it.
I tried different ways to make this line valid:
Code: [Select]
  $xml.='<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">'.NL;
changing it to:
Code: [Select]
  $xml.='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
without success.

One another thing is Google doesn't allow automatic ping of sitemaps, so in the code I commented out these lines:
Code: [Select]
$handcurl = curl_init();
  $url='http://www.google.com/webmasters/sitemaps/ping?sitemap='.$link.'sitemap.xml.gz';
  curl_setopt($handcurl, CURLOPT_URL, $url);
  curl_setopt($handcurl, CURLOPT_RETURNTRANSFER, 1);
  $answer = curl_exec($handcurl);
  echo _ADMIN ? $answer : '';
  curl_close($handcurl);


Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #9 on: January 29, 2010, 02:54:36 PM »

Uh oh...
something has been forgotten?
on my snews I got:
Code: [Select]
if ($categorySEF == "sitemap.xml") {sitemap(); die;}
I tried to call it by changing function's name:
Code: [Select]
if ($categorySEF == "sitemap.xml") {sitemapgz(); die;}
but it gives:
Quote
Fatal error: Call to undefined function sitemapgz() in.. /../html5/onair-snooze.php  on line 180
I don't get it: why this error? It should make the URL /sitemap.xml working and displaying it?
Am I wrong?

Joost

  • Guest
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #10 on: January 29, 2010, 06:20:06 PM »

Quote
Fatal error: Call to undefined function sitemapgz() in.. /../html5/onair-snooze.php  on line 180
I don't get it: why this error? It should make the URL /sitemap.xml working and displaying it?
Am I wrong?

It means it cannot find function sitemapgz(). Either you forgot to include the file containing that function, or you forgot to paste the function in onair-snooze.php.
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #11 on: January 30, 2010, 09:06:37 AM »

Quote
Fatal error: Call to undefined function sitemapgz() in.. /../html5/onair-snooze.php  on line 180
I don't get it: why this error? It should make the URL /sitemap.xml working and displaying it?
Am I wrong?

It means it cannot find function sitemapgz(). Either you forgot to include the file containing that function, or you forgot to paste the function in onair-snooze.php.
Bonjour bonjour Monsieur l'ours,
well well well... the function is in a separate file with some other Mods and the function is called inside the core further lines below and it works : I do have a compressed XML sitemap.
I tried too after having pasted the whole function inside the Core but it gave the same result.
There's something buggy somewhere but I don't get it.

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #12 on: January 30, 2010, 03:22:17 PM »

Quote
Fatal error: Call to undefined function sitemapgz() in.. /../html5/onair-snooze.php  on line 180
There's something buggy somewhere but I don't get it.

I have experienced in the past that.... if the function istelf is not already loaded before the function is called, the function-call will toss the error.
Check and see if your function-call is placed in your file before or after function sitemapgz() is being loaded/included.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #13 on: January 30, 2010, 05:13:24 PM »

Hi Doug.
Weirdo isn' it?

Code: [Select]
if ($categorySEF == "sitemap.xml") {sitemap(); die;}
is in the basic startup function.
Placing the whole sitemapgz function before, after or in a separate file gives nothing:
Code: [Select]
if ($categorySEF == "sitemap.xml") {sitemapgz(); die;}
I don't get it at all.

BTW:
I've changed the 1st lines to be more friendly with all search engines (there's not only Google on the Web planet):
Code: [Select]
  $xml='<?xml version="1.0" encoding="UTF-8"?>'.NL;
  $xml.='<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
and it is valid.

Pein

  • Newbie
  • *
  • Karma: 0
  • Posts: 28
  • n0 n33d t0 b3 l33t
Re: Sitemap *.xml.gz for google webmaster tools
« Reply #14 on: April 05, 2010, 03:06:01 PM »

Can anyone please tell me how to install that mod?

I dont understand! So please help me out, thanks!

Pein
Logged
Pages: [1] 2