Blame view

protected/components/SitemapWriter.php 1.69 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  <?php
  /**
   * @author Bogdan Savluk <savluk.bogdan@gmail.com>
   * For more info about params see:
   * @link http://www.sitemaps.org/protocol.html
   */
  class SitemapWriter
  {
      const FREQ_ALWAYS = 0;
      const FREQ_HOURLY = 1;
      const FREQ_DAILY = 2;
      const FREQ_WEEKLY = 3;
      const FREQ_MONTHLY = 4;
      const FREQ_YEARLY = 5;
      const FREQ_NEVER = 6;
      private static $freq = array(
          'always',
          'hourly',
          'daily',
          'weekly',
          'monthly',
          'yearly',
          'newer');
      private $_pull = array();
  
      public function addPage($location, $lastmod = null, $changefreq = null, $priority = null)
      {
          $this->_pull[] = array(
              $location, $lastmod, $changefreq, $priority
          );
      }
  
      public function generateXml($processOutput = false)
      {
          if ($processOutput) {
              ob_start();
          }
          echo '<?xml version="1.0" encoding="UTF-8"?>',"\n",
  
          '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',"\n";
  
          foreach ($this->_pull as &$item) {
              echo "  ",'<url>',"\n    ",'<loc>', htmlentities($item[0]), '</loc>',"\n";
              if (isset($item[1])) {
                  echo "    ",'<lastmod>', $item[1], '</lastmod>',"\n";
              }
              if (isset($item[1])) {
                  echo "    ",'<changefreq>', self::$freq[$item[2]], '</changefreq>',"\n";
  
              }
              if (isset($item[1])) {
                  echo "    ",'<priority>', $item[3], '</priority>',"\n";
              }
              echo "  ",'</url>',"\n";
          }
          echo '</urlset>';
          $res = null;
          if ($processOutput) {
              $res = ob_get_clean();
          }
          return $res;
      }
  }