Completed
Push — master ( 8f3dec...787392 )
by Mark
18s queued 12s
created

helper_plugin_spatialhelper_sitemap::createKMLSitemap()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 7.8521
c 0
b 0
f 0
cc 7
nc 8
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright (c) 2014-2016 Mark C. Prins <[email protected]>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
if (!defined('DOKU_LF')) {
19
    define('DOKU_LF', "\n");
20
}
21
22
/**
23
 * DokuWiki Plugin spatialhelper (sitemap Component).
24
 *
25
 * @license BSD license
26
 * @author Mark Prins
27
 */
28
class helper_plugin_spatialhelper_sitemap extends DokuWiki_Plugin {
29
    /**
30
     * spatial index.
31
     */
32
    private $spatial_idx = array();
33
34
    /**
35
     * constructor, load spatial index.
36
     */
37
    public function __construct() {
38
        global $conf;
39
        $idx_dir = $conf['indexdir'];
40
        if (!@file_exists($idx_dir . '/spatial.idx')) {
41
            $indexer = plugin_load('helper', 'spatialhelper_index');
42
            $indexer->generateSpatialIndex();
43
        }
44
        $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false));
45
    }
46
47
    public function getMethods() {
48
        $result[] = array(
49
                'name' => 'createGeoRSSSitemap',
50
                'desc' => 'create a spatial sitemap in GeoRSS format.',
51
                'params' => array(
52
                        'path' => 'string'
53
                ),
54
                'return' => array(
55
                        'success' => 'boolean'
56
                )
57
        );
58
        $result[] = array(
59
                'name' => 'createKMLSitemap',
60
                'desc' => 'create a spatial sitemap in KML format.',
61
                'params' => array(
62
                        'path' => 'string'
63
                ),
64
                'return' => array(
65
                        'success' => 'boolean'
66
                )
67
        );
68
        return $result;
69
    }
70
71
    /**
72
     * Create a GeoRSS Simple sitemap (Atom).
73
     *
74
     * @param $mediaID id
75
     *          for the GeoRSS file
76
     */
77
    public function createGeoRSSSitemap($mediaID) {
78
        global $conf;
79
        $namespace = getNS($mediaID);
80
81
        $idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ',';
82
83
        $RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
84
        $RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" '
85
        $RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE
Loading history...
86
        $RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF;
87
        if (!empty($conf['tagline'])) {
88
            $RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF;
89
        }
90
        $RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF;
91
        $RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF;
92
        $RSSstart .= '<link href="' . ml($mediaID, '', true, '&amp;', true) . '" rel="self" />' . DOKU_LF;
93
        $RSSstart .= '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF;
94
        $RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH) . '</id>' . DOKU_LF;
95
        $RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF;
96
97
        $RSSend = '</feed>' . DOKU_LF;
98
99
        io_createNamespace($mediaID, 'media');
100
        @touch(mediaFN($mediaID));
101
        @chmod(mediaFN($mediaID), $conf['fmode']);
102
        $fh = fopen(mediaFN($mediaID), 'w');
103
        fwrite($fh, $RSSstart);
104
105
        foreach ($this->spatial_idx as $idxEntry) {
106
            // get list of id's
107
            foreach ($idxEntry as $id) {
108
                // for document item in the index
109
                if (strpos($id, 'media__', 0) !== 0) {
110
                    if ($this->skipPage($id, $namespace)) {
111
                        continue;
112
                    }
113
114
                    $meta = p_get_metadata($id);
115
116
                    // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
117
                    $desc = strip_tags($meta['description']['abstract']);
118
119
                    $entry = '<entry>' . DOKU_LF;
120
                    $entry .= '  <title>' . $meta['title'] . '</title>' . DOKU_LF;
121
                    $entry .= '  <summary>' . $desc . '</summary>' . DOKU_LF;
122
                    $entry .= '  <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon']
123
                        . '</georss:point>' . DOKU_LF;
124
                    if ($meta['geo']['alt']) {
125
                        $entry .= '  <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF;
126
                    }
127
                    $entry .= '  <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF;
128
                    if (empty($meta['creator'])) {
129
                        $meta['creator'] = $conf['title'];
130
                    }
131
                    $entry .= '  <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF;
132
                    $entry .= '  <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF;
133
                    $entry .= '  <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF;
134
                    $entry .= '  <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':'
135
                        . parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF;
136
                    $entry .= '</entry>' . DOKU_LF;
137
                    fwrite($fh, $entry);
138
                }
139
            }
