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-2012 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.         
  93.         if($this->_outputOnlyHeaders){
  94.             $this->sendHttpHeaders();
  95.             return true;
  96.         }
  97.         
  98.         $this->_httpHeaders['Content-Type'] = 'application/xml;charset=UTF-8';
  99.  
  100.         if (count($this->urlSitemap)) {
  101.             $head = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  102.             $foot = '</sitemapindex>';
  103.             $this->contentTpl = 'jelix~sitemapindex';
  104.             $this->content->assign('sitemaps', $this->urlSitemap);
  105.         } else {
  106.             $head = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  107.             $foot = '</urlset>';
  108.             $this->content->assign('urls', $this->urlList);
  109.         }
  110.         $content = $this->content->fetch($this->contentTpl);
  111.  
  112.         // content is generated, no errors, we can send it
  113.         $this->sendHttpHeaders();
  114.         echo '<?xml version="1.0" encoding="UTF-8"?>', "\n";
  115.         echo $head, $content, $foot;
  116.         return true;
  117.     }
  118.  
  119.     /**
  120.      * add a URL in a sitemap file
  121.      * @param string $loc URL of the page
  122.      * @param string $lastmod The date of last modification of the file
  123.      * @param string $changefreq How frequently the page is likely to change
  124.      * @param string $priority The priority of this URL relative to other URLs
  125.      * @return boolean true if addition is ok, else false
  126.      */
  127.     public function addUrl($loc, $lastmod = null, $changefreq = null, $priority = null) {
  128.  
  129.         if (isset($loc[2048]) || count($this->urlList) >= $this->maxUrl) {
  130.             return false;
  131.         }
  132.  
  133.         $url = new jSitemapUrl();
  134.         $url->loc = jApp::coord()->request->getServerURI() . $loc;
  135.  
  136.         if (($timestamp = strtotime($lastmod))) {
  137.             $url->lastmod = date('c', $timestamp);
  138.         }
  139.  
  140.         if ($changefreq && in_array($changefreq, $this->allowedChangefreq)) {
  141.             $url->changefreq = $changefreq;
  142.         }
  143.  
  144.         if ($priority && is_numeric($priority) && $priority >= 0 && $priority <= 1) {
  145.             $url->priority = sprintf('%0.1f', $priority);
  146.         }
  147.  
  148.         $this->urlList[] = $url;
  149.         return true;
  150.     }
  151.  
  152.     /**
  153.      * add a URL in a sitemap file
  154.      * @param string $loc URL of sitemap file
  155.      * @param string $lastmod The date of last modification of the sitemap file
  156.      * @return boolean true if addition is ok, else false
  157.      */
  158.     public function addSitemap($loc, $lastmod = null) {
  159.  
  160.         if (isset($loc[2048]) || count($this->urlSitemap) >= $this->maxSitemap) {
  161.             return false;
  162.         }
  163.  
  164.         $sitemap = new jSitemapIndex();
  165.         $sitemap->loc = jApp::coord()->request->getServerURI() . $loc;
  166.  
  167.         if (($timestamp = strtotime($lastmod))) {
  168.             $sitemap->lastmod = date('c', $timestamp);
  169.         }
  170.  
  171.         $this->urlSitemap[] = $sitemap;
  172.         return true;
  173.     }
  174.  
  175.     /**
  176.      * Add URLs automatically from urls.xml
  177.      * @return void 
  178.      */
  179.     public function importFromUrlsXml() {
  180.         $urls = $this->_parseUrlsXml();
  181.         foreach ($urls as $url) {
  182.             $this->addUrl($url);
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Return pathinfo URLs automatically from urls.xml
  188.      * @return array 
  189.      */
  190.     public function getUrlsFromUrlsXml() {
  191.         return $this->_parseUrlsXml();
  192.     }
  193.  
  194.     /**
  195.      * Submitting a sitemap by sending an HTTP request
  196.      * @return boolean 
  197.      */
  198.     public function ping($uri) {
  199.         $parsed_url = parse_url($uri);
  200.         if (!$parsed_url || !is_array($parsed_url)) {
  201.             return false;
  202.         }
  203.         $http = new jHttp($parsed_url['host']);
  204.         $http->get($parsed_url['path'] . '?' . $parsed_url['query']);
  205.         if ($http->getStatus() != 200) {
  206.             return false;
  207.         }
  208.         return true;
  209.     }
  210.  
  211.     /**
  212.      * Parse urls.xml and return pathinfo URLs
  213.      * @return array 
  214.      */
  215.     protected function _parseUrlsXml() {
  216.  
  217.         $urls = array();
  218.  
  219.         $conf = &jApp::config()->urlengine;
  220.         $significantFile = $conf['significantFile'];
  221.         $basePath = $conf['basePath'];
  222.         $epExt = ($conf['multiview'] ? '.php':'');
  223.  
  224.         $file = jApp::tempPath('compiled/urlsig/' . $significantFile . '.creationinfos_15.php');
  225.  
  226.         if (file_exists($file)) {
  227.             require $file;
  228.             foreach ($GLOBALS['SIGNIFICANT_CREATEURL'] as $selector => $createinfo) {
  229.                 if ($createinfo[0] != 1 && $createinfo[0] != 4) {
  230.                     continue;
  231.                 }
  232.                 if ($createinfo[0] == 4) {
  233.                     foreach ($createinfo as $k => $createinfo2) {
  234.                         if ($k == 0) continue;
  235.  
  236.                         if ($createinfo2[2] == true // https needed -> we don't take this url. FIXME
  237.                          ||count($createinfo2[3]) ) { // there are some dynamique parameters, we don't take it this we cannot guesse dynamic parameters
  238.                             continue;
  239.                         }
  240.                         $urls[] = $basePath.($createinfo2[1]?$createinfo2[1].$epExt:'').$createinfo2[5];
  241.                     }
  242.                 }
  243.                 else if ($createinfo[2] == true // https needed -> we don't take this url. FIXME
  244.                          ||  count($createinfo[3]) ) { // there are some dynamique parameters, we don't take it this we cannot guesse dynamic parameters
  245.                     continue;
  246.                 }
  247.                 else {
  248.                     $urls[] = $basePath.($createinfo[1]?$createinfo[1].$epExt:'').$createinfo[5];
  249.                 }
  250.             }
  251.         }
  252.         return $urls;
  253.     }
  254. }
  255.  
  256. /**
  257.  * Content of a URL
  258.  * @package jelix
  259.  * @subpackage core_response
  260.  * @since 1.2
  261.  */
  262. class jSitemapUrl {
  263.  
  264.     /**
  265.      * URL of the page
  266.      * @var string 
  267.      */
  268.     public $loc;
  269.  
  270.     /**
  271.      * The date of last modification of the page
  272.      * @var string 
  273.      */
  274.     public $lastmod;
  275.  
  276.     /**
  277.      * How frequently the page is likely to change
  278.      * @var string 
  279.      */
  280.     public $changefreq;
  281.  
  282.     /**
  283.      * The priority of this URL relative to other URLs
  284.      * @var string 
  285.      */
  286.     public $priority;
  287. }
  288.  
  289. /**
  290.  * Content of a sitemap file
  291.  * @package    jelix
  292.  * @subpackage core_response
  293.  * @since 1.2
  294.  */
  295. class jSitemapIndex {
  296.  
  297.     /**
  298.      * URL of the sitemap file
  299.      * @var string 
  300.      */
  301.     public $loc;
  302.  
  303.     /**
  304.      * The date of last modification of the sitemap file
  305.      * @var string 
  306.      */
  307.     public $lastmod;
  308. }

Documentation generated on Wed, 04 Jan 2017 22:56:27 +0100 by phpDocumentor 1.4.3