/**
  * Parses a query string
  *
  * @param string $strQuery
  * @param string $encoding
  * @return Zend_Search_Lucene_Search_Query
  * @throws Zend_Search_Lucene_Search_QueryParserException
  */
 public static function parse($strQuery, $encoding = null)
 {
     if (self::$_instance === null) {
         self::$_instance = new Zend_Search_Lucene_Search_QueryParser();
     }
     self::$_instance->_encoding = $encoding !== null ? $encoding : self::$_instance->_defaultEncoding;
     self::$_instance->_lastToken = null;
     self::$_instance->_context = new Zend_Search_Lucene_Search_QueryParserContext(self::$_instance->_encoding);
     self::$_instance->_contextStack = array();
     self::$_instance->_tokens = self::$_instance->_lexer->tokenize($strQuery, self::$_instance->_encoding);
     // Empty query
     if (count(self::$_instance->_tokens) == 0) {
         return new Zend_Search_Lucene_Search_Query_Empty();
     }
     foreach (self::$_instance->_tokens as $token) {
         try {
             self::$_instance->_currentToken = $token;
             self::$_instance->process($token->type);
             self::$_instance->_lastToken = $token;
         } catch (Exception $e) {
             if (strpos($e->getMessage(), 'There is no any rule for') !== false) {
                 throw new Zend_Search_Lucene_Search_QueryParserException('Syntax error at char position ' . $token->position . '.');
             }
             throw $e;
         }
     }
     if (count(self::$_instance->_contextStack) != 0) {
         throw new Zend_Search_Lucene_Search_QueryParserException('Syntax Error: mismatched parentheses, every opening must have closing.');
     }
     return self::$_instance->_context->getQuery();
 }
Beispiel #2
0
    /**
     * Parses a query string
     *
     * @param string $strQuery
     * @param string $encoding
     * @return Zend_Search_Lucene_Search_Query
     * @throws Zend_Search_Lucene_Search_QueryParserException
     */
    public static function parse($strQuery, $encoding = null)
    {
        self::_getInstance();

        // Reset FSM if previous parse operation didn't return it into a correct state
        self::$_instance->reset();

        require_once 'Zend/Search/Lucene/Search/QueryParserException.php';
        try {
            self::$_instance->_encoding     = ($encoding !== null) ? $encoding : self::$_instance->_defaultEncoding;
            self::$_instance->_lastToken    = null;
            self::$_instance->_context      = new Zend_Search_Lucene_Search_QueryParserContext(self::$_instance->_encoding);
            self::$_instance->_contextStack = array();
            self::$_instance->_tokens       = self::$_instance->_lexer->tokenize($strQuery, self::$_instance->_encoding);

            // Empty query
            if (count(self::$_instance->_tokens) == 0) {
                return new Zend_Search_Lucene_Search_Query_Insignificant();
            }


            foreach (self::$_instance->_tokens as $token) {
                try {
                    self::$_instance->_currentToken = $token;
                    self::$_instance->process($token->type);

                    self::$_instance->_lastToken = $token;
                } catch (Exception $e) {
                    if (strpos($e->getMessage(), 'There is no any rule for') !== false) {
                        throw new Zend_Search_Lucene_Search_QueryParserException( 'Syntax error at char position ' . $token->position . '.' );
                    }

                    throw $e;
                }
            }

            if (count(self::$_instance->_contextStack) != 0) {
                throw new Zend_Search_Lucene_Search_QueryParserException('Syntax Error: mismatched parentheses, every opening must have closing.' );
            }

            return self::$_instance->_context->getQuery();
        } catch (Zend_Search_Lucene_Search_QueryParserException $e) {
            if (self::$_instance->_suppressQueryParsingExceptions) {
                $queryTokens = Zend_Search_Lucene_Analysis_Analyzer::getDefault()->tokenize($strQuery, self::$_instance->_encoding);

                $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
                $termsSign = (self::$_instance->_defaultOperator == self::B_AND) ? true /* required term */ :
                                                                                   null /* optional term */;

                foreach ($queryTokens as $token) {
                    $query->addTerm(new Zend_Search_Lucene_Index_Term($token->getTermText()), $termsSign);
                }


                return $query;
            } else {
                throw $e;
            }
        }
    }