Dotclear


Version 3 (modified by Tomtom33, 13 years ago) (diff)

Improved documentation

Search engines creation

Getting started

Search engines creation is very simple. All you need to do is create a simple plugin with search class. This one is an extension of the original one contains in the dcOpenSearch plugin.

Let's start.

Plugin definition

Like usual, the story start with a folder in your plugins witch contains a _define.php file:

<?php

if (!defined('DC_RC_PATH')) { return; }

$this->registerModule(
	/* Name */			"mySearchEngine",
	/* Description*/		"My great search engine",
	/* Author */			"Google inc.",
	/* Version */			'1.0',
	/* Permissions */		'usage',
	/* Priority */			200
);

?>

Please notice that you have to specify a priority higher than 10.

Now, it's time to declare the new search engine to the object $core. Let's do this with a _prepend.php file:

<?php

if (!defined('DC_RC_PATH')) { return; }

if (array_key_exists('dcSearchEngine',$__autoload) {
	$__autoload['mySearchEngine'] = dirname(__FILE__).'/inc/class.my.search.engine.php';
	$core->searchengines[] = 'mySearchEngine';
}

?>

This file loads automatically the new search class and adds the new engine in $core->searchengines.

Search engine creation

We done the loading of search class but it doesn't exists yet. So, we have to create it. Let's start by create an /inc/ folder and a new php file called class.my.search.engine.php:

Search engine class

<?php
class mySearchEngine extends dcSearchEngine
{
	public $type;
	public $name;
	public $label;
	public $description;
	
	protected $has_gui = false;
	
	protected function setInfo()
	{
		$this->type = 'post';
		$this->label = __('Posts');
		$this->description = __('Posts search engine');
	}
}
?>

Please notice that your engine have to be a child of dcSearchEngine class. If not, it'll not works.

We are just creating a new search engine called "Posts search engine" with a description witch be displayed in search engine list. Parameter $has_gui = false says this engine has no user Interface in back-end.

Please notice that you can use $this->core to rich all object of Dotclear (BDD, blog, permissions, etc...)

getResults method (Required)

This engine is completely useless because it doesn't have a search method to return results. This method is getResults. Let's add it to the class:

<?php

class mySearchEngine extends dcSearchEngine
{
...
	public function getResults($q = '')
	{
		$res = array();
		
		$rs = $this->core->blog->getPosts(array('post_type' => 'post', 'search' => $q));
		
		while ($rs->fetch()) {
			if ($rs->post_excerpt_xhtml === '') {
				$content = $rs->post_content_xhtml;
			}
			elseif ($this->getEngineConfig('display') === 'both') {
				$content = $rs->post_excerpt_xhtml.$rs->post_content_xhtml;
			}
			elseif ($this->getEngineConfig('display') === 'excerpt') {
				$content = $rs->post_excerpt_xhtml;
			}
			else {
				$content = $rs->post_content_xhtml;
			}
			$res[] = array(
				'search_id' => $rs->post_id,
				'search_url' => $rs->post_url,
				'search_title' => $rs->post_title,
				'search_author_id' => $rs->user_id,
				'search_author_name' => $rs->getAuthorCN(),
				'search_author_url' => $rs->user_url,
				'search_cat_id' => $rs->cat_id,
				'search_cat_title' => $rs->cat_title,
				'search_cat_url' => $rs->cat_url,
	 			'search_dt' => $rs->post_creadt,
	 			'search_tz' => $rs->post_tz,
				'search_content' => $content,
				'search_comment_nb' => $rs->nb_comment,
				'search_trackback_nb' => $rs->nb_trackback,
				'search_engine' => $this->name,
				'search_lang' => $rs->post_lang,
				'search_type' => $this->type
			);
		}
		
		return $res;	
	}
...
}

?>

Here is the getResults method come from post search engine. Let's me explain this code. First of all, we get all posts corresponding to the search keywords passed by parameter $q. Then, for each result, we create a new entry in array $res. It's very important to respect format of each $res item.

Please notice that the part of code about if{} else{} blocks is about options define in admin user interface.

getItemAdminURL method (Required)

This method have to return URL of each item for the back-end. Global recordset generated by getResults method is passed in a parameter

<?php

class mySearchEngine extends dcSearchEngine
{
...
	public static function getItemAdminURL($rs)
	{
		return $GLOBALS['core']->getPostAdminURL($rs->search_type,$rs->search_id);
	}
...
}

?>

getItemPublicURL method (Required)

This method have to return URL of each item for the front-end. Global recordset generated by getResults method is passed in a parameter

<?php

class mySearchEngine extends dcSearchEngine
{
...
	public static function getItemPublicURL($rs)
	{
		return $GLOBALS['core']->blog->url.$GLOBALS['core']->getPostPublicURL($rs->search_type,$rs->search_url);
	}
...
}

?>

getItemContent method m(Required)

This method have to return URL of each item for the front-end. Global recordset generated by getResults method is passed in a parameter. This method could be use full to add some more information.

<?php

class mySearchEngine extends dcSearchEngine
{
...
	public static function getItemContent($rs)
	{
		return $rs->search_content;
	}
...
}

?>

Sites map