Completed
Pull Request — master (#34)
by Mark
42s
created
helper/search.php 2 patches
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -26,177 +26,177 @@
 block discarded – undo
26 26
  * @author  Mark Prins
27 27
  */
28 28
 class helper_plugin_spatialhelper_search extends DokuWiki_Plugin {
29
-    /**
30
-     * spatial index.
31
-     *
32
-     * @var array
33
-     */
34
-    protected $spatial_idx = array();
35
-    /**
36
-     * Precision, Distance of Adjacent Cell in Meters.
37
-     *
38
-     * @see https://stackoverflow.com/questions/13836416/geohash-and-max-distance
39
-     *
40
-     * @var float
41
-     */
42
-    private $precision = array(
43
-        5003530,
44
-        625441,
45
-        123264,
46
-        19545,
47
-        3803,
48
-        610,
49
-        118,
50
-        19,
51
-        3.7,
52
-        0.6
53
-    );
29
+	/**
30
+	 * spatial index.
31
+	 *
32
+	 * @var array
33
+	 */
34
+	protected $spatial_idx = array();
35
+	/**
36
+	 * Precision, Distance of Adjacent Cell in Meters.
37
+	 *
38
+	 * @see https://stackoverflow.com/questions/13836416/geohash-and-max-distance
39
+	 *
40
+	 * @var float
41
+	 */
42
+	private $precision = array(
43
+		5003530,
44
+		625441,
45
+		123264,
46
+		19545,
47
+		3803,
48
+		610,
49
+		118,
50
+		19,
51
+		3.7,
52
+		0.6
53
+	);
54 54
 
55
-    /**
56
-     * constructor; initialize/load spatial index.
57
-     */
58
-    public function __construct() {
59
-        // parent::__construct ();
60
-        global $conf;
55
+	/**
56
+	 * constructor; initialize/load spatial index.
57
+	 */
58
+	public function __construct() {
59
+		// parent::__construct ();
60
+		global $conf;
61 61
 
62
-        if(!plugin_load('helper', 'geophp', false, true)) {
63
-            $message = 'helper_plugin_spatialhelper_search::spatialhelper_search: required geophp plugin is not available.';
64
-            msg($message, -1);
65
-        }
62
+		if(!plugin_load('helper', 'geophp', false, true)) {
63
+			$message = 'helper_plugin_spatialhelper_search::spatialhelper_search: required geophp plugin is not available.';
64
+			msg($message, -1);
65
+		}
66 66
 
67
-        $idx_dir = $conf ['indexdir'];
68
-        if(!@file_exists($idx_dir . '/spatial.idx')) {
69
-            plugin_load('helper', 'spatialhelper_index');
70
-        }
67
+		$idx_dir = $conf ['indexdir'];
68
+		if(!@file_exists($idx_dir . '/spatial.idx')) {
69
+			plugin_load('helper', 'spatialhelper_index');
70
+		}
71 71
 
72
-        $this->spatial_idx = unserialize(io_readFile($idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
73
-    }
72
+		$this->spatial_idx = unserialize(io_readFile($idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
73
+	}
74 74
 
75
-    /**
76
-     * Find locations based on the coordinate pair.
77
-     *
78
-     * @param float $lat
79
-     *          The y coordinate (or latitude)
80
-     * @param float $lon
81
-     *          The x coordinate (or longitude)
82
-     */
83
-    public function findNearbyLatLon(float $lat, float $lon): array {
84
-        $geometry = new Point($lon, $lat);
85
-        return $this->findNearby($geometry->out('geohash'), $geometry);
86
-    }
75
+	/**
76
+	 * Find locations based on the coordinate pair.
77
+	 *
78
+	 * @param float $lat
79
+	 *          The y coordinate (or latitude)
80
+	 * @param float $lon
81
+	 *          The x coordinate (or longitude)
82
+	 */
83
+	public function findNearbyLatLon(float $lat, float $lon): array {
84
+		$geometry = new Point($lon, $lat);
85
+		return $this->findNearby($geometry->out('geohash'), $geometry);
86
+	}
87 87
 
88
-    /**
89
-     * finds nearby elements in the index based on the geohash.
90
-     * returns a list of documents and the bunding box.
91
-     *
92
-     * @param string     $geohash
93
-     * @param Point|null $p
94
-     *          optional point
95
-     * @return array of ...
96
-     * @throws Exception
97
-     */
98
-    public function findNearby(string $geohash, Point $p = null): array {
99
-        $_geohashClass = new Geohash();
100
-        if(!$p) {
101
-            $decodedPoint = $_geohashClass->read($geohash);
102
-        } else {
103
-            $decodedPoint = $p;
104
-        }
88
+	/**
89
+	 * finds nearby elements in the index based on the geohash.
90
+	 * returns a list of documents and the bunding box.
91
+	 *
92
+	 * @param string     $geohash
93
+	 * @param Point|null $p
94
+	 *          optional point
95
+	 * @return array of ...
96
+	 * @throws Exception
97
+	 */
98
+	public function findNearby(string $geohash, Point $p = null): array {
99
+		$_geohashClass = new Geohash();
100
+		if(!$p) {
101
+			$decodedPoint = $_geohashClass->read($geohash);
102
+		} else {
103
+			$decodedPoint = $p;
104
+		}
105 105
 
106
-        // find adjacent blocks
107
-        $adjacent                 = array();
108
-        $adjacent ['center']      = $geohash;
109
-        $adjacent ['top']         = Geohash::adjacent($adjacent ['center'], 'top');
110
-        $adjacent ['bottom']      = Geohash::adjacent($adjacent ['center'], 'bottom');
111
-        $adjacent ['right']       = Geohash::adjacent($adjacent ['center'], 'right');
112
-        $adjacent ['left']        = Geohash::adjacent($adjacent ['center'], 'left');
113
-        $adjacent ['topleft']     = Geohash::adjacent($adjacent ['left'], 'top');
114
-        $adjacent ['topright']    = Geohash::adjacent($adjacent ['right'], 'top');
115
-        $adjacent ['bottomright'] = Geohash::adjacent($adjacent ['right'], 'bottom');
116
-        $adjacent ['bottomleft']  = Geohash::adjacent($adjacent ['left'], 'bottom');
117
-        dbglog($adjacent, "adjacent geo hashes:");
106
+		// find adjacent blocks
107
+		$adjacent                 = array();
108
+		$adjacent ['center']      = $geohash;
109
+		$adjacent ['top']         = Geohash::adjacent($adjacent ['center'], 'top');
110
+		$adjacent ['bottom']      = Geohash::adjacent($adjacent ['center'], 'bottom');
111
+		$adjacent ['right']       = Geohash::adjacent($adjacent ['center'], 'right');
112
+		$adjacent ['left']        = Geohash::adjacent($adjacent ['center'], 'left');
113
+		$adjacent ['topleft']     = Geohash::adjacent($adjacent ['left'], 'top');
114
+		$adjacent ['topright']    = Geohash::adjacent($adjacent ['right'], 'top');
115
+		$adjacent ['bottomright'] = Geohash::adjacent($adjacent ['right'], 'bottom');
116
+		$adjacent ['bottomleft']  = Geohash::adjacent($adjacent ['left'], 'bottom');
117
+		dbglog($adjacent, "adjacent geo hashes:");
118 118
 
119
-        // find all the pages in the index that overlap with the adjacent hashes
120
-        $docIds = array();
121
-        foreach($adjacent as $adjHash) {
122
-            if(is_array($this->spatial_idx)) {
123
-                foreach($this->spatial_idx as $_geohash => $_docIds) {
124
-                    if(strpos($_geohash, $adjHash) !== false) {
125
-                        // dbglog ( "Found adjacent geo hash: $adjHash in $_geohash" );
126
-                        // if $adjHash similar to geohash
127
-                        $docIds = array_merge($docIds, $_docIds);
128
-                    }
129
-                }
130
-            }
131
-        }
132
-        $docIds = array_unique($docIds);
133
-        dbglog($docIds, "found docIDs");
119
+		// find all the pages in the index that overlap with the adjacent hashes
120
+		$docIds = array();
121
+		foreach($adjacent as $adjHash) {
122
+			if(is_array($this->spatial_idx)) {
123
+				foreach($this->spatial_idx as $_geohash => $_docIds) {
124
+					if(strpos($_geohash, $adjHash) !== false) {
125
+						// dbglog ( "Found adjacent geo hash: $adjHash in $_geohash" );
126
+						// if $adjHash similar to geohash
127
+						$docIds = array_merge($docIds, $_docIds);
128
+					}
129
+				}
130
+			}
131
+		}
132
+		$docIds = array_unique($docIds);
133
+		dbglog($docIds, "found docIDs");
134 134
 
135
-        // create associative array of pages + calculate distance
136
-        $pages   = array();
137
-        $media   = array();
138
-        $indexer = plugin_load('helper', 'spatialhelper_index');
135
+		// create associative array of pages + calculate distance
136
+		$pages   = array();
137
+		$media   = array();
138
+		$indexer = plugin_load('helper', 'spatialhelper_index');
139 139
 
140
-        foreach($docIds as $id) {
141
-            if(strpos($id, 'media__', 0) === 0) {
142
-                $id = substr($id, strlen('media__'));
143
-                if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
144
-                    $point    = $indexer->getCoordsFromExif($id);
145
-                    $line     = new LineString(
146
-                        [
147
-                            $decodedPoint,
148
-                            $point
149
-                        ]
150
-                    );
151
-                    $media [] = array(
152
-                        'id'       => $id,
153
-                        'distance' => (int) ($line->greatCircleLength()),
154
-                        'lat'      => $point->y(),
155
-                        'lon'      => $point->x()
156
-                        // optionally add other meta such as tag, description...
157
-                    );
158
-                }
159
-            } else {
160
-                if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
161
-                    $geotags  = p_get_metadata($id, 'geo');
162
-                    $point    = new Point($geotags ['lon'], $geotags ['lat']);
163
-                    $line     = new LineString(
164
-                        [
165
-                            $decodedPoint,
166
-                            $point
167
-                        ]
168
-                    );
169
-                    $pages [] = array(
170
-                        'id'          => $id,
171
-                        'distance'    => (int) ($line->greatCircleLength()),
172
-                        'description' => p_get_metadata($id, 'description')['abstract'],
173
-                        'lat'         => $geotags ['lat'],
174
-                        'lon'         => $geotags ['lon']
175
-                        // optionally add other meta such as tag...
176
-                    );
177
-                }
178
-            }
179
-        }
140
+		foreach($docIds as $id) {
141
+			if(strpos($id, 'media__', 0) === 0) {
142
+				$id = substr($id, strlen('media__'));
143
+				if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
144
+					$point    = $indexer->getCoordsFromExif($id);
145
+					$line     = new LineString(
146
+						[
147
+							$decodedPoint,
148
+							$point
149
+						]
150
+					);
151
+					$media [] = array(
152
+						'id'       => $id,
153
+						'distance' => (int) ($line->greatCircleLength()),
154
+						'lat'      => $point->y(),
155
+						'lon'      => $point->x()
156
+						// optionally add other meta such as tag, description...
157
+					);
158
+				}
159
+			} else {
160
+				if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
161
+					$geotags  = p_get_metadata($id, 'geo');
162
+					$point    = new Point($geotags ['lon'], $geotags ['lat']);
163
+					$line     = new LineString(
164
+						[
165
+							$decodedPoint,
166
+							$point
167
+						]
168
+					);
169
+					$pages [] = array(
170
+						'id'          => $id,
171
+						'distance'    => (int) ($line->greatCircleLength()),
172
+						'description' => p_get_metadata($id, 'description')['abstract'],
173
+						'lat'         => $geotags ['lat'],
174
+						'lon'         => $geotags ['lon']
175
+						// optionally add other meta such as tag...
176
+					);
177
+				}
178
+			}
179
+		}
180 180
 
181
-        // sort all the pages/media using distance
182
-        usort(
183
-            $pages, static function ($a, $b) {
184
-            return strnatcmp($a ['distance'], $b ['distance']);
185
-        }
186
-        );
187
-        usort(
188
-            $media, static function ($a, $b) {
189
-            return strnatcmp($a ['distance'], $b ['distance']);
190
-        }
191
-        );
181
+		// sort all the pages/media using distance
182
+		usort(
183
+			$pages, static function ($a, $b) {
184
+			return strnatcmp($a ['distance'], $b ['distance']);
185
+		}
186
+		);
187
+		usort(
188
+			$media, static function ($a, $b) {
189
+			return strnatcmp($a ['distance'], $b ['distance']);
190
+		}
191
+		);
192 192
 
193
-        return array(
194
-            'pages'     => $pages,
195
-            'media'     => $media,
196
-            'lat'       => $decodedPoint->y(),
197
-            'lon'       => $decodedPoint->x(),
198
-            'geohash'   => $geohash,
199
-            'precision' => $this->precision [strlen($geohash)]
200
-        );
201
-    }
193
+		return array(
194
+			'pages'     => $pages,
195
+			'media'     => $media,
196
+			'lat'       => $decodedPoint->y(),
197
+			'lon'       => $decodedPoint->x(),
198
+			'geohash'   => $geohash,
199
+			'precision' => $this->precision [strlen($geohash)]
200
+		);
201
+	}
202 202
 }
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -59,13 +59,13 @@  discard block
 block discarded – undo
59 59
         // parent::__construct ();
60 60
         global $conf;
61 61
 
62
-        if(!plugin_load('helper', 'geophp', false, true)) {
62
+        if (!plugin_load('helper', 'geophp', false, true)) {
63 63
             $message = 'helper_plugin_spatialhelper_search::spatialhelper_search: required geophp plugin is not available.';
64 64
             msg($message, -1);
65 65
         }
66 66
 
67 67
         $idx_dir = $conf ['indexdir'];
68
-        if(!@file_exists($idx_dir . '/spatial.idx')) {
68
+        if (!@file_exists($idx_dir . '/spatial.idx')) {
69 69
             plugin_load('helper', 'spatialhelper_index');
70 70
         }
71 71
 
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
      */
98 98
     public function findNearby(string $geohash, Point $p = null): array {
99 99
         $_geohashClass = new Geohash();
100
-        if(!$p) {
100
+        if (!$p) {
101 101
             $decodedPoint = $_geohashClass->read($geohash);
102 102
         } else {
103 103
             $decodedPoint = $p;
@@ -118,10 +118,10 @@  discard block
 block discarded – undo
118 118
 
119 119
         // find all the pages in the index that overlap with the adjacent hashes
120 120
         $docIds = array();
121
-        foreach($adjacent as $adjHash) {
122
-            if(is_array($this->spatial_idx)) {
123
-                foreach($this->spatial_idx as $_geohash => $_docIds) {
124
-                    if(strpos($_geohash, $adjHash) !== false) {
121
+        foreach ($adjacent as $adjHash) {
122
+            if (is_array($this->spatial_idx)) {
123
+                foreach ($this->spatial_idx as $_geohash => $_docIds) {
124
+                    if (strpos($_geohash, $adjHash) !== false) {
125 125
                         // dbglog ( "Found adjacent geo hash: $adjHash in $_geohash" );
126 126
                         // if $adjHash similar to geohash
127 127
                         $docIds = array_merge($docIds, $_docIds);
@@ -137,10 +137,10 @@  discard block
 block discarded – undo
137 137
         $media   = array();
138 138
         $indexer = plugin_load('helper', 'spatialhelper_index');
139 139
 
140
-        foreach($docIds as $id) {
141
-            if(strpos($id, 'media__', 0) === 0) {
140
+        foreach ($docIds as $id) {
141
+            if (strpos($id, 'media__', 0) === 0) {
142 142
                 $id = substr($id, strlen('media__'));
143
-                if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
143
+                if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
144 144
                     $point    = $indexer->getCoordsFromExif($id);
145 145
                     $line     = new LineString(
146 146
                         [
@@ -150,14 +150,14 @@  discard block
 block discarded – undo
150 150
                     );
151 151
                     $media [] = array(
152 152
                         'id'       => $id,
153
-                        'distance' => (int) ($line->greatCircleLength()),
153
+                        'distance' => ( int ) ($line->greatCircleLength()),
154 154
                         'lat'      => $point->y(),
155 155
                         'lon'      => $point->x()
156 156
                         // optionally add other meta such as tag, description...
157 157
                     );
158 158
                 }
159 159
             } else {
160
-                if(auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
160
+                if (auth_quickaclcheck($id) >= /*AUTH_READ*/ 1) {
161 161
                     $geotags  = p_get_metadata($id, 'geo');
162 162
                     $point    = new Point($geotags ['lon'], $geotags ['lat']);
163 163
                     $line     = new LineString(
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
                     );
169 169
                     $pages [] = array(
170 170
                         'id'          => $id,
171
-                        'distance'    => (int) ($line->greatCircleLength()),
171
+                        'distance'    => ( int ) ($line->greatCircleLength()),
172 172
                         'description' => p_get_metadata($id, 'description')['abstract'],
173 173
                         'lat'         => $geotags ['lat'],
174 174
                         'lon'         => $geotags ['lon']
@@ -180,12 +180,12 @@  discard block
 block discarded – undo
180 180
 
181 181
         // sort all the pages/media using distance
182 182
         usort(
183
-            $pages, static function ($a, $b) {
183
+            $pages, static function($a, $b) {
184 184
             return strnatcmp($a ['distance'], $b ['distance']);
185 185
         }
186 186
         );
187 187
         usort(
188
-            $media, static function ($a, $b) {
188
+            $media, static function($a, $b) {
189 189
             return strnatcmp($a ['distance'], $b ['distance']);
190 190
         }
191 191
         );
Please login to merge, or discard this patch.
helper/sitemap.php 1 patch
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -22,221 +22,221 @@
 block discarded – undo
22 22
  * @author  Mark Prins
23 23
  */
24 24
 class helper_plugin_spatialhelper_sitemap extends DokuWiki_Plugin {
25
-    /**
26
-     * spatial index.
27
-     */
28
-    private $spatial_idx;
29
-
30
-    /**
31
-     * constructor, load spatial index.
32
-     */
33
-    public function __construct() {
34
-        global $conf;
35
-        $idx_dir = $conf['indexdir'];
36
-        if(!@file_exists($idx_dir . '/spatial.idx')) {
37
-            $indexer = plugin_load('helper', 'spatialhelper_index');
38
-            if($indexer !== null) {
39
-                $indexer->generateSpatialIndex();
40
-            }
41
-        }
42
-        $this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
43
-    }
44
-
45
-    final public function getMethods(): array {
46
-        $result[] = array(
47
-            'name'   => 'createGeoRSSSitemap',
48
-            'desc'   => 'create a spatial sitemap in GeoRSS format.',
49
-            'params' => array(
50
-                'path' => 'string'
51
-            ),
52
-            'return' => array(
53
-                'success' => 'boolean'
54
-            )
55
-        );
56
-        $result[] = array(
57
-            'name'   => 'createKMLSitemap',
58
-            'desc'   => 'create a spatial sitemap in KML format.',
59
-            'params' => array(
60
-                'path' => 'string'
61
-            ),
62
-            'return' => array(
63
-                'success' => 'boolean'
64
-            )
65
-        );
66
-        return $result;
67
-    }
68
-
69
-    /**
70
-     * Create a GeoRSS Simple sitemap (Atom).
71
-     *
72
-     * @param string $mediaID id
73
-     *                        for the GeoRSS file
74
-     */
75
-    final public function createGeoRSSSitemap(string $mediaID): bool {
76
-        global $conf;
77
-        $namespace = getNS($mediaID);
78
-
79
-        $idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ',';
80
-
81
-        $RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
82
-        $RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" ';
83
-        $RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF;
84
-        $RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF;
85
-        if(!empty($conf['tagline'])) {
86
-            $RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF;
87
-        }
88
-        $RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF;
89
-        $RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF;
90
-        $RSSstart .= '<link href="' . ml($mediaID, '', true, '&amp;', true) . '" rel="self" />' . DOKU_LF;
91
-        $RSSstart .= '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF;
92
-        $RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH)
93
-            . '</id>' . DOKU_LF;
94
-        $RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF;
95
-
96
-        $RSSend = '</feed>' . DOKU_LF;
97
-
98
-        io_createNamespace($mediaID, 'media');
99
-        @touch(mediaFN($mediaID));
100
-        @chmod(mediaFN($mediaID), $conf['fmode']);
101
-        $fh = fopen(mediaFN($mediaID), 'wb');
102
-        fwrite($fh, $RSSstart);
103
-
104
-        foreach($this->spatial_idx as $idxEntry) {
105
-            // get list of id's
106
-            foreach($idxEntry as $id) {
107
-                // for document item in the index
108
-                if(strpos($id, 'media__', 0) !== 0) {
109
-                    if($this->skipPage($id, $namespace)) {
110
-                        continue;
111
-                    }
112
-
113
-                    $meta = p_get_metadata($id);
114
-
115
-                    // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
116
-                    $desc = strip_tags($meta['description']['abstract']);
117
-
118
-                    $entry = '<entry>' . DOKU_LF;
119
-                    $entry .= '  <title>' . $meta['title'] . '</title>' . DOKU_LF;
120
-                    $entry .= '  <summary>' . $desc . '</summary>' . DOKU_LF;
121
-                    $entry .= '  <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon']
122
-                        . '</georss:point>' . DOKU_LF;
123
-                    if(isset($meta['geo']['alt'])) {
124
-                        $entry .= '  <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF;
125
-                    }
126
-                    $entry .= '  <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF;
127
-                    if(empty($meta['creator'])) {
128
-                        $meta['creator'] = $conf['title'];
129
-                    }
130
-                    $entry .= '  <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF;
131
-                    $entry .= '  <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF;
132
-                    $entry .= '  <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF;
133
-                    $entry .= '  <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':'
134
-                        . parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF;
135
-                    $entry .= '</entry>' . DOKU_LF;
136
-                    fwrite($fh, $entry);
137
-                }
138
-            }
139
-        }
140
-
141
-        fwrite($fh, $RSSend);
142
-        return fclose($fh);
143
-    }
144
-
145
-    /**
146
-     * will return true for non-public or hidden pages or pages that are not below or in the namespace.
147
-     */
148
-    private function skipPage(string $id, string $namespace): bool {
149
-        dbglog("helper_plugin_spatialhelper_sitemap::skipPage, check for $id in $namespace");
150
-        if(isHiddenPage($id)) {
151
-            return true;
152
-        }
153
-        if(auth_aclcheck($id, '', null) < AUTH_READ) {
154
-            return true;
155
-        }
156
-
157
-        if(!empty($namespace)) {
158
-            // only if id is in or below namespace
159
-            if(0 !== strpos(getNS($id), $namespace)) {
160
-                // dbglog("helper_plugin_spatialhelper_sitemap::skipPage, skipping $id, not in $namespace");
161
-                return true;
162
-            }
163
-        }
164
-        return false;
165
-    }
166
-
167
-    /**
168
-     * Create a KML sitemap.
169
-     *
170
-     * @param string $mediaID id for the KML file
171
-     */
172
-    final public function createKMLSitemap(string $mediaID): bool {
173
-        global $conf;
174
-        $namespace = getNS($mediaID);
175
-
176
-        $KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
177
-        $KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" ';
178
-        $KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
179
-        $KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"';
180
-        $KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 ';
181
-        $KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF;
182
-        $KMLstart .= '<Document id="root_doc">' . DOKU_LF;
183
-        $KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF;
184
-        $KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF;
185
-        $KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF;
186
-        $KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>';
187
-        $KMLstart .= '<Icon><href>'
188
-            . DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF;
189
-
190
-        $KMLend = '</Document>' . DOKU_LF . '</kml>';
191
-
192
-        io_createNamespace($mediaID, 'media');
193
-        @touch(mediaFN($mediaID));
194
-        @chmod(mediaFN($mediaID), $conf['fmode']);
195
-
196
-        $fh = fopen(mediaFN($mediaID), 'wb');
197
-        fwrite($fh, $KMLstart);
198
-
199
-        foreach($this->spatial_idx as $idxEntry) {
200
-            // get list of id's
201
-            foreach($idxEntry as $id) {
202
-                // for document item in the index
203
-                if(strpos($id, 'media__', 0) !== 0) {
204
-                    if($this->skipPage($id, $namespace)) {
205
-                        continue;
206
-                    }
207
-
208
-                    $meta = p_get_metadata($id);
209
-
210
-                    // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
211
-                    $desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>';
212
-                    $desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] . '</a></p>';
213
-
214
-                    // create an entry and store it
215
-                    $plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF;
216
-                    $plcm .= '  <name>' . $meta['title'] . '</name>' . DOKU_LF;
217
-                    // TODO escape quotes in: title="' . $meta['title'] . '"
218
-                    $plcm .= '  <atom:link href="' . wl($id, '' . true) . '" rel="alternate" type="text/html" />'
219
-                        . DOKU_LF;
220
-                    if(!empty($meta['creator'])) {
221
-                        $plcm .= '  <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>'
222
-                            . DOKU_LF;
223
-                    }
224
-
225
-                    $plcm .= '  <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF;
226
-                    $plcm .= '  <styleUrl>#icon</styleUrl>' . DOKU_LF;
227
-
228
-                    $plcm .= '  <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat'];
229
-                    if(isset($meta['geo']['alt'])) {
230
-                        $plcm .= ',' . $meta['geo']['alt'];
231
-                    }
232
-                    $plcm .= '</coordinates></Point>' . DOKU_LF;
233
-                    $plcm .= '</Placemark>' . DOKU_LF;
234
-
235
-                    fwrite($fh, $plcm);
236
-                }
237
-            }
238
-        }
239
-        fwrite($fh, $KMLend);
240
-        return fclose($fh);
241
-    }
25
+	/**
26
+	 * spatial index.
27
+	 */
28
+	private $spatial_idx;
29
+
30
+	/**
31
+	 * constructor, load spatial index.
32
+	 */
33
+	public function __construct() {
34
+		global $conf;
35
+		$idx_dir = $conf['indexdir'];
36
+		if(!@file_exists($idx_dir . '/spatial.idx')) {
37
+			$indexer = plugin_load('helper', 'spatialhelper_index');
38
+			if($indexer !== null) {
39
+				$indexer->generateSpatialIndex();
40
+			}
41
+		}
42
+		$this->spatial_idx = unserialize(io_readFile($fn = $idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
43
+	}
44
+
45
+	final public function getMethods(): array {
46
+		$result[] = array(
47
+			'name'   => 'createGeoRSSSitemap',
48
+			'desc'   => 'create a spatial sitemap in GeoRSS format.',
49
+			'params' => array(
50
+				'path' => 'string'
51
+			),
52
+			'return' => array(
53
+				'success' => 'boolean'
54
+			)
55
+		);
56
+		$result[] = array(
57
+			'name'   => 'createKMLSitemap',
58
+			'desc'   => 'create a spatial sitemap in KML format.',
59
+			'params' => array(
60
+				'path' => 'string'
61
+			),
62
+			'return' => array(
63
+				'success' => 'boolean'
64
+			)
65
+		);
66
+		return $result;
67
+	}
68
+
69
+	/**
70
+	 * Create a GeoRSS Simple sitemap (Atom).
71
+	 *
72
+	 * @param string $mediaID id
73
+	 *                        for the GeoRSS file
74
+	 */
75
+	final public function createGeoRSSSitemap(string $mediaID): bool {
76
+		global $conf;
77
+		$namespace = getNS($mediaID);
78
+
79
+		$idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ',';
80
+
81
+		$RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
82
+		$RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" ';
83
+		$RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF;
84
+		$RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF;
85
+		if(!empty($conf['tagline'])) {
86
+			$RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF;
87
+		}
88
+		$RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF;
89
+		$RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF;
90
+		$RSSstart .= '<link href="' . ml($mediaID, '', true, '&amp;', true) . '" rel="self" />' . DOKU_LF;
91
+		$RSSstart .= '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF;
92
+		$RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH)
93
+			. '</id>' . DOKU_LF;
94
+		$RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF;
95
+
96
+		$RSSend = '</feed>' . DOKU_LF;
97
+
98
+		io_createNamespace($mediaID, 'media');
99
+		@touch(mediaFN($mediaID));
100
+		@chmod(mediaFN($mediaID), $conf['fmode']);
101
+		$fh = fopen(mediaFN($mediaID), 'wb');
102
+		fwrite($fh, $RSSstart);
103
+
104
+		foreach($this->spatial_idx as $idxEntry) {
105
+			// get list of id's
106
+			foreach($idxEntry as $id) {
107
+				// for document item in the index
108
+				if(strpos($id, 'media__', 0) !== 0) {
109
+					if($this->skipPage($id, $namespace)) {
110
+						continue;
111
+					}
112
+
113
+					$meta = p_get_metadata($id);
114
+
115
+					// $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
116
+					$desc = strip_tags($meta['description']['abstract']);
117
+
118
+					$entry = '<entry>' . DOKU_LF;
119
+					$entry .= '  <title>' . $meta['title'] . '</title>' . DOKU_LF;
120
+					$entry .= '  <summary>' . $desc . '</summary>' . DOKU_LF;
121
+					$entry .= '  <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon']
122
+						. '</georss:point>' . DOKU_LF;
123
+					if(isset($meta['geo']['alt'])) {
124
+						$entry .= '  <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF;
125
+					}
126
+					$entry .= '  <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF;
127
+					if(empty($meta['creator'])) {
128
+						$meta['creator'] = $conf['title'];
129
+					}
130
+					$entry .= '  <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF;
131
+					$entry .= '  <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF;
132
+					$entry .= '  <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF;
133
+					$entry .= '  <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':'
134
+						. parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF;
135
+					$entry .= '</entry>' . DOKU_LF;
136
+					fwrite($fh, $entry);
137
+				}
138
+			}
139
+		}
140
+
141
+		fwrite($fh, $RSSend);
142
+		return fclose($fh);
143
+	}
144
+
145
+	/**
146
+	 * will return true for non-public or hidden pages or pages that are not below or in the namespace.
147
+	 */
148
+	private function skipPage(string $id, string $namespace): bool {
149
+		dbglog("helper_plugin_spatialhelper_sitemap::skipPage, check for $id in $namespace");
150
+		if(isHiddenPage($id)) {
151
+			return true;
152
+		}
153
+		if(auth_aclcheck($id, '', null) < AUTH_READ) {
154
+			return true;
155
+		}
156
+
157
+		if(!empty($namespace)) {
158
+			// only if id is in or below namespace
159
+			if(0 !== strpos(getNS($id), $namespace)) {
160
+				// dbglog("helper_plugin_spatialhelper_sitemap::skipPage, skipping $id, not in $namespace");
161
+				return true;
162
+			}
163
+		}
164
+		return false;
165
+	}
166
+
167
+	/**
168
+	 * Create a KML sitemap.
169
+	 *
170
+	 * @param string $mediaID id for the KML file
171
+	 */
172
+	final public function createKMLSitemap(string $mediaID): bool {
173
+		global $conf;
174
+		$namespace = getNS($mediaID);
175
+
176
+		$KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF;
177
+		$KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" ';
178
+		$KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
179
+		$KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"';
180
+		$KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 ';
181
+		$KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF;
182
+		$KMLstart .= '<Document id="root_doc">' . DOKU_LF;
183
+		$KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF;
184
+		$KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF;
185
+		$KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF;
186
+		$KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>';
187
+		$KMLstart .= '<Icon><href>'
188
+			. DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF;
189
+
190
+		$KMLend = '</Document>' . DOKU_LF . '</kml>';
191
+
192
+		io_createNamespace($mediaID, 'media');
193
+		@touch(mediaFN($mediaID));
194
+		@chmod(mediaFN($mediaID), $conf['fmode']);
195
+
196
+		$fh = fopen(mediaFN($mediaID), 'wb');
197
+		fwrite($fh, $KMLstart);
198
+
199
+		foreach($this->spatial_idx as $idxEntry) {
200
+			// get list of id's
201
+			foreach($idxEntry as $id) {
202
+				// for document item in the index
203
+				if(strpos($id, 'media__', 0) !== 0) {
204
+					if($this->skipPage($id, $namespace)) {
205
+						continue;
206
+					}
207
+
208
+					$meta = p_get_metadata($id);
209
+
210
+					// $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info);
211
+					$desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>';
212
+					$desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] . '</a></p>';
213
+
214
+					// create an entry and store it
215
+					$plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF;
216
+					$plcm .= '  <name>' . $meta['title'] . '</name>' . DOKU_LF;
217
+					// TODO escape quotes in: title="' . $meta['title'] . '"
218
+					$plcm .= '  <atom:link href="' . wl($id, '' . true) . '" rel="alternate" type="text/html" />'
219
+						. DOKU_LF;
220
+					if(!empty($meta['creator'])) {
221
+						$plcm .= '  <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>'
222
+							. DOKU_LF;
223
+					}
224
+
225
+					$plcm .= '  <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF;
226
+					$plcm .= '  <styleUrl>#icon</styleUrl>' . DOKU_LF;
227
+
228
+					$plcm .= '  <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat'];
229
+					if(isset($meta['geo']['alt'])) {
230
+						$plcm .= ',' . $meta['geo']['alt'];
231
+					}
232
+					$plcm .= '</coordinates></Point>' . DOKU_LF;
233
+					$plcm .= '</Placemark>' . DOKU_LF;
234
+
235
+					fwrite($fh, $plcm);
236
+				}
237
+			}
238
+		}
239
+		fwrite($fh, $KMLend);
240
+		return fclose($fh);
241
+	}
242 242
 }
Please login to merge, or discard this patch.
helper/index.php 2 patches
Indentation   +255 added lines, -255 removed lines patch added patch discarded remove patch
@@ -24,279 +24,279 @@
 block discarded – undo
24 24
  * @author  Mark Prins
25 25
  */
26 26
 class helper_plugin_spatialhelper_index extends DokuWiki_Plugin {
27
-    /**
28
-     * directory for index files.
29
-     *
30
-     * @var string
31
-     */
32
-    protected $idx_dir = '';
27
+	/**
28
+	 * directory for index files.
29
+	 *
30
+	 * @var string
31
+	 */
32
+	protected $idx_dir = '';
33 33
 
34
-    /**
35
-     * spatial index, well lookup list/array so we can do spatial queries.
36
-     * entries should be: array("geohash" => {"id1","id3",})
37
-     *
38
-     * @var array
39
-     */
40
-    protected $spatial_idx = array();
34
+	/**
35
+	 * spatial index, well lookup list/array so we can do spatial queries.
36
+	 * entries should be: array("geohash" => {"id1","id3",})
37
+	 *
38
+	 * @var array
39
+	 */
40
+	protected $spatial_idx = array();
41 41
 
42
-    /**
43
-     * Constructor, initialises the spatial index.
44
-     */
45
-    public function __construct() {
46
-        if(!plugin_load('helper', 'geophp')) {
47
-            $message = 'helper_plugin_spatialhelper_index::spatialhelper_index: required geophp plugin is not available.';
48
-            msg($message, -1);
49
-        }
42
+	/**
43
+	 * Constructor, initialises the spatial index.
44
+	 */
45
+	public function __construct() {
46
+		if(!plugin_load('helper', 'geophp')) {
47
+			$message = 'helper_plugin_spatialhelper_index::spatialhelper_index: required geophp plugin is not available.';
48
+			msg($message, -1);
49
+		}
50 50
 
51
-        global $conf;
52
-        $this->idx_dir = $conf ['indexdir'];
53
-        // test if there is a spatialindex, if not build one for the wiki
54
-        if(!@file_exists($this->idx_dir . '/spatial.idx')) {
55
-            // creates and stores the index
56
-            $this->generateSpatialIndex();
57
-        } else {
58
-            $this->spatial_idx = unserialize(io_readFile($this->idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
59
-            dbglog($this->spatial_idx, 'done loading spatial index');
60
-        }
61
-    }
51
+		global $conf;
52
+		$this->idx_dir = $conf ['indexdir'];
53
+		// test if there is a spatialindex, if not build one for the wiki
54
+		if(!@file_exists($this->idx_dir . '/spatial.idx')) {
55
+			// creates and stores the index
56
+			$this->generateSpatialIndex();
57
+		} else {
58
+			$this->spatial_idx = unserialize(io_readFile($this->idx_dir . '/spatial.idx', false), ['allowed_classes' => false]);
59
+			dbglog($this->spatial_idx, 'done loading spatial index');
60
+		}
61
+	}
62 62
 
63
-    /**
64
-     * (re-)Generates the spatial index by running through all the pages in the wiki.
65
-     *
66
-     * @todo add an option to erase the old index
67
-     */
68
-    public function generateSpatialIndex(): bool {
69
-        global $conf;
70
-        require_once(DOKU_INC . 'inc/search.php');
71
-        $pages = array();
72
-        search($pages, $conf ['datadir'], 'search_allpages', array());
73
-        foreach($pages as $page) {
74
-            $this->updateSpatialIndex($page ['id']);
75
-        }
76
-        // media
77
-        $media = array();
78
-        search($media, $conf ['mediadir'], 'search_media', array());
79
-        foreach($media as $medium) {
80
-            if($medium ['isimg']) {
81
-                $this->indexImage($medium);
82
-            }
83
-        }
84
-        return true;
85
-    }
63
+	/**
64
+	 * (re-)Generates the spatial index by running through all the pages in the wiki.
65
+	 *
66
+	 * @todo add an option to erase the old index
67
+	 */
68
+	public function generateSpatialIndex(): bool {
69
+		global $conf;
70
+		require_once(DOKU_INC . 'inc/search.php');
71
+		$pages = array();
72
+		search($pages, $conf ['datadir'], 'search_allpages', array());
73
+		foreach($pages as $page) {
74
+			$this->updateSpatialIndex($page ['id']);
75
+		}
76
+		// media
77
+		$media = array();
78
+		search($media, $conf ['mediadir'], 'search_media', array());
79
+		foreach($media as $medium) {
80
+			if($medium ['isimg']) {
81
+				$this->indexImage($medium);
82
+			}
83
+		}
84
+		return true;
85
+	}
86 86
 
87
-    /**
88
-     * Update the spatial index for the page.
89
-     *
90
-     * @param string $id
91
-     *          the document ID
92
-     * @throws Exception
93
-     */
94
-    public function updateSpatialIndex(string $id): bool {
95
-        $geotags = p_get_metadata($id, 'geo');
96
-        if(empty ($geotags)) {
97
-            return false;
98
-        }
99
-        if(empty ($geotags ['lon']) || empty ($geotags ['lat'])) {
100
-            return false;
101
-        }
102
-        dbglog($geotags, "Geo metadata found for page $id");
103
-        $geometry = new Point($geotags ['lon'], $geotags ['lat']);
104
-        $geohash  = $geometry->out('geohash');
105
-        dbglog('Update index for geohash: ' . $geohash);
106
-        return $this->addToIndex($geohash, $id);
107
-    }
87
+	/**
88
+	 * Update the spatial index for the page.
89
+	 *
90
+	 * @param string $id
91
+	 *          the document ID
92
+	 * @throws Exception
93
+	 */
94
+	public function updateSpatialIndex(string $id): bool {
95
+		$geotags = p_get_metadata($id, 'geo');
96
+		if(empty ($geotags)) {
97
+			return false;
98
+		}
99
+		if(empty ($geotags ['lon']) || empty ($geotags ['lat'])) {
100
+			return false;
101
+		}
102
+		dbglog($geotags, "Geo metadata found for page $id");
103
+		$geometry = new Point($geotags ['lon'], $geotags ['lat']);
104
+		$geohash  = $geometry->out('geohash');
105
+		dbglog('Update index for geohash: ' . $geohash);
106
+		return $this->addToIndex($geohash, $id);
107
+	}
108 108
 
109
-    /**
110
-     * Store the hash/id entry in the index.
111
-     *
112
-     * @param string $geohash
113
-     * @param string $id
114
-     *          page or media id
115
-     * @return bool true when succesful
116
-     */
117
-    private function addToIndex(string $geohash, string $id): bool {
118
-        $pageIds = array();
119
-        // check index for key/geohash
120
-        if(!array_key_exists($geohash, $this->spatial_idx)) {
121
-            dbglog("Geohash $geohash not in index, just add $id.");
122
-            $pageIds [] = $id;
123
-        } else {
124
-            dbglog('Geohash for document is in index, find it.');
125
-            // check the index for document
126
-            $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
127
-            if(empty ($knownHashes)) {
128
-                dbglog("No index record found for document $id, just add");
129
-                $pageIds    = $this->spatial_idx [$geohash];
130
-                $pageIds [] = $id;
131
-            }
132
-            // TODO shortcut, need to make sure there is only one element, if not the index is corrupt
133
-            $knownHash = $knownHashes [0];
109
+	/**
110
+	 * Store the hash/id entry in the index.
111
+	 *
112
+	 * @param string $geohash
113
+	 * @param string $id
114
+	 *          page or media id
115
+	 * @return bool true when succesful
116
+	 */
117
+	private function addToIndex(string $geohash, string $id): bool {
118
+		$pageIds = array();
119
+		// check index for key/geohash
120
+		if(!array_key_exists($geohash, $this->spatial_idx)) {
121
+			dbglog("Geohash $geohash not in index, just add $id.");
122
+			$pageIds [] = $id;
123
+		} else {
124
+			dbglog('Geohash for document is in index, find it.');
125
+			// check the index for document
126
+			$knownHashes = $this->findHashesForId($id, $this->spatial_idx);
127
+			if(empty ($knownHashes)) {
128
+				dbglog("No index record found for document $id, just add");
129
+				$pageIds    = $this->spatial_idx [$geohash];
130
+				$pageIds [] = $id;
131
+			}
132
+			// TODO shortcut, need to make sure there is only one element, if not the index is corrupt
133
+			$knownHash = $knownHashes [0];
134 134
 
135
-            if($knownHash === $geohash) {
136
-                dbglog("Document $id was found in index and has the same geohash, nothing to do.");
137
-                return true;
138
-            }
135
+			if($knownHash === $geohash) {
136
+				dbglog("Document $id was found in index and has the same geohash, nothing to do.");
137
+				return true;
138
+			}
139 139
 
140
-            if(!empty ($knownHash)) {
141
-                dbglog("Document/media $id was found in index but has different geohash (it moved).");
142
-                $knownIds = $this->spatial_idx [$knownHash];
143
-                dbglog($knownIds, "Known id's for this hash:");
144
-                // remove it from the old geohash element
145
-                $i = array_search($id, $knownIds);
146
-                dbglog('Unsetting:' . $knownIds [$i]);
147
-                unset ($knownIds [$i]);
148
-                $this->spatial_idx [$knownHash] = $knownIds;
149
-                // set on new geohash element
150
-                $pageIds    = $this->spatial_idx [$geohash];
151
-                $pageIds [] = $id;
152
-            }
153
-        }
154
-        // store and save
155
-        $this->spatial_idx [$geohash] = $pageIds;
156
-        return $this->saveIndex();
157
-    }
140
+			if(!empty ($knownHash)) {
141
+				dbglog("Document/media $id was found in index but has different geohash (it moved).");
142
+				$knownIds = $this->spatial_idx [$knownHash];
143
+				dbglog($knownIds, "Known id's for this hash:");
144
+				// remove it from the old geohash element
145
+				$i = array_search($id, $knownIds);
146
+				dbglog('Unsetting:' . $knownIds [$i]);
147
+				unset ($knownIds [$i]);
148
+				$this->spatial_idx [$knownHash] = $knownIds;
149
+				// set on new geohash element
150
+				$pageIds    = $this->spatial_idx [$geohash];
151
+				$pageIds [] = $id;
152
+			}
153
+		}
154
+		// store and save
155
+		$this->spatial_idx [$geohash] = $pageIds;
156
+		return $this->saveIndex();
157
+	}
158 158
 
159
-    /**
160
-     * Looks up the geohash(es) for the document in the index.
161
-     *
162
-     * @param String $id
163
-     *          document ID
164
-     * @param array  $index
165
-     *          spatial index
166
-     */
167
-    public function findHashesForId(string $id, array $index): array {
168
-        $hashes = array();
169
-        foreach($index as $hash => $docIds) {
170
-            if(in_array($id, $docIds, false)) {
171
-                $hashes [] = $hash;
172
-            }
173
-        }
174
-        dbglog($hashes, "Found the following hashes for $id (should only be 1)");
175
-        return $hashes;
176
-    }
159
+	/**
160
+	 * Looks up the geohash(es) for the document in the index.
161
+	 *
162
+	 * @param String $id
163
+	 *          document ID
164
+	 * @param array  $index
165
+	 *          spatial index
166
+	 */
167
+	public function findHashesForId(string $id, array $index): array {
168
+		$hashes = array();
169
+		foreach($index as $hash => $docIds) {
170
+			if(in_array($id, $docIds, false)) {
171
+				$hashes [] = $hash;
172
+			}
173
+		}
174
+		dbglog($hashes, "Found the following hashes for $id (should only be 1)");
175
+		return $hashes;
176
+	}
177 177
 
178
-    /**
179
-     * Save spatial index.
180
-     */
181
-    private function saveIndex(): bool {
182
-        return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx));
183
-    }
178
+	/**
179
+	 * Save spatial index.
180
+	 */
181
+	private function saveIndex(): bool {
182
+		return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx));
183
+	}
184 184
 
185
-    /**
186
-     * Add an index entry for this file having EXIF / IPTC data.
187
-     *
188
-     * @param $img
189
-     *          a Dokuwiki image
190
-     * @return bool true when image was succesfully added to the index.
191
-     * @throws Exception
192
-     * @see http://www.php.net/manual/en/function.iptcparse.php
193
-     * @see http://php.net/manual/en/function.exif-read-data.php
194
-     *
195
-     */
196
-    public function indexImage($img): bool {
197
-        // test for supported files (jpeg only)
198
-        if(
199
-            (substr($img ['file'], -strlen('.jpg')) !== '.jpg') &&
200
-            (substr($img ['file'], -strlen('.jpeg')) !== '.jpeg')) {
201
-            return false;
202
-        }
185
+	/**
186
+	 * Add an index entry for this file having EXIF / IPTC data.
187
+	 *
188
+	 * @param $img
189
+	 *          a Dokuwiki image
190
+	 * @return bool true when image was succesfully added to the index.
191
+	 * @throws Exception
192
+	 * @see http://www.php.net/manual/en/function.iptcparse.php
193
+	 * @see http://php.net/manual/en/function.exif-read-data.php
194
+	 *
195
+	 */
196
+	public function indexImage($img): bool {
197
+		// test for supported files (jpeg only)
198
+		if(
199
+			(substr($img ['file'], -strlen('.jpg')) !== '.jpg') &&
200
+			(substr($img ['file'], -strlen('.jpeg')) !== '.jpeg')) {
201
+			return false;
202
+		}
203 203
 
204
-        $geometry = $this->getCoordsFromExif($img ['id']);
205
-        if(!$geometry) {
206
-            return false;
207
-        }
208
-        $geohash = $geometry->out('geohash');
209
-        // TODO truncate the geohash to something reasonable, otherwise they are
210
-        // useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too
211
-        // precise, limit at ~9 as most GPS are not submeter accurate
212
-        return $this->addToIndex($geohash, 'media__' . $img ['id']);
213
-    }
204
+		$geometry = $this->getCoordsFromExif($img ['id']);
205
+		if(!$geometry) {
206
+			return false;
207
+		}
208
+		$geohash = $geometry->out('geohash');
209
+		// TODO truncate the geohash to something reasonable, otherwise they are
210
+		// useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too
211
+		// precise, limit at ~9 as most GPS are not submeter accurate
212
+		return $this->addToIndex($geohash, 'media__' . $img ['id']);
213
+	}
214 214
 
215
-    /**
216
-     * retrieve GPS decimal coordinates from exif.
217
-     *
218
-     * @param string $id
219
-     * @return Point|false
220
-     * @throws Exception
221
-     */
222
-    public function getCoordsFromExif(string $id) {
223
-        $exif = exif_read_data(mediaFN($id), 0, true);
224
-        if(empty ($exif ['GPS'])) {
225
-            return false;
226
-        }
215
+	/**
216
+	 * retrieve GPS decimal coordinates from exif.
217
+	 *
218
+	 * @param string $id
219
+	 * @return Point|false
220
+	 * @throws Exception
221
+	 */
222
+	public function getCoordsFromExif(string $id) {
223
+		$exif = exif_read_data(mediaFN($id), 0, true);
224
+		if(empty ($exif ['GPS'])) {
225
+			return false;
226
+		}
227 227
 
228
-        $lat = $this->convertDMStoD(
229
-            array(
230
-                $exif ['GPS'] ['GPSLatitude'] [0],
231
-                $exif ['GPS'] ['GPSLatitude'] [1],
232
-                $exif ['GPS'] ['GPSLatitude'] [2],
233
-                $exif ['GPS'] ['GPSLatitudeRef']
234
-            )
235
-        );
228
+		$lat = $this->convertDMStoD(
229
+			array(
230
+				$exif ['GPS'] ['GPSLatitude'] [0],
231
+				$exif ['GPS'] ['GPSLatitude'] [1],
232
+				$exif ['GPS'] ['GPSLatitude'] [2],
233
+				$exif ['GPS'] ['GPSLatitudeRef']
234
+			)
235
+		);
236 236
 
237
-        $lon = $this->convertDMStoD(
238
-            array(
239
-                $exif ['GPS'] ['GPSLongitude'] [0],
240
-                $exif ['GPS'] ['GPSLongitude'] [1],
241
-                $exif ['GPS'] ['GPSLongitude'] [2],
242
-                $exif ['GPS'] ['GPSLongitudeRef']
243
-            )
244
-        );
237
+		$lon = $this->convertDMStoD(
238
+			array(
239
+				$exif ['GPS'] ['GPSLongitude'] [0],
240
+				$exif ['GPS'] ['GPSLongitude'] [1],
241
+				$exif ['GPS'] ['GPSLongitude'] [2],
242
+				$exif ['GPS'] ['GPSLongitudeRef']
243
+			)
244
+		);
245 245
 
246
-        return new Point($lon, $lat);
247
-    }
246
+		return new Point($lon, $lat);
247
+	}
248 248
 
249
-    /**
250
-     * convert DegreesMinutesSeconds to Decimal degrees.
251
-     *
252
-     * @param array $param array of rational DMS
253
-     * @return float
254
-     */
255
-    public function convertDMStoD(array $param): float {
256
-        if(!is_array($param)) {
257
-            $param = array($param);
258
-        }
259
-        $deg = $this->convertRationaltoFloat($param [0]);
260
-        $min = $this->convertRationaltoFloat($param [1]) / 60;
261
-        $sec = $this->convertRationaltoFloat($param [2]) / 60 / 60;
262
-        // Hemisphere (N, S, W or E)
263
-        $hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1;
264
-        return $hem * ($deg + $min + $sec);
265
-    }
249
+	/**
250
+	 * convert DegreesMinutesSeconds to Decimal degrees.
251
+	 *
252
+	 * @param array $param array of rational DMS
253
+	 * @return float
254
+	 */
255
+	public function convertDMStoD(array $param): float {
256
+		if(!is_array($param)) {
257
+			$param = array($param);
258
+		}
259
+		$deg = $this->convertRationaltoFloat($param [0]);
260
+		$min = $this->convertRationaltoFloat($param [1]) / 60;
261
+		$sec = $this->convertRationaltoFloat($param [2]) / 60 / 60;
262
+		// Hemisphere (N, S, W or E)
263
+		$hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1;
264
+		return $hem * ($deg + $min + $sec);
265
+	}
266 266
 
267
-    public function convertRationaltoFloat($param): float {
268
-        // rational64u
269
-        $nums = explode('/', $param);
270
-        if((int) $nums[1] > 0) {
271
-            return (float) $nums[0] / (int) $nums[1];
272
-        } else {
273
-            return (float) $nums[0];
274
-        }
275
-    }
267
+	public function convertRationaltoFloat($param): float {
268
+		// rational64u
269
+		$nums = explode('/', $param);
270
+		if((int) $nums[1] > 0) {
271
+			return (float) $nums[0] / (int) $nums[1];
272
+		} else {
273
+			return (float) $nums[0];
274
+		}
275
+	}
276 276
 
277
-    /**
278
-     * Deletes the page from the index.
279
-     *
280
-     * @param string $id document ID
281
-     */
282
-    public function deleteFromIndex(string $id): void {
283
-        // check the index for document
284
-        $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
285
-        if(empty ($knownHashes)) {
286
-            return;
287
-        }
277
+	/**
278
+	 * Deletes the page from the index.
279
+	 *
280
+	 * @param string $id document ID
281
+	 */
282
+	public function deleteFromIndex(string $id): void {
283
+		// check the index for document
284
+		$knownHashes = $this->findHashesForId($id, $this->spatial_idx);
285
+		if(empty ($knownHashes)) {
286
+			return;
287
+		}
288 288
 
289
-        // TODO shortcut, need to make sure there is only one element, if not the index is corrupt
290
-        $knownHash = $knownHashes [0];
291
-        $knownIds  = $this->spatial_idx [$knownHash];
292
-        $i         = array_search($id, $knownIds);
293
-        dbglog("removing: $knownIds[$i] from the index.");
294
-        unset ($knownIds [$i]);
295
-        $this->spatial_idx [$knownHash] = $knownIds;
296
-        if(empty ($this->spatial_idx [$knownHash])) {
297
-            // dbglog ( "removing key: $knownHash from the index." );
298
-            unset ($this->spatial_idx [$knownHash]);
299
-        }
300
-        $this->saveIndex();
301
-    }
289
+		// TODO shortcut, need to make sure there is only one element, if not the index is corrupt
290
+		$knownHash = $knownHashes [0];
291
+		$knownIds  = $this->spatial_idx [$knownHash];
292
+		$i         = array_search($id, $knownIds);
293
+		dbglog("removing: $knownIds[$i] from the index.");
294
+		unset ($knownIds [$i]);
295
+		$this->spatial_idx [$knownHash] = $knownIds;
296
+		if(empty ($this->spatial_idx [$knownHash])) {
297
+			// dbglog ( "removing key: $knownHash from the index." );
298
+			unset ($this->spatial_idx [$knownHash]);
299
+		}
300
+		$this->saveIndex();
301
+	}
302 302
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
      * Constructor, initialises the spatial index.
44 44
      */
45 45
     public function __construct() {
46
-        if(!plugin_load('helper', 'geophp')) {
46
+        if (!plugin_load('helper', 'geophp')) {
47 47
             $message = 'helper_plugin_spatialhelper_index::spatialhelper_index: required geophp plugin is not available.';
48 48
             msg($message, -1);
49 49
         }
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
         global $conf;
52 52
         $this->idx_dir = $conf ['indexdir'];
53 53
         // test if there is a spatialindex, if not build one for the wiki
54
-        if(!@file_exists($this->idx_dir . '/spatial.idx')) {
54
+        if (!@file_exists($this->idx_dir . '/spatial.idx')) {
55 55
             // creates and stores the index
56 56
             $this->generateSpatialIndex();
57 57
         } else {
@@ -70,14 +70,14 @@  discard block
 block discarded – undo
70 70
         require_once(DOKU_INC . 'inc/search.php');
71 71
         $pages = array();
72 72
         search($pages, $conf ['datadir'], 'search_allpages', array());
73
-        foreach($pages as $page) {
73
+        foreach ($pages as $page) {
74 74
             $this->updateSpatialIndex($page ['id']);
75 75
         }
76 76
         // media
77 77
         $media = array();
78 78
         search($media, $conf ['mediadir'], 'search_media', array());
79
-        foreach($media as $medium) {
80
-            if($medium ['isimg']) {
79
+        foreach ($media as $medium) {
80
+            if ($medium ['isimg']) {
81 81
                 $this->indexImage($medium);
82 82
             }
83 83
         }
@@ -93,10 +93,10 @@  discard block
 block discarded – undo
93 93
      */
94 94
     public function updateSpatialIndex(string $id): bool {
95 95
         $geotags = p_get_metadata($id, 'geo');
96
-        if(empty ($geotags)) {
96
+        if (empty ($geotags)) {
97 97
             return false;
98 98
         }
99
-        if(empty ($geotags ['lon']) || empty ($geotags ['lat'])) {
99
+        if (empty ($geotags ['lon']) || empty ($geotags ['lat'])) {
100 100
             return false;
101 101
         }
102 102
         dbglog($geotags, "Geo metadata found for page $id");
@@ -117,14 +117,14 @@  discard block
 block discarded – undo
117 117
     private function addToIndex(string $geohash, string $id): bool {
118 118
         $pageIds = array();
119 119
         // check index for key/geohash
120
-        if(!array_key_exists($geohash, $this->spatial_idx)) {
120
+        if (!array_key_exists($geohash, $this->spatial_idx)) {
121 121
             dbglog("Geohash $geohash not in index, just add $id.");
122 122
             $pageIds [] = $id;
123 123
         } else {
124 124
             dbglog('Geohash for document is in index, find it.');
125 125
             // check the index for document
126 126
             $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
127
-            if(empty ($knownHashes)) {
127
+            if (empty ($knownHashes)) {
128 128
                 dbglog("No index record found for document $id, just add");
129 129
                 $pageIds    = $this->spatial_idx [$geohash];
130 130
                 $pageIds [] = $id;
@@ -132,12 +132,12 @@  discard block
 block discarded – undo
132 132
             // TODO shortcut, need to make sure there is only one element, if not the index is corrupt
133 133
             $knownHash = $knownHashes [0];
134 134
 
135
-            if($knownHash === $geohash) {
135
+            if ($knownHash === $geohash) {
136 136
                 dbglog("Document $id was found in index and has the same geohash, nothing to do.");
137 137
                 return true;
138 138
             }
139 139
 
140
-            if(!empty ($knownHash)) {
140
+            if (!empty ($knownHash)) {
141 141
                 dbglog("Document/media $id was found in index but has different geohash (it moved).");
142 142
                 $knownIds = $this->spatial_idx [$knownHash];
143 143
                 dbglog($knownIds, "Known id's for this hash:");
@@ -166,8 +166,8 @@  discard block
 block discarded – undo
166 166
      */
167 167
     public function findHashesForId(string $id, array $index): array {
168 168
         $hashes = array();
169
-        foreach($index as $hash => $docIds) {
170
-            if(in_array($id, $docIds, false)) {
169
+        foreach ($index as $hash => $docIds) {
170
+            if (in_array($id, $docIds, false)) {
171 171
                 $hashes [] = $hash;
172 172
             }
173 173
         }
@@ -195,14 +195,14 @@  discard block
 block discarded – undo
195 195
      */
196 196
     public function indexImage($img): bool {
197 197
         // test for supported files (jpeg only)
198
-        if(
198
+        if (
199 199
             (substr($img ['file'], -strlen('.jpg')) !== '.jpg') &&
200 200
             (substr($img ['file'], -strlen('.jpeg')) !== '.jpeg')) {
201 201
             return false;
202 202
         }
203 203
 
204 204
         $geometry = $this->getCoordsFromExif($img ['id']);
205
-        if(!$geometry) {
205
+        if (!$geometry) {
206 206
             return false;
207 207
         }
208 208
         $geohash = $geometry->out('geohash');
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
      */
222 222
     public function getCoordsFromExif(string $id) {
223 223
         $exif = exif_read_data(mediaFN($id), 0, true);
224
-        if(empty ($exif ['GPS'])) {
224
+        if (empty ($exif ['GPS'])) {
225 225
             return false;
226 226
         }
227 227
 
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
      * @return float
254 254
      */
255 255
     public function convertDMStoD(array $param): float {
256
-        if(!is_array($param)) {
256
+        if (!is_array($param)) {
257 257
             $param = array($param);
258 258
         }
259 259
         $deg = $this->convertRationaltoFloat($param [0]);
@@ -267,10 +267,10 @@  discard block
 block discarded – undo
267 267
     public function convertRationaltoFloat($param): float {
268 268
         // rational64u
269 269
         $nums = explode('/', $param);
270
-        if((int) $nums[1] > 0) {
271
-            return (float) $nums[0] / (int) $nums[1];
270
+        if (( int ) $nums[1] > 0) {
271
+            return ( float ) $nums[0] / ( int ) $nums[1];
272 272
         } else {
273
-            return (float) $nums[0];
273
+            return ( float ) $nums[0];
274 274
         }
275 275
     }
276 276
 
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
     public function deleteFromIndex(string $id): void {
283 283
         // check the index for document
284 284
         $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
285
-        if(empty ($knownHashes)) {
285
+        if (empty ($knownHashes)) {
286 286
             return;
287 287
         }
288 288
 
@@ -293,7 +293,7 @@  discard block
 block discarded – undo
293 293
         dbglog("removing: $knownIds[$i] from the index.");
294 294
         unset ($knownIds [$i]);
295 295
         $this->spatial_idx [$knownHash] = $knownIds;
296
-        if(empty ($this->spatial_idx [$knownHash])) {
296
+        if (empty ($this->spatial_idx [$knownHash])) {
297 297
             // dbglog ( "removing key: $knownHash from the index." );
298 298
             unset ($this->spatial_idx [$knownHash]);
299 299
         }
Please login to merge, or discard this patch.