Completed
Pull Request — master (#48)
by Mark
14s
created
_test/index.test.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -14,49 +14,49 @@
 block discarded – undo
14 14
 class index_test extends DokuWikiTest
15 15
 {
16 16
 
17
-    protected $pluginsEnabled = array('spatialhelper');
18
-
19
-    /**
20
-     * Testdata for @return array
21
-     * @see index_test::test_convertDMStoD
22
-     *
23
-     */
24
-    final  public static function convertDMStoDTestdata(): array
25
-    {
26
-        return array(
27
-            array(
28
-                array(0 => '52/1', 1 => '31/1', 2 => '2/1', 3 => 'N',),
29
-                52.5172,
30
-                'Latitude in Europe'
31
-            ),
32
-            array(
33
-                array(0 => '13/1', 1 => '30/1', 2 => '38/1', 3 => 'E',),
34
-                13.5105,
35
-                'Longitude in Europe'
36
-            ),
37
-            array(
38
-                array(0 => '50/1', 1 => '34251480/1000000', 2 => '0/1', 3 => 'N',),
39
-                50.5708,
40
-                'Latitude in North America'
41
-            ),
42
-            array(
43
-                array(0 => '109/1', 1 => '28041300/1000000', 2 => '0/1', 3 => 'W',),
44
-                -109.4673,
45
-                'Longitude in North America'
46
-            ),
47
-        );
48
-    }
49
-
50
-    /**
51
-     * @dataProvider convertDMStoDTestdata
52
-     */
53
-    final  public function test_convertDMStoD(array $input, float $expected_output, string $msg): void
54
-    {
55
-        $index = plugin_load('helper', 'spatialhelper_index');
56
-        assert($index instanceof helper_plugin_spatialhelper_index);
57
-
58
-        $actual_output = $index->convertDMStoD($input);
59
-
60
-        self::assertEqualsWithDelta($expected_output, $actual_output, 0.0001, $msg);
61
-    }
17
+	protected $pluginsEnabled = array('spatialhelper');
18
+
19
+	/**
20
+	 * Testdata for @return array
21
+	 * @see index_test::test_convertDMStoD
22
+	 *
23
+	 */
24
+	final  public static function convertDMStoDTestdata(): array
25
+	{
26
+		return array(
27
+			array(
28
+				array(0 => '52/1', 1 => '31/1', 2 => '2/1', 3 => 'N',),
29
+				52.5172,
30
+				'Latitude in Europe'
31
+			),
32
+			array(
33
+				array(0 => '13/1', 1 => '30/1', 2 => '38/1', 3 => 'E',),
34
+				13.5105,
35
+				'Longitude in Europe'
36
+			),
37
+			array(
38
+				array(0 => '50/1', 1 => '34251480/1000000', 2 => '0/1', 3 => 'N',),
39
+				50.5708,
40
+				'Latitude in North America'
41
+			),
42
+			array(
43
+				array(0 => '109/1', 1 => '28041300/1000000', 2 => '0/1', 3 => 'W',),
44
+				-109.4673,
45
+				'Longitude in North America'
46
+			),
47
+		);
48
+	}
49
+
50
+	/**
51
+	 * @dataProvider convertDMStoDTestdata
52
+	 */
53
+	final  public function test_convertDMStoD(array $input, float $expected_output, string $msg): void
54
+	{
55
+		$index = plugin_load('helper', 'spatialhelper_index');
56
+		assert($index instanceof helper_plugin_spatialhelper_index);
57
+
58
+		$actual_output = $index->convertDMStoD($input);
59
+
60
+		self::assertEqualsWithDelta($expected_output, $actual_output, 0.0001, $msg);
61
+	}
62 62
 }
Please login to merge, or discard this patch.
_test/general.test.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -24,41 +24,41 @@
 block discarded – undo
24 24
 class general_plugin_spatialhelper_test extends DokuWikiTest
25 25
 {
26 26
 
27
-    protected $pluginsEnabled = array('spatialhelper');
27
+	protected $pluginsEnabled = array('spatialhelper');
28 28
 
29
-    /**
30
-     * Simple test to make sure the plugin.info.txt is in correct format.
31
-     */
32
-    final  public function test_plugininfo(): void
33
-    {
34
-        $file = __DIR__ . '/../plugin.info.txt';
35
-        self::assertFileExists($file);
29
+	/**
30
+	 * Simple test to make sure the plugin.info.txt is in correct format.
31
+	 */
32
+	final  public function test_plugininfo(): void
33
+	{
34
+		$file = __DIR__ . '/../plugin.info.txt';
35
+		self::assertFileExists($file);
36 36
 
37
-        $info = confToHash($file);
37
+		$info = confToHash($file);
38 38
 
39
-        self::assertArrayHasKey('base', $info);
40
-        self::assertArrayHasKey('author', $info);
41
-        self::assertArrayHasKey('email', $info);
42
-        self::assertArrayHasKey('date', $info);
43
-        self::assertArrayHasKey('name', $info);
44
-        self::assertArrayHasKey('desc', $info);
45
-        self::assertArrayHasKey('url', $info);
39
+		self::assertArrayHasKey('base', $info);
40
+		self::assertArrayHasKey('author', $info);
41
+		self::assertArrayHasKey('email', $info);
42
+		self::assertArrayHasKey('date', $info);
43
+		self::assertArrayHasKey('name', $info);
44
+		self::assertArrayHasKey('desc', $info);
45
+		self::assertArrayHasKey('url', $info);
46 46
 
47
-        self::assertEquals('spatialhelper', $info['base']);
48
-        self::assertRegExp('/^https?:\/\//', $info['url']);
49
-        self::assertTrue(mail_isvalid($info['email']));
50
-        self::assertRegExp('/^\d\d\d\d-\d\d-\d\d$/', $info['date']);
51
-        self::assertNotFalse(strtotime($info['date']));
52
-    }
47
+		self::assertEquals('spatialhelper', $info['base']);
48
+		self::assertRegExp('/^https?:\/\//', $info['url']);
49
+		self::assertTrue(mail_isvalid($info['email']));
50
+		self::assertRegExp('/^\d\d\d\d-\d\d-\d\d$/', $info['date']);
51
+		self::assertNotFalse(strtotime($info['date']));
52
+	}
53 53
 
54
-    /**
55
-     * test if plugin is loaded.
56
-     */
57
-    final  public function test_plugin_spatialhelper_isloaded(): void
58
-    {
59
-        global $plugin_controller;
60
-        self::assertContains(
61
-            'spatialhelper', $plugin_controller->getList(), "spatialhelper plugin is loaded"
62
-        );
63
-    }
54
+	/**
55
+	 * test if plugin is loaded.
56
+	 */
57
+	final  public function test_plugin_spatialhelper_isloaded(): void
58
+	{
59
+		global $plugin_controller;
60
+		self::assertContains(
61
+			'spatialhelper', $plugin_controller->getList(), "spatialhelper plugin is loaded"
62
+		);
63
+	}
64 64
 }
Please login to merge, or discard this patch.
admin/purge.php 1 patch
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -27,63 +27,63 @@
 block discarded – undo
27 27
  */
28 28
 class admin_plugin_spatialhelper_purge extends AdminPlugin
29 29
 {
30
-    /**
31
-     *
32
-     * @see DokuWiki_Admin_Plugin::getMenuSort()
33
-     */
34
-    final  public function getMenuSort(): int
35
-    {
36
-        return 801;
37
-    }
30
+	/**
31
+	 *
32
+	 * @see DokuWiki_Admin_Plugin::getMenuSort()
33
+	 */
34
+	final  public function getMenuSort(): int
35
+	{
36
+		return 801;
37
+	}
38 38
 
39
-    final  public function getMenuIcon(): string
40
-    {
41
-        $plugin = $this->getPluginName();
42
-        return DOKU_PLUGIN . $plugin . '/admin/purge.svg';
43
-    }
39
+	final  public function getMenuIcon(): string
40
+	{
41
+		$plugin = $this->getPluginName();
42
+		return DOKU_PLUGIN . $plugin . '/admin/purge.svg';
43
+	}
44 44
 
45
-    /**
46
-     * purge and regenerate the index and sitemaps.
47
-     *
48
-     * @see DokuWiki_Admin_Plugin::handle()
49
-     */
50
-    final  public function handle(): void
51
-    {
52
-        if (isset($_REQUEST ['purgeindex'])) {
53
-            global $conf;
54
-            $path = $conf ['indexdir'] . '/spatial.idx';
55
-            if (file_exists($path) && unlink($path)) {
56
-                msg($this->getLang('admin_purged_tiles'), 0);
57
-            }
58
-        }
45
+	/**
46
+	 * purge and regenerate the index and sitemaps.
47
+	 *
48
+	 * @see DokuWiki_Admin_Plugin::handle()
49
+	 */
50
+	final  public function handle(): void
51
+	{
52
+		if (isset($_REQUEST ['purgeindex'])) {
53
+			global $conf;
54
+			$path = $conf ['indexdir'] . '/spatial.idx';
55
+			if (file_exists($path) && unlink($path)) {
56
+				msg($this->getLang('admin_purged_tiles'), 0);
57
+			}
58
+		}
59 59
 
60
-        $indexer = plugin_load('helper', 'spatialhelper_index');
61
-        if (isset($indexer)) {
62
-            $indexer->generateSpatialIndex();
63
-        }
60
+		$indexer = plugin_load('helper', 'spatialhelper_index');
61
+		if (isset($indexer)) {
62
+			$indexer->generateSpatialIndex();
63
+		}
64 64
 
65
-        $sitemapper = plugin_load('helper', 'spatialhelper_sitemap');
66
-        if (isset($sitemapper)) {
67
-            $sitemapper->createKMLSitemap($this->getConf('media_kml'));
68
-            $sitemapper->createGeoRSSSitemap($this->getConf('media_georss'));
69
-        }
70
-    }
65
+		$sitemapper = plugin_load('helper', 'spatialhelper_sitemap');
66
+		if (isset($sitemapper)) {
67
+			$sitemapper->createKMLSitemap($this->getConf('media_kml'));
68
+			$sitemapper->createGeoRSSSitemap($this->getConf('media_georss'));
69
+		}
70
+	}
71 71
 
72
-    /**
73
-     * render the form for this plugin.
74
-     *
75
-     * @see DokuWiki_Admin_Plugin::html()
76
-     */
77
-    final  public function html(): void
78
-    {
79
-        echo $this->locale_xhtml('admin_purge_intro');
72
+	/**
73
+	 * render the form for this plugin.
74
+	 *
75
+	 * @see DokuWiki_Admin_Plugin::html()
76
+	 */
77
+	final  public function html(): void
78
+	{
79
+		echo $this->locale_xhtml('admin_purge_intro');
80 80
 
81
-        $form = new Form(
82
-            ['id' => 'spatialhelper__purgeform', 'method' => 'post']
83
-        );
84
-        $form->setHiddenField('purgeindex', 'true');
85
-        $form->addButton('submit', $this->getLang('admin_submit'))
86
-            ->attr('title', $this->getLang('admin_submit'));
87
-        echo $form->toHTML();
88
-    }
81
+		$form = new Form(
82
+			['id' => 'spatialhelper__purgeform', 'method' => 'post']
83
+		);
84
+		$form->setHiddenField('purgeindex', 'true');
85
+		$form->addButton('submit', $this->getLang('admin_submit'))
86
+			->attr('title', $this->getLang('admin_submit'));
87
+		echo $form->toHTML();
88
+	}
89 89
 }
Please login to merge, or discard this patch.
helper/index.php 2 patches
Indentation   +259 added lines, -259 removed lines patch added patch discarded remove patch
@@ -28,285 +28,285 @@
 block discarded – undo
28 28
  */
29 29
 class helper_plugin_spatialhelper_index extends Plugin
30 30
 {
31
-    /**
32
-     * directory for index files.
33
-     *
34
-     * @var string
35
-     */
36
-    protected string $idx_dir = '';
31
+	/**
32
+	 * directory for index files.
33
+	 *
34
+	 * @var string
35
+	 */
36
+	protected string $idx_dir = '';
37 37
 
38
-    /**
39
-     * spatial index, we'll look up list/array so we can do spatial queries.
40
-     * entries should be: array("geohash" => {"id1","id3",})
41
-     *
42
-     * @var array
43
-     */
44
-    protected array $spatial_idx = [];
38
+	/**
39
+	 * spatial index, we'll look up list/array so we can do spatial queries.
40
+	 * entries should be: array("geohash" => {"id1","id3",})
41
+	 *
42
+	 * @var array
43
+	 */
44
+	protected array $spatial_idx = [];
45 45
 
46
-    /**
47
-     * Constructor, initialises the spatial index.
48
-     * @throws Exception
49
-     */
50
-    public function __construct()
51
-    {
52
-        if (plugin_load('helper', 'geophp') === null) {
53
-            $message = 'helper_plugin_spatialhelper_index::spatialhelper_index: required geophp plugin is not available.';
54
-            msg($message, -1);
55
-        }
46
+	/**
47
+	 * Constructor, initialises the spatial index.
48
+	 * @throws Exception
49
+	 */
50
+	public function __construct()
51
+	{
52
+		if (plugin_load('helper', 'geophp') === null) {
53
+			$message = 'helper_plugin_spatialhelper_index::spatialhelper_index: required geophp plugin is not available.';
54
+			msg($message, -1);
55
+		}
56 56
 
57
-        global $conf;
58
-        $this->idx_dir = $conf ['indexdir'];
59
-        // test if there is a spatialindex, if not build one for the wiki
60
-        if (!@file_exists($this->idx_dir . '/spatial.idx')) {
61
-            // creates and stores the index
62
-            $this->generateSpatialIndex();
63
-        } else {
64
-            $this->spatial_idx = unserialize(
65
-                io_readFile($this->idx_dir . '/spatial.idx', false),
66
-                ['allowed_classes' => false]
67
-            );
68
-            Logger::debug('done loading spatial index', $this->spatial_idx);
69
-        }
70
-    }
57
+		global $conf;
58
+		$this->idx_dir = $conf ['indexdir'];
59
+		// test if there is a spatialindex, if not build one for the wiki
60
+		if (!@file_exists($this->idx_dir . '/spatial.idx')) {
61
+			// creates and stores the index
62
+			$this->generateSpatialIndex();
63
+		} else {
64
+			$this->spatial_idx = unserialize(
65
+				io_readFile($this->idx_dir . '/spatial.idx', false),
66
+				['allowed_classes' => false]
67
+			);
68
+			Logger::debug('done loading spatial index', $this->spatial_idx);
69
+		}
70
+	}
71 71
 
72
-    /**
73
-     * (re-)Generates the spatial index by running through all the pages in the wiki.
74
-     *
75
-     * @throws Exception
76
-     * @todo add an option to erase the old index
77
-     */
78
-    final public function generateSpatialIndex(): bool
79
-    {
80
-        global $conf;
81
-        require_once(DOKU_INC . 'inc/search.php');
82
-        $pages = [];
83
-        search($pages, $conf ['datadir'], 'search_allpages', []);
84
-        foreach ($pages as $page) {
85
-            $this->updateSpatialIndex($page ['id']);
86
-        }
87
-        // media
88
-        $media = [];
89
-        search($media, $conf ['mediadir'], 'search_media', []);
90
-        foreach ($media as $medium) {
91
-            if ($medium ['isimg']) {
92
-                $this->indexImage($medium);
93
-            }
94
-        }
95
-        return true;
96
-    }
72
+	/**
73
+	 * (re-)Generates the spatial index by running through all the pages in the wiki.
74
+	 *
75
+	 * @throws Exception
76
+	 * @todo add an option to erase the old index
77
+	 */
78
+	final public function generateSpatialIndex(): bool
79
+	{
80
+		global $conf;
81
+		require_once(DOKU_INC . 'inc/search.php');
82
+		$pages = [];
83
+		search($pages, $conf ['datadir'], 'search_allpages', []);
84
+		foreach ($pages as $page) {
85
+			$this->updateSpatialIndex($page ['id']);
86
+		}
87
+		// media
88
+		$media = [];
89
+		search($media, $conf ['mediadir'], 'search_media', []);
90
+		foreach ($media as $medium) {
91
+			if ($medium ['isimg']) {
92
+				$this->indexImage($medium);
93
+			}
94
+		}
95
+		return true;
96
+	}
97 97
 
98
-    /**
99
-     * Update the spatial index for the page.
100
-     *
101
-     * @param string $id
102
-     *          the document ID
103
-     * @throws Exception
104
-     */
105
-    final public function updateSpatialIndex(string $id): bool
106
-    {
107
-        $geotags = p_get_metadata($id, 'geo');
108
-        if (empty($geotags)) {
109
-            return false;
110
-        }
111
-        if (empty($geotags ['lon']) || empty($geotags ['lat'])) {
112
-            return false;
113
-        }
114
-        Logger::debug("Geo metadata found for page $id", $geotags);
115
-        $geometry = new Point($geotags ['lon'], $geotags ['lat']);
116
-        $geohash = $geometry->out('geohash');
117
-        Logger::debug('Update index for geohash: ' . $geohash);
118
-        return $this->addToIndex($geohash, $id);
119
-    }
98
+	/**
99
+	 * Update the spatial index for the page.
100
+	 *
101
+	 * @param string $id
102
+	 *          the document ID
103
+	 * @throws Exception
104
+	 */
105
+	final public function updateSpatialIndex(string $id): bool
106
+	{
107
+		$geotags = p_get_metadata($id, 'geo');
108
+		if (empty($geotags)) {
109
+			return false;
110
+		}
111
+		if (empty($geotags ['lon']) || empty($geotags ['lat'])) {
112
+			return false;
113
+		}
114
+		Logger::debug("Geo metadata found for page $id", $geotags);
115
+		$geometry = new Point($geotags ['lon'], $geotags ['lat']);
116
+		$geohash = $geometry->out('geohash');
117
+		Logger::debug('Update index for geohash: ' . $geohash);
118
+		return $this->addToIndex($geohash, $id);
119
+	}
120 120
 
121
-    /**
122
-     * Store the hash/id entry in the index.
123
-     *
124
-     * @param string $geohash
125
-     * @param string $id
126
-     *          page or media id
127
-     * @return bool true when succesful
128
-     */
129
-    private function addToIndex(string $geohash, string $id): bool
130
-    {
131
-        $pageIds = [];
132
-        // check index for key/geohash
133
-        if (!array_key_exists($geohash, $this->spatial_idx)) {
134
-            Logger::debug("Geohash $geohash not in index, just add $id.");
135
-            $pageIds [] = $id;
136
-        } else {
137
-            Logger::debug('Geohash for document is in index, find it.');
138
-            // check the index for document
139
-            $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
140
-            if ($knownHashes === []) {
141
-                Logger::debug("No index record found for document $id, adding it to the index.");
142
-                $pageIds = $this->spatial_idx [$geohash];
143
-                $pageIds [] = $id;
144
-            }
145
-            // TODO shortcut, need to make sure there is only one element, if not the index is corrupt
146
-            $knownHash = $knownHashes [0];
121
+	/**
122
+	 * Store the hash/id entry in the index.
123
+	 *
124
+	 * @param string $geohash
125
+	 * @param string $id
126
+	 *          page or media id
127
+	 * @return bool true when succesful
128
+	 */
129
+	private function addToIndex(string $geohash, string $id): bool
130
+	{
131
+		$pageIds = [];
132
+		// check index for key/geohash
133
+		if (!array_key_exists($geohash, $this->spatial_idx)) {
134
+			Logger::debug("Geohash $geohash not in index, just add $id.");
135
+			$pageIds [] = $id;
136
+		} else {
137
+			Logger::debug('Geohash for document is in index, find it.');
138
+			// check the index for document
139
+			$knownHashes = $this->findHashesForId($id, $this->spatial_idx);
140
+			if ($knownHashes === []) {
141
+				Logger::debug("No index record found for document $id, adding it to the index.");
142
+				$pageIds = $this->spatial_idx [$geohash];
143
+				$pageIds [] = $id;
144
+			}
145
+			// TODO shortcut, need to make sure there is only one element, if not the index is corrupt
146
+			$knownHash = $knownHashes [0];
147 147
 
148
-            if ($knownHash === $geohash) {
149
-                Logger::debug("Document $id was found in index and has the same geohash, nothing to do.");
150
-                return true;
151
-            }
148
+			if ($knownHash === $geohash) {
149
+				Logger::debug("Document $id was found in index and has the same geohash, nothing to do.");
150
+				return true;
151
+			}
152 152
 
153
-            if (!empty($knownHash)) {
154
-                Logger::debug("Document/media $id was found in index but has different geohash (it moved).");
155
-                $knownIds = $this->spatial_idx [$knownHash];
156
-                Logger::debug("Known id's for this hash:", $knownIds);
157
-                // remove it from the old geohash element
158
-                $i = array_search($id, $knownIds);
159
-                Logger::debug('Unsetting:' . $knownIds [$i]);
160
-                unset($knownIds [$i]);
161
-                $this->spatial_idx [$knownHash] = $knownIds;
162
-                // set on new geohash element
163
-                $pageIds = $this->spatial_idx[$geohash];
164
-                $pageIds [] = $id;
165
-            }
166
-        }
167
-        // store and save
168
-        $this->spatial_idx [$geohash] = $pageIds;
169
-        return $this->saveIndex();
170
-    }
153
+			if (!empty($knownHash)) {
154
+				Logger::debug("Document/media $id was found in index but has different geohash (it moved).");
155
+				$knownIds = $this->spatial_idx [$knownHash];
156
+				Logger::debug("Known id's for this hash:", $knownIds);
157
+				// remove it from the old geohash element
158
+				$i = array_search($id, $knownIds);
159
+				Logger::debug('Unsetting:' . $knownIds [$i]);
160
+				unset($knownIds [$i]);
161
+				$this->spatial_idx [$knownHash] = $knownIds;
162
+				// set on new geohash element
163
+				$pageIds = $this->spatial_idx[$geohash];
164
+				$pageIds [] = $id;
165
+			}
166
+		}
167
+		// store and save
168
+		$this->spatial_idx [$geohash] = $pageIds;
169
+		return $this->saveIndex();
170
+	}
171 171
 
172
-    /**
173
-     * Looks up the geohash(es) for the document in the index.
174
-     *
175
-     * @param String $id
176
-     *          document ID
177
-     * @param array $index
178
-     *          spatial index
179
-     */
180
-    final public function findHashesForId(string $id, array $index): array
181
-    {
182
-        $hashes = [];
183
-        foreach ($index as $hash => $docIds) {
184
-            if (in_array($id, $docIds)) {
185
-                $hashes [] = $hash;
186
-            }
187
-        }
188
-        Logger::debug("Found the following hashes for $id (should only be 1)", $hashes);
189
-        return $hashes;
190
-    }
172
+	/**
173
+	 * Looks up the geohash(es) for the document in the index.
174
+	 *
175
+	 * @param String $id
176
+	 *          document ID
177
+	 * @param array $index
178
+	 *          spatial index
179
+	 */
180
+	final public function findHashesForId(string $id, array $index): array
181
+	{
182
+		$hashes = [];
183
+		foreach ($index as $hash => $docIds) {
184
+			if (in_array($id, $docIds)) {
185
+				$hashes [] = $hash;
186
+			}
187
+		}
188
+		Logger::debug("Found the following hashes for $id (should only be 1)", $hashes);
189
+		return $hashes;
190
+	}
191 191
 
192
-    /**
193
-     * Save spatial index.
194
-     */
195
-    private function saveIndex(): bool
196
-    {
197
-        return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx));
198
-    }
192
+	/**
193
+	 * Save spatial index.
194
+	 */
195
+	private function saveIndex(): bool
196
+	{
197
+		return io_saveFile($this->idx_dir . '/spatial.idx', serialize($this->spatial_idx));
198
+	}
199 199
 
