<?php
/**
 * Implementation of fulltext service
 *
 * @category   DMS
 * @package    SeedDMS
 * @license    GPL 2
 * @version    @version@
 * @author     Uwe Steinmann <uwe@steinmann.cx>
 * @copyright  Copyright (C) 2016 Uwe Steinmann
 * @version    Release: @package_version@
 */

/**
 * Implementation of fulltext service
 *
 * @category   DMS
 * @package    SeedDMS
 * @author     Uwe Steinmann <uwe@steinmann.cx>
 * @copyright  Copyright (C) 2016 Uwe Steinmann
 * @version    Release: @package_version@
 */
class SeedDMS_FulltextService {
	/**
	 * List of services for searching fulltext
	 */
	protected $services;

	/**
	 * List of converters
	 */
	protected $converters;

	/**
	 * Max file size for imediate indexing
	 */
	protected $maxsize;

	private $index;

	private $search;

	public function __construct() {
		$this->services = array();
		$this->converters = array();
		$this->maxsize = 0;
		$this->index = null;
		$this->search = null;
		$this->cmdtimeout = 5;
	}

	public function addService($name, $service) {
		$this->services[] = $service;
	}

	public function setConverters($converters) {
		$this->converters = $converters;
	}

	public function setMaxSize($maxsize) {
		$this->maxsize = $maxsize;
	}

	public function setCmdTimeout($timeout) {
		$this->cmdtimeout = $timeout;
	}

	public function IndexedDocument($document, $forceupdate=false) {
		$nocontent = ($document->getLatestContent()->getFileSize() > $this->maxsize) && !$forceupdate;
		return new $this->services[0]['IndexedDocument']($document->getDMS(), $document, $this->converters, $nocontent, $this->cmdtimeout);
	}

	/**
	 * Returns an instance of the indexer
	 *
	 * The indexer provides access to fulltext index. It allows to add and
	 * get documents.
	 *
	 * @return object instance of class specified in 'Indexer'
	 */
	public function Indexer($recreate=false) {
		if($this->index)
			return $this->index;

		if($this->services[0]) {
			if($recreate)
				$this->index = $this->services[0]['Indexer']::create($this->services[0]['Conf']);
			else
				$this->index = $this->services[0]['Indexer']::open($this->services[0]['Conf']);
			return $this->index;
		} else
			return null;
	}

	public function Search() {
		if($this->search)
			return $this->search;
		if($this->services[0]) {
			$this->search = new $this->services[0]['Search']($this->index);
			return $this->search;
		} else {
			return null;
		}
	}
}


