From 291085e661fb62186452143339360dba9fd5c05c Mon Sep 17 00:00:00 2001 From: Lukas Bestle Date: Fri, 15 Jul 2016 16:40:09 +0200 Subject: [PATCH] Fix #1: Panel indexing widget --- algolia.php | 20 ++++++++++++++++++++ lib/algolia.php | 9 +++++++++ readme.md | 10 ++++++++++ widgets/algolia/algolia.html.php | 3 +++ widgets/algolia/algolia.php | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 widgets/algolia/algolia.html.php create mode 100644 widgets/algolia/algolia.php diff --git a/algolia.php b/algolia.php index 4c19b41..b69d995 100755 --- a/algolia.php +++ b/algolia.php @@ -26,6 +26,26 @@ function algolia() { return Kirby\Algolia::instance(); } +// Register the Panel widget +if(c::get('algolia.widget', true) && function_exists('panel')) { + $kirby->set('widget', 'algolia', __DIR__ . DS . 'widgets' . DS . 'algolia'); + + // Register the route for the widget + panel()->routes([ + [ + 'pattern' => 'widgets/algolia/index', + 'method' => 'GET', + 'filter' => 'auth', + 'action' => function() { + algolia()->index(); + + panel()->notify(':)'); + panel()->redirect('/'); + } + ] + ]); +} + /** * Panel hooks * diff --git a/lib/algolia.php b/lib/algolia.php index 40109c8..d139ffc 100755 --- a/lib/algolia.php +++ b/lib/algolia.php @@ -265,6 +265,15 @@ public static function formatPage(Page $page) { return $data; } + /** + * Returns the number of indexable pages + * + * @return int + */ + public function objectCount() { + return site()->index()->filter(array($this, 'isIndexable'))->count(); + } + /** * Returns an array of data to send to Algolia * Includes all whitelisted pages and formats their data according to the configuration diff --git a/readme.md b/readme.md index e0157bb..9186569 100755 --- a/readme.md +++ b/readme.md @@ -105,6 +105,14 @@ If you only want to use manual indexing, you can disable the automatic indexing c::set('algolia.autoindex', false); ``` +#### Indexing widget + +The Algolia plugin includes a Panel widget that allows Panel users to manually index the site. You can disable this widget with the following option: + +```php +c::set('algolia.widget', false); +``` + ### Search options Algolia has [many search options](https://www.algolia.com/doc/php#full-text-search-parameters) to fine-tune the search results. You can set these in your configuration like this: @@ -130,6 +138,8 @@ algolia()->index(); This will create a new temporary index, upload all indexable pages and replace the main index with the temporary index. Please note that manual indexing will use roughly as many Algolia "Operations" as you have indexable pages each time you call the `index` method. The amount of included/free "Operations" per month depends on your Algolia plan. +There is also a Panel widget for this that is enabled by default. + ## Search The plugin also provides a method to query/search the index from the backend. It is generally recommended to use [Algolia's JavaScript library](https://www.algolia.com/doc/javascript#quick-start) to avoid the round-trip to your server, but you should also have a server-side fallback results page, which you can implement using the `search` method: diff --git a/widgets/algolia/algolia.html.php b/widgets/algolia/algolia.html.php new file mode 100644 index 0000000..caa3518 --- /dev/null +++ b/widgets/algolia/algolia.html.php @@ -0,0 +1,3 @@ +
+
Number of indexable pages:
+
diff --git a/widgets/algolia/algolia.php b/widgets/algolia/algolia.php new file mode 100644 index 0000000..7634ade --- /dev/null +++ b/widgets/algolia/algolia.php @@ -0,0 +1,32 @@ + + * @license MIT + * @link https://getkirby.com + */ + +return array( + 'title' => [ + 'text' => 'Search Index', + 'compressed' => false + ], + 'options' => [ + [ + 'text' => 'Manual Refresh', + 'icon' => 'refresh', + 'link' => purl('widgets/algolia/index') + ] + ], + 'html' => function() { + $count = algolia()->objectCount(); + return tpl::load(__DIR__ . DS . 'algolia.html.php', compact('count')); + } +);