gforge-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gforge-commits] gforge/common/search ForumSearchQuery.class, NONE, 1.1


From: gsmet
Subject: [Gforge-commits] gforge/common/search ForumSearchQuery.class, NONE, 1.1 ExportProjectSearchQuery.class, NONE, 1.1 SearchQuery.class, NONE, 1.1 PeopleSearchQuery.class, NONE, 1.1 SkillSearchQuery.class, NONE, 1.1 ProjectSearchQuery.class, NONE, 1.1 ArtifactSearchQuery.class, NONE, 1.1
Date: Mon, 09 Feb 2004 04:22:25 -0600

Update of /cvsroot/gforge/gforge/common/search
In directory db.perdue.net:/tmp/cvs-serv17528/common/search

Added Files:
        ForumSearchQuery.class ExportProjectSearchQuery.class 
        SearchQuery.class PeopleSearchQuery.class 
        SkillSearchQuery.class ProjectSearchQuery.class 
        ArtifactSearchQuery.class 
Log Message:
implemented a new search engine architecture

--- NEW FILE: ForumSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: ForumSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

require_once('common/search/SearchQuery.class');

class ForumSearchQuery extends SearchQuery {
        
        /**
         * group id
         *
         * @var int $groupId
         */
        var $groupId;
        
        /**
         * forum id
         *
         * @var int $groupId
         */
        var $forumId;
        
        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         * @param int $groupId group id
         * @param int $forumId forum id
         */
        function ForumSearchQuery($words, $offset, $isExact, $groupId, 
$forumId) {
                $this->groupId = $groupId;
                $this->forumId = $forumId;
                
                $this->SearchQuery($words, $offset, $isExact);
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $sql = 'SELECT forum.msg_id, forum.subject, forum.post_date, 
users.realname '
                        . 'FROM forum,users '
                        . 'WHERE users.user_id=forum.posted_by '
                        . 'AND (('.$this->getIlikeCondition('forum.body', 
$this->words).') '
                        . 'OR ('.$this->getIlikeCondition('forum.subject', 
$this->words).')) '
                        . 'AND forum.group_forum_id=\''.$this->forumId.'\' '
                        . 'GROUP BY msg_id, subject, post_date, realname';

                return $sql;
        }
        
        /**
         * getSearchByIdQuery - get the sql query built to get the search 
results when we are looking for an int
         *
         * @return string sql query to execute
         */     
        function getSearchByIdQuery() {
                $sql = 'SELECT msg_id '
                        . 'FROM forum '
                        . 'WHERE msg_id=\''.$this->searchId.'\' '
                        . 'AND group_forum_id=\''.$this->forumId.'\'';

                return $sql;
        }
}

?>
--- NEW FILE: ExportProjectSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: ExportProjectSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet 
Exp $
 */

require_once('common/search/SearchQuery.class');

class ExportProjectSearchQuery extends SearchQuery {

        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         */
        function ExportProjectSearchQuery($words, $offset, $isExact) {  
                $this->SearchQuery($words, $offset, $isExact, 200);
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $groupNameCond = $this->getIlikeCondition('group_name', 
$this->words);
                $groupDescriptionCond = 
$this->getIlikeCondition('short_description', $this->words);
                $groupUnixNameCond = 
$this->getIlikeCondition('unix_group_name', $this->words);
                
                $sql = 'SELECT group_name,unix_group_name,type,groups.group_id, 
'
                        .'short_description,license,register_time '
                        .'FROM groups '
                        .'WHERE status IN (\'A\', \'H\') '
                        .'AND is_public=\'1\' '
                        .'AND groups.short_description<>\'\' '
                        .'AND (('.$groupNameCond.') OR 
('.$groupDescriptionCond.') OR ('.$groupUnixNameCond.'))';

                return $sql;
        }
}

?>
--- NEW FILE: SearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: SearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

