Source for file jResponseSitemap.class.php

Documentation is available at jResponseSitemap.class.php

  1. <?php
  2. /**
  3. * @package     jelix
  4. * @subpackage  core_response
  5. * @author      Baptiste Toinot
  6. * @contributor Laurent Jouanneau
  7. * @copyright   2008 Baptiste Toinot, 2011 Laurent Jouanneau
  8. * @link        http://www.jelix.org
  9. * @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11.  
  12. /**
  13. *
  14. */
  15. require_once(JELIX_LIB_PATH.'tpl/jTpl.class.php');
  16.  
  17. /**
  18. * Sitemap 0.9 response
  19. *
  20. * @package jelix
  21. * @subpackage core_response
  22. * @link http://www.sitemaps.org/
  23. * @since 1.2
  24. */
  25. class jResponseSitemap extends jResponse {
  26.  
  27.     /**
  28.     * Ident of the response type
  29.     * @var string 
  30.     */
  31.     protected $_type = 'sitemap';
  32.  
  33.     /**
  34.     * Frequency change url
  35.     * @var array 
  36.     */
  37.     protected $allowedChangefreq = array('always', 'hourly', 'daily', 'weekly',
  38.                                          'monthly', 'yearly', 'never');
  39.     /**
  40.     * Maximum number of URLs for a sitemap index file
  41.     * @var int 
  42.     */
  43.     protected $maxSitemap = 1000;
  44.  
  45.     /**
  46.     * Maximum number of URLs for a sitemap file
  47.     * @var int 
  48.     */
  49.     protected $maxUrl = 50000;
  50.  
  51.     /**
  52.     * List of URLs for a sitemap index file
  53.     * @var array() 
  54.     */
  55.     protected $urlSitemap = array();
  56.  
  57.     /**
  58.     * List of URLs for a sitemap file
  59.     * @var array() 
  60.     */
  61.     protected $urlList = array();
  62.  
  63.     /**
  64.      * The template container
  65.      * @var jTpl 
  66.      */
  67.     public $content;
  68.  
  69.     /**
  70.      * Selector of the template file
  71.      * @var string 
  72.      */
  73.     public $contentTpl;
  74.  
  75.     /**
  76.      * Class constructor
  77.      *
  78.      * @return void 
  79.      */
  80.     public function __construct() {
  81.         $this->content  = new jTpl();
  82.         $this->contentTpl = 'jelix~sitemap';
  83.         parent::__construct();
  84.     }
  85.  
  86.     /**
  87.      * Generate the content and send it
  88.      * Errors are managed
  89.      * @return boolean true if generation is ok, else false
  90.      */
  91.     final public function output() {
  92.         $this->_headSent = false;
  93.         $this->_httpHeaders['Content-Type'] = 'application/xml;charset=UTF-8';
  94.         $this->sendHttpHeaders();
  95.         echo '<?xml version="1.0" encoding="UTF-8"?>', "\n";
  96.  
  97.         if (count($this->urlSitemap)) {
  98.             echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  99.             $this->_headSent = true;
  100.             $this->contentTpl = 'jelix~sitemapindex';
  101.             $this->content->assign('sitemaps', $this->urlSitemap);
  102.         } else {
  103.             echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  104.             $this->_headSent = true;
  105.             $this->content->assign('urls', $this->urlList);
  106.         }
  107.  
  108.         $this->content->display($this->contentTpl);
  109.  
  110.         if ($this->hasErrors()) {
  111.             echo $this->getFormatedErrorMsg();
  112.         }
  113.  
  114.         if (count($this->urlSitemap)) {
  115.             echo '</sitemapindex>';
  116.         } else {
  117.             echo '</urlset>';
  118.         }
  119.         return true;
  120.     }
  121.  
  122.     /**
  123.      * output errors
  124.      */
  125.     final public function outputErrors() {
  126.         if (!$this->_headSent) {
  127.             if (!$this->_httpHeadersSent) {
  128.                 header("HTTP/1.0 500 Internal Server Error");
  129.                 header('Content-Type: text/xml;charset=UTF-8');
  130.             }
  131.             echo '<?xml version="1.0" encoding="UTF-8"?>';
  132.         }
  133.  
  134.         echo '<errors xmlns="http://jelix.org/ns/xmlerror/1.0">';
  135.         if ($this->hasErrors()) {
  136.             echo $this->getFormatedErrorMsg();
  137.         } else {
  138.             echo '<error>Unknow Error</error>';
  139.         }
  140.         echo '</errors>';
  141.     }
  142.  
  143.     /**
  144.      * Format error messages
  145.      * @return string formated errors
  146.      */
  147.     protected function getFormatedErrorMsg() {
  148.         $errors = '';
  149.         foreach ($GLOBALS['gJCoord']->errorMessages as $e) {
  150.            $errors .=  '<error xmlns="http://jelix.org/ns/xmlerror/1.0" type="'. $e[0] .'" code="'. $e[1] .'" file="'. $e[3] .'" line="'. $e[4] .'">'. $e[2] .'</error>'. "\n";
  151.         }
  152.         return $errors;
  153.     }
  154.  
  155.     /**
  156.      * add a URL in a sitemap file
  157.      * @param string $loc URL of the page
  158.      * @param string $lastmod The date of last modification of the file
  159.      * @param string $changefreq How frequently the page is likely to change
  160.      * @param string $priority The priority of this URL relative to other URLs
  161.      * @return boolean true if addition is ok, else false
  162.      */
  163.     public function addUrl($loc, $lastmod = null, $changefreq = null, $priority = null) {
  164.  
  165.         if (isset($loc[2048]) || count($this->urlList) >= $this->maxUrl) {
  166.             return false;
  167.         }
  168.         global $gJCoord;
  169.         $url = new jSitemapUrl();
  170.         $url->loc = $gJCoord->request->getServerURI() . $loc;
  171.  
  172.         if (($timestamp = strtotime($lastmod))) {
  173.             $url->lastmod = date('c', $timestamp);
  174.         }
  175.  
  176.         if ($changefreq && in_array($changefreq, $this->allowedChangefreq)) {
  177.             $url->changefreq = $changefreq;
  178.         }
  179.  
  180.         if ($priority && is_numeric($priority) && $priority >= 0 && $priority <= 1) {
  181.             $url->priority = sprintf('%0.1f', $priority);
  182.         }
  183.  
  184.         $this->urlList[] = $url;
  185.         return true;
  186.     }
  187.  
  188.     /**
  189.      * add a URL in a sitemap file
  190.      * @param string $loc URL of sitemap file
  191.      * @param string $lastmod The date of last modification of the sitemap file
  192.      * @return boolean true if addition is ok, else false
  193.      */
  194.     public function addSitemap($loc, $lastmod = null) {
  195.  
  196.         if (isset($loc[2048]) || count($this->urlSitemap) >= $this->maxSitemap) {
  197.             return false;
  198.         }
  199.  
  200.         global $gJCoord;
  201.         $sitemap = new jSitemapIndex();
  202.         $sitemap->loc = $gJCoord->request->getServerURI() . $loc;
  203.  
  204.         if (($timestamp = strtotime($lastmod))) {
  205.             $sitemap->lastmod = date('c', $timestamp);
  206.         }
  207.  
  208.         $this->urlSitemap[] = $sitemap;
  209.         return true;
  210.     }
  211.  
  212.     /**
  213.      * Add URLs automatically from urls.xml
  214.      * @return void 
  215.      */
  216.     public function importFromUrlsXml() {
  217.         $urls = $this->_parseUrlsXml();
  218.         foreach ($urls as $url) {
  219.             $this->addUrl($url);
  220.         }
  221.     }
  222.  
  223.     /**
  224.      * Return pathinfo URLs automatically from urls.xml
  225.      * @return array 
  226.      */
  227.     public function getUrlsFromUrlsXml() {
  228.         return $this->_parseUrlsXml();
  229.     }
  230.  
  231.     /**
  232.      * Submitting a sitemap by sending an HTTP request
  233.      * @return boolean 
  234.      */
  235.     public function ping($uri) {
  236.         $parsed_url = parse_url($uri);
  237.         if (!$parsed_url || !is_array($parsed_url)) {
  238.             return false;
  239.         }
  240.         $http = new jHttp($parsed_url['host']);
  241.         $http->get($parsed_url['path'] . '?' . $parsed_url['query']);
  242.         if ($http->getStatus() != 200) {
  243.             return false;
  244.         }
  245.         return true;
  246.     }
  247.  
  248.     /**
  249.      * Parse urls.xml and return pathinfo URLs
  250.      * @return array 
  251.      */
  252.     protected function _parseUrlsXml() {
  253.         global $gJConfig;
  254.  
  255.         $urls = array();
  256.         $significantFile = $gJConfig->urlengine['significantFile'];
  257.         $basePath = $gJConfig->urlengine['basePath'];
  258.         $epExt = ($gJConfig->urlengine['multiview']?$gJConfig->urlengine['entrypointExtension']:'');
  259.  
  260.         $file = JELIX_APP_TEMP_PATH.'compiled/urlsig/' . $significantFile . '.creationinfos.php';
  261.  
  262.         if (file_exists($file)) {
  263.             require $file;
  264.             foreach ($GLOBALS['SIGNIFICANT_CREATEURL'] as $selector => $createinfo) {
  265.                 if ($createinfo[0] != 1 && $createinfo[0] != 4) {
  266.                     continue;
  267.                 }
  268.                 if ($createinfo[0] == 4) {
  269.                     foreach ($createinfo as $k => $createinfo2) {
  270.                         if ($k == 0) continue;
  271.  
  272.                         if ($createinfo2[2] == true // https needed -> we don't take this url. FIXME
  273.                          ||count($createinfo2[3]) ) { // there are some dynamique parameters, we don't take it this we cannot guesse dynamic parameters
  274.                             continue;
  275.                         }
  276.                         $urls[] = $basePath.($createinfo2[1]?$createinfo2[1].$epExt:'').$createinfo2[5];
  277.                     }
  278.                 }
  279.                 else if ($createinfo[2] == true // https needed -> we don't take this url. FIXME
  280.                          ||  count($createinfo[3]) ) { // there are some dynamique parameters, we don't take it this we cannot guesse dynamic parameters
  281.                     continue;
  282.                 }
  283.                 else {
  284.                     $urls[] = $basePath.($createinfo[1]?$createinfo[1].$epExt:'').$createinfo[5];
  285.                 }
  286.             }
  287.         }
  288.         return $urls;
  289.     }
  290. }
  291.  
  292. /**
  293.  * Content of a URL
  294.  * @package jelix
  295.  * @subpackage core_response
  296.  * @since 1.2
  297.  */
  298. class jSitemapUrl {
  299.  
  300.     /**
  301.      * URL of the page
  302.      * @var string 
  303.      */
  304.     public $loc;
  305.  
  306.     /**
  307.      * The date of last modification of the page
  308.      * @var string 
  309.      */
  310.     public $lastmod;
  311.  
  312.     /**
  313.      * How frequently the page is likely to change
  314.      * @var string 
  315.      */
  316.     public $changefreq;
  317.  
  318.     /**
  319.      * The priority of this URL relative to other URLs
  320.      * @var string 
  321.      */
  322.     public $priority;
  323. }
  324.  
  325. /**
  326.  * Content of a sitemap file
  327.  * @package    jelix
  328.  * @subpackage core_response
  329.  * @since 1.2
  330.  */
  331. class jSitemapIndex {
  332.  
  333.     /**
  334.      * URL of the sitemap file
  335.      * @var string 
  336.      */
  337.     public $loc;
  338.  
  339.     /**
  340.      * The date of last modification of the sitemap file
  341.      * @var string 
  342.      */
  343.     public $lastmod;
  344. }

Documentation generated on Thu, 19 Sep 2013 00:06:55 +0200 by phpDocumentor 1.4.3