summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/index/Zend/Search/Lucene/Search/QueryParser.php
blob: 9387afca2ec5c17963d7488ad97b0e60b5bbeea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to version 1.0 of the Zend Framework
 * license, that is bundled with this package in the file LICENSE, and
 * is available through the world-wide-web at the following URL:
 * http://www.zend.com/license/framework/1_0.txt. If you did not receive
 * a copy of the Zend Framework license and are unable to obtain it
 * through the world-wide-web, please send a note to license@zend.com
 * so we can mail you a copy immediately.
 *
 * @package    Zend_Search_Lucene
 * @subpackage Search
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
 */


/** Zend_Search_Lucene_Search_QueryTokenizer */
require_once 'Zend/Search/Lucene/Search/QueryTokenizer.php';

/** Zend_Search_Lucene_Index_Term */
require_once 'Zend/Search/Lucene/Index/Term.php';

/** Zend_Search_Lucene_Search_Query_Term */
require_once 'Zend/Search/Lucene/Search/Query/Term.php';

/** Zend_Search_Lucene_Search_Query_MultiTerm */
require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php';

/** Zend_Search_Lucene_Search_Query_Phrase */
require_once 'Zend/Search/Lucene/Search/Query/Phrase.php';


/** Zend_Search_Lucene_Exception */
require_once 'Zend/Search/Lucene/Exception.php';


/**
 * @package    Zend_Search_Lucene
 * @subpackage Search
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
 */
class Zend_Search_Lucene_Search_QueryParser
{

    /**
     * Parses a query string, returning a Zend_Search_Lucene_Search_Query
     *
     * @param string $strQuery
     * @return Zend_Search_Lucene_Search_Query
     */
    static public function parse($strQuery)
    {
        $tokens = new Zend_Search_Lucene_Search_QueryTokenizer($strQuery);

        // Empty query
        if (!$tokens->count()) {
            throw new Zend_Search_Lucene_Exception('Syntax error: query string cannot be empty.');
        }

        // Term query
        if ($tokens->count() == 1) {
            if ($tokens->current()->type == Zend_Search_Lucene_Search_QueryToken::TOKTYPE_WORD) {
                return new Zend_Search_Lucene_Search_Query_Term(new Zend_Search_Lucene_Index_Term($tokens->current()->text, 'contents'));
            } else {
                throw new Zend_Search_Lucene_Exception('Syntax error: query string must contain at least one word.');
            }
        }


        /**
         * MultiTerm Query
         *
         * Process each token that was returned by the tokenizer.
         */
        $terms = array();
        $signs = array();
        $prevToken = null;
        $openBrackets = 0;
        $field = 'contents';
        foreach ($tokens as $token) {
            switch ($token->type) {
                case Zend_Search_Lucene_Search_QueryToken::TOKTYPE_WORD:
                    $terms[] = new Zend_Search_Lucene_Index_Term($token->text, $field);
                    $field = 'contents';
                    if ($prevToken !== null &&
                        $prevToken->type == Zend_Search_Lucene_Search_QueryToken::TOKTYPE_SIGN) {
                            if ($prevToken->text == "+") {
                                $signs[] = true;
                            } else {
                                $signs[] = false;
                            }
                    } else {
                        $signs[] = null;
                    }
                    break;
                case Zend_Search_Lucene_Search_QueryToken::TOKTYPE_SIGN:
                    if ($prevToken !== null &&
                        $prevToken->type == Zend_Search_Lucene_Search_QueryToken::TOKTYPE_SIGN) {
                            throw new Zend_Search_Lucene_Exception('Syntax error: sign operator must be followed by a word.');
                    }
                    break;
                case Zend_Search_Lucene_Search_QueryToken::TOKTYPE_FIELD:
                    $field = $token->text;
                    // let previous token to be signed as next $prevToken
                    $token = $prevToken;
                    break;
                case Zend_Search_Lucene_Search_QueryToken::TOKTYPE_BRACKET:
                    $token->text=='(' ? $openBrackets++ : $openBrackets--;
            }
            $prevToken = $token;
        }

        // Finish up parsing: check the last token in the query for an opening sign or parenthesis.
        if ($prevToken->type == Zend_Search_Lucene_Search_QueryToken::TOKTYPE_SIGN) {
            throw new Zend_Search_Lucene_Exception('Syntax Error: sign operator must be followed by a word.');
        }

        // Finish up parsing: check that every opening bracket has a matching closing bracket.
        if ($openBrackets != 0) {
            throw new Zend_Search_Lucene_Exception('Syntax Error: mismatched parentheses, every opening must have closing.');
        }

        switch (count($terms)) {
            case 0:
                throw new Zend_Search_Lucene_Exception('Syntax error: bad term count.');
            case 1:
                return new Zend_Search_Lucene_Search_Query_Term($terms[0],$signs[0] !== false);
            default:
                return new Zend_Search_Lucene_Search_Query_MultiTerm($terms,$signs);
        }
    }

}