system/classes/searchcriteria.class.php
author Sami Barakat
Sun, 18 Oct 2009 02:05:53 +0100
branchHEAD
changeset 7387 39932e68c099
parent 7342 187ee0efcee2
child 7391 0bee991937cc
permissions -rwxr-xr-x
Looks like the Search API function setComment() is not required after all (cf. bug #0000902)
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Geeklog 1.6                                                               |
     6 // +---------------------------------------------------------------------------+
     7 // | searchcriteria.class.php                                                  |
     8 // |                                                                           |
     9 // | This class acts as a container which allows data to be passed between the |
    10 // | search engine and plugins.                                                |
    11 // +---------------------------------------------------------------------------+
    12 // | Copyright (C) 2000-2009 by the following authors:                         |
    13 // |                                                                           |
    14 // | Authors: Sami Barakat, s.m.barakat AT gmail DOT com                       |
    15 // +---------------------------------------------------------------------------+
    16 // |                                                                           |
    17 // | This program is free software; you can redistribute it and/or             |
    18 // | modify it under the terms of the GNU General Public License               |
    19 // | as published by the Free Software Foundation; either version 2            |
    20 // | of the License, or (at your option) any later version.                    |
    21 // |                                                                           |
    22 // | This program is distributed in the hope that it will be useful,           |
    23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    25 // | GNU General Public License for more details.                              |
    26 // |                                                                           |
    27 // | You should have received a copy of the GNU General Public License         |
    28 // | along with this program; if not, write to the Free Software Foundation,   |
    29 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    30 // |                                                                           |
    31 // +---------------------------------------------------------------------------+
    32 
    33 class SearchCriteria {
    34 
    35     // PRIVATE PROPERTIES
    36     var $_sql = '';
    37     var $_ftsql = '';
    38     var $_pluginLabel;
    39     var $_pluginName;
    40     var $_rank;
    41     var $_url_rewrite;
    42     var $_append_query;
    43     var $_results = array();
    44 
    45     function SearchCriteria( $pluginName, $pluginLabel )
    46     {
    47         $this->_pluginName = $pluginName;
    48         $this->_pluginLabel = $pluginLabel;
    49         $this->_rank = 3;
    50         $this->_url_rewrite = false;
    51         $this->_append_query = true;
    52     }
    53 
    54     function setSQL( $sql )
    55     {
    56         $this->_sql = $sql;
    57     }
    58 
    59     function setFTSQL( $ftsql )
    60     {
    61         $this->_ftsql = $ftsql;
    62     }
    63 
    64     function setRank( $rank )
    65     {
    66         $this->_rank = $rank;
    67     }
    68 
    69     function setURLRewrite( $url_rewrite )
    70     {
    71         $this->_url_rewrite = $url_rewrite;
    72     }
    73 
    74     function setAppendQuery( $append_query )
    75     {
    76         $this->_append_query = $append_query;
    77     }
    78 
    79     function setResults( $result_arr )
    80     {
    81         $this->_results = $result_arr;
    82     }
    83 
    84     function getSQL()
    85     {
    86         return $this->_sql;
    87     }
    88 
    89     function getFTSQL()
    90     {
    91         global $_DB_dbms;
    92 
    93         // When only one SQL statment is set we assume it is for MySQL
    94         if ($this->_ftsql != '' && (is_string($this->_ftsql) && $_DB_dbms == 'mysql') || is_array($this->_ftsql)) {
    95             return $this->_ftsql;
    96         } else {
    97             return '';
    98         }
    99     }
   100 
   101     function getName()
   102     {
   103         return $this->_pluginName;
   104     }
   105 
   106     function getLabel()
   107     {
   108         return $this->_pluginLabel;
   109     }
   110 
   111     function getRank()
   112     {
   113         return $this->_rank;
   114     }
   115 
   116     function UrlRewriteEnable()
   117     {
   118         return $this->_url_rewrite;
   119     }
   120 
   121     function AppendQueryEnable()
   122     {
   123         return $this->_append_query;
   124     }
   125 
   126     function getResults()
   127     {
   128         return $this->_results;
   129     }
   130 
   131     function buildSearchSQL( $keyType, $query, $columns, $sql = '' )
   132     {
   133         if ($keyType == 'all')
   134         {
   135             // must contain ALL of the keywords
   136             $words = explode(' ', $query);
   137             $sep = 'AND';
   138 
   139             $ftwords['mysql'] = '+' . str_replace(' ', ' +', $query);
   140             $ftwords['mssql'] = '"' . str_replace(' ', '" AND "', $query) . '"';
   141         }
   142         else if ($keyType == 'any')
   143         {
   144             // must contain ANY of the keywords
   145             $words = explode(' ', $query);
   146             $sep = 'OR ';
   147             $ftwords['mysql'] = $query;
   148             $ftwords['mssql'] = '"' . str_replace(' ', '" OR "', $query) . '"';
   149         }
   150         else
   151         {
   152             // do an exact phrase search (default)
   153             $words = array($query);
   154             $sep = '   ';
   155 
   156             // Puttings quotes around a single word in mysql really slows things down
   157             if (strpos($query, ' ') !== false) {
   158                 $ftwords['mysql'] = '"' . $query . '"';
   159             } else {
   160                 $ftwords['mysql'] = $query;
   161             }
   162             $ftwords['mssql'] = '"' . $query . '"';
   163         }
   164 
   165         $titles = (isset($_GET['title']) && isset($columns['title'])) ? true : false;
   166 
   167         if ($titles) {
   168             $strcol = $columns['title'];
   169         } else {
   170             $strcol = implode(',', $columns);
   171         }
   172 
   173         $ftsql['mysql'] = $sql . "AND MATCH($strcol) AGAINST ('{$ftwords['mysql']}' IN BOOLEAN MODE)";
   174         $ftsql['mssql'] = $sql . "AND CONTAINS(($strcol), '{$ftwords['mssql']}')";
   175 
   176         $tmp = 'AND (';
   177         foreach ($words AS $word)
   178         {
   179             $word = trim($word);
   180             $tmp .= '(';
   181 
   182             if ($titles) {
   183                 $tmp .= $columns['title'] . " LIKE '%$word%' OR ";
   184             } else {
   185                 foreach ($columns AS $col) {
   186                     $tmp .= "$col LIKE '%$word%' OR ";
   187                 }
   188             }
   189             $tmp = substr($tmp, 0, -4) . ") $sep ";
   190         }
   191         $sql .= substr($tmp, 0, -5) . ') ';
   192 
   193         return array($sql,$ftsql);
   194     }
   195 
   196     function getDateRangeSQL( $type = 'WHERE', $column, $datestart, $dateend )
   197     {
   198         if (!empty($datestart) && !empty($dateend))
   199         {
   200             $delim = substr($datestart, 4, 1);
   201             if (!empty($delim))
   202             {
   203                 $DS = explode($delim, $datestart);
   204                 $DE = explode($delim, $dateend);
   205                 $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]);
   206                 $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]);
   207                 return " $type (UNIX_TIMESTAMP($column) BETWEEN '$startdate' AND '$enddate') ";
   208             }
   209         }
   210 
   211         return "";
   212     }
   213 }
   214 
   215 ?>