class SearchQuery extends Error {
        /**
         * the operator between each part of the query. Can be AND or OR.
         *
         * @var string $operator
         */
        var $operator;  
        /**
         * Number of rows per page
         *
         * @var int $rowsPerPage
         */
        var $rowsPerPage;
        /**
         * Number of rows we will display on the page
         *
         * @var int $rowsCount
         */
        var $rowsCount = 0;
        /**
         * Number of rows returned by the query
         *
         * @var int $rowsTotalCount
         */
        var $rowsTotalCount = 0;
        /**
         * Offset
         *
         * @var int $offset
         */
        var $offset = 0;
        /**
         * Result handle
         *
         * @var resource $result
         */
        var $result;
        /**
         * When search by id is enabled, the id to search for
         *
         * @var int $searchId
         */
        var $searchId = false;
        /**
         * if we want to search for all the words or if only one is sufficient
         *
         * @var boolean $isExact
         */
         var $isExact = false;

        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one is sufficient
         * @param int $rowsPerPage number of rows per page
         */
        function SearchQuery($words, $offset, $isExact, $rowsPerPage = 
SEARCH__DEFAULT_ROWS_PER_PAGE) {
                $this->cleanSearchWords($words);
                
                $this->rowsPerPage = $rowsPerPage;
                $this->offset = $offset;
                $this->isExact = $isExact;
                $this->operator = $this->getOperator();
        }
        
        /**
         * cleanSearchWords - clean the words we are searching for
         *
         * @param string $words words we are searching for
         */
        function cleanSearchWords($words) {
                $words = trim($words);
                if(!$words) {
                        $this->setError('error_criteria_not_specified');
                        return;
                }
                if(is_numeric($words) && $this->implementsSearchById()) {
                        $this->searchId = (int) $words;
                } else {
                        $words = htmlspecialchars($words);
                        $words = preg_replace("/[ \t]+/", ' ', $words);
                        if(strlen($words) < 3) {
                                $this->setError('error_search_minlength');
                                return;
                        }
                        $this->words = explode(' ', quotemeta($words));
                }
        }
        
        /**
         * executeQuery - execute the SQL query to get the results
         */ 
        function executeQuery() {
                if($this->searchId) {
                        $query = $this->getSearchByIdQuery();
                } else {
                        $query = $this->getQuery();
                }

                $this->result = db_query(
                        $query,
                        $this->rowsPerPage + 1,
                        $this->offset,
                        SYS_DB_SEARCH
                );
                $this->rowsTotalCount = db_numrows($this->result);
                $this->rowsCount = min($this->rowsPerPage, 
$this->rowsTotalCount);
        }
        
        /**
         * getQuery - returns the sql query built to get the search results
         * This is an abstract method. It _MUST_ be implemented in children 
classes.
         *
         * @return string sql query to execute
         */
        function getQuery() {
                return;
        }

        /**
         * getIlikeCondition - build the ILIKE condition of the SQL query for a 
given field name
         *
         * @param string $fieldName name of the field in the ILIKE condition
         * @return string the condition
         */
        function getIlikeCondition($fieldName) {
                return $fieldName." ILIKE '%" . implode("%' ".$this->operator." 
".$fieldName." ILIKE '%", $this->words) ."%'";
        }
        
        /**
         * getOperator - get the operator we have to use in ILIKE condition
         *
         * @return string AND if it is an exact search, OR otherwise
         */
        function getOperator() {
                if($this->isExact) {
                        return 'AND';
                } else {
                        return 'OR';
                }
        }
        
        /**
         * implementsSearchById - check if the current object implements the 
search by id feature by having a getSearchByIdQuery method
         *
         * @return boolean true if our object implements search by id, false 
otherwise.
         */
        function implementsSearchById() {
                return method_exists($this, 'getSearchByIdQuery');
        }
        
        /**
         * getResult - returns the result set
         *
         * @return resource result set
         */
        function & getResult() {
                return $this->result;
        }
        
        /**
         * getRowsCount - returns number of rows for the current page
         *
         * @return int rows count for the current page
         */
        function getRowsCount() {
                return $this->rowsCount;
        }
        
        /**
         * getRowsTotalCount - returns total number of rows
         *
         * @return int rows count
         */
        function getRowsTotalCount() {
                return $this->rowsTotalCount;
        }
        
        /**
         * getOffset - returns the offset
         *
         * @return int offset
         */
        function getOffset() {
                return $this->offset;
        }
        
        /**
         * getRowsPerPage - returns number of rows per page
         *
         * @return int number of rows per page
         */
        function getRowsPerPage() {
                return $this->rowsPerPage;
        }
        
        /**
         * getWords - returns the array containing words we are searching for
         *
         * @return array words we are searching for
         */
        function getWords() {
                return $this->words;
        }

}

?>
--- NEW FILE: PeopleSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: PeopleSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

require_once('common/search/SearchQuery.class');

class PeopleSearchQuery extends SearchQuery {

        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         */
        function PeopleSearchQuery($words, $offset, $isExact) { 
                $this->SearchQuery($words, $offset, $isExact);
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $sql = 'SELECT user_name,user_id,realname ' 
                        . 'FROM users ' 
                        . 'WHERE (('.$this->getIlikeCondition('user_name', 
$this->words).') ' 
                        . 'OR ('.$this->getIlikeCondition('realname', 
$this->words).')) ' 
                        . 'AND (status=\'A\') ' 
                        . 'ORDER BY user_name';
                return $sql;
        }
}

?>
--- NEW FILE: SkillSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: SkillSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

require_once('common/search/SearchQuery.class');

class SkillSearchQuery extends SearchQuery {

        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         */
        function SkillSearchQuery($words, $offset, $isExact) {  
                $this->SearchQuery($words, $offset, $isExact);  
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $sql = 'SELECT * '
                        . 'FROM skills_data, users, skills_data_types '
                        . 'WHERE 
(('.$this->getIlikeCondition('skills_data.title', $this->words).') '
                        . 'OR 
('.$this->getIlikeCondition('skills_data.keywords', $this->words).')) '
                        . 'AND (skills_data.user_id=users.user_id) '
                        . 'AND (skills_data.type=skills_data_types.type_id) '
                        . 'ORDER BY finish DESC';
                return $sql;
        }
        
}

?>
--- NEW FILE: ProjectSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: ProjectSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

require_once('common/search/SearchQuery.class');

class ProjectSearchQuery extends SearchQuery {

        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         */
        function ProjectSearchQuery($words, $offset, $isExact) {        
                $this->SearchQuery($words, $offset, $isExact);
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $groupNameCond = $this->getIlikeCondition('group_name', 
$this->words);
                $groupDescriptionCond = 
$this->getIlikeCondition('short_description', $this->words);
                $groupUnixNameCond = 
$this->getIlikeCondition('unix_group_name', $this->words);
                
                $sql = 'SELECT group_name, unix_group_name, type, group_id, 
short_description '
                        .'FROM groups '
                        .'WHERE status IN (\'A\', \'H\') '
                        .'AND is_public=\'1\' '
                        .'AND (('.$groupNameCond.') OR 
('.$groupDescriptionCond.') OR ('.$groupUnixNameCond.'))';

