forked from OXIDprojects/google_sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_sitemap_xml.php
358 lines (325 loc) · 10.5 KB
/
google_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
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
<?php
/**
* Google Sitemap Xml v.0.1
* --------------------------------
* oxid ESales - SEO
* --------------------------------
* by DIATOM Internet & Medien GmbH
* --------------------------------
* c: 27.07.2009 | u: 27.07.2009
*
* =====================================================================
* modified by: 010|nullzehn - digitale medien @2010-01-23:
* - creating multiple sitemaps for large shops
* - compress sitemaps (gzip)
* - recalls self to avoid memory breaks
* - run as a cronjob or manually in cli
*
* you can call the script by crontab: "googlesitemap.sh -c 1"
* (or without params if you like)
* therefore add "#! /usr/bin/php -q " before the <?php in first line and
* save as googlesitemap.sh
* =====================================================================
* / install
*
*(1) insert your paths and data below //configuration
*(2) upload file to your webspace
*(3) adjust chmod if needed
*(4) open with your browser
*(5) open sitemap.xml and check content
*
* / transfer to google
*
*(1) open www.google.com/webmasters/tools
*(2) log in with your account
*(3) choose website
*(4) "XML-Sitemaps" -> "add Sitemap"
*(5) specify URL of your sitemap.xml
* =====================================================================
*/
// init
$mod_cnf = array();
$error = array();
$xmlInsert = array();
$xmlList = array();
$xmlList_cat = array();
$xmlList_cms = array();
$xmlList_prod = array();
// configuration
$mod_cnf['filepath'] = './'; // fullpath to sitemaps
$mod_cnf['filename'] = 'sitemap-de'; // basename for sitemaps
$mod_cnf['offset'] = 20000; // how many product-urls in each sitemap? (max. allowed: 50.000 urls (total, with cats and cms) && max. filesize: 10Mb (uncompressed!))
$mod_cnf['siteurl'] = 'http://www.my-domain.tld/'; // shop url (with ending slash!)
$mod_cnf['dbhost'] = 'localhost'; // dbhost
$mod_cnf['dbname'] = 'insert db_name'; // dbname
$mod_cnf['dbuser'] = 'insert db_user'; // dbuser
$mod_cnf['dbpass'] = 'insert db_password'; // dbpass
// which run?: script calls with '-c [n]'
// first run (without params) -> call #1
if ("-c" != $_SERVER['argv'][1])
{
$pcall = 1;
}
else {
$pcall = $_SERVER['argv'][2];
if (!preg_match("/[\d]/",$pcall))
{
die("Illegal call.\n");
}
}
// db connection
$sqlConnect = mysql_connect(
$mod_cnf['dbhost'],
$mod_cnf['dbuser'],
$mod_cnf['dbpass']) OR die('error connecting to database.');
mysql_select_db(
$mod_cnf['dbname'],
$sqlConnect) OR die('error selecting table.');
//** get number of needed script-calls, based on active items with valid seo-url. cms and categories will be added to first sitemap automatically.
$cntCalls = ceil(getCountScriptCalls() / $mod_cnf['offset']);
// store cms- and category-data only at first call, further calls are products only
if (1 == $pcall)
{
// get cms data from shop - only at first script-run! (-c 1)
$xmlList_cms = getCmsSite();
// get all categories
$xmlList_cat = getCategories();
}
// get products (with offset)
$xmlList_prod = getProducts($pcall);
// build xml-data and output
$xmlList = array_merge($xmlList_prod, $xmlList_cat, $xmlList_cms);
// create sitemap
$sitemapdata = createSitemap($xmlList);
$smfile = createXmlFile($sitemapdata);
// compress sitemap
compressSitemapFile($smfile);
// create global sitemaps-index-file (watch sitemaps.org for more infos..)
createSitemapIndex();
//** RECALL SCRIPT
if ($pcall < $cntCalls)
{
// memory seems to hold list-array-values, maybe depends on local environment
unset($xmlList,$xmlList_cat,$xmlList_cms,$xmlList_prod);
// call itself
$exec = './googlesitemap.sh -c '.($pcall+1);
//echo "\n".$exec."\n"; //debug
system($exec);
exit(0);
}
//** exit all
//echo "\nready.\n"; // debug
exit(0);
// ** FUNCTIONS
/** get all active and visible categories from database
* @return array
*/
function getCategories()
{
global $mod_cnf;
$list = array();
$sql = "SELECT
seo.oxseourl
FROM
oxcategories as oxcats
LEFT JOIN
oxseo as seo ON (oxcats.oxid=seo.oxobjectid)
WHERE
oxcats.oxactive = 1 AND
oxcats.oxhidden = 0 AND
seo.oxtype='oxcategory' AND
seo.oxstdurl NOT LIKE ('%pgNr=%') AND
seo.oxlang = 0
GROUP BY
oxcats.oxid;";
$sql_query = mysql_query($sql);
while ($sql_row = mysql_fetch_array($sql_query))
{
$list[] = array(
'loc' => $mod_cnf['siteurl'] . strtolower($sql_row['oxseourl']),
'priority' => '0.6',
'lastmod' => date("Y-m-d") . 'T' . date("h:i:s") . '+00:00',
'changefreq' => 'weekly',
);
}
mysql_free_result($sql_query);
return $list;
}
/** get active cms content from database
* @return array
*/
function getCmsSite()
{
global $mod_cnf;
$list = array();
$sql = "SELECT
seo.oxseourl
FROM
oxcontents as content
LEFT JOIN
oxseo as seo ON (content.oxid=seo.oxobjectid)
WHERE
content.oxactive = 1 AND
content.oxfolder = 'CMSFOLDER_USERINFO'
AND seo.oxseourl <> ''
AND seo.oxseourl NOT LIKE ('%META%')
AND seo.oxlang = 0
GROUP BY
content.oxid;";
$sql_query = mysql_query($sql);
while ($sql_row = mysql_fetch_array($sql_query))
{
$list[] = array(
'loc' => $mod_cnf['siteurl'] . strtolower($sql_row['oxseourl']),
'priority' => '0.5',
'lastmod' => date("Y-m-d") . 'T' . date("h:i:s") . '+00:00',
'changefreq' => 'weekly',
);
}
mysql_free_result($sql_query);
return $list;
}
/** get active products from database with offset
* @return array
*/
function getProducts($limit)
{
global $mod_cnf;
$list = array();
// calculate offset
$start = $mod_cnf['offset'];
if (1 == $limit)
{
$end = 0;
}
else {
$end = (($limit-1) * $mod_cnf['offset']) - 1;
}
$sql = "SELECT
oxart.oxtimestamp,
seo.oxseourl
FROM
oxarticles as oxart
LEFT JOIN oxobject2category as oxobj2cat
ON (oxobj2cat.oxobjectid = oxart.oxid)
LEFT JOIN oxcategories as oxcat
ON (oxcat.oxid = oxobj2cat.oxcatnid)
LEFT JOIN oxseo as seo
ON (oxart.oxid = seo.oxobjectid)
WHERE
oxart.oxactive = 1 AND
oxcat.oxactive = 1 AND
oxcat.oxhidden = 0 AND
seo.oxlang = 0 AND
seo.oxtype='oxarticle' AND
seo.oxstdurl LIKE ('%cnid=%')
GROUP BY
oxart.oxid
LIMIT ".$start." OFFSET ".$end.";";
$sql_query = mysql_query($sql);
while ($sql_row = mysql_fetch_array($sql_query))
{
$lastmod = $sql_row['oxtimestamp'];
if ("0000-00-00 00:00:00" == $lastmod)
{
$lastmod = date("Y-m-d") . 'T' . date("h:i:s") . '+00:00';
}
$lastmod = date("Y-m-d") . 'T' . date("h:i:s") . '+00:00';
$list[] = array(
'loc' => $mod_cnf['siteurl'] . strtolower($sql_row['oxseourl']),
'priority' => '0.7',
'lastmod' => $lastmod,
'changefreq' => 'daily',
);
}
mysql_free_result($sql_query);
return $list;
}
/** get total number of 'seo-active' products in shop
* @return integer
*/
function getCountScriptCalls()
{
global $mod_cnf;
$sql = "SELECT
oxart.oxid
FROM
oxarticles as oxart
LEFT JOIN oxobject2category as oxobj2cat
ON (oxobj2cat.oxobjectid = oxart.oxid)
LEFT JOIN oxcategories as oxcat
ON (oxcat.oxid = oxobj2cat.oxcatnid)
LEFT JOIN oxseo as seo
ON (oxart.oxid = seo.oxobjectid)
WHERE
oxart.oxactive = 1 AND
oxcat.oxactive = 1 AND
oxcat.oxhidden = 0 AND
seo.oxlang = 0 AND
seo.oxtype='oxarticle'
GROUP BY
oxart.oxid;";
$query = mysql_query($sql);
return mysql_num_rows($query);
}
/** creates xml data / sitemap-content
* @return array
*/
function createSitemap($data)
{
global $mod_cnf;
$mapdata[] = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($data as $key => $val)
{
$mapdata[] = '<url><loc>'. $val['loc'] .'</loc><priority>'. $val['priority'] .'</priority><lastmod>'. $val['lastmod'] .'</lastmod><changefreq>'. $val['changefreq'] .'</changefreq></url>';
}
$mapdata[] = '</urlset>';
return $mapdata;
}
/** stores xml-file to filesystem
* @return string
*/
function createXmlFile($smdata)
{
global $mod_cnf,$pcall;
$fname = $mod_cnf['filepath'].$mod_cnf['filename'].$pcall.".xml";
$fp = fopen($fname, "w+");
fwrite($fp, implode("\n", $smdata));
fclose($fp);
return $fname;
}
/** compress sitemap-file: new file is sitemap.gz
* @return void
*/
function compressSitemapFile($fname)
{
if (file_exists($fname))
{
system("gzip -fq -9 ".$fname);
}
return;
}
/** append new sitemap to sitemap index
* @return void
*/
function createSitemapIndex()
{
global $pcall,$mod_cnf;
$sitemaps = array();
$maps = array();
// build xml-content
$smindex = '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
for ($i=1;$i<=$pcall;$i++)
{
$loc = '<loc>'.$mod_cnf['siteurl'].$mod_cnf['filename'].$i.'.xml.gz</loc>';
$last = '<lastmod>'.date("Y-m-d").'T'.date("H:i:s").'+00:00</lastmod>';
$sitemaps[] = '<sitemap>'.$loc.$last.'</sitemap>';
}
$maps = $smindex . "\n" . implode("\n",$sitemaps);
$sitemapindex = $maps . "\n</sitemapindex>";
// write to file
@file_put_contents($mod_cnf['filename'].'.xml',$sitemapindex);
return;
}
?>