diff --git a/xmlsitemap.admin.inc b/xmlsitemap.admin.inc index 2b34c02..191ec4b 100644 --- a/xmlsitemap.admin.inc +++ b/xmlsitemap.admin.inc @@ -280,7 +280,12 @@ function xmlsitemap_settings_form($form, &$form_state) { '#description' => t('When enabled, this will fetch all URL aliases at once instead of one at a time during sitemap generation. For medium or large sites, it is recommended to disable this feature as it uses a lot of memory.'), '#default_value' => variable_get('xmlsitemap_prefetch_aliases', 1), ); - + $form['xmlsitemap_multilingual'] = array( + '#type' => 'checkbox', + '#title' => t('Multilingual sitemap'), + '#description' => t('When enabled, this add multilingual sitemap like !link.', array('!link' => l(t('here'), 'https://support.google.com/webmasters/answer/2620865?hl=en'))), + '#default_value' => variable_get('xmlsitemap_multilingual', 0), + ); $form['advanced'] = array( '#type' => 'fieldset', '#title' => t('Advanced settings'), @@ -288,6 +293,7 @@ function xmlsitemap_settings_form($form, &$form_state) { '#collapsed' => !variable_get('xmlsitemap_developer_mode', 0), '#weight' => 10, ); + //$form['advanced']['xmlsitemap_gz'] = array( // '#type' => 'checkbox', // '#title' => t('Generate additional compressed sitemaps using gzip.'), diff --git a/xmlsitemap.generate.inc b/xmlsitemap.generate.inc index 747347a..0ebdf10 100644 --- a/xmlsitemap.generate.inc +++ b/xmlsitemap.generate.inc @@ -150,7 +150,7 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $link_count = 0; $query = db_select('xmlsitemap', 'x'); - $query->fields('x', array('loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status')); + $query->fields('x', array('loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status', 'type', 'id')); $query->condition('x.access', 1); $query->condition('x.status', 1); $query->orderBy('x.language', 'DESC'); @@ -166,6 +166,7 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, while ($link = $links->fetchAssoc()) { $link['language'] = $link['language'] != LANGUAGE_NONE ? xmlsitemap_language_load($link['language']) : $url_options['language']; if ($url_options['alias']) { + $link['original'] = $link['loc']; $link['loc'] = xmlsitemap_get_path_alias($link['loc'], $link['language']->language); } $link_options = array( @@ -190,6 +191,11 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer, $element = array(); $element['loc'] = $link_url; + + if (variable_get('xmlsitemap_multilingual', 0)) { + $element['xhtml:link']['attributes'] = xmlsitemap_generate_multilingual($link); + } + if ($link['lastmod']) { $element['lastmod'] = gmdate($lastmod_format, $link['lastmod']); // If the link has a lastmod value, update the changefreq so that links @@ -499,3 +505,47 @@ function xmlsitemap_get_rebuildable_link_types() { return $rebuild_types; } + +/** + * Generate multilingual attributes. + * See https://support.google.com/webmasters/answer/2620865?hl=en. + * + * @param array $link + * @return array + */ +function xmlsitemap_generate_multilingual($link) { + if (module_exists('entity_translation')) { + $translation = db_select('entity_translation', 'et') + ->fields('et', array('language')) + ->condition('et.entity_type', $link['type']) + ->condition('et.entity_id', $link['id']) + ->execute()->fetchCol(); + } + else { + $tnid = db_select('node', 'n') + ->fields('n', array('tnid')) + ->condition('n.nid', $link['id']) + ->execute()->fetchField(); + + $translation = db_select('node', 'n') + ->fields('n', array('language')) + ->condition('n.tnid', $tnid) + ->execute()->fetchCol(); + } + + if ($translation) { + foreach (language_list() as $lang) { + if (!$lang->enabled || !in_array($lang->language, $translation)){ + continue; + } + + $xhtml_links[] = array( + 'rel' => 'alternative', + 'href' => url($link['original'], array('language' => $lang, 'absolute' => TRUE)), + 'lang' => $lang->language + ); + } + } + + return isset($xhtml_links) ? $xhtml_links : array(); +} diff --git a/xmlsitemap.install b/xmlsitemap.install index 169bd96..4340e44 100644 --- a/xmlsitemap.install +++ b/xmlsitemap.install @@ -544,6 +544,15 @@ function xmlsitemap_update_7203() { _xmlsitemap_sitemap_rehash_all(); } +/** + * Implement hook_update_N(). + * Change primary keys to id, type, language of xmlsitemap table. + */ +function xmlsitemap_update_7204() { + db_drop_primary_key('xmlsitemap'); + db_add_primary_key('xmlsitemap', array('id', 'type', 'language')); +} + function _xmlsitemap_sitemap_rehash_all() { // Reload the schema cache and reprocess all sitemap hashes into smids. drupal_load('module', 'xmlsitemap'); diff --git a/xmlsitemap.xmlsitemap.inc b/xmlsitemap.xmlsitemap.inc index 94fdef1..95767e6 100644 --- a/xmlsitemap.xmlsitemap.inc +++ b/xmlsitemap.xmlsitemap.inc @@ -69,6 +69,7 @@ class XMLSitemapWriter extends XMLWriter { */ public function getRootAttributes() { $attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9'; + $attributes['xmlns:xhtml'] = 'http://www.w3.org/1999/xhtml'; if (variable_get('xmlsitemap_developer_mode', 0)) { $attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'; $attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'; @@ -116,20 +117,38 @@ class XMLSitemapWriter extends XMLWriter { * * @param $name * The element name. - * @param $content + * @param $data * The element contents or an array of the elements' sub-elements. */ - public function writeElement($name, $content = '') { - if (is_array($content)) { + public function writeElement($name, $data = array()) { + if (is_string($data)) { + $data = array( + 'content' => $data, + ); + } + + + if (isset($data['attributes']) || isset($data['content'])) { + if (!empty($data['attributes'])) { + foreach ($data['attributes'] as $attributes) { + $this->startElement($name); + foreach ($attributes as $attr_title => $attr_content) { + parent::writeAttribute($attr_title, $attr_content); + } + $this->endElement(); + } + } + if (!empty($data['content'])) { + parent::writeElement($name, $data['content']); + } + } + else { $this->startElement($name); - foreach ($content as $sub_name => $sub_content) { + foreach ($data as $sub_name => $sub_content) { $this->writeElement($sub_name, $sub_content); } $this->endElement(); } - else { - parent::writeElement($name, $content); - } } public function getURI() {