bludit/bl-plugins/search/plugin.php

228 lines
6.0 KiB
PHP
Raw Permalink Normal View History

2018-10-01 16:34:28 +02:00
<?php
class pluginSearch extends Plugin {
private $pagesFound = array();
private $numberOfItems = 0;
public function init()
{
// Fields and default values for the database of this plugin
$this->dbFields = array(
'label'=>'Search',
'wordsToCachePerPage'=>800,
'showButtonSearch'=>false
2018-10-01 16:34:28 +02:00
);
}
public function form()
{
global $L;
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$html .= '<div>';
2018-10-01 16:50:04 +02:00
$html .= '<label>'.$L->get('Label').'</label>';
$html .= '<input name="label" type="text" value="'.$this->getValue('label').'">';
$html .= '<span class="tip">'.$L->get('This title is almost always used in the sidebar of the site').'</span>';
2018-10-01 16:34:28 +02:00
$html .= '</div>';
$html .= '<div>';
$html .= '<label>'.$L->get('Show button search').'</label>';
$html .= '<select name="showButtonSearch">';
$html .= '<option value="true" '.($this->getValue('showButtonSearch')===true?'selected':'').'>'.$L->get('enabled').'</option>';
$html .= '<option value="false" '.($this->getValue('showButtonSearch')===false?'selected':'').'>'.$L->get('disabled').'</option>';
$html .= '</select>';
$html .= '</div>';
$html .= '<div>';
2018-10-01 16:34:28 +02:00
return $html;
}
// HTML for sidebar
public function siteSidebar()
{
2018-10-14 12:54:42 +02:00
global $L;
2018-10-01 16:34:28 +02:00
$html = '<div class="plugin plugin-search">';
$html .= '<h2 class="plugin-label">'.$this->getValue('label').'</h2>';
$html .= '<div class="plugin-content">';
$html .= '<input type="text" id="jspluginSearchText" /> ';
if ($this->getValue('showButtonSearch')) {
$html .= '<input type="button" value="'.$L->get('Search').'" onClick="pluginSearch()" />';
}
$html .= '</div>';
$html .= '</div>';
$DOMAIN_BASE = DOMAIN_BASE;
$html .= <<<EOF
<script>
function pluginSearch() {
var text = document.getElementById("jspluginSearchText").value;
window.open('$DOMAIN_BASE'+'search/'+text, '_self');
return false;
}
document.getElementById("jspluginSearchText").onkeypress = function(e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13'){
pluginSearch();
return false;
}
}
</script>
EOF;
2018-10-01 16:34:28 +02:00
return $html;
}
public function install($position=0)
{
parent::install($position);
return $this->createCache();
}
2018-10-01 16:50:04 +02:00
// Method called when the user click on button save in the settings of the plugin
2018-10-01 16:34:28 +02:00
public function post()
{
parent::post();
return $this->createCache();
2018-10-01 16:34:28 +02:00
}
public function afterPageCreate()
{
$this->createCache();
}
public function afterPageModify()
{
$this->createCache();
}
public function afterPageDelete()
{
$this->createCache();
}
public function beforeAll()
{
// Check if the URL match with the webhook
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
global $site;
global $url;
// Change the whereAmI to avoid load pages in the rule 69.pages
// This is only for performance purpose
2018-10-01 16:34:28 +02:00
$url->setWhereAmI('search');
// Get the string to search from the URL
$stringToSearch = $this->webhook($webhook, true, false);
$stringToSearch = trim($stringToSearch, '/');
// Search the string in the cache and get all pages with matches
$list = $this->search($stringToSearch);
$this->numberOfItems = count($list);
// Split the content in pages
// The first page number is 1, so the real is 0
$realPageNumber = $url->pageNumber() - 1;
$itemsPerPage = $site->itemsPerPage();
$chunks = array_chunk($list, $itemsPerPage);
if (isset($chunks[$realPageNumber])) {
$this->pagesFound = $chunks[$realPageNumber];
}
}
}
public function paginator()
{
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
// Get the pre-defined variable from the rule 99.paginator.php
// Is necessary to change this variable to fit the paginator with the result from the search
global $numberOfItems;
$numberOfItems = $this->numberOfItems;
}
}
public function beforeSiteLoad()
{
$webhook = 'search';
if ($this->webhook($webhook, false, false)) {
global $url;
global $WHERE_AM_I;
$WHERE_AM_I = 'search';
2018-10-01 16:34:28 +02:00
// Get the pre-defined variable from the rule 69.pages.php
// We change the content to show in the website
global $content;
$content = array();
foreach ($this->pagesFound as $pageKey) {
try {
$page = new Page($pageKey);
array_push($content, $page);
} catch (Exception $e) {
// continue
}
}
}
}
// Generate the cache file
2018-10-02 00:13:47 +02:00
// This function is necessary to call it when you create, edit or remove content
2018-10-01 16:34:28 +02:00
private function createCache()
{
2018-10-02 00:13:47 +02:00
// Get all pages published
2018-10-01 16:34:28 +02:00
global $pages;
$list = $pages->getList($pageNumber=1, $numberOfItems=-1, $published=true, $static=true, $sticky=true, $draft=false, $scheduled=false);
2018-10-01 16:34:28 +02:00
$cache = array();
foreach ($list as $pageKey) {
$page = buildPage($pageKey);
// Process content
2018-10-02 00:13:47 +02:00
$words = $this->getValue('wordsToCachePerPage') * 5; // Asumming avg of characters per word is 5
2018-10-01 16:34:28 +02:00
$content = $page->content();
$content = Text::removeHTMLTags($content);
2018-10-02 00:13:47 +02:00
$content = Text::truncate($content, $words, '');
2018-10-01 16:34:28 +02:00
2018-10-02 00:13:47 +02:00
// Include the page to the cache
2018-10-01 16:34:28 +02:00
$cache[$pageKey]['title'] = $page->title();
2018-10-02 00:13:47 +02:00
$cache[$pageKey]['description'] = $page->description();
2018-10-01 16:34:28 +02:00
$cache[$pageKey]['content'] = $content;
}
// Generate JSON file with the cache
$json = json_encode($cache);
return file_put_contents($this->cacheFile(), $json, LOCK_EX);
}
2018-10-02 00:13:47 +02:00
// Returns the absolute path of the cache file
2018-10-01 16:34:28 +02:00
private function cacheFile()
{
return $this->workspace().'cache.json';
}
// Search text inside the cache
2018-10-02 00:13:47 +02:00
// Returns an array with the pages keys related to the text
// The array is sorted by score
2018-10-01 16:34:28 +02:00
private function search($text)
{
2018-10-02 00:13:47 +02:00
// Read the cache file
2018-10-01 16:34:28 +02:00
$json = file_get_contents($this->cacheFile());
$cache = json_decode($json, true);
2018-11-27 20:48:19 +01:00
// Inlcude Fuzz algorithm
require_once($this->phpPath().'vendors/fuzz.php');
$fuzz = new Fuzz($cache, 10, 1, true);
$results = $fuzz->search($text, 5);
2018-10-02 00:13:47 +02:00
2018-11-27 20:48:19 +01:00
return(array_keys($results));
2018-10-01 16:34:28 +02:00
}
2018-10-14 12:54:42 +02:00
}