200
-    /**
201
-     * Add an index entry for this file having EXIF / IPTC data.
202
-     *
203
-     * @param $img
204
-     *          a Dokuwiki image
205
-     * @return bool true when image was succesfully added to the index.
206
-     * @throws Exception
207
-     * @see http://www.php.net/manual/en/function.iptcparse.php
208
-     * @see http://php.net/manual/en/function.exif-read-data.php
209
-     *
210
-     */
211
-    final  public function indexImage(array $img): bool
212
-    {
213
-        // test for supported files (jpeg only)
214
-        if (
215
-            (!str_ends_with($img ['file'], '.jpg')) &&
216
-            (!str_ends_with($img ['file'], '.jpeg'))
217
-        ) {
218
-            return false;
219
-        }
200
+	/**
201
+	 * Add an index entry for this file having EXIF / IPTC data.
202
+	 *
203
+	 * @param $img
204
+	 *          a Dokuwiki image
205
+	 * @return bool true when image was succesfully added to the index.
206
+	 * @throws Exception
207
+	 * @see http://www.php.net/manual/en/function.iptcparse.php
208
+	 * @see http://php.net/manual/en/function.exif-read-data.php
209
+	 *
210
+	 */
211
+	final  public function indexImage(array $img): bool
212
+	{
213
+		// test for supported files (jpeg only)
214
+		if (
215
+			(!str_ends_with($img ['file'], '.jpg')) &&
216
+			(!str_ends_with($img ['file'], '.jpeg'))
217
+		) {
218
+			return false;
219
+		}
220 220
 
221
-        $geometry = $this->getCoordsFromExif($img ['id']);
222
-        if (!$geometry) {
223
-            return false;
224
-        }
225
-        $geohash = $geometry->out('geohash');
226
-        // TODO truncate the geohash to something reasonable, otherwise they are
227
-        // useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too
228
-        // precise, limit at ~9 as most GPS are not submeter accurate
229
-        return $this->addToIndex($geohash, 'media__' . $img ['id']);
230
-    }
221
+		$geometry = $this->getCoordsFromExif($img ['id']);
222
+		if (!$geometry) {
223
+			return false;
224
+		}
225
+		$geohash = $geometry->out('geohash');
226
+		// TODO truncate the geohash to something reasonable, otherwise they are
227
+		// useless as an indexing mechanism eg. u1h73weckdrmskdqec3c9 is far too
228
+		// precise, limit at ~9 as most GPS are not submeter accurate
229
+		return $this->addToIndex($geohash, 'media__' . $img ['id']);
230
+	}
231 231
 
