eZ Publish  [4.0]
ezsearch.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZSearch class
00004 //
00005 // Created on: <25-Jun-2002 10:56:09 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZSearch
00033   \ingroup eZKernel
00034   \brief eZSearch handles indexing of objects to the search engine
00035 
00036 */
00037 
00038 //include_once( 'lib/ezutils/classes/ezini.php' );
00039 
00040 class eZSearch
00041 {
00042     /*!
00043     */
00044     function eZSearch()
00045     {
00046 
00047     }
00048 
00049     /*!
00050      \static
00051      Will remove the index from the given object from the search engine
00052     */
00053     static function removeObject( $contentObject )
00054     {
00055         $searchEngine = eZSearch::getEngine();
00056 
00057         if ( is_object( $searchEngine ) )
00058         {
00059             $searchEngine->removeObject( $contentObject );
00060         }
00061     }
00062 
00063     /*!
00064      \static
00065      Will index the content object to the search engine.
00066     */
00067     static function addObject( $contentObject )
00068     {
00069         $searchEngine = eZSearch::getEngine();
00070 
00071         if ( is_object( $searchEngine ) )
00072         {
00073             $searchEngine->addObject( $contentObject, '/content/view/' );
00074         }
00075     }
00076 
00077     /*!
00078      \static
00079      Runs a query to the search engine.
00080     */
00081     static function search( $searchText, $params, $searchTypes = array() )
00082     {
00083         $searchEngine = eZSearch::getEngine();
00084 
00085         if ( is_object( $searchEngine ) )
00086         {
00087             return $searchEngine->search( $searchText, $params, $searchTypes );
00088         }
00089     }
00090 
00091     /*!
00092      \static
00093     */
00094     static function normalizeText( $text )
00095     {
00096         $searchEngine = eZSearch::getEngine();
00097 
00098         if ( is_object( $searchEngine ) )
00099         {
00100             return $searchEngine->normalizeText( $text );
00101         }
00102 
00103         return '';
00104     }
00105 
00106     /*!
00107      \static
00108       returns search parameters in array based on supported search types and post variables
00109      */
00110     static function buildSearchArray()
00111     {
00112         $searchEngine = eZSearch::getEngine();
00113 
00114         $searchArray = array();
00115         $andSearchParts = array();
00116         $searchTypesDefinition = array( 'types' => array(), 'general_filter' => array() );
00117 
00118         if ( is_object( $searchEngine ) )
00119         {
00120             // This method was renamed in pre 3.5 trunk
00121             if ( method_exists( $searchEngine, 'supportedSearchTypes' ) )
00122             {
00123                 $searchTypesDefinition = $searchEngine->supportedSearchTypes();  // new and correct
00124             }
00125             else
00126             {
00127                 $searchTypesDefinition = $searchEngine->suportedSearchTypes();  // deprecated
00128             }
00129         }
00130 
00131         $http = eZHTTPTool::instance();
00132 
00133         foreach ( $searchTypesDefinition['types'] as $searchType )
00134         {
00135             $postVariablePrefix = 'Content_search_' . $searchType['type'] . '_' . $searchType['subtype'] . '_';
00136             //print $postVariablePrefix . "\n";
00137             //print_r( $searchType['params'] );
00138             $searchArrayPartForType = array();
00139 
00140             $searchPart = array();
00141             $valuesFetched = false;
00142             $valuesMissing = false;
00143             foreach ( $searchType['params'] as $parameter )
00144             {
00145                 eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $postVariablePrefix . $parameter,
00146                                             'post variable to check' );
00147 
00148                 if ( $http->hasVariable( $postVariablePrefix . $parameter ) )
00149                 {
00150                     $values = $http->variable( $postVariablePrefix . $parameter );
00151                     eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $values, 'fetched values' );
00152 
00153                     foreach ( $values as $i => $value )
00154                     {
00155                         $searchArrayPartForType[$i][$parameter] = $values[$i];
00156                         $valuesFetched = true;
00157                     }
00158                 }
00159                 else
00160                 {
00161                     eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $postVariablePrefix . $parameter,
00162                                                 'post variable does not exist' );
00163                     $valuesMissing = true;
00164                     break;
00165                 }
00166             }
00167 
00168             if ( $valuesFetched == true && $valuesMissing == false )
00169             {
00170                 eZDebugSetting::writeDebug( 'kernel-search-ezsearch', 'adding values to search' );
00171                 foreach ( array_keys( $searchArrayPartForType ) as $key )
00172                 {
00173                     $part =& $searchArrayPartForType[$key];
00174                     $part['type'] = $searchType['type'];
00175                     $part['subtype'] = $searchType['subtype'];
00176 
00177                     if ( $part['type'] == 'attribute' )
00178                     {
00179                         // Remove incomplete search parts from the search.
00180                         // An incomplete search part is for instance an empty text field,
00181                         // or a select box with no selected values.
00182 
00183                         // This functionality has been moved to the search engine.
00184                         // Checking if it is defined in the search engine
00185                         if ( method_exists( $searchEngine, 'isSearchPartIncomplete' ) )
00186                         {
00187                             $removePart = $searchEngine->isSearchPartIncomplete( $part );
00188                         }
00189                         else // for backwards compatibility
00190                         {
00191                             $removePart = false;
00192                             switch ( $part['subtype'] )
00193                             {
00194                                 case 'fulltext':
00195                                 {
00196                                     if ( !isset( $part['value'] ) || $part['value'] == '' )
00197                                         $removePart = true;
00198                                 }
00199                                 break;
00200 
00201                                 case 'patterntext':
00202                                 {
00203                                     if ( !isset( $part['value'] ) || $part['value'] == '' )
00204                                         $removePart = true;
00205                                 }
00206                                 break;
00207 
00208                                 case 'integer':
00209                                 {
00210                                     if ( !isset( $part['value'] ) || $part['value'] == '' )
00211                                         $removePart = true;
00212                                 }
00213                                 break;
00214 
00215                                 case 'integers':
00216                                 {
00217                                     if ( !isset( $part['values'] ) || count( $part['values'] ) == 0 )
00218                                         $removePart = true;
00219                                 }
00220                                 break;
00221 
00222                                 case 'byrange':
00223                                 {
00224                                     if ( !isset( $part['from'] ) || $part['from'] == '' ||
00225                                          !isset( $part['to'] ) || $part['to'] == '' )
00226                                         $removePart = true;
00227                                 }
00228                                 break;
00229 
00230                                 case 'byidentifier':
00231                                 {
00232                                     if ( !isset( $part['value'] ) || $part['value'] == '' )
00233                                         $removePart = true;
00234                                 }
00235                                 break;
00236 
00237                                 case 'byidentifierrange':
00238                                 {
00239                                     if ( !isset( $part['from'] ) || $part['from'] == '' ||
00240                                          !isset( $part['to'] ) || $part['to'] == '' )
00241                                         $removePart = true;
00242                                 }
00243                                 break;
00244 
00245                                 case 'integersbyidentifier':
00246                                 {
00247                                     if ( !isset( $part['values'] ) || count( $part['values'] ) == 0 )
00248                                         $removePart = true;
00249                                 }
00250                                 break;
00251 
00252                                 case 'byarea':
00253                                 {
00254                                     if ( !isset( $part['from'] ) || $part['from'] == '' ||
00255                                          !isset( $part['to'] ) || $part['to'] == '' ||
00256                                          !isset( $part['minvalue'] ) || $part['minvalue'] == '' ||
00257                                          !isset( $part['maxvalue'] ) || $part['maxvalue'] == '' )
00258                                     {
00259                                         $removePart = true;
00260                                     }
00261                                 }
00262                             }
00263                         }
00264 
00265                         if ( $removePart )
00266                         {
00267                             eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $searchArrayPartForType[$key],
00268                                                         'removing incomplete search part' );
00269                             unSet( $searchArrayPartForType[$key] );
00270                         }
00271                     }
00272                 }
00273                 $andSearchParts = array_merge( $andSearchParts, $searchArrayPartForType );
00274             }
00275         }
00276         $generalFilter = array();
00277         foreach ( $searchTypesDefinition['general_filter'] as $searchType )
00278         {
00279 
00280             $postVariablePrefix = 'Content_search_' . $searchType['type'] . '_' . $searchType['subtype'] . '_';
00281 
00282             $searchArrayPartForType = array();
00283 
00284             $searchPart = array();
00285             $valuesFetched = false;
00286             $valuesMissing = false;
00287 
00288             foreach ( $searchType['params'] as $parameter )
00289             {
00290                 $varName = '';
00291                 $paramName = '';
00292                 if ( is_array( $parameter ) )
00293                 {
00294                     $varName = $postVariablePrefix . $parameter['value'];
00295                     $paramName = $parameter['value'];
00296                 }
00297                 else
00298                 {
00299                     $varName = $postVariablePrefix . $parameter;
00300                     $paramName = $parameter;
00301                 }
00302 
00303                 eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $varName,
00304                                             'post variable to check' );
00305 
00306                 if ( $http->hasVariable( $varName ) )
00307                 {
00308                     $values = $http->variable( $varName );
00309                     eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $values, 'fetched values' );
00310                     $searchArrayPartForType[$paramName] = $values;
00311                     $valuesFetched = true;
00312 
00313                 }
00314                 else
00315                 {
00316                     eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $varName,
00317                                                 'post variable does not exist' );
00318                     $valuesMissing = true;
00319                     break;
00320                 }
00321             }
00322 
00323             if ( $valuesFetched == true && $valuesMissing == false )
00324             {
00325                 eZDebugSetting::writeDebug( 'kernel-search-ezsearch', 'adding values to search' );
00326 
00327                 $part =& $searchArrayPartForType;
00328 
00329                 $part['type'] = $searchType['type'];
00330                 $part['subtype'] = $searchType['subtype'];
00331 
00332                 if ( $part['type'] == 'general' )
00333                 {
00334                         // Remove incomplete search parts from the search.
00335                         // An incomplete search part is for instance an empty text field,
00336                         // or a select box with no selected values.
00337                     $removePart = false;
00338                     switch ( $part['subtype'] )
00339                     {
00340                         case 'class':
00341                         {
00342                             if ( !isset( $part['value'] ) ||
00343                                  ( is_array( $part['value'] ) && count( $part['value'] ) == 0 ) ||
00344                                  ( !is_array( $part['value'] ) && $part['value'] == '' ) )
00345                                 $removePart = true;
00346                         }
00347                         break;
00348                         case 'publishdate':
00349                         {
00350                             if ( !isset( $part['value'] ) ||
00351                                  ( is_array( $part['value'] ) && count( $part['value'] ) == 0 ) ||
00352                                  ( !is_array( $part['value'] ) && $part['value'] == '' ) )
00353                                 $removePart = true;
00354                         }
00355                         break;
00356                         case 'subtree':
00357                         {
00358                             if ( !isset( $part['value'] ) ||
00359                                  ( is_array( $part['value'] ) && count( $part['value'] ) == 0 ) ||
00360                                  ( !is_array( $part['value'] ) && $part['value'] == '' ) )
00361 
00362                                 $removePart = true;
00363                         }
00364                         break;
00365                     }
00366 
00367                     if ( $removePart )
00368                     {
00369                         eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $searchArrayPartForType[$key],
00370                                                     'removing incomplete search part' );
00371                         unSet( $searchArrayPartForType[$key] );
00372                         continue;
00373                     }
00374                 }
00375 
00376                 $generalFilter = array_merge( $generalFilter, array( $searchArrayPartForType ) );
00377             }
00378 
00379 
00380         }
00381 
00382         if ( $andSearchParts != null )
00383         {
00384             $searchArray['and'] = $andSearchParts;
00385         }
00386         if ( $generalFilter != null )
00387         {
00388             $searchArray['general'] = $generalFilter;
00389         }
00390 
00391         eZDebugSetting::writeDebug( 'kernel-search-ezsearch', $searchArray, 'search array' );
00392         return $searchArray;
00393     }
00394 
00395     /*!
00396      \static
00397      Tells the current search engine to cleanup up all data.
00398     */
00399     static function cleanup()
00400     {
00401         $searchEngine = eZSearch::getEngine();
00402 
00403         if ( is_object( $searchEngine ) && method_exists( $searchEngine, 'cleanup' ) )
00404         {
00405             $searchEngine->cleanup();
00406         }
00407     }
00408 
00409     /*!
00410      \static
00411      Get object instance of eZSearch engine to use.
00412 
00413      \return instance of eZSearch class.
00414     */
00415     static function getEngine()
00416     {
00417         // Get instance if already created.
00418         $instanceName = 'eZSearchPlugin_' . $GLOBALS['eZCurrentAccess'];
00419         if ( isset( $GLOBALS[$instanceName] ) )
00420         {
00421             return $GLOBALS[$instanceName];
00422         }
00423 
00424         //include_once( 'lib/ezutils/classes/ezini.php' );
00425         $ini = eZINI::instance();
00426 
00427         $searchEngineString = 'ezsearch';
00428         if ( $ini->hasVariable( 'SearchSettings', 'SearchEngine' ) == true )
00429         {
00430             $searchEngineString = $ini->variable( 'SearchSettings', 'SearchEngine' );
00431         }
00432 
00433         $directoryList = array();
00434         //include_once( 'lib/ezutils/classes/ezextension.php' );
00435         if ( $ini->hasVariable( 'SearchSettings', 'ExtensionDirectories' ) )
00436         {
00437             $extensionDirectories = $ini->variable( 'SearchSettings', 'ExtensionDirectories' );
00438             if ( is_array( $extensionDirectories ) )
00439             {
00440                 $directoryList = eZExtension::expandedPathList( $extensionDirectories, 'search/plugins' );
00441             }
00442         }
00443 
00444         $kernelDir = array( 'kernel/search/plugins' );
00445         $directoryList = array_merge( $kernelDir, $directoryList );
00446 
00447         foreach( $directoryList as $directory )
00448         {
00449             $searchEngineFile = implode( '/', array( $directory, strtolower( $searchEngineString ), strtolower( $searchEngineString ) ) ) . '.php';
00450 
00451             if ( file_exists( $searchEngineFile ) )
00452             {
00453                 eZDebugSetting::writeDebug( 'kernel-search-ezsearch', 'Loading search engine from ' . $searchEngineFile, 'eZSearch::getEngine' );
00454 
00455                 include_once( $searchEngineFile );
00456                 $GLOBALS[$instanceName] = new $searchEngineString();
00457                 return $GLOBALS[$instanceName];
00458             }
00459         }
00460 
00461         eZDebug::writeDebug( 'Unable to find the search engine:' . $searchEngineString, 'eZSearch' );
00462         eZDebug::writeDebug( 'Tried paths: ' . implode( ', ', $directoryList ), 'eZSearch' );
00463         return false;
00464     }
00465 
00466 }
00467 
00468 ?>