140
        }
141
142
        fwrite($fh, $RSSend);
143
        return fclose($fh);
144
    }
145
146
    /**
147
     * Create a KML sitemap.
148
     *
149
     * @param $mediaID id
150
     *          for the KML file
151
     */
152
    public function createKMLSitemap($mediaID) {
153
        global $conf;
154
        $namespace = getNS($mediaID);
155
156
        $KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
157
        $KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" ';
158
        $KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
159
        $KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"';
160
        $KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 ';
161
        $KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF;
162
        $KMLstart .= '<Document id="root_doc">' . DOKU_LF;
163
        $KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF;
164
        $KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF;
165
        $KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF;
166
        $KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>';
167
        $KMLstart .= '<Icon><href>'
168
            . DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF;
169
170
        $KMLend = '</Document>' . DOKU_LF . '</kml>';
171
172
        io_createNamespace($mediaID, 'media');
173
        @touch(mediaFN($mediaID));
174
        @chmod(mediaFN($mediaID), $conf['fmode']);
175
176
        $fh = fopen(mediaFN($mediaID), 'w');
177
        fwrite($fh, $KMLstart);
178
179
        foreach ($this->spatial_idx as $idxEntry) {
180
            // get list of id's
181
            foreach ($idxEntry as $id) {
182
                // for document item in the index
183
                if (strpos($id, 'media__', 0) !== 0) {
184
                    if ($this->skipPage($id, $namespace)) {
185
                        continue;
186
                    }
187
188
                    $meta = p_get_metadata($id);
189
190
                    // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
191
                    $desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>';
192
                    $desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] . '</a></p>';
193
194
                    // create an entry and store it
195
                    $plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF;
196
                    $plcm .= '  <name>' . $meta['title'] . '</name>' . DOKU_LF;
197
                    // TODO escape quotes in: title="' . $meta['title'] . '"
198
                    $plcm .= '  <atom:link href="' . wl($id, '' . true) . '" rel="alternate" type="text/html" />'
199
                        . DOKU_LF;
200
                    if (!empty($meta['creator'])) {
201
                        $entry .= '  <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>'
202
                            . DOKU_LF;
203
                    }
204
205
                    $plcm .= '  <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF;
206
                    $plcm .= '  <styleUrl>#icon</styleUrl>' . DOKU_LF;
207
208
                    $plcm .= '  <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat'];
209
                    if ($meta['geo']['alt']) {
210
                        $plcm .= ',' . $meta['geo']['alt'];
211
                    }
212
                    $plcm .= '</coordinates></Point>' . DOKU_LF;
213
                    $plcm .= '</Placemark>' . DOKU_LF;
214
215
                    fwrite($fh, $plcm);
216
                }
217
            }
218
        }
219
        fwrite($fh, $KMLend);
220
        return fclose($fh);
221
    }
222
    /**
223
     * will return true for non-public or hidden pages or pages that are not below or in the namespace.
224
     */
225
    private function skipPage($id, $namespace) {
226
        dbglog("helper_plugin_spatialhelper_sitemap::skipPage, check for $id in $namespace");
227
        if (isHiddenPage($id)) {
228
            return true;
229
        }
230
        if (auth_aclcheck($id, '', '') < AUTH_READ) {
231
            return true;
232
        }
233
234
        if (!empty($namespace)) {
235
            // only if id is in or below namespace
236
            if (0 !== strpos(getNS($id), $namespace)) {
237
                // dbglog("helper_plugin_spatialhelper_sitemap::skipPage, skipping $id, not in $namespace");
238
                return true;
239
            }
240
        }
241
        return false;
242
    }
243
}
244