decatur-vote/web-search

一个用于轻松向网站添加搜索支持的小型库,内置搜索GUI。

v1.0.x-dev 2023-12-06 14:33 UTC

This package is auto-updated.

Last update: 2024-09-06 22:09:54 UTC


README

网络搜索

这是DecaturVote.com的搜索软件。使用taeluf/liaisontaeluf/big-db构建。

开发中 - 此搜索库功能齐全,但仍处于开发中。我们尚未记录任何与标签相关的内容。某些功能可能会更改或出现问题。

请在youtube上查看早期示例。

安装

composer require decatur-vote/web-search v1.0.x-dev   

或在您的composer.json

{"require":{ "decatur-vote/web-search": "v1.0.x-dev"}}  

路由

目前,无法更改路由,因为它在JavaScript和PHP表单中是硬编码的。

  • GET /search/:加载包含最新搜索项的搜索页面(HTML)
  • GET /search/?q=Some+Query:加载包含查询结果q的搜索页面(HTML)
  • GET /search/?format=json&q=Some+Query:获取匹配查询的json数组。内置搜索脚本使用此方法。包含
    • uuid - 项目的uuid
    • uri - 项目的url。通常是同一网站的/rel/url,但也可以是完整的url。
    • title - 项目的标题
    • summary - 项目的300字符摘要。(搜索支持数据库中的1024个字符,但只发送300个字符到浏览器。好吧,如果计算末尾的...,则是303个字符)。
    • created_at - mysql兼容的日期时间字符串
    • updated_at - 用户友好的格式Y-m-d g:i a

用法

添加和删除搜索项非常简单,您甚至可以使用SearchDb来创建自己的搜索结果GUI。

通常,您还需要执行以下设置步骤。

示例

<?php  
// $pdo = new \PDO(...)  
$searchDb = new \DecaturVote\SearchDb($pdo);  
  
// add searchable items to the index.  
$searchDb->add_searchable($uuid=uniqid('search'), 'Title 1 x', 'Summary v', '/article/title/1/');  
$searchDb->add_searchable(uniqid('search'), 'Title 2 v', 'Summary x', '/article/title/2/');  
$searchDb->add_searchable(uniqid('search'), 'Title 3 y', 'Summary r', '/article/title/3/');  
  
// get an array of `DecaturVote\SearchDb\Search` items (they are BigOrm subclasses)  
$items  = $searchDb->search("x"); # returns the first & 2nd item in that order, since they both contain x & title takes precedent.   
  
// delete an item by uuid  
$searchDb->delete_from_search($uuid);  

设置

要设置搜索:初始化数据库,使用Liaison设置,设计样式,并添加用于所需回调的集成类。

请参阅DecaturVote.com集成示例

初始化数据库

在您的CLI中运行此命令

vendor/bin/dv-search create-db -pdo path/to/pdo.php  

您必须创建文件path/to/pdo.php并使其返回PDO实例。将使用该实例创建表。

使用Liaison设置

<?php  
$integration = new \DecaturVote\Search\Test\SearchIntegration($pdo);  
$site_app = new \DecaturVote\Search\LiaisonPackage($lia, $integration);  
  
$lia->addResourceFile(\Tlf\Js\Autowire::filePath());  

设置集成

定义MyIntegrationClass。请参阅DecaturVote.com集成示例。它必须实现DecaturVote\Search\IntegrationInterface

<?php  
  
namespace DecaturVote\Search;  
  
interface IntegrationInterface {  
    /** get a valid PDO instance */  
    public function getPdo(): \PDO;  
  
    /** Get an array of RSS channel information, per the 2.0 RSS spec. See https://validator.w3.org/feed/docs/rss2.html */  
    public function getRssChannel(): array;  
  
    /** get a schem & host (like `https://example.com`) to prepend to any relative urls in your search items, primarily for rss */  
    public function getHostWebsite(): string;  
  
    /** called when the page view is loaded. You should use this to add your own styles.   
     *  
     * @param $lia the liaison instance  
     */  
    public function page_will_display(\Lia $lia): void;  
  
    /**  
     * Called when the view `search/tag-feed` is loaded. Use this to add styles.  
     */  
    public function tag_feed_will_display(\Lia $lia): void;  
  
    public function add_tag_will_display(\Lia $lia): void;  
  
    /**   
     * Check if tag creation is allowed for current user.  
     *  
     * @param $lia liaison instance  
     * @param $tag The tag that will be created, if allowed  
     *  
     * @return true if creation allowed, false if not.  
     */  
    public function can_create_tag(\Lia $lia, \DecaturVote\SearchDb\Tag $tag): bool;  
}  

样式

文档中没有提供样式。请参阅DecaturVote.com集成示例。以下是一个CSS模板文件,您可以根据需要进行填写。

/** The root node. Contains an h1, form and div.ResultsList */  
.DecaturVoteSearch {  
    --selected_button_color: green;  
}  
/** Search page heading */  
.DecaturVoteSearch h1 {}  
  
/** Parent Node for each .SearchResult */  
.DecaturVoteSearch .ResultsList {}  
  
/** Container for search result (child of .ResultsList) */  
.DecaturVoteSearch .SearchResult {}  
  
/** heading for each search result */  
.DecaturVoteSearch .SearchResult h2 {}  
/** search result title link */  
.DecaturVoteSearch .SearchResult h2 a {}  
/** search result title link, hover/active */  
.DecaturVoteSearch .SearchResult h2 a:hover, .DecaturVoteSearch .SearchResult h2 a:active, .DecaturVoteSearch .SearchResult h2 a:focus {}  
/** Summary of search result items */  
.DecaturVoteSearch .SearchResult > p {}  
  
.DecaturVoteSearch nav.pages a {  
    display:inline-block;  
    padding:4px;  
    margin:4px;  
}  
.DecaturVoteSearch nav.pages a.current_page {  
    text-decoration:none;  
}  
  
.DecaturVoteSearch a.reset_search {  
    font-style:italic;  
}  
  
.DecaturVoteSearch .SearchResult .search_footer {  
    display:flex;  
    flex-direction:row;  
    justify-content:space-between;  
}  
  
/** The datetime in the bottom-right corner of the result */  
.DecaturVoteSearch .SearchResult .date {  
    margin-right:8px;  
    margin-top:8px;  
}  
  
/** The item's type in the bottom-left corner of the result */  
.DecaturVoteSearch .SearchResult .type{  
    margin-right:8px;  
    margin-top:8px;  
}  
  
/** the search form */  
.DecaturVoteSearch form {}  
/** the search form's input */  
.DecaturVoteSearch form input {}  
  
.DecaturVoteSearch form button.SearchTagButton.selected {  
    background: var(--selected_button_color);  
}  

测试

设置Mysql测试配置

Search的根目录下,创建文件mysql_settings.json,并填写您的开发环境设置

警告:运行测试将删除您的数据库中的搜索索引

{  
    "database": "dbname",  
    "host": "localhost",  
    "user": "user_name",  
    "password": "bad_password"  
}  

然后运行vendor/bin/phptest

我们在这个仓库中没有Liaison集成,所以无法在浏览器中进行测试。

截图

这些来自DecaturVote.com/search/。由DecaturVote.com集成进行样式设计

/search/:
A 'Search' Heading, a search input box, and four search results in a vertical list, each showing a title, summary, and date + time. This screenshot shows design, and is not intended to share text-content. Some names are redacted with black boxes.