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

Documentation generated on Mon, 19 Sep 2011 14:13:27 +0200 by phpDocumentor 1.4.3