📄 expr.inc.php
字号:
<?php
/**
* $Id:$
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008 KnowledgeTree Inc.
* Portions copyright The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*
*/
//require_once('../../config/dmsDefaults.php');
/**
* This is the ideal case, but more complex
*
*/
// TODO: search expression evaluation needs some optimisation
require_once('indexing/indexerCore.inc.php');
require_once('search/fieldRegistry.inc.php');
require_once('search/exprConstants.inc.php');
class RankManager
{
/**
* This array contains the rankings of fields on database tables.
*
* @var array
*/
private $db;
/**
* Contains the rankings of metadata fields on fieldset/field combinations.
*
* @var array
*/
private $metadata;
/**
* Contains ranking factor for discussion matching
*
* @var float
*/
private $discussion;
/**
* Contains the ranking factor for text matching
*
* @var float
*/
private $text;
private function __construct()
{
$this->dbfields=array();
$sql = "SELECT groupname, itemname, ranking, type FROM search_ranking";
$rs = DBUtil::getResultArray($sql);
foreach($rs as $item)
{
switch ($item['type'])
{
case 'T':
$this->db[$item['groupname']][$item['itemname']] = $item['ranking']+0;
break;
case 'M':
$this->metadata[$item['groupname']][$item['itemname']] = $item['ranking']+0;
break;
case 'S':
switch($item['groupname'])
{
case 'Discussion':
$this->discussion = $item['ranking']+0;
break;
case 'DocumentText':
$this->text = $item['ranking']+0;
break;
}
break;
}
}
}
/**
* Enter description here...
*
* @return RankManager
*/
public static function get()
{
static $singleton = null;
if (is_null($singleton))
{
$singleton = new RankManager();
}
return $singleton;
}
public function scoreField($groupname, $type='T', $itemname='')
{
switch($type)
{
case 'T':
return $this->db[$groupname][$itemname];
case 'M':
return $this->metadata[$groupname][$itemname];
case 'S':
switch($groupname)
{
case 'Discussion':
return $this->discussion;
case 'DocumentText':
return $this->text;
default:
return 0;
}
default:
return 0;
}
}
}
class Expr
{
/**
* The parent expression
*
* @var Expr
*/
protected $parent;
protected static $node_id = 0;
protected $expr_id;
protected $context;
public function __construct()
{
$this->expr_id = Expr::$node_id++;
}
public function appliesToContext()
{
return ExprContext::DOCUMENT;
}
public function setContext($context)
{
$this->context = $context;
}
public function getContext()
{
return $this->context;
}
public function getExprId()
{
return $this->expr_id;
}
/**
* Coverts the expression to a string
*
* @return string
*/
public function __toString()
{
throw new Exception(sprintf(_kt('Not yet implemented in %s'), get_class($this)));
}
/**
* Reference to the parent expression
*
* @return Expr
*/
public function &getParent()
{
return $this->parent;
}
/**
* Sets the parent expiression
*
* @param Expr $parent
*/
public function setParent(&$parent)
{
$this->parent = &$parent;
}
/**
* Is the expression valid
*
* @return boolean
*/
public function is_valid()
{
return true;
}
public function isExpr()
{
return $this instanceof OpExpr;
}
public function isOpExpr()
{
return $this instanceof OpExpr;
}
public function isValueExpr()
{
return $this instanceof ValueExpr;
}
public function isValueListExpr()
{
return $this instanceof ValueListExpr;
}
public function isDbExpr()
{
return $this instanceof DBFieldExpr;
}
public function isFieldExpr()
{
return $this instanceof FieldExpr;
}
public function isSearchableText()
{
return $this instanceof SearchableText ;
}
public function isMetadataField()
{
return $this instanceof MetadataField;
}
public function toViz(&$str, $phase)
{
throw new Exception('To be implemented' . get_class($this));
}
public function toVizGraph($options=array())
{
$str = "digraph tree {\n";
if (isset($options['left-to-right']) && $options['left-to-right'])
{
$str .= "rankdir=LR\n";
}
$this->toViz($str, 0);
$this->toViz($str, 1);
$str .= "}\n";
if (isset($options['tofile']))
{
$path=dirname($options['tofile']);
$filename=basename($options['tofile']);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$base = substr($filename, 0, -strlen($ext)-1);
$curdir = getcwd();
chdir($_ENV['PWD']);
$dotfile="$base.$ext";
$jpgfile="$base.jpg";
$fp = fopen($dotfile,'wt');
fwrite($fp, $str);
fclose($fp);
system("dot -Tjpg -o$jpgfile $dotfile 2>1 >/dev/null ");
if (isset($options['view']) && $options['view'])
{
system("eog $jpgfile");
}
chdir($curdir);
}
return $str;
}
}
class FieldExpr extends Expr
{
/**
* Name of the field
*
* @var string
*/
protected $field;
protected $alias;
protected $display;
/**
* Constructor for the field expression
*
* @param string $field
*/
public function __construct($field, $display=null)
{
parent::__construct();
$this->field=$field;
if (is_null($display))
{
$display=get_class($this);
}
$this->display = $display;
$this->setAlias(get_class($this));
}
public function setAlias($alias)
{
$this->alias=$alias;
}
public function getDisplay()
{
return $this->display;
}
public function getAlias()
{
return $this->alias;
}
public function getFullName()
{
return $this->alias . '.' . $this->field;
}
/**
* Returns the field
*
* @return string
*/
public function getField()
{
return $this->field;
}
/**
* Coverts the expression to a string
*
* @return string
*/
public function __toString()
{
return $this->display;
}
public function toViz(&$str, $phase)
{
if ($phase == 0)
{
$expr_id = $this->getExprId();
$str .= "struct$expr_id [style=rounded, label=\"$expr_id: FIELD[$this->alias]\"]\n";
}
}
public function rewrite(&$left, &$op, &$right, $not=false)
{
$input = $left->getInputRequirements();
if ($input['value']['type'] != FieldInputType::FULLTEXT)
{
return;
}
if ($right->isValueExpr())
{
$value = $right->getValue();
}
else
{
$value = $right;
}
if ((substr($value,0,1) == '\'' && substr($value,-1) == '\'') || (substr($value,0,1) == '"' && substr($value,-1) == '"'))
{
$value = trim( substr($value,1,-1) );
$right = new ValueExpr($value);
}
else
{
OpExpr::rewriteString($left, $op, $right, $not);
}
}
}
class ExprContext
{
const DOCUMENT = 1;
const FOLDER = 2;
const DOCUMENT_AND_FOLDER = 3;
}
class DBFieldExpr extends FieldExpr
{
/**
* The table the field is associated with
*
* @var string
*/
protected $table;
protected $jointable;
protected $joinfield;
protected $matchfield;
protected $quotedvalue;
/**
* Constructor for the database field
*
* @param string $field
* @param string $table
*/
public function __construct($field, $table, $display=null)
{
if (is_null($display))
{
$display = get_class($this);
}
parent::__construct($field, $display);
$this->table=$table;
$this->jointable = null;
$this->joinfield = null;
$this->matchfield = null;
$this->quotedvalue=true;
}
/**
* Returns the table name
*
* @return string
*/
public function getTable()
{
return $this->table;
}
public function joinTo($table, $field)
{
$this->jointable=$table;
$this->joinfield=$field;
}
public function matchField($field)
{
$this->matchfield = $field;
}
public function modifyName($name)
{
return $name;
}
public function modifyValue($value)
{
return $value;
}
public function getJoinTable() { return $this->jointable; }
public function getJoinField() { return $this->joinfield; }
public function getMatchingField() { return $this->matchfield; }
public function isValueQuoted($quotedvalue = null)
{
if (isset($quotedvalue))
{
$this->quotedvalue = $quotedvalue;
}
return $this->quotedvalue;
}
}
class MetadataField extends DBFieldExpr
{
protected $fieldset;
protected $fieldid;
protected $fieldsetid;
public function __construct($fieldset, $field, $fieldsetid, $fieldid)
{
parent::__construct($field, 'document_fields_link');
$this->fieldset=$fieldset;
$this->fieldid=$fieldid;
$this->fieldsetid=$fieldsetid;
}
public function getFieldSet()
{
return $this->fieldset;
}
public function getFieldId()
{
return $this->fieldid;
}
public function getFieldSetId()
{
return $this->fieldsetid;
}
public function getInputRequirements()
{
return array('value'=>array('type'=>FieldInputType::TEXT));
}
/**
* Coverts the expression to a string
*
* @return string
*/
public function __toString()
{
return "METADATA[$this->fieldset][$this->field]";
}
}
class SearchableText extends FieldExpr
{
}
class ValueExpr extends Expr
{
/**
* The value
*
* @var mixed
*/
protected $value;
protected $fuzzy;
protected $proximity;
/**
* Constructor for the value expression
*
* @param mixed $value
*/
public function __construct($value, $fuzzy=null, $proximity=null)
{
parent::__construct();
// some keywords are used by lucene, and may conflict. for some reason, if it is lowercase, the problem does not occur.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -