Подскажите как настроить Sitemap под мультиязычность
-
Есть снипет который уже не обслуживается но очень хорошо работает… точнее работает на сайтах без мультиязичности. Проблема в том что он выводит все ссылки без учета Site_url (Base_url) определенного контекста.
Например главная под RU выводит
Ru: site. net/
EN: site. net/.а должно быть… site. net/en
отсутствует подраздел контекста....
подскажите что исправить.
Еще в контекстах есть настройка Site_start где указан ID главной каждого контекста. Как сделать проверку чтоб к главним в контекстах не добавлялся Алиас<?php /** * snippet shk_sitemap * * @author slaad * */ /* http://modx-shopkeeper.ru/forum/viewtopic.php?pid=20545#p20545 Examples 1. Only resources (one or more contexts): [[shk_sitemap? &packageNames=`modResource` &classNames=`modResource` ]] 2. Resources and shop package (Shopkeeper), two contexts: [[shk_sitemap? &packageNames=`modResource,shop` &classNames=`modResource,ShopContent` &contexts=`web,catalog` ]] 3. Excluding resources excluding resources with ids 17,23,31,16: [[shk_sitemap? &packageNames=`modResource,shop` &classNames=`modResource,ShopContent` &contexts=`web,catalog` &excludeModResIds=`17,23,31,16` ]] and excluding resources with ids 17,23,31,16 and 1 level childs of resources with ids 17,11,23,14 [[shk_sitemap? &packageNames=`modResource,shop` &classNames=`modResource,ShopContent` &contexts=`web,catalog` &excludeModResIds=`17,23,31,16` &excludeModContIds=`17,11,23,14` ]] */ $config = array( 'packageNames' => 'modResource,shop', 'classNames' => 'modResource,ShopContent', 'contexts' => 'web,catalog', 'site_url'=> $modx->getOption('site_url'), // $modx->context->get('key') 'site_start'=> $modx->getOption('site_start', null, 1), 'excludeModResIds'=>'17,23,31,16', 'excludeModContIds'=>'17,11,23,14' ); $contentType = $modx->getObject('modContentType',array('name'=>'HTML')); $config['urlSuffix']= $contentType->getExtension(); $config['containerSuffix']=$modx->getOption('container_suffix'); $config['cultureKey']=$modx->context->get('contextKey'); $config = array_merge( $config, $scriptProperties ); $packageNames = explode(',',$config['packageNames']); $classNames = explode(',',$config['classNames']); $contexts = explode(',',$config['contexts']); $output = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL; if(!function_exists('getMapQuery')){ function getMapQuery($className,$select,$where){ global $modx; $query = $modx->newQuery($className); $query->select($select); $query->where($where); $query->sortby('id','ASC'); if ( $query->prepare() && $query->stmt->execute() ){ $query_out= array(); //$modx->log(modX::LOG_LEVEL_ERROR, $query->toSql()); $query_out=$query->stmt->fetchAll(PDO::FETCH_ASSOC); foreach($query_out as $r){ if (!isset($resources[$r['id']])) $resources[$r['id']]=array(); foreach ($select as $s){ $resources[$r['id']][$s] = $r[$s]; } } } return $resources; } } if (!function_exists('datediff')) { /** * @param string $interval Can be: yyyy - Number of full years q - Number of full quarters m - Number of full months y - Difference between day numbers (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".) d - Number of full days w - Number of full weekdays ww - Number of full weeks h - Number of full hours n - Number of full minutes s - Number of full seconds (default) * @param $datefrom * @param $dateto * @param bool $using_timestamps * @return float|int|string * @package googlesitemap */ function datediff($interval, $datefrom, $dateto, $using_timestamps = false) { if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); $dateto = strtotime($dateto, 0); } $difference = $dateto - $datefrom; /* Difference in seconds */ switch($interval) { case 'yyyy': /* Number of full years */ $years_difference = floor($difference / 31536000); if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) { $years_difference--; } if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) { $years_difference++; } $datediff = $years_difference; break; case 'q': /* Number of full quarters */ $quarters_difference = floor($difference / 8035200); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $quarters_difference++; } $quarters_difference--; $datediff = $quarters_difference; break; case 'm': /* Number of full months */ $months_difference = floor($difference / 2678400); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $months_difference--; $datediff = $months_difference; break; case 'y': /* Difference between day numbers */ $datediff = date('z',$dateto) - date('z',$datefrom); break; case 'd': /* Number of full days */ $datediff = floor($difference / 86400); break; case 'w': /* Number of full weekdays */ $days_difference = floor($difference / 86400); $weeks_difference = floor($days_difference / 7); /* Complete weeks */ $first_day = date('w', $datefrom); $days_remainder = floor($days_difference % 7); /* Do we have a Saturday or Sunday in the remainder? */ $odd_days = $first_day + $days_remainder; if ($odd_days > 7) { /* Sunday */ $days_remainder--; } if ($odd_days > 6) { /* Saturday */ $days_remainder--; } $datediff = ($weeks_difference * 5) + $days_remainder; break; case 'ww': /* Number of full weeks */ $datediff = floor($difference / 604800); break; case 'h': /* Number of full hours */ $datediff = floor($difference / 3600); break; case 'n': /* Number of full minutes */ $datediff = floor($difference / 60); break; default: /* Number of full seconds (default) */ $datediff = $difference; break; } return $datediff; } } foreach ( $packageNames as $key => $packageName ){ $parentName = $packageName == 'modResource' ? "parent" : "resource_id"; $select = array('id','alias','editedon','createdon',$parentName); if( $packageName != 'modResource' ){ $modelpath = $modx->getOption('core_path') . 'components/' . $packageName . '/model/'; $modx->addPackage($packageName, $modelpath); } else{ $select = array_merge( $select, array('context_key','isfolder') ); } $where=array( 'published' => 1 ); if($config['excludeModResIds'] && $packageName == 'modResource'){ $ids=array('id:NOT IN' => explode(',',$config['excludeModResIds'] )); array_push($where,$ids); } if($config['excludeModContIds']&& $packageName == 'modResource'){ $ids=array('parent:NOT IN' => explode(',',$config['excludeModContIds'] )); array_push($where,$ids); } $resources = getMapQuery($classNames[$key],$select,$where); if(!empty($resources)){ foreach ( $resources as $resource ){ if(!empty($resource['alias'])){ if (!isset($resource['context_key'])) { $resource['context_key'] = !empty( $contexts[$key] ) ? $contexts[$key] : $contexts[0]; } if ( $resource[$parentName] != 0 ){ $url = $modx->makeUrl($resource[$parentName],$resource['context_key'],'','full'); } else{ $url = $config['site_url']; } $url .=substr($url, -1)=='/' ? $resource['alias'] : '/'.$resource['alias']; $url .= !empty( $resource['isfolder'] ) ? $config['containerSuffix'] : $config['urlSuffix']; if ($packageName == 'modResource'&& $resource['id']==$config['site_start']){ $url=$config['site_url']; } $date = !empty( $resource['editedon'] ) ? $resource['editedon'] : $resource['createdon']; $date = strftime( '%Y-%m-%d', $date ); $date = date("Y-m-d", strtotime($date)); /* Get the date difference */ $datediff = datediff("d", $date, date("Y-m-d")); if ($datediff <=30) { $priority = '1.0'; $update = 'weekly'; } elseif (($datediff >30) && ($datediff<=50)) { $priority = '0.85'; $update = 'weekly'; } elseif (($datediff >50) && ($datediff<=100)) { $priority = '0.65'; $update = 'weekly'; } else { $priority = '0.25'; $update = 'monthly'; } $output .= " <url> <loc>{$url}</loc> <lastmod>{$date}</lastmod> </url>"; /* <priority>{$priority}</priority> <changefreq>{$update}</changefreq>*/ } } } } unset($key); $output .= PHP_EOL . "</urlset>"; return $output;
-
Пользователь @admin написал в Подскажите как настроить Sitemap под мультиязычность:
if (!empty($resource['context_key']) && $resource['context_key'] != 'ru') {
$url = str_replace('site.net/', 'site.net/' . $resource['context_key'] . '/', $url);
}такая ситуация, каталоги и подкаталоги гдето теряются - их вообще перестал видеть снипет. Выводит только 1ю линию глубины от Base_url.
Подскажи пожалуйста. Какой кусок за это отвечает - чтоб подкаталоги выводились?
Ваш вариант тоже отработал немного не коректно...$resources = getMapQuery($classNames[$key],$select,$where); if(!empty($resources)){ foreach ( $resources as $resource ){ if(!empty($resource['alias'])){ if (!isset($resource['context_key'])) { $resource['context_key'] = !empty( $contexts[$key] ) ? $contexts[$key] : $contexts[0]; } // site_start Context $ctx = $modx->getContext($resource['context_key']); $settings = $ctx->config; $siteStart = $settings["site_start"]; // if ( $resource[$parentName] != 0 ){ // $url = $modx->makeUrl($resource[$parentName],$resource['context_key'],'','full'); // } if ( $resource['context_key'] != 'web' ){ $url = $config['site_url'].$resource['context_key']; // // $url = $modx->makeUrl($config['site_url'],$resource['context_key'],'','full'); // // $url = $modx->makeUrl(3, $resource['context_key'],'','full'); } else{ $url = $config['site_url']; } if ( $siteStart != $resource['id'] ){ $url .=substr($url, -1)=='/' ? $resource['alias'] : '/'.$resource['alias'].$siteStart.$resource[$parentName]; // dobavl alias vkontse $url .= !empty( $resource['isfolder'] ) ? $config['containerSuffix'] : $config['urlSuffix']; // okonchanie / abo .html } if ($packageName == 'modResource'&& $resource['id']==$config['site_start']){ $url=$config['site_url']; }
-
кажись разобрался
поменялif ( $resource[$parentName] != 0 ){ $url = $modx->makeUrl($resource[$parentName],$resource['context_key'],'','full'); }
на
if ( $resource['context_key'] != 'web' ){ $url = $config['site_url'].$resource['context_key']; }
Только база выводится к en и it не правильно с учетом Алиаса, а надо базу без.......
получается https:// site .com/en/my-en а надо https:// site .com/en/
@Admin помоги плизКак прописать в снипете и вытянуть настройку "site_start" с настроек текущего контекста и вытянуть текущий id ресурса и сравнить их ???
-
Если основной язык "ru", то как-то так:
if (!empty($resource['context_key']) && $resource['context_key'] != 'ru') { $url = str_replace('site.net/', 'site.net/' . $resource['context_key'] . '/', $url); }
-
@admin спасибо большое Вам.
А если языков штук 5, то как это проставить?if (isset($resource['context_key']) && $resource['context_key'] == 'en') { $url = str_replace('site.net/', 'site.net/en/', $url); }
У меня к каждому контекту вот такие настройки идут
-
EN: site. net/.а должно быть… site. net/en
Сделайте просто через замену подстроки:
if (isset($resource['context_key']) && $resource['context_key'] == 'en') { $url = str_replace('site.net/', 'site.net/en/', $url); }
-
@admin я вас прошу)) рабочий вариант который генерит sitemap - меня больше и не интересует. Этот же вариант стоит у меня на 3х сайтах…. Просто тут нужно стало на нескольких язиках- а он подпапку не вставляет.
И это не для последней версии Модх 3, у меня 2.7 - тройка она ещё сырая - поэтому не нужно новый, просто изменения
Поможите? -
@alex18 Хотите использовать страшное и неудобное старьё - это без меня
-
@admin помогите пожалуйста, это же по сути и ваше детище))) люди до сих пор пользуются вашими снипеттами- и хвалят их.
Плюс вы лучше знаете что именно надо менять в нем чтоб работало.
Или покажите в каком кусочке что надо изменить
Спасибо -
@alex18 Я думаю вам нужно спросить на каком-то форуме, посвященном MODX.