xref: /dokuwiki/inc/Sitemap/Mapper.php (revision a32da6dda625a55bd713657b22f3ab332757fca6)
1<?php
2/**
3 * Sitemap handling functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Michael Hamann <michael@content-space.de>
7 */
8
9namespace dokuwiki\Sitemap;
10
11use dokuwiki\Extension\Event;
12use dokuwiki\HTTP\DokuHTTPClient;
13use dokuwiki\Search\Indexer;
14
15/**
16 * A class for building sitemaps and pinging search engines with the sitemap URL.
17 *
18 * @author Michael Hamann
19 */
20class Mapper {
21    /**
22     * Builds a Google Sitemap of all public pages known to the indexer
23     *
24     * The map is placed in the cache directory named sitemap.xml.gz - This
25     * file needs to be writable!
26     *
27     * @author Michael Hamann
28     * @author Andreas Gohr
29     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
30     * @link   http://www.sitemaps.org/
31     *
32     * @return bool
33     */
34    public static function generate()
35    {
36        global $conf;
37        if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) return false;
38
39        $sitemap = Mapper::getFilePath();
40
41        if (file_exists($sitemap)) {
42            if (!is_writable($sitemap)) return false;
43        } else {
44            if (!is_writable(dirname($sitemap))) return false;
45        }
46
47        if (@filesize($sitemap) &&
48            @filemtime($sitemap) > (time()-($conf['sitemap']*86400)) // 60*60*24=86400
49        ) {
50            dbglog('Sitemapper::generate(): Sitemap up to date');
51            return false;
52        }
53
54        dbglog("Sitemapper::generate(): using $sitemap");
55
56        $pages = (new Indexer())->getPages();
57        dbglog('Sitemapper::generate(): creating sitemap using '.count($pages).' pages');
58        $items = array();
59
60        // build the sitemap items
61        foreach ($pages as $id) {
62            //skip hidden, non existing and restricted files
63            if (isHiddenPage($id)) continue;
64            if (auth_aclcheck($id,'',array()) < AUTH_READ) continue;
65            $item = Item::createFromID($id);
66            if ($item !== null)
67                $items[] = $item;
68        }
69
70        $eventData = array('items' => &$items, 'sitemap' => &$sitemap);
71        $event = new Event('SITEMAP_GENERATE', $eventData);
72        if ($event->advise_before(true)) {
73            //save the new sitemap
74            $event->result = io_saveFile($sitemap, Mapper::getXML($items));
75        }
76        $event->advise_after();
77
78        return $event->result;
79    }
80
81    /**
82     * Builds the sitemap XML string from the given array auf SitemapItems.
83     *
84     * @param $items array The SitemapItems that shall be included in the sitemap.
85     * @return string The sitemap XML.
86     *
87     * @author Michael Hamann
88     */
89    private static function getXML($items)
90    {
91        ob_start();
92        echo '<?xml version="1.0" encoding="UTF-8"?>'.NL;
93        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
94        foreach ($items as $item) {
95            /** @var Item $item */
96            echo $item->toXML();
97        }
98        echo '</urlset>'.NL;
99        $result = ob_get_contents();
100        ob_end_clean();
101        return $result;
102    }
103
104    /**
105     * Helper function for getting the path to the sitemap file.
106     *
107     * @return string The path to the sitemap file.
108     *
109     * @author Michael Hamann
110     */
111    public static function getFilePath()
112    {
113        global $conf;
114
115        $sitemap = $conf['cachedir'].'/sitemap.xml';
116        if (self::sitemapIsCompressed()) {
117            $sitemap .= '.gz';
118        }
119
120        return $sitemap;
121    }
122
123    /**
124     * Helper function for checking if the sitemap is compressed
125     *
126     * @return bool If the sitemap file is compressed
127     */
128    public static function sitemapIsCompressed()
129    {
130        global $conf;
131        return $conf['compression'] === 'bz2' || $conf['compression'] === 'gz';
132    }
133
134    /**
135     * Pings search engines with the sitemap url. Plugins can add or remove
136     * urls to ping using the SITEMAP_PING event.
137     *
138     * @author Michael Hamann
139     *
140     * @return bool
141     */
142    public static function pingSearchEngines()
143    {
144        //ping search engines...
145        $http = new DokuHTTPClient();
146        $http->timeout = 8;
147
148        $encoded_sitemap_url = urlencode(wl('', array('do' => 'sitemap'), true, '&'));
149        $ping_urls = array(
150            'google'    => 'http://www.google.com/webmasters/sitemaps/ping?sitemap='.$encoded_sitemap_url,
151            'microsoft' => 'http://www.bing.com/webmaster/ping.aspx?siteMap='.$encoded_sitemap_url,
152            'yandex'    => 'http://blogs.yandex.ru/pings/?status=success&url='.$encoded_sitemap_url
153        );
154
155        $data = array(
156            'ping_urls' => $ping_urls,
157            'encoded_sitemap_url' => $encoded_sitemap_url
158        );
159        $event = new Event('SITEMAP_PING', $data);
160        if ($event->advise_before(true)) {
161            foreach ($data['ping_urls'] as $name => $url) {
162                dbglog("Sitemapper::PingSearchEngines(): pinging $name");
163                $resp = $http->get($url);
164                if ($http->error) dbglog("Sitemapper:pingSearchengines(): $http->error");
165                dbglog('Sitemapper:pingSearchengines(): '.preg_replace('/[\n\r]/',' ',strip_tags($resp)));
166            }
167        }
168        $event->advise_after();
169
170        return true;
171    }
172}
173