Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created February 22, 2012 12:48

Revisions

  1. Darkflib created this gist Feb 22, 2012.
    71 changes: 71 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    <?php

    header('Content-type: text/xml');
    //connect to db
    $link=mysql_pconnect($db['write']['host'],$db['write']['user'],$db['write']['pass']) or die ("Could not connect to datadase");
    mysql_select_db($db['write']['name']) or die ("could not select database");

    $base='http://'.$_SERVER['HTTP_HOST'];
    $xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?>\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />');

    //main page
    $url = $xml->addChild('url');
    $url->addChild('loc',$base.'/');
    //$url->addChild('lastmod',gmdate('c',filemtime('main.html')));
    $url->addChild('priority','1.0');
    //pages
    $query="select pagename, unix_timestamp(staticpage.modified) as modified from page";
    $result=mysql_query($query);
    if ($result) {
    while ($line=mysql_fetch_assoc($result)) {
    $url = $xml->addChild('url');
    $url->addChild('loc',$base.'/'.$line['pagename']);
    if ($line['modified']!=0) $url->addChild('lastmod',gmdate('c',$line['modified']));
    $url->addChild('priority','0.5');
    }
    }

    //category
    $query="select * from category";
    $result=mysql_query($query);
    if ($result) {
    while ($line=mysql_fetch_assoc($result)) {
    $url = $xml->addChild('url');
    $entryurl='/category/'.$line['categorysafename'];
    $url->addChild('loc',$base.$entryurl);
    $url->addChild('priority','0.8');
    }
    }

    //articles
    $query="select article.*,unix_timestamp(article.modified) as modified from article";
    $result=mysql_query($query);
    if ($result) {
    while ($line=mysql_fetch_assoc($result)) {
    $url = $xml->addChild('url');
    $entryurl='/article/'.$line['pagename'];
    $url->addChild('loc',$base.$entryurl);
    // some entries had a missing modified data, this only adds it if present.
    if ($line['modified']!=0) $url->addChild('lastmod',gmdate('c',$line['modified']));
    $url->addChild('priority','0.6');
    }
    }

    //feeds
    $url = $xml->addChild('url');
    $url->addChild('loc',$base.'/events/rss');
    $url->addChild('priority','0.6');
    $query="select * from blogcategory";
    $result=mysql_query($query);
    if ($result) {
    while ($line=mysql_fetch_assoc($result)) {
    $url = $xml->addChild('url');
    $url->addChild('loc',$base.'/'.$line['categoryname'].'/rss');
    $url->addChild('priority','0.6');
    }
    }

    $output=$xml->asXML();
    //removed caching and output encoding code.
    echo $output;
    ?>