]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/search/SearchIBM_DB2.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / includes / search / SearchIBM_DB2.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21  * @file
22  * @ingroup Search
23  */
24
25 /**
26  * Search engine hook base class for IBM DB2
27  * @ingroup Search
28  */
29 class SearchIBM_DB2 extends SearchEngine {
30         function __construct($db) {
31                 $this->db = $db;
32         }
33
34         /**
35          * Perform a full text search query and return a result set.
36          *
37          * @param $term String: raw search term
38          * @return SqlSearchResultSet
39          */
40         function searchText( $term ) {
41                 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
42                 return new SqlSearchResultSet($resultSet, $this->searchTerms);
43         }
44
45         /**
46          * Perform a title-only search query and return a result set.
47          *
48          * @param $term String: taw search term
49          * @return SqlSearchResultSet
50          */
51         function searchTitle($term) {
52                 $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
53                 return new SqlSearchResultSet($resultSet, $this->searchTerms);
54         }
55
56
57         /**
58          * Return a partial WHERE clause to exclude redirects, if so set
59          * @return String
60          */
61         function queryRedirect() {
62                 if ($this->showRedirects) {
63                         return '';
64                 } else {
65                         return 'AND page_is_redirect=0';
66                 }
67         }
68
69         /**
70          * Return a partial WHERE clause to limit the search to the given namespaces
71          * @return String
72          */
73         function queryNamespaces() {
74                 if( is_null($this->namespaces) )
75                         return '';
76                 $namespaces = implode(',', $this->namespaces);
77                 if ($namespaces == '') {
78                         $namespaces = '0';
79                 }
80                 return 'AND page_namespace IN (' . $namespaces . ')';
81         }
82
83         /**
84          * Return a LIMIT clause to limit results on the query.
85          * @return String
86          */
87         function queryLimit($sql) {
88                 return $this->db->limitResult($sql, $this->limit, $this->offset);
89         }
90
91         /**
92          * Does not do anything for generic search engine
93          * subclasses may define this though
94          * @return String
95          */
96         function queryRanking($filteredTerm, $fulltext) {
97                 // requires Net Search Extender or equivalent
98                 // return ' ORDER BY score(1)';
99                 return '';
100         }
101
102         /**
103          * Construct the full SQL query to do the search.
104          * The guts shoulds be constructed in queryMain()
105          * @param string $filteredTerm String
106          * @param bool $fulltext Boolean
107          */
108         function getQuery( $filteredTerm, $fulltext ) {
109                 return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
110                         $this->queryRedirect() . ' ' .
111                         $this->queryNamespaces() . ' ' .
112                         $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
113         }
114
115
116         /**
117          * Picks which field to index on, depending on what type of query.
118          * @param $fulltext Boolean
119          * @return String
120          */
121         function getIndexField($fulltext) {
122                 return $fulltext ? 'si_text' : 'si_title';
123         }
124
125         /**
126          * Get the base part of the search query.
127          *
128          * @param string $filteredTerm String
129          * @param bool $fulltext Boolean
130          * @return String
131          */
132         function queryMain( $filteredTerm, $fulltext ) {
133                 $match = $this->parseQuery($filteredTerm, $fulltext);
134                 $page        = $this->db->tableName('page');
135                 $searchindex = $this->db->tableName('searchindex');
136                 return 'SELECT page_id, page_namespace, page_title ' .
137                         "FROM $page,$searchindex " .
138                         'WHERE page_id=si_page AND ' . $match;
139         }
140
141         /** @todo document */
142         function parseQuery($filteredText, $fulltext) {
143                 global $wgContLang;
144                 $lc = SearchEngine::legalSearchChars();
145                 $this->searchTerms = array();
146
147                 # FIXME: This doesn't handle parenthetical expressions.
148                 $m = array();
149                 $q = array();
150
151                 if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
152                           $filteredText, $m, PREG_SET_ORDER)) {
153                         foreach($m as $terms) {
154
155                                 // Search terms in all variant forms, only
156                                 // apply on wiki with LanguageConverter
157                                 $temp_terms = $wgContLang->autoConvertToAllVariants( $terms[2] );
158                                 if( is_array( $temp_terms )) {
159                                         $temp_terms = array_unique( array_values( $temp_terms ));
160                                         foreach( $temp_terms as $t )
161                                                 $q[] = $terms[1] . $wgContLang->normalizeForSearch( $t );
162                                 }
163                                 else
164                                         $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
165
166                                 if (!empty($terms[3])) {
167                                         $regexp = preg_quote( $terms[3], '/' );
168                                         if ($terms[4])
169                                                 $regexp .= "[0-9A-Za-z_]+";
170                                 } else {
171                                         $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
172                                 }
173                                 $this->searchTerms[] = $regexp;
174                         }
175                 }
176
177                 $searchon = $this->db->strencode(join(',', $q));
178                 $field = $this->getIndexField($fulltext);
179                 
180                 // requires Net Search Extender or equivalent
181                 //return " CONTAINS($field, '$searchon') > 0 ";
182                 
183                 return " lcase($field) LIKE lcase('%$searchon%')";
184         }
185
186         /**
187          * Create or update the search index record for the given page.
188          * Title and text should be pre-processed.
189          *
190          * @param $id Integer
191          * @param $title String
192          * @param $text String
193          */
194         function update($id, $title, $text) {
195                 $dbw = wfGetDB(DB_MASTER);
196                 $dbw->replace('searchindex',
197                         array('si_page'),
198                         array(
199                                 'si_page' => $id,
200                                 'si_title' => $title,
201                                 'si_text' => $text
202                         ), 'SearchIBM_DB2::update' );
203                 // ?
204                 //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
205                 //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
206         }
207
208         /**
209          * Update a search index record's title only.
210          * Title should be pre-processed.
211          *
212          * @param $id Integer
213          * @param $title String
214          */
215         function updateTitle($id, $title) {
216                 $dbw = wfGetDB(DB_MASTER);
217
218                 $dbw->update('searchindex',
219                         array('si_title' => $title),
220                         array('si_page'  => $id),
221                         'SearchIBM_DB2::updateTitle',
222                         array());
223         }
224 }