Skip to content

Commit

Permalink
[4.0] Decoupling highlighter plugin from com_finder (#20571)
Browse files Browse the repository at this point in the history
* Decoupling highlighter plugin from com_finder

* Codestyle

* Codestyle

* Version comment

* Fixing bug due to #20185

* Using url for display and route for route-URL

* Cleaning up finder plugins and adding cleanURL attribute in model

* Making OutputFilter::stringJSSafe() utf8 aware
  • Loading branch information
Hackwar authored and laoneo committed Jul 6, 2018
1 parent 1a00baf commit 0650916
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 92 deletions.
28 changes: 0 additions & 28 deletions administrator/components/com_finder/helpers/indexer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,34 +368,6 @@ public static function getPrimaryLanguage($lang)
return $data[$lang];
}

/**
* Method to get the path (SEF route) for a content item.
*
* @param string $url The non-SEF route to the content item.
*
* @return string The path for the content item.
*
* @since 2.5
*/
public static function getContentPath($url)
{
static $router;

// Only get the router once.
if (!($router instanceof Router))
{
// Get and configure the site router.
$router = Router::getInstance('site');
}

// Build the relative route.
$uri = $router->build($url);
$route = $uri->toString(array('path', 'query', 'fragment'));
$route = str_replace(JUri::base(true) . '/', '', $route);

return $route;
}

/**
* Method to get extra data for a content before being indexed. This is how
* we add Comments, Tags, Labels, etc. that should be available to Finder.
Expand Down
1 change: 1 addition & 0 deletions components/com_finder/Model/SearchModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function getItems()
{
// Build the result object.
$result = unserialize($row->object);
$result->cleanURL = $result->route;

// Add the result back to the stack.
$results[] = $result;
Expand Down
70 changes: 38 additions & 32 deletions components/com_finder/View/Search/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
defined('_JEXEC') or die;

use Joomla\CMS\Helper\SearchHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Profiler\Profiler;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;

/**
Expand Down Expand Up @@ -112,19 +116,19 @@ class HtmlView extends BaseHtmlView
*/
public function display($tpl = null)
{
$app = \JFactory::getApplication();
$params = $app->getParams();
$app = Factory::getApplication();
$this->params = $app->getParams();

// Get view data.
$state = $this->get('State');
$query = $this->get('Query');
\JDEBUG ? \JProfiler::getInstance('Application')->mark('afterFinderQuery') : null;
$results = $this->get('Items');
\JDEBUG ? \JProfiler::getInstance('Application')->mark('afterFinderResults') : null;
$total = $this->get('Total');
\JDEBUG ? \JProfiler::getInstance('Application')->mark('afterFinderTotal') : null;
$pagination = $this->get('Pagination');
\JDEBUG ? \JProfiler::getInstance('Application')->mark('afterFinderPagination') : null;
$this->state = $this->get('State');
$this->query = $this->get('Query');
\JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderQuery') : null;
$this->results = $this->get('Items');
\JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderResults') : null;
$this->total = $this->get('Total');
\JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderTotal') : null;
$this->pagination = $this->get('Pagination');
\JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderPagination') : null;

// Check for errors.
if (count($errors = $this->get('Errors')))
Expand All @@ -133,24 +137,16 @@ public function display($tpl = null)
}

// Configure the pathway.
if (!empty($query->input))
if (!empty($this->query->input))
{
$app->getPathway()->addItem($this->escape($query->input));
$app->getPathway()->addItem($this->escape($this->query->input));
}

// Push out the view data.
$this->state = &$state;
$this->params = &$params;
$this->query = &$query;
$this->results = &$results;
$this->total = &$total;
$this->pagination = &$pagination;

// Check for a double quote in the query string.
if (strpos($this->query->input, '"'))
{
// Get the application router.
$router = &$app::getRouter();
$router = $app->getRouter();

// Fix the q variable in the URL.
if ($router->getVar('q') !== $this->query->input)
Expand All @@ -159,16 +155,28 @@ public function display($tpl = null)
}
}

// Run an event on each result item
if (is_array($this->results))
{
// Import Finder plugins
PluginHelper::importPlugin('finder');

foreach ($this->results as $result)
{
$app->triggerEvent('onFinderResult', array(&$result, &$this->query));
}
}

// Log the search
SearchHelper::logSearch($this->query->input, 'com_finder');

