googlesitemap_model.class.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_sys_class('model', '', 0);
  4. class googlesitemap_model extends model {
  5. public function __construct() {
  6. $this->db_config = pc_base::load_config('database');
  7. $this->header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
  8. $this->charset = "UTF-8";
  9. $this->footer = "\t</urlset>\n";
  10. $this->items = array();
  11. parent::__construct();
  12. }
  13. /**
  14. * 增加一个新的子项
  15. *@access public
  16. *@param google_sitemap item $new_item
  17. */
  18. function add_item($new_item) {
  19. $this->items[] = $new_item;
  20. }
  21. /**
  22. * 生成XML文档
  23. *@access public
  24. *@param string $file_name 如果提供了文件名则生成文件,否则返回字符串.
  25. *@return [void|string]
  26. */
  27. function build( $file_name = null ) {
  28. $map = $this->header . "\n";
  29. foreach ($this->items AS $item) {
  30. $map .= "\t\t<url>\n\t\t\t<loc>$item->loc</loc>\n";
  31. // lastmod
  32. if ( !empty( $item->lastmod ) )
  33. $map .= "\t\t\t<lastmod>$item->lastmod</lastmod>\n";
  34. // changefreq
  35. if ( !empty( $item->changefreq ) )
  36. $map .= "\t\t\t<changefreq>$item->changefreq</changefreq>\n";
  37. // priority
  38. if ( !empty( $item->priority ) )
  39. $map .= "\t\t\t<priority>$item->priority</priority>\n";
  40. $map .= "\t\t</url>\n\n";
  41. }
  42. $map .= $this->footer . "\n";
  43. if (!is_null($file_name))
  44. {
  45. return file_put_contents($file_name, $map);
  46. }
  47. else
  48. {
  49. return $map;
  50. }
  51. }
  52. }
  53. ?>