-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_xmlsitemap.module
535 lines (457 loc) · 14.7 KB
/
easy_xmlsitemap.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/**
* @file main module file
*/
// Define same content entry types as in module "SEO Meta Tags"
define('SEO_META_TYPE_NODE', 2);
define('SEO_META_TYPE_VIEW', 3);
define('SEO_META_TYPE_TAXONOMY', 4);
/**
* Implements hook_config_info().
*/
function easy_xmlsitemap_config_info() {
$prefixes['easy_xmlsitemap.settings'] = array(
'label' => t('Easy XML Sitemap'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*
*/
function easy_xmlsitemap_permission() {
return array(
'administer easy_xmlsitemap settings' => array(
'title' => t('Administer Easy XML Sitemap settings')
),
);
}
/**
* Implements hook_menu().
*
*/
function easy_xmlsitemap_menu() {
$sub_path = 'search';
$backdrop_version = explode('.', BACKDROP_VERSION);
if ($backdrop_version[0] == 1 && $backdrop_version[1] > 5) {
$sub_path = 'metadata';
}
$items = array();
$items['admin/config/' . $sub_path . '/easy_xmlsitemap'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => 'Easy XML Sitemap',
'page callback' => 'backdrop_get_form',
'page arguments' => array('easy_xmlsitemap_settings_form'),
'file' => 'easy_xmlsitemap.admin.inc',
'access callback' => 'user_access',
'access arguments' => array('administer easy_xmlsitemap settings'),
);
$items['sitemap.xml'] = array(
'page callback' => 'easy_xmlsitemap_output',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Wrapper for core language_list(), just remove excluded languages
*/
function easy_xmlsitemap_language_list() {
$config = config('easy_xmlsitemap.settings');
$excluded_langs = $config->get('excluded_langs');
$site_langs = language_list(TRUE);
if (!empty($excluded_langs)) {
foreach ($excluded_langs as $lang => $excluded) {
if ($excluded !== 0) {
unset($site_langs[$lang]);
}
}
}
return $site_langs;
}
/**
* Main sitemap build function
* @return callback save to file
*/
function easy_xmlsitemap_build() {
set_time_limit(1800);
$config = config('easy_xmlsitemap.settings');
$force_https = $config->get('force_https');
$proto = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $force_https) ? "https://" : "http://";
$host = $_SERVER['HTTP_HOST'];
$config_base_url = rtrim($config->get('base_url'), '/');
$base_url = !empty($config_base_url) ? $config_base_url : $proto . $host;
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
. "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">";
$output .= easy_xmlsitemap_build_front_url($base_url);
$output .= easy_xmlsitemap_build_node_items($base_url);
$output .= easy_xmlsitemap_build_taxonomy_items($base_url);
$output .= easy_xmlsitemap_build_views_items($base_url);
$output .= "\n" . '</urlset>';
return easy_xmlsitemap_file_save($output);
}
/**
* Check if given URL should be excluded from sitemap
* @param string $url (e.g. "/about.html" or "/node/123")
* @return boolean
*/
function easy_xmlsitemap_url_excluded($url) {
$config = config('easy_xmlsitemap.settings');
$excluded_raw_urls = $config->get('excluded_urls');
$excluded_urls = easy_xmlsitemap_textarea_to_array($excluded_raw_urls);
$excluded = FALSE;
$masked = array();
if (!empty($excluded_urls)) {
// Check for url groups masked by *, then full url
foreach ($excluded_urls as $excluded_url) {
if (substr($excluded_url, -1) === '*') {
$masked[] = substr($excluded_url, 0, -1);
}
if (!empty($masked)) {
foreach ($masked as $found) {
$position = strpos($url, $found);
if ($position !== FALSE) {
$excluded = TRUE;
continue 2;
}
else {
$excluded = easy_xml_found($url, $excluded_urls);
}
}
}
else {
$excluded = easy_xml_found($url, $excluded_urls);
}
}
}
return $excluded;
}
/**
* Found in an array
* @param string $url
* @param array $excluded_urls
* @return boolean
*/
function easy_xml_found($url, $excluded_urls) {
$found = FALSE;
if (in_array(substr($url, 1), $excluded_urls)) {
$found = TRUE;
}
return $found;
}
/**
* Return node nid if this node set as front page
* @return string or 0
*/
function easy_xmlsitemap_get_frontpage_nid() {
$frontpage_nid = 0;
$site_frontpage = config_get('system.core', 'site_frontpage');
if (strpos($site_frontpage, 'node/') !== FALSE) {
$frontpage_nid = substr($site_frontpage, 5);
}
return $frontpage_nid;
}
/**
* Find and print front page(s) URL
* @param string $base_url
* @return string
*/
function easy_xmlsitemap_build_front_url($base_url) {
$site_langs = easy_xmlsitemap_language_list();
$output = '';
if (count($site_langs) == 1) {
$output .= easy_xmlsitemap_wrap_url($base_url . '/', key($site_langs));
}
else {
foreach ($site_langs as $lang) {
$lang_suffix = $lang->default ? '/' : '/' . $lang->langcode . '/';
$url = $base_url . $lang_suffix;
$output .= "\n" . ' <url>' . "\n" .
' <loc>' . $url . '</loc>';
foreach ($site_langs as $lang) {
$hlang_suffix = $lang->default ? '/' : '/' . $lang->langcode . '/';
$hurl = $base_url . $hlang_suffix;
$output .= "\n" .
' <xhtml:link rel="alternate" hreflang="'. $lang->langcode . '" href="' . $hurl . '" />';
}
$output .= "\n" . ' </url>';
}
}
return $output;
}
/**
* Find and print nodes URLs
* @global object $language
* @param string $base_url
* @return string
*/
function easy_xmlsitemap_build_node_items($base_url) {
$config = config('easy_xmlsitemap.settings');
$default_langcode = config_get('system.core', 'language_default');
$site_langs = easy_xmlsitemap_language_list();
$urls_block = array();
$output = '';
$excluded_types = $config->get('excluded_types');
$types = array_values($excluded_types);
foreach (array_keys($types, 0, true) as $key) {
unset($types[$key]);
}
// @todo - fix fake array
if (empty($types)) {
$types = array('nonexistent_type' => 'nonexistent_type');
}
$frontpage_nid = easy_xmlsitemap_get_frontpage_nid();
$result = db_select('node', 'n')
->addTag('node_access')
->fields('n')
->condition('n.type', $types, 'NOT IN')
->condition('n.status', 0, '>')
->execute()
->fetchAll();
foreach ($result as $node) {
// skip node if set as frontpage
if (!empty($frontpage_nid) && $node->nid == $frontpage_nid) {
continue;
}
$url = url('node/' . $node->nid, array('absolute' => FALSE, 'language' => (object) array('langcode' => $node->langcode)));
// skip excluded URLs
if (easy_xmlsitemap_seo_meta_check_robots(SEO_META_TYPE_NODE, $node->nid) || easy_xmlsitemap_url_excluded($url)) {
continue;
}
//skip nodes with disabled languages but with "und"
if ($node->langcode != 'und' && !array_key_exists($node->langcode, $site_langs)) {
continue;
}
$urls_block[$node->tnid][] = array(
'nid' => $node->nid,
'tnid' => $node->tnid,
'langcode' => $node->langcode,
);
}
foreach ($urls_block as $tnid => $urls) {
if ($tnid == '0') {
foreach ($urls as $node) {
$url = $base_url . url('node/' . $node['nid'], array('absolute' => FALSE, 'language' => (object) array('langcode' => $node['langcode'])));
$langcode = $node['langcode'] != 'und' ? $node['langcode'] : $default_langcode;
$output .= easy_xmlsitemap_wrap_url($url, $langcode);
}
}
else {
foreach ($urls as $node) {
$url = url('node/' . $node['nid'], array('absolute' => FALSE, 'language' => (object) array('langcode' => $node['langcode'])));
$output .= "\n" . ' <url>' . "\n" .
' <loc>' . $base_url . $url . '</loc>';
foreach ($urls as $tnid) {
$hurl = url('node/' . $tnid['nid'], array('absolute' => FALSE, 'language' => (object) array('langcode' => $tnid['langcode'])));
$output .= "\n"
. ' <xhtml:link rel="alternate" hreflang="'. $tnid['langcode'] . '" href="' . $base_url . $hurl . '" />';
}
$output .= "\n" . ' </url>';
}
}
}
return $output;
}
/**
* Find and print non-empty taxonomy term pages
* @global object $language
* @param string $base_url
* @return string
*/
function easy_xmlsitemap_build_taxonomy_items($base_url) {
// Default language only for such URLs
$langcode = config_get('system.core', 'language_default');
$output = '';
if (module_exists('taxonomy')) {
$result = db_select('taxonomy_term_data', 'ttd')
->fields('ttd', array('tid'))
->execute();
foreach ($result as $term) {
// add term only if have nodes assigned to
$array_of_nodes = taxonomy_select_nodes($term->tid);
if (!empty($array_of_nodes)) {
$url = url('taxonomy/term/' . $term->tid, array('absolute' => FALSE, 'language' => (object) array('langcode' => $langcode)));
// skip excluded URLs
if (easy_xmlsitemap_seo_meta_check_robots(SEO_META_TYPE_TAXONOMY, $term->tid) || easy_xmlsitemap_url_excluded($url)) {
continue;
}
$output .= easy_xmlsitemap_wrap_url($base_url . $url, $langcode);
}
}
return $output;
}
}
/**
* Find and print public available views pages URLs
* @global object $language
* @param string $base_url
* @return string
*/
function easy_xmlsitemap_build_views_items($base_url) {
// Default language only for such URLs
$langcode = config_get('system.core', 'language_default');
$output = '';
$views = views_get_enabled_views();
foreach ($views as $view) {
if (empty($view->display)) {
// Skip this view as it is broken.
debug(t("Skipping broken view @view", array('@view' => $view->name)));
continue;
}
foreach (array_keys($view->display) as $id) {
$plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin);
if ($plugin['name'] == 'page') {
$view_name = $view->name;
$page_path = $view->display[$id]->display_options['path'];
// Skip admin views
if (strpos($page_path, 'admin/') === FALSE) {
$url = url('/' . $page_path, array('absolute' => FALSE, 'language' => (object) array('langcode' => $langcode)));
// skip excluded URLs
if (easy_xmlsitemap_seo_meta_check_robots(SEO_META_TYPE_VIEW, str_replace('-', '_', $view_name)) || easy_xmlsitemap_url_excluded($url)) {
continue;
}
$output .= easy_xmlsitemap_wrap_url($base_url . $url, $langcode);
}
}
}
}
return $output;
}
/**
* Wrap to tags a single URL
* @param string $full_url
* @param string $langcode
* @return string
*/
function easy_xmlsitemap_wrap_url($full_url, $langcode) {
$output = "\n" . ' <url>' . "\n" .
' <loc>' . $full_url . '</loc>';
$output .= "\n"
. ' <xhtml:link rel="alternate" hreflang="'. $langcode . '" href="' . $full_url . '" />';
$output .= "\n" . ' </url>';
return $output;
}
/**
* Save string to file
* @param string $content
* @return boolean
*/
function easy_xmlsitemap_file_save($content) {
if (empty($content)) {
return FALSE;
}
$config = config('easy_xmlsitemap.settings');
$file_dir = 'public://' . $config->get('sitemap_dir');
$file_path = $file_dir . '/' . 'sitemap.xml';
if (!file_exists($file_dir)) {
backdrop_mkdir($file_dir);
}
// make backup for previous version of file
if (file_exists($file_path)) {
rename($file_path, $file_path . '.bak');
}
$content = $content . "\n" . '<!-- ' . format_date(time(), 'short') . ' -->';
file_put_contents($file_path, $content);
$success = file_exists($file_path);
if ($success) {
state_set('easy_xmlsitemap_last_build_date', time());
watchdog('easy_xmlsitemap', 'Sitemap file saved.');
backdrop_set_message(t('File "sitemap.xml" has been saved successfully.'));
}
return $success;
}
/**
* Output sitemap from file
*
*/
function easy_xmlsitemap_output() {
$config = config('easy_xmlsitemap.settings');
$file_dir = 'public://' . $config->get('sitemap_dir');
$file_path = $file_dir . '/' . 'sitemap.xml';
if (!file_exists($file_path) || !is_readable($file_path)) {
backdrop_not_found();
backdrop_exit();
}
else {
$file = file_get_contents($file_path);
backdrop_add_http_header('Content-type', 'text/xml; charset=utf-8');
backdrop_add_http_header('X-Robots-Tag', 'noindex, follow');
echo $file;
exit();
}
}
/**
* Utility function
* @param string $raw_string
* @return array of strings from textarea
*/
function easy_xmlsitemap_textarea_to_array($raw_string = '') {
$array_of_strings = array();
if (!empty($raw_string)) {
$raw_array = preg_split("/\\r\\n|\\r|\\n/", $raw_string);
$array_of_strings = array_map('trim', $raw_array);
}
return $array_of_strings;
}
/**
* Implementation of hook_cron().
*/
function easy_xmlsitemap_cron() {
$config = config('easy_xmlsitemap.settings');
$last_build_date = state_get('easy_xmlsitemap_last_build_date', 0);
$rebuild_frequency = $config->get('rebuild_frequency');
if ($rebuild_frequency == 'manually') {
return;
}
if ($rebuild_frequency == 'daily') {
$today = date("Y-m-d");
$day = date("Y-m-d", $last_build_date);
if ($day != $today) {
easy_xmlsitemap_build();
}
}
else {
easy_xmlsitemap_build();
}
}
/**
* Check given page for meta-tag "robots"
* @param string $ce_type
* @param string $item_id
* @return boolean TRUE if found value "noindex"
*/
function easy_xmlsitemap_seo_meta_check_robots($ce_type, $item_id) {
if (!module_exists('seo_meta')) {
return FALSE;
}
else {
$seo_meta_integration = config_get('easy_xmlsitemap.settings', 'seo_meta_integration');
if ($seo_meta_integration == 1) {
$found = FALSE;
if ($ce_type == SEO_META_TYPE_TAXONOMY) {
$term = taxonomy_term_load($item_id);
$ce_id = $term->name;
}
else {
$ce_id = $item_id;
}
$result = db_select('seo_meta', 'sm')
->fields('sm', array(
'meta_robots',
)
)
->condition('ce_type', $ce_type, '=')
->condition('ce_id', $ce_id, '=')
->execute()
->fetchAssoc();
if ($result) {
$meta_robots = unserialize($result['meta_robots']);
if ($meta_robots['NOINDEX'] !== 0) {
$found = TRUE;
}
}
return $found;
}
}
}