eZ Publish  [trunk]
ezsearchlog.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZSearchLog class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZSearchLog ezsearchlog.php
00013   \brief eZSearchLog handles logging of search phrases
00014 
00015 */
00016 
00017 class eZSearchLog
00018 {
00019     /*!
00020      Logs a search query so that we can retrieve statistics afterwords.
00021     */
00022     static function addPhrase( $phrase, $returnCount )
00023     {
00024         $db = eZDB::instance();
00025         $db->begin();
00026 
00027         $trans = eZCharTransform::instance();
00028         $phrase = $trans->transformByGroup( trim( $phrase ), 'search' );
00029 
00030         // 250 is the numbers of characters accepted by the DB table, so shorten to fit
00031         if ( strlen( $phrase ) > 250 )
00032         {
00033             $phrase = substr( $phrase , 0 , 247 ) . "...";
00034         }
00035         $phrase = $db->escapeString( $phrase );
00036 
00037         // find or store the phrase
00038         $phraseRes = $db->arrayQuery( "SELECT id FROM ezsearch_search_phrase WHERE phrase='$phrase'" );
00039 
00040         if ( count( $phraseRes ) == 1 )
00041         {
00042             $phraseID = $phraseRes[0]['id'];
00043             $db->query( "UPDATE ezsearch_search_phrase
00044                          SET    phrase_count = phrase_count + 1,
00045                                 result_count = result_count + $returnCount
00046                          WHERE  id = $phraseID" );
00047         }
00048         else
00049         {
00050             $db->query( "INSERT INTO
00051                               ezsearch_search_phrase ( phrase, phrase_count, result_count )
00052                          VALUES ( '$phrase', 1, $returnCount )" );
00053 
00054             /* when breaking BC: delete next line */
00055             $phraseID = $db->lastSerialID( 'ezsearch_search_phrase', 'id' );
00056         }
00057 
00058         /* when breaking BC: delete next lines */
00059         /* ezsearch_return_count is not used any more by eZ Publish
00060            but perhaps someone else added some functionality... */
00061         $time = time();
00062         // store the search result
00063         $db->query( "INSERT INTO
00064                            ezsearch_return_count ( phrase_id, count, time )
00065                      VALUES ( '$phraseID', '$returnCount', '$time' )" );
00066         /* end of BC breaking delete*/
00067 
00068         $db->commit();
00069     }
00070 
00071     /*!
00072      Returns the most frequent search phrases, which did not get hits.
00073     */
00074     static function mostFrequentPhraseArray( $parameters = array( ) )
00075     {
00076         $db = eZDB::instance();
00077 
00078         $query = 'SELECT phrase_count, result_count / phrase_count AS result_count, id, phrase
00079                   FROM   ezsearch_search_phrase
00080                   ORDER BY phrase_count DESC';
00081 
00082         return $db->arrayQuery( $query, $parameters );
00083     }
00084 
00085     /*!
00086      \static
00087      Removes all stored phrases and search match counts from the database.
00088     */
00089     static function removeStatistics()
00090     {
00091         $db = eZDB::instance();
00092         $query = "DELETE FROM ezsearch_search_phrase";
00093         $db->query( $query );
00094         /* when breaking BC: delete those two lines */
00095         $query = "DELETE FROM ezsearch_return_count";
00096         $db->query( $query );
00097     }
00098 }
00099 
00100 ?>