Added Module Custom Search
Added Google custom search library Added HTTP client extension libaray Updated demo example to use the Module Custom Search
This commit is contained in:
@@ -123,6 +123,7 @@ feature -- Hooks configuration
|
||||
lnk.set_weight (98)
|
||||
a_menu_system.primary_menu.extend (lnk)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
feature -- Handler
|
||||
|
||||
1
modules/custom_search/Readme.md
Normal file
1
modules/custom_search/Readme.md
Normal file
@@ -0,0 +1 @@
|
||||
Google Custom Search Module.
|
||||
18
modules/custom_search/custom_search.ecf
Normal file
18
modules/custom_search/custom_search.ecf
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-14-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-14-0 http://www.eiffel.com/developers/xml/configuration-1-14-0.xsd" name="custom_search" uuid="054E9C5C-ACCB-4A4D-B825-6C574AEC30A9" library_target="custom_search">
|
||||
<target name="custom_search">
|
||||
<root all_classes="true"/>
|
||||
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
|
||||
<library name="cms" location="$ISE_LIBRARY\unstable\library\web\cms\cms-safe.ecf" readonly="false"/>
|
||||
<library name="cms_app_env" location="$ISE_LIBRARY\unstable\library\web\cms\library\app_env\app_env-safe.ecf" readonly="false"/>
|
||||
<library name="cms_config" location="$ISE_LIBRARY\unstable\library\web\cms\library\configuration\config-safe.ecf"/>
|
||||
<library name="error" location="$ISE_LIBRARY\contrib\library\utility\general\error\error-safe.ecf"/>
|
||||
<library name="google_cse" location="..\..\library\gcse\gcse-safe.ecf"/>
|
||||
<library name="http" location="$ISE_LIBRARY\contrib\library\network\protocol\http\http-safe.ecf"/>
|
||||
<library name="net" location="$ISE_LIBRARY\library\net\net-safe.ecf"/>
|
||||
<library name="time" location="$ISE_LIBRARY\library\time\time-safe.ecf"/>
|
||||
<library name="wsf" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\wsf\wsf-safe.ecf"/>
|
||||
<library name="wsf_encoder" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\text\encoder\encoder-safe.ecf"/>
|
||||
<cluster name="src" location="src\" recursive="true"/>
|
||||
</target>
|
||||
</system>
|
||||
6
modules/custom_search/site/config/custom_search.json
Normal file
6
modules/custom_search/site/config/custom_search.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"gcse": {
|
||||
"cx":"",
|
||||
"secret_key":""
|
||||
}
|
||||
}
|
||||
40
modules/custom_search/site/templates/block_search.tpl
Normal file
40
modules/custom_search/site/templates/block_search.tpl
Normal file
@@ -0,0 +1,40 @@
|
||||
<section>
|
||||
<header>
|
||||
<h2>Results for <kbd>{$result.current_page.search_terms/}</kbd></h2>
|
||||
</header>
|
||||
|
||||
<!-- list of results -->
|
||||
<ol>
|
||||
|
||||
<!-- Item result -->
|
||||
{foreach from="$result.items" item="item"}
|
||||
<li>
|
||||
<article>
|
||||
<header>
|
||||
<h3>
|
||||
<cite>
|
||||
<a href="{$item.link/}">{$item.title/}</a>
|
||||
</cite>
|
||||
</h3>
|
||||
</header>
|
||||
<blockquote cite="{$item.link/}">
|
||||
<p>{htmlentities}{$item.snippet/}{/htmlentities}</p>
|
||||
<footer>
|
||||
<p><abbr title="Uniform Resource Locator">Source</abbr> <a href="{$item.link/}">{$item.display_link/}</a></p>
|
||||
</footer>
|
||||
</blockquote>
|
||||
</article>
|
||||
</li>
|
||||
{/foreach}
|
||||
|
||||
|
||||
</ol>
|
||||
<ul class="cms-page-links">
|
||||
{if isset="$result.previous_page"}
|
||||
<li><a href="{$site_url/}gcse/?q={$result.previous_page.search_terms/}&start={$result.previous_page.start_index/}&num={$result.previous_page.count/}">Previous</a></li>
|
||||
{/if}
|
||||
{if isset="$result.next_page"}
|
||||
<li><a href="{$site_url/}gcse/?q={$result.next_page.search_terms/}&start={$result.next_page.start_index/}&num={$result.next_page.count/}">Next</a></li>
|
||||
{/if}
|
||||
</ul>
|
||||
</section>
|
||||
154
modules/custom_search/src/google_custom_search_module.e
Normal file
154
modules/custom_search/src/google_custom_search_module.e
Normal file
@@ -0,0 +1,154 @@
|
||||
note
|
||||
description: "[
|
||||
Module that provide Google Custom Search functionality.
|
||||
]"
|
||||
date: "$Date: 2015-10-09 20:50:01 -0300 (vi. 09 de oct. de 2015) $"
|
||||
revision: "$Revision: 97982 $"
|
||||
|
||||
class
|
||||
GOOGLE_CUSTOM_SEARCH_MODULE
|
||||
|
||||
inherit
|
||||
|
||||
CMS_MODULE
|
||||
redefine
|
||||
register_hooks
|
||||
end
|
||||
|
||||
CMS_HOOK_BLOCK_HELPER
|
||||
|
||||
CMS_HOOK_AUTO_REGISTER
|
||||
|
||||
CMS_HOOK_MENU_SYSTEM_ALTER
|
||||
|
||||
SHARED_EXECUTION_ENVIRONMENT
|
||||
export
|
||||
{NONE} all
|
||||
end
|
||||
|
||||
REFACTORING_HELPER
|
||||
|
||||
SHARED_LOGGER
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Create current module
|
||||
do
|
||||
version := "1.0"
|
||||
description := "Google custome search module"
|
||||
package := "search"
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
name: STRING = "custom_search"
|
||||
-- <Precursor>
|
||||
|
||||
feature -- Router
|
||||
|
||||
setup_router (a_router: WSF_ROUTER; a_api: CMS_API)
|
||||
-- Router configuration.
|
||||
do
|
||||
a_router.handle ("/gcse", create {WSF_URI_AGENT_HANDLER}.make (agent handle_search (a_api, ?, ?)), a_router.methods_head_get)
|
||||
end
|
||||
|
||||
feature -- Recaptcha
|
||||
|
||||
gcse_secret_key (api: CMS_API): detachable READABLE_STRING_8
|
||||
-- Get recaptcha security key.
|
||||
local
|
||||
utf: UTF_CONVERTER
|
||||
do
|
||||
if attached api.module_configuration (Current, Void) as cfg then
|
||||
if
|
||||
attached cfg.text_item ("gcse.secret_key") as l_recaptcha_key and then
|
||||
not l_recaptcha_key.is_empty
|
||||
then
|
||||
Result := utf.utf_32_string_to_utf_8_string_8 (l_recaptcha_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
gcse_cx_key (api: CMS_API): detachable READABLE_STRING_8
|
||||
-- Get recaptcha security key.
|
||||
local
|
||||
utf: UTF_CONVERTER
|
||||
do
|
||||
if attached api.module_configuration (Current, Void) as cfg then
|
||||
if
|
||||
attached cfg.text_item ("gcse.cx") as l_recaptcha_key and then
|
||||
not l_recaptcha_key.is_empty
|
||||
then
|
||||
Result := utf.utf_32_string_to_utf_8_string_8 (l_recaptcha_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Hooks configuration
|
||||
|
||||
register_hooks (a_response: CMS_RESPONSE)
|
||||
-- Module hooks configuration.
|
||||
do
|
||||
auto_subscribe_to_hooks (a_response)
|
||||
end
|
||||
|
||||
feature -- Hooks
|
||||
|
||||
menu_system_alter (a_menu_system: CMS_MENU_SYSTEM; a_response: CMS_RESPONSE)
|
||||
-- Hook execution on collection of menu contained by `a_menu_system'
|
||||
-- for related response `a_response'.
|
||||
do
|
||||
end
|
||||
|
||||
block_list: ITERABLE [like {CMS_BLOCK}.name]
|
||||
do
|
||||
Result := <<"search">>
|
||||
end
|
||||
|
||||
feature -- Handler
|
||||
|
||||
handle_search (api: CMS_API; req: WSF_REQUEST; res: WSF_RESPONSE)
|
||||
local
|
||||
r: CMS_RESPONSE
|
||||
l_parameters:GCSE_QUERY_PARAMETERS
|
||||
l_search: GCSE_API
|
||||
do
|
||||
-- TODO handle errors!!!
|
||||
write_debug_log (generator + ".handle_search")
|
||||
create {GENERIC_VIEW_CMS_RESPONSE} r.make (req, res, api)
|
||||
if attached {WSF_STRING} req.query_parameter ("q") as l_query then
|
||||
if
|
||||
attached gcse_cx_key (api) as l_cx and then
|
||||
attached gcse_secret_key (api) as l_key
|
||||
then
|
||||
create l_parameters.make (l_key, l_cx, l_query.value )
|
||||
if
|
||||
attached {WSF_STRING} req.query_parameter ("start") as l_index and then
|
||||
attached {WSF_STRING} req.query_parameter ("num") as l_num
|
||||
then
|
||||
l_parameters.set_start (l_index.value)
|
||||
l_parameters.set_num (l_num.value)
|
||||
end
|
||||
create l_search.make (l_parameters)
|
||||
l_search.search
|
||||
if attached template_block (Current, "search", r) as l_tpl_block then
|
||||
l_tpl_block.set_value (l_search.last_result, "result")
|
||||
r.add_block (l_tpl_block, "content")
|
||||
end
|
||||
else
|
||||
-- If no key are provided, at least output google search result page.
|
||||
if req.is_https then
|
||||
r.set_redirection ("https://www.google.com/search?sitesearch=" + r.absolute_url ("", Void) + "&q=" + l_query.url_encoded_value)
|
||||
else
|
||||
r.set_redirection ("http://www.google.com/search?sitesearch=" + r.absolute_url ("", Void) + "&q=" + l_query.url_encoded_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
r.execute
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user