searchutil.inc.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 394 行 · 第 1/2 页
PHP
394 行
<?php
/**
* $Id: searchutil.inc.php 8387 2008-04-22 16:36:04Z kevin_fourie $
*
* 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(KT_LIB_DIR . '/search/savedsearch.inc.php');
require_once(KT_LIB_DIR . '/browse/Criteria.inc');
class KTSearchUtil {
// {{{ _oneCriteriaSetToSQL
/**
* Handles leaf criteria set (ie, no subgroups), generating SQL for
* the values in the criteria.
*
* (This would be the place to extend criteria to support contains,
* starts with, ends with, greater than, and so forth.)
*/
function _oneCriteriaSetToSQL($aOneCriteriaSet) {
$aSQL = array();
$aJoinSQL = array();
$criteria_set = array();
/*
* First phase: get criterion object for search or the direct
* SQL to use.
*
* XXX: Why is there $order there?
*/
foreach ($aOneCriteriaSet as $order => $dataset) {
$type = KTUtil::arrayGet($dataset, "type");
$sql = KTUtil::arrayGet($dataset, "sql");
if (!empty($type)) {
$oCriteriaRegistry =& KTCriteriaRegistry::getSingleton();
$oCriterion = $oCriteriaRegistry->getCriterion($dataset['type']);
if (PEAR::isError($oCriterion)) {
return PEAR::raiseError(_kt('Invalid criteria specified.'));
}
$criteria_set[] = array($oCriterion, $dataset["data"]);
} else if (!empty($sql)) {
$criteria_set[] = $sql;
} else {
return PEAR::raiseError(_kt('Invalid criteria specified.'));
}
}
/*
* Second phase: Create an individual SQL query per criteria.
*/
foreach ($criteria_set as $oCriterionPair) {
$oCriterion = $oCriterionPair[0];
$aReq = $oCriterionPair[1];
if (is_object($oCriterion)) {
$res = $oCriterion->searchSQL($aReq);
if (!is_null($res)) {
$aSQL[] = $res;
}
$res = $oCriterion->searchJoinSQL();
if (!is_null($res)) {
$aJoinSQL[] = $res;
}
} else {
$aSQL[] = array($oCriterion, $aReq);
}
}
/*
* Third phase: build up $aCritQueries and $aCritParams, and put
* parentheses around them.
*/
$aCritParams = array();
$aCritQueries = array();
foreach ($aSQL as $sSQL) {
if (is_array($sSQL)) {
$aCritQueries[] = '('.$sSQL[0].')';
$aCritParams = kt_array_merge($aCritParams , $sSQL[1]);
} else {
$aCritQueries[] = '('.$sSQL.')';
}
}
if (count($aCritQueries) == 0) {
return PEAR::raiseError(_kt("No search criteria were specified"));
}
return array($aCritQueries, $aCritParams, $aJoinSQL);
}
// }}}
// {{{ criteriaSetToSQL
/**
* Converts a criteria set to the SQL joins, where clause, and
* parameters necessary to ensure that the criteria listed restrict
* the documents returned to those that match the criteria.
*
* Perhaps poorly called recursively to handle criteria that involve
* subgroups to allow infinitely nested criteria.
*
* Returns a list of the following elements:
* - String representing the where clause
* - Array of parameters that go with the where clause
* - String with the SQL necessary to join with the tables in the
* where clause
*/
function criteriaSetToSQL($aCriteriaSet, $iRecurseLevel = 0) {
$aJoinSQL = array();
$aSearchStrings = array();
$aParams = array();
/*
* XXX: We unnecessarily force the base criteria to have
* subgroups at the top level, even though we most often only
* have a single "subgroup".
*/
foreach ($aCriteriaSet["subgroup"] as $k => $aOneCriteriaSet) {
/*
* Each subgroup will either have values or it will have
* subgroups. They can't be mixed.
*/
$aValues = KTUtil::arrayGet($aOneCriteriaSet, "values");
$aSubgroup = KTUtil::arrayGet($aOneCriteriaSet, "subgroup");
if (!empty($aValues)) {
$res = KTSearchUtil::_oneCriteriaSetToSQL($aOneCriteriaSet["values"]);
if(PEAR::isError($res)) {
return $res;
}
list($aThisCritQueries, $aThisParams, $aThisJoinSQL) = $res;
$aJoinSQL = kt_array_merge($aJoinSQL, $aThisJoinSQL);
$aParams = kt_array_merge($aParams, $aThisParams);
$tabs = str_repeat("\t", ($iRecurseLevel + 2));
$aSearchStrings[] = "\n$tabs(\n$tabs\t" . join("\n " . KTUtil::arrayGet($aOneCriteriaSet, 'join', "AND") . " ", $aThisCritQueries) . "\n$tabs)";
} else if (!empty($aSubgroup)) {
/*
* Recurse if we have a criteria set with subgroups.
* Recurselevel makes the tabs increase as we recurse so
* that the SQL statement is somewhat understandable.
*/
list($sThisSearchString, $aThisParams, $sThisJoinSQL) =
KTSearchUtil::criteriaSetToSQL($aOneCriteriaSet, $iRecurseLevel + 1);
$aJoinSQL[] = $sThisJoinSQL;
$aParams = kt_array_merge($aParams, $aThisParams);
$aSearchStrings[] = $sThisSearchString;
}
}
$aJoinSQL = array_unique($aJoinSQL);
$sJoinSQL = join(" ", $aJoinSQL);
$tabs = str_repeat("\t", $iRecurseLevel + 1);
$sSearchString = "\n$tabs(" . join("\n$tabs\t" . $aCriteriaSet['join'] . " ", $aSearchStrings) . "\n$tabs)";
return array($sSearchString, $aParams, $sJoinSQL);
}
// }}}
// {{{ permissionToSQL
/**
* Generates the necessary joins and where clause and parameters to
* ensure that all the documents returns are accessible to the user
* given for the permission listed.
*
* Returns a list of the following elements:
* - String representing the where clause
* - Array of parameters that go with the where clause
* - String with the SQL necessary to join with the tables in the
* where clause
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?