232
-    /**
233
-     * retrieve GPS decimal coordinates from exif.
234
-     *
235
-     * @param string $id
236
-     * @return Point|false
237
-     * @throws Exception
238
-     */
239
-    final  public function getCoordsFromExif(string $id): Point|false
240
-    {
241
-        $exif = exif_read_data(mediaFN($id), 0, true);
242
-        if (empty($exif ['GPS'])) {
243
-            return false;
244
-        }
232
+	/**
233
+	 * retrieve GPS decimal coordinates from exif.
234
+	 *
235
+	 * @param string $id
236
+	 * @return Point|false
237
+	 * @throws Exception
238
+	 */
239
+	final  public function getCoordsFromExif(string $id): Point|false
240
+	{
241
+		$exif = exif_read_data(mediaFN($id), 0, true);
242
+		if (empty($exif ['GPS'])) {
243
+			return false;
244
+		}
245 245
 
246
-        $lat = $this->convertDMStoD(
247
-            [$exif ['GPS'] ['GPSLatitude'] [0], $exif ['GPS'] ['GPSLatitude'] [1], $exif ['GPS'] ['GPSLatitude'] [2], $exif ['GPS'] ['GPSLatitudeRef']]
248
-        );
246
+		$lat = $this->convertDMStoD(
247
+			[$exif ['GPS'] ['GPSLatitude'] [0], $exif ['GPS'] ['GPSLatitude'] [1], $exif ['GPS'] ['GPSLatitude'] [2], $exif ['GPS'] ['GPSLatitudeRef']]
248
+		);
249 249
 
250
-        $lon = $this->convertDMStoD(
251
-            [$exif ['GPS'] ['GPSLongitude'] [0], $exif ['GPS'] ['GPSLongitude'] [1], $exif ['GPS'] ['GPSLongitude'] [2], $exif ['GPS'] ['GPSLongitudeRef']]
252
-        );
250
+		$lon = $this->convertDMStoD(
251
+			[$exif ['GPS'] ['GPSLongitude'] [0], $exif ['GPS'] ['GPSLongitude'] [1], $exif ['GPS'] ['GPSLongitude'] [2], $exif ['GPS'] ['GPSLongitudeRef']]
252
+		);
253 253
 
254
-        return new Point($lon, $lat);
255
-    }
254
+		return new Point($lon, $lat);
255
+	}
256 256
 
