-
Notifications
You must be signed in to change notification settings - Fork 6
/
AutoSitemap_body.php
305 lines (257 loc) · 12.7 KB
/
AutoSitemap_body.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
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
<?php
# Special:AutoSitemap MediaWiki extension
# Version 1.4
#
# Copyright 2006 François Boutines-Vignard, 2008-2012 Jehy, 2016-2017 Dolfinus.
#
# A special page to generate Google Sitemap XML files.
# see http://www.google.com/schemas/sitemap/0.84/sitemap.xsd for details.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
#
# Revisions:
#GoogleSitemap
# 0.0.2: date format correction, lighter markup. (2006/09/15)
# 0.0.3: added 'priority' and 'changefreq' tags management in the 'Options' form. (2006/09/16)
# 0.0.4: Unicode support, gmdate format, exponential and quadratic priorities. (2006/09/17)
# 0.0.5: Possibility to sort by last page revision. (2006/09/19)
#ManualSitemap
# 0.1: Jehy took maintenance. Bugfix, new options (2008/11/12)
# 0.2: Thomas added functions for excluding pages, warning if notify fails and setting of servers base url (2009/04/08)
# 1.0: Script rewritten, allowing easier usage (2009/11/30)
# 1.1: Added discussion pages exclusion option
# 1.2: Fixed compatibility issues for MW 1.19.2
#AutoSitemap
#1.0: Rewrote extension for automatic sitemap generation
#1.1: Upgrade to MediaWiki 1.25, code review
#1.2: Search engines notifications improvements & fixes
#1.2.1: Write sitemap to tempfile and then rename it
#1.2.2: Randomize temp file name
#1.3: Set priority for pages or namespaces
#1.4: MW 1.34 support
#1.4.1: Fix MW 1.34 support
#1.5: Fix PHP warning
#1.6: Fix MW 1.37 compatibility issue
#1.7: Remove Bing search notifications from default config
#1.7.1: Fix MW 1.37 compatibility warning
#1.8: Add rate-limit feature and warning messages for too large sitemap
#1.8.1: Remove deprecated 'notify' option
#1.8.2: Fix MW 1.39 deprecation warning
if (!defined('MEDIAWIKI')) {
die('This file is a MediaWiki extension, it is not a valid entry point');
}
use MediaWiki\MediaWikiServices;
global $wgAutoSitemap, $wgServer, $wgCanonicalServer, $wgScriptPath;
if (!isset($wgAutoSitemap["filename"] )) $wgAutoSitemap["filename"] = "sitemap.xml";
if (!isset($wgAutoSitemap["server"] )) $wgAutoSitemap["server"] = isset($wgCanonicalServer) ? $wgCanonicalServer : $wgServer;
if (!isset($wgAutoSitemap["exclude_namespaces"])) $wgAutoSitemap["exclude_namespaces"] = [
NS_TALK,
NS_USER,
NS_USER_TALK,
NS_PROJECT_TALK,
NS_FILE_TALK,
NS_MEDIAWIKI,
NS_MEDIAWIKI_TALK,
NS_TEMPLATE,
NS_TEMPLATE_TALK,
NS_HELP,
NS_HELP_TALK,
NS_CATEGORY_TALK
];
if (!isset($wgAutoSitemap["exclude_pages"] )) $wgAutoSitemap["exclude_pages"] = [];
if (!isset($wgAutoSitemap["priority"] )) $wgAutoSitemap["priority"] = [];
if (!isset($wgAutoSitemap["freq"] )) $wgAutoSitemap["freq"] = "daily";
if (!isset($wgAutoSitemap["header"] )) $wgAutoSitemap["header"] =
'<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="'.$wgAutoSitemap["server"].$wgScriptPath.'/'.'extensions/AutoSitemap/sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';
if (!isset($wgAutoSitemap["footer"] )) $wgAutoSitemap["footer"] = "\n</urlset>";
if (!isset($wgAutoSitemap["min_age"] )) $wgAutoSitemap["min_age"] = 0; // No rate limit
class AutoSitemap {
static public function writeSitemap() {
global $wgAutoSitemap;
$filename = $wgAutoSitemap["filename"];
$min_age = $wgAutoSitemap["min_age"];
$mtime = filemtime($filename);
if ($mtime !== FALSE && time() - $mtime < $min_age) {
// Sitemap is young, no need to update.
return;
}
$server = $wgAutoSitemap["server"];
$tmp_filename = $filename.'.tmp'.bin2hex(random_bytes(16)).'.tmp';
$file_handle = fopen($tmp_filename, 'w');
if ($file_handle === FALSE) {
error_log("Couldn't fopen file: $tmp_filename");
return;
}
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
$res = $dbr->query(self::getSQL());
// Sitemaps are limited to 50,000 URLs and 50 MB.
$entries = 0;
$bytes = 0;
$maxEntries = 50000;
$maxBytes = 50000000;
/* Note:
*
* Actually, https://www.sitemaps.org/protocol.html says the limit is 52,428,800 bytes,
* which is 50 MiB (MiB = 1024 * 1024), but it uses the old notation of "MB" which has
* since been standardized in ISO to mean 1000 * 1000. We use the lower limit just in
* case, because some systems may interpret the "50MB" to mean 50 * 1000 * 1000.
*
* It's very unlikely to reach the size limit before reaching the 50k URL limit anyway,
* unless there are tons of *extremely* long URLs in a sitemap.
*/
$error = FALSE;
try {
$bytes += self::write($file_handle, $wgAutoSitemap["header"]);
while($row = $res->fetchObject()) {
$entries += 1;
$bytes += self::write($file_handle, self::formatResult($server, $row));
}
$bytes += self::write($file_handle, $wgAutoSitemap["footer"]);
} catch (Exception $e) {
error_log("Writing sitemap failed: $e");
$error = TRUE;
} finally {
fclose($file_handle);
}
// Show warning message if sitemap is large than 80% of the limit.
// If limit was exceeded, show error message
if ($entries >= $maxEntries) {
error_log("ERROR: Sitemap is exceeded size limit of $maxEntries items! Please use https://www.mediawiki.org/wiki/Manual:GenerateSitemap.php for generating sitemap file instead of Extensions:AutoSitemap.");
} else if ($entries >= $maxEntries * 0.8) {
error_log("WARNING: Sitemap is exceeded 80% of size limit of $maxEntries items. Please use https://www.mediawiki.org/wiki/Manual:GenerateSitemap.php for generating sitemap file instead of Extensions:AutoSitemap.");
}
if ($bytes >= $maxBytes) {
error_log("ERROR: Sitemap is exceeded size limit of $maxBytes bytes!. Please use https://www.mediawiki.org/wiki/Manual:GenerateSitemap.php for generating sitemap file instead of Extensions:AutoSitemap.");
} else if ($bytes >= $maxBytes * 0.8) {
error_log("WARNING: Sitemap is exceeded 80% of size limit of $maxBytes bytes.. Please use https://www.mediawiki.org/wiki/Manual:GenerateSitemap.php for generating sitemap file instead of Extensions:AutoSitemap.");
}
if ($error) {
if (!unlink($tmp_filename)) {
error_log("Warning: Couldn't delete $tmp_filename.");
}
} else {
rename($tmp_filename, $filename);
}
}
static function write($handle, $data) {
$bytes = fwrite($handle, $data);
if ($bytes === FALSE || $bytes === 0) {
throw new Exception("Call to fwrite failed (returned $bytes).");
}
return $bytes;
}
static function getSQL() {
global $wgAutoSitemap;
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
$page = $dbr->tableName('page');
$revision = $dbr->tableName('revision');
$sql='SELECT "Popularpages" AS type,
page_id AS id,
page_namespace AS namespace,
page_title AS title,
(MAX(rev_timestamp)) AS last_modification,
rev_timestamp AS value
FROM
'.$page.',
'.$revision.'
WHERE
page_is_redirect = 0
AND rev_page = page_id
';
if(is_array($wgAutoSitemap["exclude_namespaces"])) {
if (count($wgAutoSitemap["exclude_namespaces"]) > 0) {
$sql.='AND page_namespace NOT IN ('.implode(",", $wgAutoSitemap["exclude_namespaces"]). ")\n";
}
}
if (is_array($wgAutoSitemap["exclude_pages"])) {
if (count($wgAutoSitemap["exclude_pages"]) > 0) {
$sql.='AND page_title NOT IN (\'' .implode("','", $wgAutoSitemap["exclude_pages"]). "')\n";
}
}
$sql .= 'GROUP BY page_id';
return $sql;
}
static function getPriority($title) {
global $wgAutoSitemap;
$priority = $wgAutoSitemap["priority"];
if (!is_array($priority)) {
return $priority;
}
$namespace = $title->getNamespace();
if (array_key_exists($namespace, $priority)) {
return $priority[$namespace];
}
$pageName = $title->getPrefixedText();
if (array_key_exists($pageName, $priority)) {
return $priority[$pageName];
}
return 1.0;
}
static function getChangeFreq($page_id) {
global $wgAutoSitemap;
if ($wgAutoSitemap["freq"] !== "adjust") {
return $wgAutoSitemap["freq"];
}
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
$revision = $dbr->tableName('revision');
$sql = "SELECT
MIN(rev_timestamp) AS creation_timestamp,
COUNT(rev_timestamp) AS revision_count
FROM $revision WHERE rev_page = $page_id";
$res = $dbr->query($sql);
$count = $res->numRows();
if($count < 1) {
return "daily";
} else {
$item1 = $res->fetchObject();
$cur = time() ;
$first = wfTimestamp(TS_UNIX, $item1->creation_timestamp);
$diff = ($cur - $first) / $item1->revision_count;
switch(true) {
case $diff < 3600: return "hourly";
case $diff < 24*3600: return "daily";
case $diff < 7*24*3600: return "weekly";
case $diff < 30.33*24*3600: return "monthly";
case $diff < 365.25*24*3600: return "yearly";
default: return "daily";
}
}
}
static function formatResult($server, $result) {
global $wgContLang;
$title = Title::makeTitle($result->namespace, $result->title);
$url = $title->getLocalURL();
$priority = sprintf("%01.1f", self::getPriority($title));
$last_modification = gmdate("Y-m-d\TH:i:s\Z", wfTimestamp(TS_UNIX, $result->last_modification));
$freq = self::getChangeFreq($result->id);
return self::prepareLine($server.$url, $priority, $last_modification, $freq);
}
static function prepareLine($url, $priority, $last_modification, $freq) {
return '
<url>
<loc>'.self::encodeUrl($url).'</loc>
<priority>'.$priority.'</priority>
<lastmod>'.$last_modification.'</lastmod>
<changefreq>'.$freq.'</changefreq>
</url>';
}
static function encodeUrl($url) {
return str_replace(array('(',')'),array('%28','%29'), $url);
}
}