add sitemap support. generates with ?action=sitemap
[wikifarm.git] / install / cookbook / sitemap.php
1 <?php if (!defined('PmWiki')) exit();
2 /*
3 $Id: sitemap.php,v 1.7 2005/12/29 10:26:50 pts00065 Exp $
4 This file is NOT part of PmWiki; still you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9
10 This script generated a google sitemap, either automaticly or as action=sitemap or both.
11
12 The automtic version creates a .gz file and is more efficient compared to the action version.
13
14 google requires that the generated file is located in the root of your pmwiki installation, typically the same place as your pmwiki.php or equivalent file. Thus:
15
16 http://www.brambring.nl/pmwiki.php -> sitemap must be in: http://www.brambring.nl/sitemap.xml.gz
17
18 place the file in the cookbook or local directory
19 and include the file from your farmconfig.php or config.php
20 include_once("$FarmD/cookbook/sitemap.php");
21
22
23 Then add the URL to your sitemap in the google webform or ping google
24
25 automatic google ping is not implemented yet.
26
27 regards
28
29 bram
30
31 http://www.brambring.nl
32
33 $Log: sitemap.php,v $
34 Revision 1.7 2005/12/29 10:26:50 pts00065
35 * support EnablePageListProtect
36 * Added Site to exclude pattern
37
38
39
40 */
41 SDV($LastModFile, "$WorkDir/.lastmod"); # same as in caching
42 SDV($SitemapFile, "sitemap.xml.gz"); #will need write access must be in root dir. Ensure dir is writable or create (symbolic) link
43
44 SDV($HandleActions['sitemap'], 'HandleSitemap'); # it is not usefull to have both an action
45 // and automatic creation ( SitemapDelay >= 0
46 SDV($SitemapDelay, 3600); # Seconds to wait after last edit set to -1 to disable automatic generation
47 SDV($SitemapSquelch, 12*3600); # Squelch between generations of sitemap
48
49
50 SDVA($SitemapSearchPatterns, array());
51 $SitemapSearchPatterns[] = '!\.(All)?Recent(Changes|Uploads|Pages)$!';
52 $SitemapSearchPatterns[] = '!\.Group(Print)?(Header|Footer|Attributes)$!';
53 $SitemapSearchPatterns[] = '!^PmWiki\.!';
54 $SitemapSearchPatterns[] = '!^Site\.!';
55 $SitemapSearchPatterns[] = '!\.SideBar!';
56
57 SDV($SitemapMaxItems, 50000); # maximum items to display defined by google
58 SDV($SitemapMaxSize, 10); # maximum size is 10 Mbytes TODO
59 SDV($SitemapPing, "http://www.google.com/"); # Use ping with long SitemapDelay (like 24*60*60 ) TODO
60
61 // SDV($SitemapTimeFmt,'%Y-%m-%dT%H:%M:%sZ'); # seems to break in current version of google
62 SDV($SitemapTimeFmt, '%Y-%m-%d');
63
64 SDV($SiteMapItems, array());
65 SDV($SitemapChannelFmt, '<?xml version="1.0" encoding="UTF-8"?>
66 <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
67 ');
68 SDV($SitemapItemFmt, '
69 <url>
70 <loc>$PageUrl</loc>
71 <lastmod>$SitemapItemPubDate</lastmod>
72 <changefreq>$SitemapChangeFreq</changefreq>
73 <priority>$SitemapPriority</priority>
74 </url>');
75 SDV($HandleSitemapFmt, array(&$SitemapChannelFmt, &$SitemapItems, '</urlset>'));
76
77 if ( $action == 'browse' ) {
78 if ($SitemapDelay >= 0) {
79 $l = @filemtime($LastModFile);
80 $s = @filemtime($SitemapFile);
81 if ((($Now - $l) > $SitemapDelay) && ($l > $s) && (($Now - $s) > $SitemapSquelch)) {
82 $fp = @fopen($SitemapFile, "w");
83 if ($fp) {
84 ob_start();
85 MakeSitemap();
86 $x = gzencode (ob_get_clean(), 9);
87 fwrite($fp, $x);
88 fclose($fp);
89 }
90 }
91 }
92 }
93
94
95
96 function HandleSitemap()
97 {
98 header("Content-type: text/xml");
99 MakeSitemap();
100 exit;
101 }
102
103 function MakeSitemap()
104 {
105 global $SitemapMaxItems, $SitemapChannelFmt, $SitemapTimeFmt,
106 $SitemapItems, $SitemapItemFmt, $SearchPatterns,$FarmD,
107 $EnablePageListProtect,
108 $HandleSitemapFmt, $FmtV, $SitemapSearchPatterns, $Now;
109 global $EntitiesTable;
110 if (IsEnabled($EnablePageListProtect, 1)) $readf = 1000;
111
112 $t = array();
113 $t = @ListPages($SitemapSearchPatterns);
114 $daily_weekly = 60 * 60 * 24 * 6; #TODO
115 foreach ($t as $i => $pn) {
116 $page= ($readf >= 1000)
117 ? RetrieveAuthPage($pn, 'read', false, READPAGE_CURRENT)
118 : ReadPage($pn, READPAGE_CURRENT);
119 if (!$page) continue;
120
121 // foreach ( $page as $k => $l ) { print "$k == $l <br />\n"; }
122 if ( (count($SitemapItems) > $SitemapMaxItems)) continue;
123 $FmtV['$SitemapChangeFreq'] = ($Now - $page['time'] < $daily_weekly)?'daily':'weekly'; #TODO
124 $FmtV['$SitemapPriority'] = '0.5'; #TODO
125 $FmtV['$SitemapItemPubDate'] = gmstrftime($SitemapTimeFmt, $page['time']);
126 $SitemapItems[] = FmtPageName($SitemapItemFmt, $page['name']);
127 }
128
129 #PrintFmt('', str_replace(array_keys($EntitiesTable), array_values($EntitiesTable), $HandleSitemapFmt));
130 PrintFmt('', $HandleSitemapFmt);
131 }
132
133