257
-    /**
258
-     * convert DegreesMinutesSeconds to Decimal degrees.
259
-     *
260
-     * @param array $param array of rational DMS
261
-     */
262
-    final  public function convertDMStoD(array $param): float
263
-    {
264
-    //        if (!(is_array($param))) {
265
-    //            $param = [$param];
266
-    //        }
267
-        $deg = $this->convertRationaltoFloat($param [0]);
268
-        $min = $this->convertRationaltoFloat($param [1]) / 60;
269
-        $sec = $this->convertRationaltoFloat($param [2]) / 60 / 60;
270
-        // Hemisphere (N, S, W or E)
271
-        $hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1;
257
+	/**
258
+	 * convert DegreesMinutesSeconds to Decimal degrees.
259
+	 *
260
+	 * @param array $param array of rational DMS
261
+	 */
262
+	final  public function convertDMStoD(array $param): float
263
+	{
264
+	//        if (!(is_array($param))) {
265
+	//            $param = [$param];
266
+	//        }
267
+		$deg = $this->convertRationaltoFloat($param [0]);
268
+		$min = $this->convertRationaltoFloat($param [1]) / 60;
269
+		$sec = $this->convertRationaltoFloat($param [2]) / 60 / 60;
270
+		// Hemisphere (N, S, W or E)
271
+		$hem = ($param [3] === 'N' || $param [3] === 'E') ? 1 : -1;
272 272
 
273
-        return $hem * ($deg + $min + $sec);
274
-    }
273
+		return $hem * ($deg + $min + $sec);
274
+	}
275 275
 
