<?PHP

//require_once("plugins/xsearch/inc/rumor/rumor.php");	// rumor
require_once("plugins/xsearch/inc/phpmorphy/src/common.php"); // phpMorphy
require_once("plugins/xsearch/inc/xsearch.stemmer.php");

$stopw = array(
	"а",
	"без","более","бы","был","была","были","было","быть",
	"в","вам","вас","весь","во","вот","все","всего","всех","вы",
	"где","да","даже","для","до",
	"его","ее","если","есть","еще",
	"ж","же",
	"за","здесь",
	"и","из","или","им","их",
	"к","как","ко","когда","кто",
	"ли","либо",
	"мне","может","мы",
	"на","надо","наш",
	"не","него","нее","нет","ни","них","но","ну",
	"о","об","однако","он","она","они","оно","от","очень",
	"по","под","при",
	"с","со",
	"так","также","такой","там","те","тем","то","того","тоже","той","только","том","ты",
	"у","уже",
	"хотя",
	"чего","чей","чем","что","чтобы","чье","чья",
	"эта","эти","это",
	"я");

$pattern[0] = '/\&amp;/';
$pattern[1] = '/\&lt;/';
$pattern[2] = '/\&gt;/';
$pattern[3] = '/<br>/';
$pattern[4] = '/\&quot;/';
$pattern[5] = '/\&#39;/';
$pattern[6] = '/\&#37;/';
$pattern[7] = '/\&#40;/';
$pattern[8] = '/\&#41;/';
$pattern[9] = '/\&#43;/';
$pattern[10] = '/\&#45;/';
$pattern[11] = '/\&nbsp;/';
$pattern[12] = '/\&laquo;/';
$pattern[13] = '/\&raquo;/';
$pattern[14] = '/\&mdash;/';
$pattern[15] = '/\&middot;/';

$replacement[0] = '';
$replacement[1] = '';
$replacement[2] = '';
$replacement[3] = ' ';
$replacement[4] = '';
$replacement[5] = '';
$replacement[6] = '';
$replacement[7] = '';
$replacement[8] = '';
$replacement[9] = '';
$replacement[10] = ' ';
$replacement[11] = ' ';
$replacement[12] = '';
$replacement[13] = '';
$replacement[14] = '';
$replacement[15] = '';

// QUERY PROCESSING FUNCTION
// Input:	query string
// Output:	query word's array or FALSE with $error setting
function sq_processing($squery)
{
	global $error, $L, $cfg;
	$sq = $squery;

	if (strlen($sq)<3)
	{
		$error .= "<p class=\"error\">".$L['xsearch_querytooshort']."</p>";
		$a = '';
		return(false);
	}

	$words = textToBaseFormWords($sq);
	$words = array_unique ($words);	
	return($words);
}

// BUILD PAGNAV FUNCTION
// Input:	number of query results, query string
// Output:	pagnav links
function build_pagnav ($items, $sq, $type)
{
	global $L, $cfg;
	$pages = ceil($items / $cfg['plugin']['xsearch']['maxitems']);
	$pagesb = ($pages + 1) - $pages;
	$startb = 0;
	if ($items > $cfg['plugin']['xsearch']['maxitems'])
	{
		$pagnav = $L['Page'] . ": ";
		while ($pages != 0)
		{
			$pagnav .= "[<a href=\"plug.php?e=xsearch&amp;sq=".$sq."&amp;start=".$startb."&amp;type=".$type."\">".$pagesb."</a>] ";
			$pages--;
			$pagesb++;
			$startb = $startb + $cfg['plugin']['xsearch']['maxitems'];
		}
	}
	else { $pagnav = ""; }
	return $pagnav;
}

// TEXT PROCESSING FUNCTION
// Input:	text
// Output:	word's array
function textToBaseFormWords($text, $mode="hd")
{
	global $stopw, $pattern, $replacement;

	$dict_bundle = new phpMorphy_FilesBundle("plugins/xsearch/inc/phpmorphy/dicts", "rus");
	// setlocale(LC_CTYPE, array('ru_RU.CP1251', 'Russian_Russia.1251')); // phpMorphy

	$stemmer = new Lingua_Stem_Ru();	
	
	switch($mode)
	{
		case 'hd':
			//prep_dict(""); // rumor
			$morphy = new phpMorphy($dict_bundle, array("storage" => PHPMORPHY_STORAGE_FILE, "with_gramtab" => false, "predict_by_suffix" => true, "predict_by_db" => true)); // phpMorphy
			break;
			
		case 'mem':
			//prep_dict_MEM(""); // rumor
			$morphy = new phpMorphy($dict_bundle, array("storage" => PHPMORPHY_STORAGE_SHM, "with_gramtab" => false, "predict_by_suffix" => true, "predict_by_db" => true)); // phpMorphy
			break;
		
		default:
			break;
	}

	
	$zzz = strip_tags($text);
	$zzz = preg_replace('/\[[^\]]*\]/', ' ', $zzz);
	$zzz = str_replace("ё", "е", $zzz);
	$zzz = preg_replace($pattern, $replacement, $zzz);
	$zzz = ereg_replace('[^a-zA-Zа-яА-Я0-9 ]',' ',$zzz); 
	$zzz = preg_replace('/\s+/', ' ', $zzz);

	//$zzz = strtolower($zzz); // rumor

	$zzzword = explode(" ", $zzz);
	$tmp_arr= array();
	
	$l=sizeof($zzzword);	
	for ($a=0; $a < $l; $a++)
	{
		if (in_array($zzzword[$a], $stopw))
		{
			$zzzword[$a]='';
		}
	}
	
	$l=sizeof($zzzword);
	for ($a=0; $a < $l; $a++)
	{
		if (strlen($zzzword[$a])>2)
		{
			//$mybase = get_base_form($zzzword[$a]); // rumor
			$mybase = $morphy->getBaseForm(strtoupper($zzzword[$a])); // phpMorphy
			if ($mybase)
			{
				foreach ($mybase as $v) 
				{
					if (strlen($v)>3)
					{
						array_push ($tmp_arr, $v);
					}
					else
					{
						array_push ($tmp_arr, $v."XXXX");
					}
				}
			}
			else
			{
				$zzzword[$a] = $stemmer->stem_word($zzzword[$a]);
				if (strlen($zzzword[$a])<4) 
				{
					$zzzword[$a] = $zzzword[$a]."XXXX";
				}
				array_push ($tmp_arr, $zzzword[$a]);
			}
		}
	}
	//$tmp_arr = array_unique ($tmp_arr);
	return $tmp_arr;
}

?>
