From 0657a1ed6f54299bbc9165fbddd21dd674520b50 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 26 Jun 2018 08:54:17 -0400 Subject: [PATCH] Updating Search add-on with various new features Added database field, management model and view, and other stuff to manage Server URL Started adding code to index posts and pages when updated or removed, not doing that right now. Additional prep --- classes/data/dataManagement.php | 8 ++ classes/glmSearch.php | 106 +++++++++++------- css/front.css | 3 + deactivate.php | 3 +- index.php | 2 +- models/admin/management/search.php | 58 +++++++++- models/admin/search/ossRequest.php | 98 ++++++++++++++++ models/front/search/index.php | 16 ++- readme.txt | 11 +- setup/adminHooks.php | 17 +++ .../create_database_V0.0.3.sql | 1 + setup/databaseScripts/dbVersions.php | 7 +- .../update_database_V0.0.4.sql | 13 +++ setup/validActions.php | 6 +- views/admin/management/search.html | 22 +++- views/front/footer.html | 8 +- views/front/search/index.html | 5 + 17 files changed, 319 insertions(+), 65 deletions(-) create mode 100644 models/admin/search/ossRequest.php create mode 100644 setup/databaseScripts/update_database_V0.0.4.sql diff --git a/classes/data/dataManagement.php b/classes/data/dataManagement.php index 7d28a7b..3d532b5 100644 --- a/classes/data/dataManagement.php +++ b/classes/data/dataManagement.php @@ -115,6 +115,14 @@ class GlmDataSearchManagement extends GlmDataAbstract 'use' => 'a' ), + // OSS Server + 'server' => array ( + 'field' => 'server', + 'type' => 'text', + 'required' => true, + 'use' => 'a' + ), + // OSS Search Index 'search_index' => array ( 'field' => 'search_index', diff --git a/classes/glmSearch.php b/classes/glmSearch.php index 820ebb0..c42200d 100644 --- a/classes/glmSearch.php +++ b/classes/glmSearch.php @@ -3,6 +3,7 @@ * GLM Search Engine - Support Classes * * PHP version 5 + * Uses OSS API v2 (RESTFul JSON) * * @category Support_Services * @package GLMSearch @@ -12,13 +13,6 @@ * @link */ -/**** NOTE : THIS FILE HAS NOT HAD A THOUROUGH REVIEW SINCE BEING GRABBED FROM THE OLD GLM COMMON APPS *****/ - -/* - * Set OpenSearchServer base URL and port - */ -define('OpenSearchServer', 'http://oss.gaslightmedia.com:9090'); - /** * GLM Search - Web Site Search Support Class * @@ -34,6 +28,14 @@ define('OpenSearchServer', 'http://oss.gaslightmedia.com:9090'); class GLMSearch { + /** + * URL for the OpenSearchServer that should be used. This + * information is stored in Management. + * + * @var $server + * @access private + */ + private $server; /** * Name of the index an instance of this class will search. * An index can have multiple Websites in it. If when calling @@ -60,12 +62,12 @@ class GLMSearch */ private $auth; /** - * General error message describing any failure + * Error messages array * * @var $curlError * @access public */ - public $errorMessage; + public $errorMessages; /** * Exception object caused by curl or other failure * @@ -176,42 +178,63 @@ class GLMSearch /** * Constructor * - * @param text $index Optional index name if Web site is part of a larger index - * @param string $website Name of the Web site - * @param string $login Login to OpenSearchServer - * @param string $key Key for login - * @param text $filterType Optional filter type (when $this->filter is true). + * @param string $server URL of server (i.e. http://oss2.gaslightmedia.com:9090) + * @param string $index Optional index name if Web site is part of a larger index + * @param string $website Name of the Web site + * @param string $login Login to OpenSearchServer + * @param string $key Key for login + * @param string $filterType Optional filter type (when $this->filter is true). * Filter types are "title", "titleExact", "titlePhonetic", "content", * "contentExact", "contactPhonetic", "urlSplit", "urlExact", "urlPhonetic", * "full", "fullExact", "fullPhonetic" - * @param text $filterValue Filter value to use with $filterType. + * @param string $filterValue Filter value to use with $filterType. * * @return void * @access public */ - public function __construct($index, $website, $login, $key, $exclude_home_page, $filterType = false, $filterValue = false) + public function __construct($server, $index, $website, $login, $key, $exclude_home_page = false, $filterType = false, $filterValue = false) { - // If there's a problem with Filter and validate supplied parameters - if ( - (trim($website) != '' && ($website = filter_var($website, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) || - ($login = filter_var($login, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false || - ($key = filter_var($key, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false - ) { - return false; + // If there's a problem with any of the required parameters + $messages = array(); + $server = trim($server); + if ($server == '' || ($server = filter_var($server, FILTER_SANITIZE_URL, FILTER_FLAG_STRIP_HIGH)) == false) { + $this->errorMessages[] = "Server URL setting was not supplied or was invalid."; } + $index = trim($index); + if ($index == '' || ($index = filter_var($index, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { + $this->errorMessages[] = "Search Index setting was not supplied or was invalid."; + } + $website = trim($website); + if (($website = filter_var($website, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { + $this->errorMessages[] = "Website setting was invalid."; + } + $login = trim($login); + if ($login == '' || ($login = filter_var($login, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { + $this->errorMessages[] = "Search Server Login setting was not supplied or invalid."; + } + $key = trim($key); + if ($key == '' || ($key = filter_var($key, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { + $this->errorMessages[] = "Search Key Login setting was not supplied or invalid."; + } + if (count($this->errorMessages) > 0) { + $this->error = true; + } else { + + // Store supplied parameters + $this->server = $server; + $this->index = $index; + $this->website = $website; + $this->exclude_home_page = $exclude_home_page; - // Store supplied parameters - $this->index = $index; - $this->website = $website; - $this->exclude_home_page = $exclude_home_page; + // Build authentication parameters for submissions + $this->auth = "login=$login&key=$key"; - // Build authentication parameters for submissions - $this->auth = "login=$login&key=$key"; + // Create curl instance - and set to return response + $this->curl = curl_init(); + $x = curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); - // Create curl instance - and set to return response - $this->curl = curl_init(); - curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); + } } @@ -269,10 +292,10 @@ class GLMSearch // Create request URL switch ($this->type) { case 'field': - $url = OpenSearchServer.'/services/rest/index/'.$this->index.'/search/field/?'.$this->auth; + $url = $this->server.'/services/rest/index/'.$this->index.'/search/field/?'.$this->auth; break; case 'pattern': - $url = OpenSearchServer.'/services/rest/index/'.$this->index.'/search/pattern/?'.$this->auth; + $url = $this->server.'/services/rest/index/'.$this->index.'/search/pattern/?'.$this->auth; break; } @@ -518,26 +541,26 @@ class GLMSearch /** * Tell OpenSearchServer to crawl a particular URL pattern * - * @param integer $pattern URL Pattern + * @param string $url URL to crawl * * @return array Object Results from Search Engine or false if failure. * * @access public */ - public function crawlURL($pattern) + public function crawlURL($url) { // Clear all the results data $this->clearResults(); // Filter and validate supplied parameters - if (($pattern = filter_var($pattern, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { + if (($url = filter_var($url, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH)) == false) { return false; } // Create request URL - $param = 'url='.$pattern; - $url = OpenSearchServer.'/services/rest/index/'.$this->index.'/crawler/web/crawl?'.$this->auth.'&'.$param; + $param = 'url='.$url; + $url = $this->server.'/services/rest/index/'.$this->index.'/crawler/web/crawl?'.$this->auth.'&'.$param; // Set curl options curl_setopt_array($this->curl, array( @@ -566,7 +589,7 @@ class GLMSearch } // Debug information - $this->errorMessage = 'crawlURL() - Completed successfully.'; + $this->errorMessages[] = 'crawlURL() - Completed successfully.'; return json_decode($this->rawResults); } @@ -591,6 +614,7 @@ class GLMSearch 'lang' => 'ENGLISH', // Language (in all caps) 'operator' => 'AND', // Either AND or OR - And matches pages containing all words 'collapsing' => array( + 'field' => 'url', 'max' => 2, 'mode' => 'OFF', 'type' => 'OPTIMIZED' @@ -642,7 +666,7 @@ class GLMSearch $this->rawResults = false; $this->resultArray = false; $this->result = false; - $this->errorMessage = false; + $this->errorMessages = array(); $this->error = false; } diff --git a/css/front.css b/css/front.css index c80ae71..816cb46 100644 --- a/css/front.css +++ b/css/front.css @@ -38,3 +38,6 @@ color: #008000; margin-top: .2em; } +.GLMSresultWarning { + color: red; +} \ No newline at end of file diff --git a/deactivate.php b/deactivate.php index ff0c734..0ed51dc 100644 --- a/deactivate.php +++ b/deactivate.php @@ -48,12 +48,13 @@ class glmMembersSearchPluginDeactivate */ public function __construct ($wpdb, $config) { - +/* // Save WordPress Database object $this->wpdb = $wpdb; // Save plugin configuration object $this->config = $config; +*/ // Delete our version from WordPress Options delete_option('glmMembersDatabaseSearchPluginVersion'); diff --git a/index.php b/index.php index 04b1f5e..a0e8afd 100644 --- a/index.php +++ b/index.php @@ -44,7 +44,7 @@ if (!defined('ABSPATH')) { * version from this plugin. */ define('GLM_MEMBERS_SEARCH_PLUGIN_VERSION', '1.2.2'); -define('GLM_MEMBERS_SEARCH_PLUGIN_DB_VERSION', '0.0.3'); +define('GLM_MEMBERS_SEARCH_PLUGIN_DB_VERSION', '0.0.4'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_SEARCH_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.2.0'); diff --git a/models/admin/management/search.php b/models/admin/management/search.php index bc00e1a..d301905 100644 --- a/models/admin/management/search.php +++ b/models/admin/management/search.php @@ -83,6 +83,8 @@ class GlmMembersAdmin_management_search extends GlmDataSearchManagement $searchSettigns = false; $settingsUpdated = false; $settingsUpdateError = false; + $searchTestFailure = false; + $searchTestMessage = array(); // Determine if current user can edit configurations @@ -117,6 +119,51 @@ class GlmMembersAdmin_management_search extends GlmDataSearchManagement $settingsUpdateError = true; } + // Perform a search to test server, index, Website, login and key settings + require_once GLM_MEMBERS_SEARCH_PLUGIN_CLASS_PATH.'/glmSearch.php'; + $settings = $searchSettings['fieldData']; + $GlmSearch = new GLMSearch( + $settings['server'], + $settings['search_index'], + $settings['website'], + $settings['login'], + $settings['login_key'] + ); + + // If false then parameters aren't all correct + if (!$GlmSearch) { + $searchTestFailure = true; + $searchTestMessages[] = "The GlmSearch class could not be instatiated."; + } elseif ($GlmSearch->error) { + $searchTestFailure = true; + $searchTestMessages = $GlmSearch->errorMessages; + } else { + + $searchResult = $GlmSearch->glmSearch("A", 'OR', 0, 2); + + // Check for total failure + if (!$searchResult) { + + $searchTestFailure = true; + $searchTestMessages[] = " + General failure when trying to test search. + Are Server URL, Search Index, Server Login, and Login Key all correct? + "; + + if (count($searchTestMessages) == 0) { + $searchTestMessagesp[] = 'Unknown error'; + } + } else { + + // Check for no results returned + if (!is_array($searchResult) || !isset($searchResult['totalResults']) || !$searchResult['totalResults'] > 0 ) { + + $searchTestFailure = true; + $searchTestMessages[] = "No results returned from OpenSearchServer. Is Website setting correct and has Website been indexed?"; + } + } + } + break; // Default is to get the current settings and display the form @@ -131,11 +178,12 @@ class GlmMembersAdmin_management_search extends GlmDataSearchManagement // Compile template data $templateData = array( - 'reason' => '', - 'searchSettings' => $searchSettings, - 'settingsUpdated' => $settingsUpdated, - 'settingsUpdateError' => $settingsUpdateError - ); + 'searchSettings' => $searchSettings, + 'settingsUpdated' => $settingsUpdated, + 'settingsUpdateError' => $settingsUpdateError, + 'searchTestFailure' => $searchTestFailure, + 'searchTestMessages' => $searchTestMessages + ); // Return status, suggested view, and data to controller return array( diff --git a/models/admin/search/ossRequest.php b/models/admin/search/ossRequest.php new file mode 100644 index 0000000..d05c827 --- /dev/null +++ b/models/admin/search/ossRequest.php @@ -0,0 +1,98 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release ossRequest.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +// Load GLM Search class +require_once GLM_MEMBERS_SEARCH_PLUGIN_CLASS_PATH.'/glmSearch.php'; + +/** + * GlmMembersAdmin_search_ossRequest + * + * PHP version 5 + * + * @category Model + * @package GLM Member DB + * @author Chuck Scott + * @license http://www.gaslightmedia.com Gaslightmedia + * @release SVN: $Id: search.php,v 1.0 2011/01/25 19:31:47 cscott + * Exp $ + */ +class GlmMembersAdmin_search_ossRequest extends GLMSearch +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + + /* + * Constructor + * + * This constructor performs the work for this model. This model returns + * an array containing the following. + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'view' + * + * A suggested view name that the contoller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + * @wpdb object WordPress database object + * + * @return array Array containing status, suggested view, and any data + */ + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + + // Get the Open Search Server parameters from management settings + $server = $this->config['settings']['server']; + $index = $this->config['settings']['search_index']; + $website = $this->config['settings']['website']; + $login = $this->config['settings']['login']; + $key = $this->config['settings']['login_key']; + $exclude_home_page = $this->config['settings']['exclude_home_page']; + + // Run constructor for GLMSearch data class + parent::__construct($server, $index, $website, $login, $key, $exclude_home_page); + + } + + public function modelAction($actionData = false) + { + + // Get the requested option + + } +} + +?> \ No newline at end of file diff --git a/models/front/search/index.php b/models/front/search/index.php index f64dbdb..4a49dd2 100644 --- a/models/front/search/index.php +++ b/models/front/search/index.php @@ -56,14 +56,15 @@ class GlmMembersFront_search_index extends GLMSearch $this->config = $config; // Get the Open Search Server parameters from management settings + $server = $this->config['settings']['server']; $index = $this->config['settings']['search_index']; $website = $this->config['settings']['website']; $login = $this->config['settings']['login']; $key = $this->config['settings']['login_key']; $exclude_home_page = $this->config['settings']['exclude_home_page']; - // Run constructor for members data class - parent::__construct($index, $website, $login, $key, $exclude_home_page); + // Run constructor for GLMSearch data class + parent::__construct($server, $index, $website, $login, $key, $exclude_home_page); } @@ -106,6 +107,8 @@ class GlmMembersFront_search_index extends GLMSearch $haveSearchRequest = false; $haveSearchResult = false; + $searchFailure = false; + $failureMessage = false; $status = true; $query = false; $start = 0; @@ -139,6 +142,13 @@ class GlmMembersFront_search_index extends GLMSearch // Send request to OpenSearchServer and get results if (trim($query) != '') { $searchResult = $this->glmSearch($query, $operator, $start, $rows); + if (!$searchResult) { + $searchFailure = true; + $failureMessage = $this->rawResults; + if (!$failureMessage) { + $failureMessage = 'Unknown error'; + } + } if (is_array($searchResult) && isset($searchResult['totalResults']) && $searchResult['totalResults'] > 0 ) { $haveSearchResult = true; } @@ -150,6 +160,8 @@ class GlmMembersFront_search_index extends GLMSearch $templateData = array( 'haveSearchRequest' => $haveSearchRequest, 'haveSearchResult' => $haveSearchResult, + 'searchFailure' => $searchFailure, + 'failureMessage' => $failureMessage, 'searchResult' => $searchResult, 'query' => $query ); diff --git a/readme.txt b/readme.txt index 6f98eef..e51df00 100644 --- a/readme.txt +++ b/readme.txt @@ -13,11 +13,18 @@ This is the Gaslight Media Members Database Search Child Plugin. == Description == The Gaslight Media Members Database Search Child Plugin is an add-on to the Gaslight Media Members Database, -which is required to install and run this plugin +which is required to install and run this plugin. + +This plugin provides a shortcode that can display a search form and search results on a page. You can also +create a custom form anywhere on the site provided that the form action targets a search page that includes +the [glm-members-search] shortcode and includes the following submit parameters. + +* "query" = Search string entered by user +* "matchAll" = Optional checkbox value to match all terms vs any terms. Default is any. == Installation == -This section describes how to install the plugin and get it working. +Use normal Wordpress plugin installation proceedures. e.g. diff --git a/setup/adminHooks.php b/setup/adminHooks.php index cbd18d7..9f42b9f 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -48,3 +48,20 @@ add_filter( 'glm-member-db-dashboard-widget-warnings', function( $content ) { $content .= $this->controller('dashboardWidget', 'search'); return $content; }); + +// Ask the search engine to crawl the post or page whenever it's updated +function glm_member_db_admin_search_crawlUrl( $post_id ) { + + $post_title = get_the_title($post_id); + $post_url = get_permalink($post_id); + $post_status = get_post_status($post_id); // Status string + $post_password_protected = post_password_required($post_id); // True if password protectedd + + /* + * get_post_type + */ + +// $content .= $this->controller('search', 'ossRequest', array('request' => 'crawl', 'url' => $post_url)); + +} +add_action( 'save_post', 'glm_member_db_admin_search_crawlUrl' ); \ No newline at end of file diff --git a/setup/databaseScripts/create_database_V0.0.3.sql b/setup/databaseScripts/create_database_V0.0.3.sql index 9cef0f0..62e3eea 100644 --- a/setup/databaseScripts/create_database_V0.0.3.sql +++ b/setup/databaseScripts/create_database_V0.0.3.sql @@ -9,6 +9,7 @@ -- Search Management Settings CREATE TABLE {prefix}management ( id INT NOT NULL AUTO_INCREMENT, + server TINYTEXT NULL, -- OSS Search Server URL search_index TINYTEXT NULL, -- OSS Search Index to use (normally "Index_1") website TINYTEXT NULL, -- Hostname for the Website exclude_home_page BOOLEAN NULL, -- Flag to say whether to exclude home page from search results diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index d466cda..62f2e23 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -14,8 +14,9 @@ */ $glmMembersSearchDbVersions = array( - '0.0.1' => array('version' => '0.0.1', 'tables' => 1, 'date' => '6/20/16'), - '0.0.2' => array('version' => '0.0.2', 'tables' => 1, 'date' => '7/5/16'), - '0.0.3' => array('version' => '0.0.3', 'tables' => 1, 'date' => '7/7/16') + '0.0.1' => array('version' => '0.0.1', 'tables' => 1, 'date' => '06/20/16'), + '0.0.2' => array('version' => '0.0.2', 'tables' => 1, 'date' => '07/05/16'), + '0.0.3' => array('version' => '0.0.3', 'tables' => 1, 'date' => '07/07/16'), + '0.0.4' => array('version' => '0.0.4', 'tables' => 1, 'date' => '06/05/18') ); diff --git a/setup/databaseScripts/update_database_V0.0.4.sql b/setup/databaseScripts/update_database_V0.0.4.sql new file mode 100644 index 0000000..2653735 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.4.sql @@ -0,0 +1,13 @@ +-- Gaslight Media Members Database +-- File Created: 7/5/16 15:27:15 +-- Database Version: 0.0.4 +-- Database Update From Previous Version Script +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashses + +ALTER TABLE {prefix}management ADD COLUMN server TINYTEXT; + +---- + +UPDATE {prefix}management SET server = ''; diff --git a/setup/validActions.php b/setup/validActions.php index 3563bda..04aacc8 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -48,12 +48,16 @@ $glmMembersSearchAddOnValidActions = array( ), 'dashboardWidget' => array( 'search' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG + ), + 'search' => array( + 'ossRequest' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG ) + ), 'frontActions' => array( 'search' => array( 'index' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG, - 'sitemap' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG + 'sitemap' => GLM_MEMBERS_SEARCH_PLUGIN_SLUG ) ) ); diff --git a/views/admin/management/search.html b/views/admin/management/search.html index 6e7c286..6b922bb 100644 --- a/views/admin/management/search.html +++ b/views/admin/management/search.html @@ -12,9 +12,27 @@ {if $settingsUpdated}