// Push out the query data.
\JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
$this->suggested = \JHtml::_('query.suggested', $query);
$this->explained = \JHtml::_('query.explained', $query);
$this->suggested = \JHtml::_('query.suggested', $this->query);
$this->explained = \JHtml::_('query.explained', $this->query);

// Escape strings for HTML output
$this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx'));
$this->pageclass_sfx = htmlspecialchars($this->params->get('pageclass_sfx'));

// Check for layout override only if this is not the active menu item
// If it is the active menu item, then the view and category id will match
Expand All @@ -180,13 +188,13 @@ public function display($tpl = null)
$this->setLayout($active->query['layout']);
}

$this->prepareDocument($query);
$this->prepareDocument($this->query);

\JDEBUG ? \JProfiler::getInstance('Application')->mark('beforeFinderLayout') : null;
\JDEBUG ? Profiler::getInstance('Application')->mark('beforeFinderLayout') : null;

parent::display($tpl);

\JDEBUG ? \JProfiler::getInstance('Application')->mark('afterFinderLayout') : null;
\JDEBUG ? Profiler::getInstance('Application')->mark('afterFinderLayout') : null;
}

/**
Expand Down Expand Up @@ -239,9 +247,8 @@ protected function getLayoutFile($layout = null)
$file = $this->_layout . '_' . preg_replace('/[^A-Z0-9_\.-]/i', '', $layout);

// Check if the file exists.
jimport('joomla.filesystem.path');
$filetofind = $this->_createFileName('template', array('name' => $file));
$exists = \JPath::find($this->_path['template'], $filetofind);
$exists = Path::find($this->_path['template'], $filetofind);

return ($exists ? $layout : 'result');
}
Expand All @@ -257,9 +264,8 @@ protected function getLayoutFile($layout = null)
*/
protected function prepareDocument($query)
{
$app = \JFactory::getApplication();
$app = Factory::getApplication();
$menus = $app->getMenu();
$title = null;

// Because the application sets a default page title,
// we need to get it from the menu item itself
Expand Down
16 changes: 2 additions & 14 deletions components/com_finder/tmpl/search/default_result.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@

$description = JHtml::_('string.truncate', StringHelper::substr($this->result->description, $start), $desc_length, true);
}

$route = $this->result->route;

// Get the route with highlighting information.
if (!empty($this->query->highlight)
&& empty($this->result->mime)
&& $this->params->get('highlight_terms', 1)
&& JPluginHelper::isEnabled('system', 'highlight'))
{
$route .= '&highlight=' . base64_encode(json_encode($this->query->highlight));
}

?>
<li class="com-finder__result">
<h4 class="result-title <?php echo $mime; ?>">
<a href="<?php echo JRoute::_($route); ?>">
<a href="<?php echo JRoute::_($this->result->route); ?>">
<?php echo $this->result->title; ?>
</a>
</h4>
Expand All @@ -61,7 +49,7 @@
<?php endif; ?>
<?php if ($this->params->get('show_url', 1)) : ?>
<div class="small result-url">
<?php echo $this->baseUrl, JRoute::_($this->result->route); ?>
<?php echo $this->baseUrl, JRoute::_($this->result->cleanURL); ?>
</div>
<?php endif; ?>
</li>
7 changes: 5 additions & 2 deletions libraries/src/Filter/OutputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ public static function linkXHTMLSafe($input)
*/
public static function stringJSSafe($string)
{
for ($i = 0, $l = strlen($string), $new_str = ''; $i < $l; $i++)
$chars = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
$new_str = '';

foreach ($chars as $chr)
{
$new_str .= (ord(substr($string, $i, 1)) < 16 ? '\\x0' : '\\x') . dechex(ord(substr($string, $i, 1)));
$new_str .= '\\u' . str_pad(dechex(StringHelper::ord($chr)), 4, '0', STR_PAD_LEFT);
}

return $new_str;
Expand Down
10 changes: 5 additions & 5 deletions plugins/finder/categories/categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ protected function index(FinderIndexerResult $item, $format = 'html')
// Trigger the onContentPrepare event.
$item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params);

// Build the necessary route and path information.
// Create a URL as identifier to recognise items again.
$item->url = $this->getUrl($item->id, $item->extension, $this->layout);

