xref: /dokuwiki/inc/Sitemap/Mapper.php (revision a19c9aa0217112e3ab7ebc160354c7e9fbabe8eb)
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(@filesize($sitemap) &&
49           @filemtime($sitemap) > (time()-($conf['sitemap']*86400))){ // 60*60*24=86400
50            Logger::debug('Sitemapper::generate(): Sitemap up to date');
51            return false;
52        }
53
54        Logger::debug("Sitemapper::generate(): using $sitemap");
55
56        $pages = idx_get_indexer()->getPages();
57        Logger::debug('Sitemapper::generate(): creating sitemap using '.count($pages).' pages');
58        $items = [];
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, '', []) < AUTH_READ) continue;
65            $item = Item::createFromID($id);
66            if ($item instanceof Item)
67                $items[] = $item;
68        }
69
70        $eventData = ['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, (new 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 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('', ['do' => 'sitemap'], true, '&'));
149        $ping_urls = [
150            'google'    => 'https://www.google.com/ping?sitemap='.$encoded_sitemap_url,
151            'yandex'    => 'https://webmaster.yandex.com/ping?sitemap='.$encoded_sitemap_url
152        ];
153
154        $data = [
155            'ping_urls' => $ping_urls,
156            'encoded_sitemap_url' => $encoded_sitemap_url
157        ];
158        $event = new Event('SITEMAP_PING', $data);
159        if ($event->advise_before(true)) {
160            foreach ($data['ping_urls'] as $name => $url) {
161                Logger::debug("Sitemapper::PingSearchEngines(): pinging $name");
162                $resp = $http->get($url);
163                if($http->error) {
164                    Logger::debug("Sitemapper:pingSearchengines(): $http->error", $resp);
165                }
166            }
167        }
168        $event->advise_after();
169
170        return true;
171    }
172}
173