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