]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/SearchMySQL.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / includes / SearchMySQL.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  * Search engine hook base class for MySQL.
22  * Specific bits for MySQL 3 and 4 variants are in child classes.
23  * @addtogroup Search
24  */
25 class SearchMySQL extends SearchEngine {
26         /**
27          * Perform a full text search query and return a result set.
28          *
29          * @param string $term - Raw search term
30          * @return MySQLSearchResultSet
31          * @access public
32          */
33         function searchText( $term ) {
34                 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
35                 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
36         }
37
38         /**
39          * Perform a title-only search query and return a result set.
40          *
41          * @param string $term - Raw search term
42          * @return MySQLSearchResultSet
43          * @access public
44          */
45         function searchTitle( $term ) {
46                 $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
47                 return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
48         }
49
50
51         /**
52          * Return a partial WHERE clause to exclude redirects, if so set
53          * @return string
54          * @private
55          */
56         function queryRedirect() {
57                 if( $this->showRedirects ) {
58                         return '';
59                 } else {
60                         return 'AND page_is_redirect=0';
61                 }
62         }
63
64         /**
65          * Return a partial WHERE clause to limit the search to the given namespaces
66          * @return string
67          * @private
68          */
69         function queryNamespaces() {
70                 $namespaces = implode( ',', $this->namespaces );
71                 if ($namespaces == '') {
72                         $namespaces = '0';
73                 }
74                 return 'AND page_namespace IN (' . $namespaces . ')';
75         }
76
77         /**
78          * Return a LIMIT clause to limit results on the query.
79          * @return string
80          * @private
81          */
82         function queryLimit() {
83                 return $this->db->limitResult( '', $this->limit, $this->offset );
84         }
85
86         /**
87          * Does not do anything for generic search engine
88          * subclasses may define this though
89          * @return string
90          * @private
91          */
92         function queryRanking( $filteredTerm, $fulltext ) {
93                 return '';
94         }
95
96         /**
97          * Construct the full SQL query to do the search.
98          * The guts shoulds be constructed in queryMain()
99          * @param string $filteredTerm
100          * @param bool $fulltext
101          * @private
102          */
103         function getQuery( $filteredTerm, $fulltext ) {
104                 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
105                         $this->queryRedirect() . ' ' .
106                         $this->queryNamespaces() . ' ' .
107                         $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
108                         $this->queryLimit();
109         }
110
111
112         /**
113          * Picks which field to index on, depending on what type of query.
114          * @param bool $fulltext
115          * @return string
116          */
117         function getIndexField( $fulltext ) {
118                 return $fulltext ? 'si_text' : 'si_title';
119         }
120
121         /**
122          * Get the base part of the search query.
123          * The actual match syntax will depend on the server
124          * version; MySQL 3 and MySQL 4 have different capabilities
125          * in their fulltext search indexes.
126          *
127          * @param string $filteredTerm
128          * @param bool $fulltext
129          * @return string
130          * @private
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         /**
142          * Create or update the search index record for the given page.
143          * Title and text should be pre-processed.
144          *
145          * @param int $id
146          * @param string $title
147          * @param string $text
148          */
149         function update( $id, $title, $text ) {
150                 $dbw = wfGetDB( DB_MASTER );
151                 $dbw->replace( 'searchindex',
152                         array( 'si_page' ),
153                         array(
154                                 'si_page' => $id,
155                                 'si_title' => $title,
156                                 'si_text' => $text
157                         ), 'SearchMySQL4::update' );
158         }
159
160         /**
161          * Update a search index record's title only.
162          * Title should be pre-processed.
163          *
164          * @param int $id
165          * @param string $title
166          */
167     function updateTitle( $id, $title ) {
168                 $dbw = wfGetDB( DB_MASTER );
169
170                 $dbw->update( 'searchindex',
171                         array( 'si_title' => $title ),
172                         array( 'si_page'  => $id ),
173                         'SearchMySQL4::updateTitle',
174                         array( $dbw->lowPriorityOption() ) );
175         }
176 }
177
178 /**
179  * @addtogroup Search
180  */
181 class MySQLSearchResultSet extends SearchResultSet {
182         function MySQLSearchResultSet( $resultSet, $terms ) {
183                 $this->mResultSet = $resultSet;
184                 $this->mTerms = $terms;
185         }
186
187         function termMatches() {
188                 return $this->mTerms;
189         }
190
191         function numRows() {
192                 return $this->mResultSet->numRows();
193         }
194
195         function next() {
196                 $row = $this->mResultSet->fetchObject();
197                 if( $row === false ) {
198                         return false;
199                 } else {
200                         return new SearchResult( $row );
201                 }
202         }
203         
204         function free() {
205                 $this->mResultSet->free();
206         }
207 }
208
209