                return $sql;
        }
        
}

?>
--- NEW FILE: ArtifactSearchQuery.class ---
<?php

/**
 * GForge Search Engine
 *
 * Portions Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
 *
 * http://gforge.org
 *
 * @version $Id: ArtifactSearchQuery.class,v 1.1 2004/02/09 10:22:21 gsmet Exp $
 */

require_once('common/search/SearchQuery.class');

class ArtifactSearchQuery extends SearchQuery {
        
        /**
         * group id
         *
         * @var int $groupId
         */
        var $groupId;
        
        /**
         * artifact id
         *
         * @var int $artifactId
         */
        var $artifactId;
        
        /**
         * Constructor
         *
         * @param string $words words we are searching for
         * @param int $offset offset
         * @param boolean $isExact if we want to search for all the words or if 
only one matching the query is sufficient
         * @param int $groupId group id
         * @param int $artifactId artifact id
         */
        function ArtifactSearchQuery($words, $offset, $isExact, $groupId, 
$artifactId) {
                $this->groupId = $groupId;
                $this->artifactId = $artifactId;
                
                $this->SearchQuery($words, $offset, $isExact);
        }

        /**
         * getQuery - get the sql query built to get the search results
         *
         * @return string sql query to execute
         */
        function getQuery() {
                $sql = 'SELECT DISTINCT ON (a.group_artifact_id,a.artifact_id) 
a.group_artifact_id,a.artifact_id,a.summary,a.open_date,users.realname '
                        . 'FROM artifact a,users ' 
                        . 'WHERE a.group_artifact_id=\''.$this->artifactId.'\' '
                        . 'AND users.user_id=a.submitted_by '
                        . 'AND (('.$this->getIlikeCondition('a.details', 
$this->words).') ' 
                        . 'OR ('.$this->getIlikeCondition('a.summary', 
$this->words).')) '
                        . 'ORDER BY group_artifact_id ASC, artifact_id ASC';

                return $sql;
        }

        /**
         * getSearchByIdQuery - get the sql query built to get the search 
results when we are looking for an int
         *
         * @return string sql query to execute
         */     
        function getSearchByIdQuery() {
                $sql = 'SELECT DISTINCT ON (a.group_artifact_id,a.artifact_id) 
a.group_artifact_id, a.artifact_id '
                        . 'FROM artifact a ' 
                        . 'WHERE a.group_artifact_id=\''.$this->artifactId.'\' '
                        . 'AND a.artifact_id=\''.$this->searchId.'\'';

                return $sql;
        }
        
}

?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]