Settings Updated

{/if} {if $settingsUpdateError}Settings Update Error{/if} + {if $searchTestFailure} +

+ Search Engine Test Failure +
    + {foreach $searchTestMessages as $m} +
  • {$m}
  • + {/foreach} +
+

+ {/if} +

General Search Settings

+ + Server URL: + + + {if $searchSettings.fieldFail.server}

{$searchSettings.fieldFail.server}

{/if} + + Search Index: @@ -32,14 +50,14 @@ - OSS Login: + Search Server Login: {if $searchSettings.fieldFail.login}

{$searchSettings.fieldFail.login}

{/if} - OSS login key: + Search Server login key: {if $searchSettings.fieldFail.login_key}

{$searchSettings.fieldFail.login_key}

{/if} diff --git a/views/front/footer.html b/views/front/footer.html index f8b0015..e2612e8 100644 --- a/views/front/footer.html +++ b/views/front/footer.html @@ -1,10 +1,4 @@ - - {if $frontDebug} - - {/if} - + \ No newline at end of file diff --git a/views/front/search/index.html b/views/front/search/index.html index 50ad4a8..b8774a3 100644 --- a/views/front/search/index.html +++ b/views/front/search/index.html @@ -91,6 +91,11 @@ {else}

No results matching your search.

+ + {if $searchFailure} +

We're sorry, there was some problem with getting search results. Please try again later.

+

({$failureMessage})

+ {/if} {/if} {else} -- 2.17.1