/*
* Build the necessary route information.
* Need to import component route helpers dynamically, hence the reason it's handled here.
*/
$class = $extension . 'HelperRoute';

// Need to import component route helpers dynamically, hence the reason it's handled here.
JLoader::register($class, JPATH_SITE . '/components/' . $item->extension . '/helpers/route.php');

if (class_exists($class) && method_exists($class, 'getCategoryRoute'))
Expand All @@ -304,8 +306,6 @@ protected function index(FinderIndexerResult $item, $format = 'html')
$item->route = ContentHelperRoute::getCategoryRoute($item->id, $item->language);
}

$item->path = FinderIndexerHelper::getContentPath($item->route);

// Get the menu title if it exists.
$title = $this->getItemMenuTitle($item->url);

Expand Down
8 changes: 3 additions & 5 deletions plugins/finder/contacts/contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ protected function index(FinderIndexerResult $item, $format = 'html')
// Initialize the item parameters.
$item->params = new Registry($item->params);

// Build the necessary route and path information.
// Create a URL as identifier to recognise items again.
$item->url = $this->getUrl($item->id, $this->extension, $this->layout);

// Build the necessary route and path information.
$item->route = ContactHelperRoute::getContactRoute($item->slug, $item->catslug, $item->language);
$item->path = FinderIndexerHelper::getContentPath($item->route);

// Get the menu title if it exists.
$title = $this->getItemMenuTitle($item->url);
Expand Down Expand Up @@ -392,9 +393,6 @@ protected function setup()
// Load dependent classes.
JLoader::register('ContactHelperRoute', JPATH_SITE . '/components/com_contact/helpers/route.php');

// This is a hack to get around the lack of a route helper.
FinderIndexerHelper::getContentPath('index.php?option=com_contact');

return true;
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/finder/content/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ protected function index(FinderIndexerResult $item, $format = 'html')
$item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params, $item);
$item->body = FinderIndexerHelper::prepareContent($item->body, $item->params, $item);

// Build the necessary route and path information.
// Create a URL as identifier to recognise items again.
$item->url = $this->getUrl($item->id, $this->extension, $this->layout);

// Build the necessary route and path information.
$item->route = ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language);
$item->path = FinderIndexerHelper::getContentPath($item->route);

// Get the menu title if it exists.
$title = $this->getItemMenuTitle($item->url);
Expand Down
5 changes: 3 additions & 2 deletions plugins/finder/newsfeeds/newsfeeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ protected function index(FinderIndexerResult $item, $format = 'html')

$item->metadata = new Registry($item->metadata);

// Build the necessary route and path information.
// Create a URL as identifier to recognise items again.
$item->url = $this->getUrl($item->id, $this->extension, $this->layout);

// Build the necessary route and path information.
$item->route = NewsfeedsHelperRoute::getNewsfeedRoute($item->slug, $item->catslug, $item->language);
$item->path = FinderIndexerHelper::getContentPath($item->route);

/*
* Add the metadata processing instructions based on the newsfeeds
Expand Down
5 changes: 3 additions & 2 deletions plugins/finder/tags/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,11 @@ protected function index(FinderIndexerResult $item, $format = 'html')

$item->metadata = new Registry($item->metadata);

// Build the necessary route and path information.
// Create a URL as identifier to recognise items again.
$item->url = $this->getUrl($item->id, $this->extension, $this->layout);

// Build the necessary route and path information.
$item->route = TagsHelperRoute::getTagRoute($item->slug);
$item->path = FinderIndexerHelper::getContentPath($item->route);

// Get the menu title if it exists.
$title = $this->getItemMenuTitle($item->url);
Expand Down
28 changes: 28 additions & 0 deletions plugins/system/highlight/highlight.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,32 @@ public function onAfterDispatch()

return true;
}

/**
* Method to catch the onFinderResult event.
*
* @param FinderIndexerResult $item The search result
* @param array $query The search query of this result
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onFinderResult($item, $query)
{
static $params;

if (is_null($params))
{
$params = ComponentHelper::getParams('com_finder');
}

// Get the route with highlighting information.
if (!empty($query->highlight)
&& empty($item->mime)
&& $params->get('highlight_terms', 1))
{
$item->route .= '&highlight=' . base64_encode(json_encode($query->highlight));
}
}
}

0 comments on commit 0650916

Please sign in to comment.