276
-    final public function convertRationaltoFloat(string $param): float
277
-    {
278
-        // rational64u
279
-        $nums = explode('/', $param);
280
-        if ((int)$nums[1] > 0) {
281
-            return (float)$nums[0] / (int)$nums[1];
282
-        }
276
+	final public function convertRationaltoFloat(string $param): float
277
+	{
278
+		// rational64u
279
+		$nums = explode('/', $param);
280
+		if ((int)$nums[1] > 0) {
281
+			return (float)$nums[0] / (int)$nums[1];
282
+		}
283 283
 
284
-        return (float)$nums[0];
285
-    }
284
+		return (float)$nums[0];
285
+	}
286 286
 
287
-    /**
288
-     * Deletes the page from the index.
289
-     *
290
-     * @param string $id document ID
291
-     */
292
-    final public function deleteFromIndex(string $id): void
293
-    {
294
-        // check the index for document
295
-        $knownHashes = $this->findHashesForId($id, $this->spatial_idx);
296
-        if ($knownHashes === []) {
297
-            return;
298
-        }
287
+	/**
288
+	 * Deletes the page from the index.
289
+	 *
290
+	 * @param string $id document ID
291
+	 */
292
+	final public function deleteFromIndex(string $id): void
293
+	{
294
+		// check the index for document
295
+		$knownHashes = $this->findHashesForId($id, $this->spatial_idx);
296
+		if ($knownHashes === []) {
297
+			return;
298
+		}
299 299
 
300
-        // TODO shortcut, need to make sure there is only one element, if not the index is corrupt
301
-        $knownHash = $knownHashes [0];
302
-        $knownIds = $this->spatial_idx [$knownHash];
303
-        $i = array_search($id, $knownIds);
304
-        Logger::debug("removing: $knownIds[$i] from the index.");
305
-        unset($knownIds [$i]);
306
-        $this->spatial_idx [$knownHash] = $knownIds;
307
-        if (empty($this->spatial_idx [$knownHash])) {
308
-            unset($this->spatial_idx [$knownHash]);
309
-        }
310
-        $this->saveIndex();
311
-    }
300
+		// TODO shortcut, need to make sure there is only one element, if not the index is corrupt
301
+		$knownHash = $knownHashes [0];
302
+		$knownIds = $this->spatial_idx [$knownHash];
303
+		$i = array_search($id, $knownIds);
304
+		Logger::debug("removing: $knownIds[$i] from the index.");
305
+		unset($knownIds [$i]);
306
+		$this->spatial_idx [$knownHash] = $knownIds;
307
+		if (empty($this->spatial_idx [$knownHash])) {
308
+			unset($this->spatial_idx [$knownHash]);
309
+		}
310
+		$this->saveIndex();
311
+	}
312 312
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
      * @return Point|false
237 237
      * @throws Exception
238 238
      */
239
-    final  public function getCoordsFromExif(string $id): Point|false
239
+    final  public function getCoordsFromExif(string $id): Point | false
240 240
     {
241 241
         $exif = exif_read_data(mediaFN($id), 0, true);
242 242
         if (empty($exif ['GPS'])) {
@@ -277,11 +277,11 @@  discard block
 block discarded – undo
277 277
     {
278 278
         // rational64u
279 279
         $nums = explode('/', $param);
280
-        if ((int)$nums[1] > 0) {
281
-            return (float)$nums[0] / (int)$nums[1];
280
+        if (( int ) $nums[1] > 0) {
281
+            return ( float ) $nums[0] / ( int ) $nums[1];
282 282
         }
283 283
 
284
-        return (float)$nums[0];
284
+        return ( float ) $nums[0];
285 285
     }
286 286
 
287 287
     /**
Please login to merge, or discard this patch.