Example #1
0
function sitemap($file = "./sitemap.xml")
{
    //define( "ENT_XML1",        16    );//Porque no conoce esto el PHP?????!!!
    $sl = "\n";
    $sg = "\t";
    $fp = fopen($file, "w");
    $where = "visible=1";
    $order = "id asc";
    $limit = "";
    $tipo = "arrIds";
    $arrIdsProd = Producto::AllToArray($where, $order, $limit, $tipo);
    $where = "visible=1";
    $order = "id asc";
    $limit = "";
    $tipo = "arrIds";
    $arrIdsCat = Categoria::AllToArray($where, $order, $limit, $tipo);
    $contents = '<?xml version="1.0" encoding="UTF-8" ?>' . $sl;
    $contents .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . $sl;
    foreach ($arrIdsCat as $idCat) {
        $objCat = new Categoria($idCat);
        $contents .= $sg . '<url>' . $sl;
        $contents .= $sg . $sg . '<loc>';
        $contents .= str_replace("&", "&amp;", $objCat->url(false));
        $contents .= '</loc>' . $sl;
        $contents .= $sg . $sg . '<lastmod>';
        $contents .= Fecha::toW3C(Fecha::fromMysql($objCat->GETupdate()));
        $contents .= '</lastmod>' . $sl;
        $contents .= $sg . $sg . '<changefreq>';
        $contents .= "weekly";
        $contents .= '</changefreq>' . $sl;
        $contents .= $sg . $sg . '<priority>';
        $contents .= '0.5';
        //TODO: mejora: establecer este valor en base a las valoraciones de los usuarios
        $contents .= '</priority>' . $sl;
        $contents .= $sg . '</url>' . $sl;
    }
    foreach ($arrIdsProd as $idProd) {
        $objProd = new Producto($idProd);
        $contents .= $sg . '<url>' . $sl;
        $contents .= $sg . $sg . '<loc>';
        $contents .= str_replace("&", "&amp;", $objProd->url(false));
        $contents .= '</loc>' . $sl;
        $contents .= $sg . $sg . '<lastmod>';
        $contents .= Fecha::toW3C(Fecha::fromMysql($objProd->GETupdate()));
        $contents .= '</lastmod>' . $sl;
        $contents .= $sg . $sg . '<changefreq>';
        $contents .= "weekly";
        $contents .= '</changefreq>' . $sl;
        $contents .= $sg . $sg . '<priority>';
        $contents .= '0.5';
        //TODO: mejora: establecer este valor en base a las valoraciones de los usuarios
        $contents .= '</priority>' . $sl;
        $contents .= $sg . '</url>' . $sl;
    }
    $contents .= '</urlset>' . $sl;
    fwrite($fp, $contents);
    fclose($fp);
    chmod($file, 0666);
    //comprimirlo
    $gzfile = $file . ".gz";
    // Open the gz file (w9 is the highest compression)
    $fp = gzopen($gzfile, 'w9');
    // Compress the file
    gzwrite($fp, file_get_contents($file));
    // Close the gz file and we are done
    gzclose($fp);
    chmod($gzfile, 0666);
    unlink($file);
}