From d565a44ec902b7011ce852bc18d8c2522a885aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B4me=20Bakker?= Date: Tue, 18 Jun 2024 13:33:38 +0200 Subject: [PATCH] changed: updated for Elgg 6 --- .github/workflows/phpunit.yml | 4 ++- README.md | 2 +- .../ColdTrick/OpenSearch/Di/DeleteQueue.php | 6 ++-- .../OpenSearch/Di/IndexingService.php | 2 +- .../OpenSearch/Menus/AdminHeader.php | 2 +- classes/ColdTrick/OpenSearch/SearchEvents.php | 4 +-- classes/ColdTrick/OpenSearch/SearchParams.php | 8 ++--- .../OpenSearch/SearchParams/Initialize.php | 2 +- classes/ColdTrick/OpenSearch/SearchResult.php | 12 ++------ composer.json | 2 +- composer.lock | 28 +++++++++--------- lib/functions.php | 23 ++++++++------- views/default/admin/opensearch/search.php | 1 + .../default/forms/opensearch/admin_search.js | 14 --------- .../default/forms/opensearch/admin_search.mjs | 15 ++++++++++ .../default/forms/opensearch/admin_search.php | 29 ++++++++++++------- views/default/opensearch/stats/elgg.php | 9 +++--- views/default/plugins/opensearch/settings.php | 4 +-- 18 files changed, 85 insertions(+), 82 deletions(-) delete mode 100644 views/default/forms/opensearch/admin_search.js create mode 100644 views/default/forms/opensearch/admin_search.mjs diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 67fe092..6147873 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -3,6 +3,8 @@ name: PHPUnit Plugin Tests on: [push, pull_request] jobs: - lint: + phpunit: name: Run PHPUnit test suites uses: ColdTrick/.github/.github/workflows/phpunit.yml@master + with: + elgg_major_version: 6 diff --git a/README.md b/README.md index b658234..0d625c7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # OpenSearch -![Elgg 5.1](https://img.shields.io/badge/Elgg-5.1-green.svg) +![Elgg 6.0](https://img.shields.io/badge/Elgg-6.0-green.svg) ![OpenSearch 2.5](https://img.shields.io/badge/OpenSearch-2.5-green.svg) ![Lint Checks](https://github.com/ColdTrick/opensearch/actions/workflows/lint.yml/badge.svg?event=push) [![Latest Stable Version](https://poser.pugx.org/coldtrick/opensearch/v/stable.svg)](https://packagist.org/packages/coldtrick/opensearch) diff --git a/classes/ColdTrick/OpenSearch/Di/DeleteQueue.php b/classes/ColdTrick/OpenSearch/Di/DeleteQueue.php index 6605d20..a626167 100644 --- a/classes/ColdTrick/OpenSearch/Di/DeleteQueue.php +++ b/classes/ColdTrick/OpenSearch/Di/DeleteQueue.php @@ -37,7 +37,7 @@ public static function name(): string { * * @param int $number number of items to dequeue */ - public function dequeue(int $number = 100) { + public function dequeue(int $number = 100): ?array { // get a record for processing $select = Select::fromTable(self::TABLE_NAME); $select->select('*') @@ -49,7 +49,7 @@ public function dequeue(int $number = 100) { $rows = $this->db->getData($select); if (empty($rows)) { - return; + return null; } $ids = []; @@ -65,7 +65,7 @@ public function dequeue(int $number = 100) { ->andWhere($update->expr()->isNull('worker')); if ($this->db->updateData($update, true) < 1) { - return; + return null; } // remove locked record from database diff --git a/classes/ColdTrick/OpenSearch/Di/IndexingService.php b/classes/ColdTrick/OpenSearch/Di/IndexingService.php index efa32b9..0f92858 100644 --- a/classes/ColdTrick/OpenSearch/Di/IndexingService.php +++ b/classes/ColdTrick/OpenSearch/Di/IndexingService.php @@ -124,7 +124,7 @@ public function bulkDeleteDocuments(): bool { // document was removed opensearch_remove_document_for_deletion($guid); } else { - // some error occured, reschedule delete + // some error occurred, reschedule delete opensearch_add_document_for_deletion($guid, $documents[$guid], '+1 hour'); } } diff --git a/classes/ColdTrick/OpenSearch/Menus/AdminHeader.php b/classes/ColdTrick/OpenSearch/Menus/AdminHeader.php index 159c797..67d2875 100644 --- a/classes/ColdTrick/OpenSearch/Menus/AdminHeader.php +++ b/classes/ColdTrick/OpenSearch/Menus/AdminHeader.php @@ -12,7 +12,7 @@ class AdminHeader { /** * Add menu items to the admin page menu * - * @param \Elgg\Event $event 'register', 'menu:page' + * @param \Elgg\Event $event 'register', 'menu:admin_header' * * @return null|MenuItems */ diff --git a/classes/ColdTrick/OpenSearch/SearchEvents.php b/classes/ColdTrick/OpenSearch/SearchEvents.php index 9ca2c6d..0a52c89 100644 --- a/classes/ColdTrick/OpenSearch/SearchEvents.php +++ b/classes/ColdTrick/OpenSearch/SearchEvents.php @@ -537,7 +537,7 @@ protected static function detectUnsupportedSearchParams(array $params): bool { if (!elgg_in_context('search')) { // make sure all supplied type_subtype_pairs are supported for search - // if a non supported type/subtype is found don't handle the search through OpenSearch + // if a non-supported type/subtype is found don't handle the search through OpenSearch // if not in the search context $temp = new SearchParams(); $normalized_params = $temp->normalizeOptions($params); @@ -577,7 +577,7 @@ protected static function transformSearchResults(SearchResult $result, array $ev return $result->getCount(); } - return $result->toEntities($event_params); + return $result->toEntities(); } /** diff --git a/classes/ColdTrick/OpenSearch/SearchParams.php b/classes/ColdTrick/OpenSearch/SearchParams.php index 8035988..d89a362 100644 --- a/classes/ColdTrick/OpenSearch/SearchParams.php +++ b/classes/ColdTrick/OpenSearch/SearchParams.php @@ -133,7 +133,7 @@ public function getBody(bool $count = false): array { * * @return mixed */ - protected function getParam($name, $default = null) { + protected function getParam(string $name, mixed $default = null): mixed { if (!isset($this->params[$name])) { return $default; } @@ -259,7 +259,7 @@ public function setNoMatchFilter(array $filter): void { * * @return null|array */ - public function getNoMatchFilter(): array { + public function getNoMatchFilter(): ?array { return $this->getParam('no_match_filter'); } @@ -324,7 +324,7 @@ public function setSort(array $sort = []): void { * * @return void */ - public function addSort(string $field, $sort_config = []): void { + public function addSort(string $field, array $sort_config = []): void { if (empty($field)) { return; } @@ -519,7 +519,7 @@ public function getAggregation(): ?array { /** * Set suggestion params for search * - * @param string $query search query + * @param null|string $query search query * * @return void */ diff --git a/classes/ColdTrick/OpenSearch/SearchParams/Initialize.php b/classes/ColdTrick/OpenSearch/SearchParams/Initialize.php index e864be6..f0d4029 100644 --- a/classes/ColdTrick/OpenSearch/SearchParams/Initialize.php +++ b/classes/ColdTrick/OpenSearch/SearchParams/Initialize.php @@ -105,7 +105,7 @@ protected function getQueryFields(array $search_params = []): array { break; case 'annotations': // support user profile fields - if (strpos($name, 'profile:') === 0) { + if (str_starts_with($name, 'profile:')) { $name = substr($name, strlen('profile:')); $result[] = "metadata.{$name}"; break; diff --git a/classes/ColdTrick/OpenSearch/SearchResult.php b/classes/ColdTrick/OpenSearch/SearchResult.php index 1ddc483..400aa68 100644 --- a/classes/ColdTrick/OpenSearch/SearchResult.php +++ b/classes/ColdTrick/OpenSearch/SearchResult.php @@ -7,19 +7,13 @@ */ class SearchResult { - protected array $result; - - protected array $search_params; - /** * Create a new SearchResults helper * * @param array $result results from OpenSearch * @param array $search_params original search params */ - public function __construct(array $result, array $search_params) { - $this->result = $result; - $this->search_params = $search_params; + public function __construct(protected array $result, protected array $search_params) { } /** @@ -100,11 +94,9 @@ public function getSuggestions(): array { /** * Convert search results to entities * - * @param array $params additional params - * * @return \ElggEntity[] */ - public function toEntities($params): array { + public function toEntities(): array { $hits = $this->getHits(); if (empty($hits)) { return []; diff --git a/composer.json b/composer.json index fdc8565..8d6f370 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,6 @@ "opensearch-project/opensearch-php": "^2.0.0" }, "conflict": { - "elgg/elgg": "<5.1" + "elgg/elgg": "<6.0" } } diff --git a/composer.lock b/composer.lock index 3c5f78c..6d3287c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1bb8ab36295c9acac4d8dd1d116adf45", + "content-hash": "7696eb731186c300e52d1b0dd794cddc", "packages": [ { "name": "ezimuel/guzzlestreams", @@ -118,16 +118,16 @@ }, { "name": "opensearch-project/opensearch-php", - "version": "2.2.0", + "version": "2.3.0", "source": { "type": "git", "url": "https://github.com/opensearch-project/opensearch-php.git", - "reference": "8b6cdbce1c5c2436fa2458fc1c9f698d0fc760c7" + "reference": "b9dacd11f14460656aaceb3bae00287a23beef91" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opensearch-project/opensearch-php/zipball/8b6cdbce1c5c2436fa2458fc1c9f698d0fc760c7", - "reference": "8b6cdbce1c5c2436fa2458fc1c9f698d0fc760c7", + "url": "https://api.github.com/repos/opensearch-project/opensearch-php/zipball/b9dacd11f14460656aaceb3bae00287a23beef91", + "reference": "b9dacd11f14460656aaceb3bae00287a23beef91", "shasum": "" }, "require": { @@ -179,9 +179,9 @@ ], "support": { "issues": "https://github.com/opensearch-project/opensearch-php/issues", - "source": "https://github.com/opensearch-project/opensearch-php/tree/2.2.0" + "source": "https://github.com/opensearch-project/opensearch-php/tree/2.3.0" }, - "time": "2023-05-20T16:57:42+00:00" + "time": "2024-04-24T07:21:51+00:00" }, { "name": "psr/log", @@ -235,23 +235,23 @@ }, { "name": "react/promise", - "version": "v2.10.0", + "version": "v2.11.0", "source": { "type": "git", "url": "https://github.com/reactphp/promise.git", - "reference": "f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38" + "reference": "1a8460931ea36dc5c76838fec5734d55c88c6831" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/reactphp/promise/zipball/f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38", - "reference": "f913fb8cceba1e6644b7b90c4bfb678ed8a3ef38", + "url": "https://api.github.com/repos/reactphp/promise/zipball/1a8460931ea36dc5c76838fec5734d55c88c6831", + "reference": "1a8460931ea36dc5c76838fec5734d55c88c6831", "shasum": "" }, "require": { "php": ">=5.4.0" }, "require-dev": { - "phpunit/phpunit": "^9.5 || ^5.7 || ^4.8.36" + "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36" }, "type": "library", "autoload": { @@ -295,7 +295,7 @@ ], "support": { "issues": "https://github.com/reactphp/promise/issues", - "source": "https://github.com/reactphp/promise/tree/v2.10.0" + "source": "https://github.com/reactphp/promise/tree/v2.11.0" }, "funding": [ { @@ -303,7 +303,7 @@ "type": "open_collective" } ], - "time": "2023-05-02T15:15:43+00:00" + "time": "2023-11-16T16:16:50+00:00" } ], "packages-dev": [], diff --git a/lib/functions.php b/lib/functions.php index 08aa145..0e3d479 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -4,6 +4,7 @@ */ use ColdTrick\OpenSearch\Di\DeleteQueue; +use Elgg\Database\MetadataTable; use Elgg\Database\QueryBuilder; use Elgg\Database\Select; use Elgg\Exceptions\ExceptionInterface; @@ -94,19 +95,19 @@ function opensearch_get_bulk_options(string $type = 'no_index_ts'): ?array { return array_merge($defaults, [ 'wheres' => [ function (QueryBuilder $qb, $main_alias) { - $select = $qb->subquery('metadata', 'mdi'); - $select->select('mdi.entity_guid') - ->where($qb->compare('mdi.name', '=', OPENSEARCH_INDEXED_NAME, ELGG_VALUE_STRING)); + $select = $qb->subquery(MetadataTable::TABLE_NAME, 'mdi'); + $select->select("{$select->getTableAlias()}.entity_guid") + ->where($qb->compare("{$select->getTableAlias()}.name", '=', OPENSEARCH_INDEXED_NAME, ELGG_VALUE_STRING)); return $qb->compare("{$main_alias}.guid", 'NOT IN', $select->getSQL()); }, function (QueryBuilder $qb, $main_alias) { - $select = $qb->subquery('metadata', 'b'); - $select->select('b.entity_guid') - ->joinEntitiesTable('b', 'entity_guid', 'inner', 'be'); + $select = $qb->subquery(MetadataTable::TABLE_NAME, 'b'); + $select->select("{$select->getTableAlias()}.entity_guid") + ->joinEntitiesTable($select->getTableAlias(), 'entity_guid', 'inner', 'be'); $select->where($qb->compare('be.type', '=', 'user', ELGG_VALUE_STRING)) - ->andWhere($qb->compare('b.name', '=', 'banned', ELGG_VALUE_STRING)) - ->andWhere($qb->compare('b.value', '=', 'yes', ELGG_VALUE_STRING)); + ->andWhere($qb->compare("{$select->getTableAlias()}.name", '=', 'banned', ELGG_VALUE_STRING)) + ->andWhere($qb->compare("{$select->getTableAlias()}.value", '=', 'yes', ELGG_VALUE_STRING)); return $qb->compare("{$main_alias}.guid", 'NOT IN', $select->getSQL()); }, @@ -167,7 +168,7 @@ function (QueryBuilder $qb, $main_alias) { * * @return void */ -function opensearch_add_document_for_deletion(int $guid, array $info, $time_offset = null): void { +function opensearch_add_document_for_deletion(int $guid, array $info, mixed $time_offset = null): void { try { $queue = DeleteQueue::instance(); } catch (\Exception $e) { @@ -205,7 +206,7 @@ function opensearch_add_document_for_deletion(int $guid, array $info, $time_offs */ function opensearch_remove_document_for_deletion(int $guid): void { // check if the entity still exists in Elgg (could be unregistered as searchable) - // and remove indexing timestamp so it can be reindexed when needed + // and remove indexing timestamp, so it can be reindexed when needed elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($guid) { $entity = get_entity($guid); if ($entity instanceof \ElggEntity) { @@ -257,7 +258,7 @@ function opensearch_get_documents_for_deletion(): array { * * @return null|array */ -function opensearch_inspect_show_values($key, array $merged_values, array $elgg_values, array $opensearch_values, int $depth = 0): ?array { +function opensearch_inspect_show_values(mixed $key, array $merged_values, array $elgg_values, array $opensearch_values, int $depth = 0): ?array { $rows = []; if (empty($depth)) { $rows[] = elgg_format_element('tr', [], elgg_format_element('th', ['colspan' => 3], $key)); diff --git a/views/default/admin/opensearch/search.php b/views/default/admin/opensearch/search.php index 3c6f8ec..13fe5e6 100644 --- a/views/default/admin/opensearch/search.php +++ b/views/default/admin/opensearch/search.php @@ -2,6 +2,7 @@ echo elgg_view_form('opensearch/admin_search', [ 'prevent_double_submit' => false, + 'class' => 'mbl', ]); echo elgg_view_module('info', elgg_echo('opensearch:admin_search:results'), elgg_echo('opensearch:admin_search:results:info'), [ diff --git a/views/default/forms/opensearch/admin_search.js b/views/default/forms/opensearch/admin_search.js deleted file mode 100644 index 8223d0c..0000000 --- a/views/default/forms/opensearch/admin_search.js +++ /dev/null @@ -1,14 +0,0 @@ -define(['jquery', 'elgg/Ajax'], function($, Ajax) { - $(document).on('submit', 'form.elgg-form-opensearch-admin-search', function(event) { - event.preventDefault(); - - var $form = $(this); - var ajax = new Ajax(); - ajax.action('opensearch/admin_search', { - data: ajax.objectify($form), - success: function(data) { - $('#opensearch-admin-search-results > .elgg-body').html(data); - } - }); - }); -}); diff --git a/views/default/forms/opensearch/admin_search.mjs b/views/default/forms/opensearch/admin_search.mjs new file mode 100644 index 0000000..ef57047 --- /dev/null +++ b/views/default/forms/opensearch/admin_search.mjs @@ -0,0 +1,15 @@ +import 'jquery'; +import Ajax from 'elgg/Ajax'; + +$(document).on('submit', 'form.elgg-form-opensearch-admin-search', function(event) { + event.preventDefault(); + + var $form = $(this); + var ajax = new Ajax(); + ajax.action('opensearch/admin_search', { + data: ajax.objectify($form), + success: function(data) { + $('#opensearch-admin-search-results > .elgg-body').html(data); + } + }); +}); diff --git a/views/default/forms/opensearch/admin_search.php b/views/default/forms/opensearch/admin_search.php index 00f1ee7..fc9e6dd 100644 --- a/views/default/forms/opensearch/admin_search.php +++ b/views/default/forms/opensearch/admin_search.php @@ -9,7 +9,7 @@ return; } -elgg_require_js('forms/opensearch/admin_search'); +elgg_import_esm('forms/opensearch/admin_search'); echo elgg_view_field([ '#type' => 'plaintext', @@ -26,17 +26,24 @@ return; } +$elgg_index = elgg_get_plugin_setting('index', 'opensearch'); $indices = array_keys($status); echo elgg_view_field([ - '#type' => 'select', - 'name' => 'index', - 'options' => $indices, - 'value' => $index_client->getReadAlias(), + '#type' => 'fieldset', + 'align' => 'horizontal', + 'class' => 'elgg-level', + 'fields' => [ + [ + '#type' => 'submit', + 'icon' => 'search', + 'text' => elgg_echo('search'), + ], + [ + '#type' => 'select', + 'name' => 'index', + 'options' => $indices, + 'value' => $index_client->getElggIndex($elgg_index), + ], + ], ]); - -$footer = elgg_view_field([ - '#type' => 'submit', - 'text' => elgg_echo('search'), -]); -elgg_set_form_footer($footer); diff --git a/views/default/opensearch/stats/elgg.php b/views/default/opensearch/stats/elgg.php index 47a1dbf..62982f7 100644 --- a/views/default/opensearch/stats/elgg.php +++ b/views/default/opensearch/stats/elgg.php @@ -59,9 +59,10 @@ $content .= elgg_view('output/url', [ 'confirm' => true, - 'text' => elgg_view_icon('refresh'), + 'icon' => 'refresh', + 'text' => false, 'title' => $reindex_title, - 'href' => 'action/opensearch/admin/reindex', + 'href' => elgg_generate_action_url('opensearch/admin/reindex'), 'class' => 'mlm' ]); @@ -78,8 +79,8 @@ } $content .= ''; -$content .= '' . elgg_echo('opensearch:stats:elgg:delete') . ''; -$content .= "{$count}"; +$content .= elgg_format_element('td', [], elgg_echo('opensearch:stats:elgg:delete')); +$content .= elgg_format_element('td', [], $count); $content .= ''; $content .= ''; diff --git a/views/default/plugins/opensearch/settings.php b/views/default/plugins/opensearch/settings.php index 43a76b2..b1d84d7 100644 --- a/views/default/plugins/opensearch/settings.php +++ b/views/default/plugins/opensearch/settings.php @@ -1,7 +1,5 @@ 'text', '#class' => 'man', 'name' => "params[{$setting_name}]", - 'value' => $plugin->$setting_name, + 'value' => $plugin->{$setting_name}, 'pattern' => '[0-9.]+', 'title' => elgg_echo('opensearch:settings:pattern:float'), ]));