| Current Path : /var/www/iplanru/data/www/i-plan.ru/plugins/system/zoofinder/ |
| Current File : /var/www/iplanru/data/www/i-plan.ru/plugins/system/zoofinder/zoofinder.php |
<?php
// No direct access
defined( '_JEXEC' ) or die;
/**
*
* @package Joomla.Plugin
* @subpackage System.Zoofinder
* @since 2.5+
* @author
*/
class plgSystemZoofinder extends JPlugin
{
/**
* Class Constructor
* @param object $subject
* @param array $config
*/
public function __construct( & $subject, $config )
{
parent::__construct( $subject, $config );
$this->loadLanguage();
}
function onAfterRender()
{
require_once( $_SERVER['DOCUMENT_ROOT'].'/libraries/phpmorphy/src/common.php');
$dir = $_SERVER['DOCUMENT_ROOT'].'/libraries/phpmorphy/dicts';
$lang = 'ru_RU';
$opts = array( 'storage' => PHPMORPHY_STORAGE_MEM);
try {
$morphy = new phpMorphy($dir, $lang, $opts);
} catch(phpMorphy_Exception $e) {
die('Error occured while creating phpMorphy instance: ' . $e->getMessage());
}
$db= JFactory::getDBO();
$doc = JFactory::getDocument();
$app =JFactory::getApplication();
$jinput = JFactory::getApplication()->input;
$item_id=$jinput->get( 'item_id', 0,'INT' );
$mat_id='';
if ($jinput->get( 'option', '' )=="com_content")
{
$mat_id=$jinput->get( 'id', 0,'INT' );
}
// проверка, что мы не в административной панели
if ($app->getName()!= 'site') {
return true;
}
//prepare params
$zoo_types=explode("\r\n", $this->params->get('zootypes'));
$type_name=array();
foreach($zoo_types as $type)
{
$mtype=explode("|", $type);
$type_name[]=$mtype[0];
}
$type_name = array_unique($type_name);
$temp=explode("\r\n", $this->params->get('fixwords'));
$fixwords=array();
foreach($temp as $word)
{
$tmp=explode("|", $word);
if (isset($tmp[1])) {$fixwords[$tmp[0]]=$tmp[1];}
}
unset($temp,$tmp);
$temp=explode("\r\n", mb_strtoupper($this->params->get('pluswords')));
$pluswords=array();
foreach($temp as $word)
{
$tmp=explode("|", $word);
if (isset($tmp[1])) {$pluswords[$tmp[0]]=$tmp[1];}
}
unset($temp,$tmp);
foreach ($type_name as $type) $sql_type[]="i.type=\"".$type."\"";
$sql_type=implode(" or ", $sql_type);
if ($sql_type) $sql_type=' ('.$sql_type.') ';
$livetime=$this->params->get('livetime');
if ($this->params->get('zooenabled')=="Да")
{
$zoo_items=Array();
$b=true;
//текущий материал принудительно
/*if ($item_id)
{
$zoo_items[]=$item_id; $b=false;
}*/
//текущий материал если отсутствует или устарел
if ($item_id)
{
$item = $db->setQuery(" SELECT id
FROM jos_zoo_finders
where type=1 and matid={$item_id} and date > DATE_SUB(CURRENT_DATE, INTERVAL {$livetime} DAY)")->loadAssoc();
if ($item['id']=="") { $zoo_items[]=$item_id; $b=false;}
}
//материал из индекса но устаревший.
if ($b)
{
$old = $db->setQuery(" SELECT f.matid matid
FROM jos_zoo_finders f, jos_zoo_item i
where f.matid=i.id and
f.type=1 and
i.publish_up and {$sql_type} and
f.date < DATE_SUB(CURRENT_DATE, INTERVAL {$livetime} DAY)
ORDER BY i.modified DESC
limit 1")->loadAssoc();
if ($old['matid']) { $zoo_items[]=$old[matid]; $b=false;}
}
//материал отсутствующий в индексе
if ($b)
{
$noindex = $db->setQuery("SELECT i.id
FROM jos_zoo_item i
left JOIN jos_zoo_finders f
ON f.matid=i.id
where f.matid is NULL and i.publish_up and {$sql_type}
ORDER BY i.modified DESC
limit 1
")->loadAssoc();
if ($noindex['id']<>"") { $zoo_items[]=$noindex['id']; $b=false;}
}
/*SELECT i.id
FROM jos_zoo_item i
left JOIN jos_zoo_finders f
ON f.matid=i.id
where f.matid is NULL and i.publish_up and (i.type="news" or i.type="product" or i.type="proektnyj-opyt")
ORDER BY i.modified DESC*/
}
//indexing zoo
if ($zoo_items and $this->params->get('zooenabled')=="Да")
{
foreach ($zoo_items as $item)
{
$zoo_item_fields = $db->setQuery('SELECT *, year(created) year FROM jos_zoo_item where id='.$item)->loadAssoc();
if ($zoo_item_fields)
{
//создание записи finders
$mat = $db->setQuery("SELECT id
FROM jos_zoo_finders
WHERE type=\"1\" and matid=\"{$item}\"")->loadAssoc();
if ($mat[id])
{
$db->setQuery("UPDATE jos_zoo_finders SET date=NOW() where type=\"1\" and matid=\"{$item}\"")->Execute();
$last=$mat[id];
}
else
{
$db->setQuery("INSERT INTO jos_zoo_finders (id,type,matid,date)
VALUES(NULL,\"1\",\"{$item}\",NOW())")->Execute();
$mat = $db->setQuery("SELECT id
FROM jos_zoo_finders
WHERE type=\"1\" and matid=\"{$item}\"")->loadAssoc();
$last=$mat[id];
}
$a=json_decode($zoo_item_fields['elements'],true);
$buf='';
$buf.=$zoo_item_fields['name'].' ';
foreach($zoo_types as $type)
{
$mtype=explode("|", $type);
if ($zoo_item_fields['type']==$mtype[0])
{
$buf.=$a[$mtype[1]]['0']['value'].' ';
}
}
//buf prepare
$buf=trim($buf);
$buf=strip_tags($buf);
$buf=html_entity_decode($buf);
//$buf=preg_replace ("/&(.+?);/","",$buf);
$buf=mb_ereg_replace ("[^a-zA-Zа-яА-Я0-9ёЁ\s]","",$buf);
$buf=str_replace(" ", " ", $buf);
$buf=str_replace(" ", " ", $buf);
$buf=mb_strtoupper( $buf, 'UTF-8' );
$buf=explode(" ", $buf);
//lemmatize
$res=array();
foreach($buf as $key=>$word)
{
$word=mb_ereg_replace ("[^a-zA-Zа-яА-Я0-9ёЁ\-]","",$word);
if ($pluswords[$word] or is_numeric($word))
{
$res[$key]=$word;
}
else
{
$lemm = $morphy->lemmatize( $word );
$res[$key]=$lemm[0];
}
}
$buf=$res;
unset($res);
//count array
$buf_cnt=array_count_values($buf);
//расчет влияния даты на вес
if ($zoo_item_fields['year'])
$weight_date=(int)(($zoo_item_fields['year']-2008)*1.5);
else
$weight_date=(int)(1*1.5);
$double=array();
foreach($buf as $word)
{
$continue=false;
if (mb_strlen($word)<=2){$continue=true;}
if (!$morphy->lemmatize($word, phpMorphy:: IGNORE_PREDICT)) {$continue=true;}
if ($pluswords[$word]) {$continue=false;}
if (is_numeric($word)) {$continue=false;}
if (in_array($word,$double)) {$continue=true;}
if (!$last) {$continue=true;}
if (!$word) {$continue=true;}
if ($continue) {continue;}
$double[]=$word;
if ($fixwords[$word])
{
$zn=$fixwords[$word];
}
elseif($pluswords[$word])
{
$zn=$pluswords[$word];
}
else
{
//set_weight
$partsOfSpeech = $morphy->getPartOfSpeech( $word );
$profile=false;
if ( !$profile ) {
$profile = [
// Служебные части речи //
'ПРЕДЛ' => 0,
'СОЮЗ' => 0,
'МЕЖД' => 0,
'ВВОДН' => 0,
'ЧАСТ' => 0,
'МС' => 0,
// Наиболее значимые части речи //
'С' => 5,
'Г' => 5,
'П' => 3,
'Н' => 3,
// Остальные части речи //
'DEFAULT' => 1
];
}
if ( !$partsOfSpeech ) { $weight= $profile[ 'DEFAULT' ]; }
else
{
for ( $i = 0; $i < count( $partsOfSpeech ); $i++ ) {
if ( isset( $profile[ $partsOfSpeech[ $i ] ] ) ) {
$range[] = $profile[ $partsOfSpeech[ $i ] ];
} else {
$range[] = $profile[ 'DEFAULT' ];
}
}
$weight= max( $range );
}
//count
$count=$buf_cnt[$word];
if ($count==''){ $count=0;}
//рассчет значимости
$zn=$count*$weight*$weight_date;
}
//record to db
$index = $db->setQuery("SELECT f.id id
FROM jos_zoo_finder f, jos_zoo_finders fs
WHERE fs.type=\"1\" and f.fid=fs.id and fs.matid=\"{$item}\" and f.word=\"{$word}\"")->loadAssoc();
if ($index[id])
$db->setQuery("UPDATE jos_zoo_finder SET weight=\"{$zn}\" where id=\"{$index[id]}\" and fid=\"{$last}\" and word=\"{$word}\"")->Execute();
else
$db->setQuery("INSERT INTO jos_zoo_finder (id,fid,word,weight)
VALUES(NULL,\"{$last}\",\"{$word}\",\"{$zn}\")")->Execute();
}
}
}
}
/*main material===============================================*/
$mat_items=Array();
if (!$zoo_items and $mat_id and $this->params->get('matenabled')=="Да")
{
$b=true;
//текущий материал принудительно
/*if ($mat_id)
{
$mat_items[]=$mat_id; $b=false;
}*/
//текущий материал если отсутствует или устарел
if ($mat_id and $b)
{
$item = $db->setQuery(" SELECT id
FROM jos_zoo_finders
where type=0 and matid={$mat_id} and date > DATE_SUB(CURRENT_DATE, INTERVAL {$livetime} DAY)")->loadAssoc();
if ($item['id']=="") { $mat_items[]=$mat_id; $b=false;}
}
}
//indexing mat
if (!$zoo_items and $mat_items and $this->params->get('matenabled')=="Да")
{
foreach ($mat_items as $item)
{
$mat_item_fields = $db->setQuery('SELECT *, year(created) year FROM jos_content where id='.$item)->loadAssoc();
if ($mat_item_fields)
{
//создание записи finders
$mat = $db->setQuery("SELECT id
FROM jos_zoo_finders
WHERE type=\"0\" and matid=\"{$item}\"")->loadAssoc();
if ($mat[id])
{
$db->setQuery("UPDATE jos_zoo_finders SET date=NOW() where type=\"0\" and matid=\"{$item}\"")->Execute();
$last=$mat[id];
}
else
{
$db->setQuery("INSERT INTO jos_zoo_finders (id,type,matid,date)
VALUES(NULL,\"0\",\"{$item}\",NOW())")->Execute();
$mat = $db->setQuery("SELECT id
FROM jos_zoo_finders
WHERE type=\"0\" and matid=\"{$item}\"")->loadAssoc();
$last=$mat[id];
}
$a=json_decode($mat_item_fields['elements'],true);
$buf='';
$buf.=$mat_item_fields['name'].' '.$mat_item_fields['introtext'].' '.$mat_item_fields['fulltext'];
//buf prepare
$buf=trim($buf);
$buf=strip_tags($buf);
$buf=html_entity_decode($buf);
//$buf=preg_replace ("/&(.+?);/","",$buf);
$buf=mb_ereg_replace ("[^a-zA-Zа-яА-Я0-9ёЁ\s]","",$buf);
$buf=str_replace(" ", " ", $buf);
$buf=str_replace(" ", " ", $buf);
$buf=mb_strtoupper( $buf, 'UTF-8' );
$buf=explode(" ", $buf);
//lemmatize
$res=array();
foreach($buf as $key=>$word)
{
$word=mb_ereg_replace ("[^a-zA-Zа-яА-Я0-9ёЁ\-]","",$word);
if ($pluswords[$word] or is_numeric($word))
{
$res[$key]=$word;
}
else
{
$lemm = $morphy->lemmatize( $word );
$res[$key]=$lemm[0];
}
}
$buf=$res;
unset($res);
//count array
$buf_cnt=array_count_values($buf);
//расчет влияния даты на вес
if ($mat_item_fields['year'])
$weight_date=(int)(($mat_item_fields['year']-2008)*1.5);
else
$weight_date=(int)(1*1.5);
$double=array();
foreach($buf as $word)
{
$continue=false;
if (mb_strlen($word)<=2){$continue=true;}
if (!$morphy->lemmatize($word, phpMorphy:: IGNORE_PREDICT)) {$continue=true;}
if ($pluswords[$word]) {$continue=false;}
if (is_numeric($word)) {$continue=false;}
if (in_array($word,$double)) {$continue=true;}
if (!$last) {$continue=true;}
if (!$word) {$continue=true;}
if ($continue) {continue;}
//echo $word.' ';
$double[]=$word;
if ($fixwords[$word])
{
$zn=$fixwords[$word];
}
elseif($pluswords[$word])
{
$zn=$pluswords[$word];
}
else
{
//set_weight
$partsOfSpeech = $morphy->getPartOfSpeech( $word );
$profile=false;
if ( !$profile ) {
$profile = [
// Служебные части речи //
'ПРЕДЛ' => 0,
'СОЮЗ' => 0,
'МЕЖД' => 0,
'ВВОДН' => 0,
'ЧАСТ' => 0,
'МС' => 0,
// Наиболее значимые части речи //
'С' => 5,
'Г' => 5,
'П' => 3,
'Н' => 3,
// Остальные части речи //
'DEFAULT' => 1
];
}
if ( !$partsOfSpeech ) { $weight= $profile[ 'DEFAULT' ]; }
else
{
for ( $i = 0; $i < count( $partsOfSpeech ); $i++ ) {
if ( isset( $profile[ $partsOfSpeech[ $i ] ] ) ) {
$range[] = $profile[ $partsOfSpeech[ $i ] ];
} else {
$range[] = $profile[ 'DEFAULT' ];
}
}
$weight= max( $range );
}
//count
$count=$buf_cnt[$word];
if ($count==''){ $count=0;}
//рассчет значимости
$zn=$count*$weight*$weight_date;
}
//record to db
$index = $db->setQuery("SELECT f.id id
FROM jos_zoo_finder f, jos_zoo_finders fs
WHERE fs.type=\"0\" and f.fid=fs.id and fs.matid=\"{$item}\" and f.word=\"{$word}\"")->loadAssoc();
if ($index[id])
$db->setQuery("UPDATE jos_zoo_finder SET weight=\"{$zn}\" where id=\"{$index[id]}\" and fid=\"{$last}\" and word=\"{$word}\"")->Execute();
else
$db->setQuery("INSERT INTO jos_zoo_finder (id,fid,word,weight)
VALUES(NULL,\"{$last}\",\"{$word}\",\"{$zn}\")")->Execute();
}
}
}
}
}
public function onBeforeCompileHead(){}
public function onAfterInitialise(){ }
public function onAfterRoute(){ }
public function onAfterDispatch(){ }
public function onBeforeRender(){ }
}