forked from npatel2012/myfaq-php-isa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsitemap.xml.php
164 lines (149 loc) · 5.56 KB
/
sitemap.xml.php
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
<?php
/**
* The dynamic Google Sitemap builder.
*
* http://[...]/sitemap.xml.php
* http://[...]/sitemap.xml.php?gz=1
* http://[...]/sitemap.xml
* http://[...]/sitemap.gz
* http://[...]/sitemap.xml.gz
*
* The Google Sitemap protocol is described here:
* http://www.google.com/webmasters/sitemaps/docs/en/protocol.html
*
* PHP Version 5.3
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* @category phpMyFAQ
* @package SEO
* @author Matteo Scaramuccia <matteo@phpmyfaq.de>
* @copyright 2006-2015 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link http://www.phpmyfaq.de
* @since 2006-06-26
*/
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_ALWAYS', 'always');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_HOURLY', 'hourly');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_DAILY', 'daily');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_WEEKLY', 'weekly');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_MONTHLY', 'monthly');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_YEARLY', 'yearly');
define('PMF_SITEMAP_GOOGLE_CHANGEFREQ_NEVER', 'never');
define('PMF_SITEMAP_GOOGLE_MAX_URL_LENGTH', 2048);
define('PMF_SITEMAP_GOOGLE_MAX_URLS', 50000);
define('PMF_SITEMAP_GOOGLE_MAX_FILE_LENGTH', 10485760); // 10MB
define('PMF_SITEMAP_GOOGLE_PRIORITY_MIN', '0.0');
define('PMF_SITEMAP_GOOGLE_PRIORITY_MAX', '1.0');
define('PMF_SITEMAP_GOOGLE_PRIORITY_DEFAULT', '0.5');
define('PMF_SITEMAP_GOOGLE_GET_GZIP', 'gz');
define('PMF_SITEMAP_GOOGLE_GET_INDEX', 'idx');
define('PMF_SITEMAP_GOOGLE_FILENAME', 'sitemap.xml');
define('PMF_SITEMAP_GOOGLE_FILENAME_GZ', 'sitemap.xml.gz');
define('PMF_SITEMAP_GOOGLE_INDEX_FILENAME', 'sitemap_index.xml');
define('PMF_ROOT_DIR', __DIR__);
define('IS_VALID_PHPMYFAQ', null);
//
// Bootstrapping
//
require 'inc/Bootstrap.php';
//
// Initalizing static string wrapper
//
PMF_String::init('en');
// {{{ Functions
function buildSitemapNode($location, $lastmod = null, $changeFreq = null, $priority = null)
{
if (!isset($lastmod)) {
$lastmod = PMF_Date::createIsoDate($_SERVER['REQUEST_TIME'], DATE_W3C, false);
}
if (!isset($changeFreq)) {
$changeFreq = PMF_SITEMAP_GOOGLE_CHANGEFREQ_DAILY;
}
$node =
'<url>'
.'<loc>'.PMF_String::htmlspecialchars($location).'</loc>'
.'<lastmod>'.$lastmod.'</lastmod>'
.'<changefreq>'.$changeFreq.'</changefreq>'
.(isset($priority) ? '<priority>'.$priority.'</priority>' : '')
.'</url>';
return $node;
}
//
// Future improvements
// WHEN a User PMF Sitemap will be:
// a. bigger than 10MB (!)
// b. w/ more than 50K URLs (!)
// we'll manage this issue using a Sitemap Index Files produced by this PHP code
// including Sitemap URLs always produced by this same PHP code (see PMF_SITEMAP_GOOGLE_GET_INDEX)
//
$oFaq = new PMF_Faq($faqConfig);
// Load the faq
$items = $oFaq->getTopTenData(PMF_SITEMAP_GOOGLE_MAX_URLS - 1);
$visitsMax = 0;
$visitMin = 0;
if (count($items) > 0) {
$visitsMax = $items[0]['visits'];
$visitMin = $items[count($items)-1]['visits'];
}
// Sitemap header
$sitemap =
'<?xml version="1.0" encoding="UTF-8"?>'
.'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"'
.' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
.' xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84'
.' http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
// 1st entry: the faq server itself
$sitemap .= buildSitemapNode(
$faqConfig->get('main.referenceURL'),
PMF_Date::createIsoDate($_SERVER['REQUEST_TIME'], DATE_W3C, false),
PMF_SITEMAP_GOOGLE_CHANGEFREQ_DAILY,
PMF_SITEMAP_GOOGLE_PRIORITY_MAX
);
// nth entry: each faq
foreach ($items as $item) {
$priority = PMF_SITEMAP_GOOGLE_PRIORITY_DEFAULT;
if (($visitsMax - $visitMin) > 0) {
$priority = sprintf('%.1f', PMF_SITEMAP_GOOGLE_PRIORITY_DEFAULT * (1 + (($item['visits'] - $visitMin)/($visitsMax - $visitMin))));
}
// a. We use plain PMF urls w/o any SEO schema
$link = str_replace($_SERVER['SCRIPT_NAME'], '/index.php', $item['url']);
// b. We use SEO PMF urls
if (PMF_SITEMAP_GOOGLE_USE_SEO) {
if (isset($item['thema'])) {
$oL = new PMF_Link($link, $faqConfig);
$oL->itemTitle = $item['thema'];
$link = $oL->toString();
}
}
$sitemap .= buildSitemapNode(
$faqConfig->get('main.referenceURL').$link,
PMF_Date::createIsoDate($item['date'], DATE_W3C),
// @todo: manage changefreq node with the info provided by faqchanges,
// if this will not add a big load to the server (+1 query/faq)
PMF_SITEMAP_GOOGLE_CHANGEFREQ_DAILY,
$priority
);
}
$sitemap .= '</urlset>';
$getgezip = PMF_Filter::filterInput(INPUT_GET, PMF_SITEMAP_GOOGLE_GET_GZIP, FILTER_VALIDATE_INT);
if (!is_null($getgezip) && (1 == $getgezip)) {
if (function_exists('gzencode')) {
$sitemapGz = gzencode($sitemap);
header('Content-Type: application/x-gzip');
header('Content-Disposition: attachment; filename="'.PMF_SITEMAP_GOOGLE_FILENAME_GZ.'"');
header('Content-Length: '.strlen($sitemapGz));
print $sitemapGz;
} else {
$http = new PMF_Helper_Http();
$http->sendStatus(404);
}
} else {
header('Content-Type: text/xml');
header('Content-Disposition: inline; filename="'.PMF_SITEMAP_GOOGLE_FILENAME.'"');
header('Content-Length: '.PMF_String::strlen($sitemap));
print $sitemap;
}
$faqConfig->getDb()->close();