Skip to content

Commit

Permalink
Обновил
Browse files Browse the repository at this point in the history
  • Loading branch information
myindexlike committed Mar 9, 2016
1 parent a5809a6 commit 826811b
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# sitemapPro
Big sitemap for MODX EVO


Создать папку sitemap в корне сайта и дать ей права 775
161 changes: 161 additions & 0 deletions sitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

/**
* Created by PhpStorm.
* User: myindexlike
* Date: 09.03.2016
* Time: 3:37
*/
class sitemap
{
const COUNTINFILE = 5000;

private $folder;
private $parent;

function __construct($parent,$folder='xml') {
$this->parent=$parent;
$this->path_folder=MODX_BASE_PATH.$folder.'/';
$this->url_folder=MODX_SITE_URL.$folder.'/';

}


/**
* Создаем файл xml по родителю
*
* @return bool|string
*/
public function creatingXML (){
global $modx;
$list = array();
$mes ='';
// получаем количество товаров
$count = $this->CountChildren ($this->parent);

//echo '$count='.$count;

if ($count!=FALSE){
$iteration = ceil($count/self::COUNTINFILE); // получаем количество итераций - файлов
for ($x=0; $x<$iteration; $x++) {
$list=$this->getURLlist($this->parent, $x*self::COUNTINFILE, self::COUNTINFILE);
if (count($list)>0){
$li='';
foreach ($list as $key => $value) {
$li .= implode('', $value);
}
// var_dump($list);
$str ='<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.$li.'</urlset>';

echo $str;
$mes = $this->saveFile ($str,$this->parent.'-'.$x);
}
}
}
// return $mes;
}


/**
* подсчитываем количество дочерних документов (прямых потомков)
* @param $parents
*
* @return bool
*/
private function CountChildren ($parents){
global $modx;
$result=$modx->db->query("SELECT count(*) as cc
FROM " . $modx->getFullTableName('site_content') . "
WHERE `parent` in (".$parents.") and `deleted`=0 and `published`=1");
while ($row=$modx->db->getRow($result)) {
return $row['cc'];
}
return FALSE;
}

/**
* Формируем список ссылок
*
* @param $parents
* @param int $start
* @param int $kolvo
*
* @return array
*/
function getURLlist ($parents, $start=0, $kolvo=self::COUNTINFILE, $k=0){
global $modx;
$k ++;
$list = array();
$result=$modx->db->query("SELECT `id`, `isfolder`, `parent`, `template`,
FROM_UNIXTIME(`editedon`, '%Y-%m-%d') as lastmod
FROM " . $modx->getFullTableName('site_content') . "
WHERE `parent` in (".$parents.") and `deleted`=0 and `published`=1 limit ".$start.", ".$kolvo." ");
while ($row=$modx->db->getRow($result)) {
$url = $modx->makeUrl($row['id'], '', '', 'full');
$changefreq = $this->getChangefreq($row['template']);
$priority = $this->getPriority($row['template'],$k);

$list[$row['parent']][] ='<url><loc>'.$url.'</loc><lastmod>'.$row['lastmod'].'</lastmod>'.$changefreq.''.$priority.'</url>';

if ($row['isfolder']==1){

$list_dop = $this->getURLlist($row['id'], 0, self::COUNTINFILE, $k);
if (is_array($list_dop)){
$list=array_merge($list, $list_dop);
}
}

}
return $list;
}


function getChangefreq($template){
return '<changefreq>weekly</changefreq>';

}

function getPriority($template, $k){
$p = 1;
switch ($k) {
case 0:
$p='1';
break;
case 1:
$p='0.9';
break;
case 2:
$p='0.8';
break;
case 3:
$p='0.7';
break;
case 4:
$p='0.6';
break;
}
return '<priority>'.$p.'</priority>';

}

/**
* @param $list
* @param $parentid
*
* @return string
*/
function saveFile ($list,$parentid){

$fp = fopen($this->path_folder.$parentid.'.xml', 'w');

// var_dump($this->path_folder.$parentid.'.xml');

$test = fwrite($fp, $list);
if ($test) $mes= 'Данные в файл успешно занесены.';
else $mes= 'Ошибка при записи в файл.';
fclose($fp); //Закрытие файла
return $mes;
}

}
83 changes: 83 additions & 0 deletions sitemapPro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Created by PhpStorm.
* User: myindexlike
* Date: 09.03.2016
* Time: 3:29
*/
if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');}

require_once "sitemap.php";

$map = new sitemap(5,'sitemap');

echo $map->creatingXML();


/**
* формирование итогового файла cо списокм остальных
*
* @param $file_ar
*
* @return bool|string
*/
/*
function creationGeneralizingXML ($file_ar){
global $config;
if (count($file_ar)>0){
$list='';
foreach ($file_ar as $key => $value) {
if ($value!='index.html' and $value!='all.xml'){
$list.='<sitemap><loc>' . $config['files_xml_dir_url'] . $value . '</loc></sitemap>';
}
}
$str ='<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.$list.'</sitemapindex>';
return saveFile ($str,'all');
}
else {
return FALSE;
}
}
*/

/**
* получаем список файлов из папки импорта
* @return string
*/
/*
function get_file_from_folder () {
global $config;
$st=array();
$out='';
if ($handle = opendir($config['files_xml_dir'])) {
$file_ar = array();
$i=0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.html") {
// echo 'opendir='.$config['files_import_dir'].'<br />';
// echo "Файл $file в последний раз был изменен: " . date("F d Y H:i:s.", filectime($config['files_import_dir'].$file)).'<br />';
$date = date("F d Y H:i:s.", filectime($config['files_xml_dir'].$file));
$i++;
$date_U = date("U", filectime($config['files_xml_dir'].$file))+$i;
$st[$date_U]= '<tr><td><a target="_blank" href="' .$config['files_xml_dir_url'].$file.'">' .$config['files_xml_dir_url'].$file.'</a></td><td>'.$date.'</td></tr>';
$file_ar[]=$file;
}
}
if (count($st)>0){
creationGeneralizingXML($file_ar);
krsort($st);
$out='<table class="table table-striped">' . implode("", $st) . '</table>';
closedir($handle);
}
}
return $out;
}
*/





0 comments on commit 826811b

Please sign in to comment.