Класс Sitemap
Объект $Sitemap:
Объект для генерации
sitemap.xml
Исходный код
class Sitemap extends \Cms\Site\Base { … }
Свойства
$scheme
$Sitemap->scheme = 'http';
Исходный код
var $scheme = 'http';
$host
$Sitemap->host = 'localhost';
Исходный код
var $host = 'localhost';
$sitemap
$Sitemap->sitemap = array();
Исходный код
var $sitemap = array();
$stdout
$Sitemap->stdout
Исходный код
var $stdout;
$debug
$Sitemap->debug = false;
Исходный код
var $debug = false;
$table_add
$Sitemap->table_add = array();
Исходный код
var $table_add = array();
$table_del
$Sitemap->table_del = array();
Исходный код
var $table_del = array();
$type_del
$Sitemap->type_del = array( 'banner', 'sitemap', 'find', 'user' );
Исходный код
var $type_del = array( 'banner', 'sitemap', 'find', 'user' );
$has_pager
$Sitemap->has_pager = true;
Исходный код
var $has_pager = true;
Методы
__construct()
new Sitemap();
Конструктор
Исходный код
function __construct() {
if (defined('STDOUT')) eval('$this->stdout = STDOUT;');
}
setUrl()
$Sitemap->setUrl($url);
Исходный код
function setUrl($url) {
if ($info = parse_url($url)) {
if ($scheme = $info['scheme']) $this->scheme = $scheme;
if ($host = $info['host']) $this->host = $host;
return true;
}
}
addUrl()
$Sitemap->addUrl($href, $info=null);
Исходный код
function addUrl($href, $info = null) {
if ($href[0] !== '/') return;
if (!is_array($info)) $info = array();
if (!isset($this->sitemap[$href])) $this->sitemap[$href] = $info;
else foreach ($info as $k => $v) $this->sitemap[$href][$k] = $v;
}
addItem()
$Sitemap->addItem($row);
Исходный код
function addItem($row) {
$Data = $this->Data();
$Main = $this->Main();
$info = array();
if (!$id = $row['id']) return;
if (!$href = $Main->href($id)) return;
if ($row['auth']) return;
if ($row['href']) return;
if ($row['type'] === 'banner') return;
if (($menu = $row['menu']) === '') {
if (!$menu) if ($Data->getRow("SELECT `menu` FROM `menu` WHERE `id`={$Data->quote($id)} LIMIT 1")) $menu = 'Y';
if (!$menu) return;
}
if (false
|| substr($id, 0, 7) == '/admin/'
|| in_array($id, array( 'admin', 'search', 'cart', 'register', 'remind', 'profile', 'personal', 'enter', 'cms', 'pms', 'pay' ))
|| in_array($row['parent'], array( 'admin', 'search', 'cart', 'register', 'remind', 'profile', 'personal', 'enter', 'cms', 'pms', 'pay' ))
) return;
if ($row['noindex']) return;
if ($Data->dLookup("SELECT `value` FROM `info` WHERE `id`={$Data->quote($id)} AND `key`='noindex' LIMIT 1")) return;
$info['id'] = $id;
if ($type = $row['type']) {
$info['type'] = $type;
if (in_array($type, $this->type_del)) return;
}
if ($stamp = $row['stamp']) $info['stamp'] = $stamp;
$this->addUrl($href, $info);
if ($this->hasPager($row)) $this->addPager($href);
}
hasPager()
$Sitemap->hasPager(&$row);
Исходный код
function hasPager(&$row) {
if (!$this->has_pager) return;
$Main = $this->Main();
if ($row['list']) return true;
if (is_object($Main->Modules) && is_array($Main->Modules->modules)) $module = $Main->Modules->modules[$row['type']];
elseif (is_array($Main->modules)) $module = $Main->modules[$row['type']];
if (is_array($module) && $module['list']) return true;
if ($row['type'] != 'page') {
$file = '/cms/html/detail.' . $row['type'] . '.php';
$r = $this->_has_pager[$file];
if (!isset($r)) {
$r = false;
if (strlen($code = @file_get_contents($this->root() . $file)))
if (strpos($code, 'displayBlock') !== false || strpos($code, 'Display->block') !== false || strpos($code, 'Display()->block') !== false || strpos($code, 'displayPager') !== false)
if (strpos($code, '9999') === false)
$r = true;
$this->_has_pager[$file] = $r;
}
if ($r) return true;
}
if (true) {
$file = '/' . trim($row['id'], '/') . '/index.php';
if (strlen($code = @file_get_contents($this->root() . $file)))
if (strpos($code, 'displayBlock') !== false || strpos($code, 'Display->block') !== false || strpos($code, 'Display()->block') !== false || strpos($code, 'displayPager') !== false)
if (strpos($code, '9999') === false)
return true;
}
}
addPager()
$Sitemap->addPager($href);
Исходный код
function addPager($href) {
$html = $this->getHtml($href);
if (preg_match_all('~ href="(' . preg_quote($href, '~') . ')?(\\?page=\\d+)"~', $html, $m)) {
foreach ($m[2] as $q) if ($q !== '?page=1') $this->addUrl($href . $q, $this->sitemap[$href]);
}
}
getHtml()
$Sitemap->getHtml($href);
Исходный код
function getHtml($href) {
$url = $this->scheme . '://' . $this->host . $href;
if (!isset($this->curl)) {
$curl = $this->curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Site PRO Sitemap Generator crawler)');
} else {
$curl = $this->curl;
sleep(1);
}
curl_setopt($curl, CURLOPT_URL, $url);
ob_start();
curl_exec($curl);
$html = ob_get_clean();
return $html;
}
getTableList()
$Sitemap->getTableList();
Исходный код
function getTableList() {
if (isset($this->_table_list)) return $this->_table_list;
$Main = $this->Main();
$r = array();
if (is_object($Main->Storage) && is_array($Main->Storage->tables)) $r = array_keys($Main->Storage->tables);
elseif (is_array($Main->tables)) $r = array_keys($Main->tables);
if (!in_array('item', $r)) array_unshift($r, 'item');
foreach (array( 'auth', 'cart', 'talk', 'user', 'group', 'image', 'place', 'banner' ) as $table) if (in_array($table, $r)) unset($r[array_search($table, $r)]);
if (is_array($this->table_add)) foreach ($this->table_add as $table) if (!in_array($table, $r)) $r[] = $table;
if (is_array($this->table_del)) foreach ($this->table_del as $table) if (in_array($table, $r)) unset($r[array_search($table, $r)]);
if ($this->debug) fwrite(STDERR, "DEBUG: Tables:\n" . var_export($r, true) . ";\n");
return $this->_table_list = $r;
}
getStampMain()
$Sitemap->getStampMain();
Исходный код
function getStampMain() {
$stamp = null;
$Data = $this->Data();
foreach ($this->getTableList() as $table) {
if ($_stamp = $Data->dLookup("SELECT MAX(`stamp`) FROM `$table`")) if ($_stamp > $stamp) $stamp = $_stamp;
}
return $stamp;
}
addItemList()
$Sitemap->addItemList();
Исходный код
function addItemList() {
$Data = $this->Data();
foreach ($this->getTableList() as $table) {
foreach ($Data->getRows("SELECT * FROM `$table` ORDER BY `id`") as $row) {
$this->addItem($row);
}
}
}
addHtmlList()
$Sitemap->addHtmlList();
Исходный код
function addHtmlList() {
return;
}
generate()
$Sitemap->generate();
Исходный код
function generate() {
$this->addUrl('/');
$this->addItemList();
$this->addHtmlList();
$this->addUrl('/', array( 'stamp' => $this->getStampMain() ));
return true;
}
output()
$Sitemap->output($file='sitemap.xml');
Исходный код
function output($file = 'sitemap.xml') {
if (!$this->sitemap['/'] || !$this->sitemap['/']['stamp']) return;
if ($file) $stdout = @fopen($this->root() . '/' . ltrim($file, '/'), 'w');
else $stdout = $this->stdout;
fwrite($stdout, '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' . "\n");
fwrite($stdout, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n");
fwrite($stdout, '<!-- Site PRO Sitemap Generator PHP -->' . "\n");
foreach ($this->sitemap as $href => $info) {
$url = $this->scheme . "://" . $this->host . $href;
fwrite($stdout, '<url>');
fwrite($stdout, '<loc>' . $url . '</loc>');
if ($info['stamp']) if ($stamp = strtotime($info['stamp'])) {
fwrite($stdout, '<lastmod>' . strtr(gmdate('Y-m-d H:i:s+00:00', $stamp), ' ', 'T') . '</lastmod>');
}
fwrite($stdout, '</url>' . "\n");
}
fwrite($stdout, '</urlset>' . "\n");
}
__main()
$Sitemap->__main();
Исходный код
function __main() {
@ini_set('memory_limit', '1G');
if (!$this->debug)
@ini_set('error_reporting', 0);
if ($_SERVER['HTTP_HOST']) {
if (!$this->setUrl(($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'])) return;
if (!$this->generate()) return;
$this->output($this->debug ? null : 'sitemap.xml');
} else {
if (count($_SERVER['argv']) < 2) return fwrite(STDERR, "Usage: {$_SERVER['argv'][0]} <URL>\n");
if (!$this->setUrl($url = $_SERVER['argv'][1])) return fwrite(STDERR, "ERROR: URL $url\n");
if (!$this->generate()) return fwrite(STDERR, 'ERROR');
$this->output($this->debug ? null : 'sitemap.xml');
}
}