-<?php\r
-# Copyright (C) 2004 Brion Vibber <brion@pobox.com>\r
-# http://www.mediawiki.org/\r
-#\r
-# This program is free software; you can redistribute it and/or modify\r
-# it under the terms of the GNU General Public License as published by\r
-# the Free Software Foundation; either version 2 of the License, or\r
-# (at your option) any later version.\r
-#\r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-# GNU General Public License for more details.\r
-#\r
-# You should have received a copy of the GNU General Public License along\r
-# with this program; if not, write to the Free Software Foundation, Inc.,\r
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
-# http://www.gnu.org/copyleft/gpl.html\r
-\r
-/**\r
- * @file\r
- * @ingroup Search\r
- */\r
-\r
-/**\r
- * Search engine hook base class for IBM DB2\r
- * @ingroup Search\r
- */\r
-class SearchIBM_DB2 extends SearchEngine {\r
- function __construct($db) {\r
- $this->db = $db;\r
- }\r
-\r
- /**\r
- * Perform a full text search query and return a result set.\r
- *\r
- * @param string $term - Raw search term\r
- * @return IBM_DB2SearchResultSet\r
- * @access public\r
- */\r
- function searchText( $term ) {\r
- $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));\r
- return new IBM_DB2SearchResultSet($resultSet, $this->searchTerms);\r
- }\r
-\r
- /**\r
- * Perform a title-only search query and return a result set.\r
- *\r
- * @param string $term - Raw search term\r
- * @return IBM_DB2SearchResultSet\r
- * @access public\r
- */\r
- function searchTitle($term) {\r
- $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));\r
- return new MySQLSearchResultSet($resultSet, $this->searchTerms);\r
- }\r
-\r
-\r
- /**\r
- * Return a partial WHERE clause to exclude redirects, if so set\r
- * @return string\r
- * @private\r
- */\r
- function queryRedirect() {\r
- if ($this->showRedirects) {\r
- return '';\r
- } else {\r
- return 'AND page_is_redirect=0';\r
- }\r
- }\r
-\r
- /**\r
- * Return a partial WHERE clause to limit the search to the given namespaces\r
- * @return string\r
- * @private\r
- */\r
- function queryNamespaces() {\r
- if( is_null($this->namespaces) )\r
- return '';\r
- $namespaces = implode(',', $this->namespaces);\r
- if ($namespaces == '') {\r
- $namespaces = '0';\r
- }\r
- return 'AND page_namespace IN (' . $namespaces . ')';\r
- }\r
-\r
- /**\r
- * Return a LIMIT clause to limit results on the query.\r
- * @return string\r
- * @private\r
- */\r
- function queryLimit($sql) {\r
- return $this->db->limitResult($sql, $this->limit, $this->offset);\r
- }\r
-\r
- /**\r
- * Does not do anything for generic search engine\r
- * subclasses may define this though\r
- * @return string\r
- * @private\r
- */\r
- function queryRanking($filteredTerm, $fulltext) {\r
- // requires Net Search Extender or equivalent\r
- // return ' ORDER BY score(1)';\r
- return '';\r
- }\r
-\r
- /**\r
- * Construct the full SQL query to do the search.\r
- * The guts shoulds be constructed in queryMain()\r
- * @param string $filteredTerm\r
- * @param bool $fulltext\r
- * @private\r
- */\r
- function getQuery( $filteredTerm, $fulltext ) {\r
- return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .\r
- $this->queryRedirect() . ' ' .\r
- $this->queryNamespaces() . ' ' .\r
- $this->queryRanking( $filteredTerm, $fulltext ) . ' ');\r
- }\r
-\r
-\r
- /**\r
- * Picks which field to index on, depending on what type of query.\r
- * @param bool $fulltext\r
- * @return string\r
- */\r
- function getIndexField($fulltext) {\r
- return $fulltext ? 'si_text' : 'si_title';\r
- }\r
-\r
- /**\r
- * Get the base part of the search query.\r
- *\r
- * @param string $filteredTerm\r
- * @param bool $fulltext\r
- * @return string\r
- * @private\r
- */\r
- function queryMain( $filteredTerm, $fulltext ) {\r
- $match = $this->parseQuery($filteredTerm, $fulltext);\r
- $page = $this->db->tableName('page');\r
- $searchindex = $this->db->tableName('searchindex');\r
- return 'SELECT page_id, page_namespace, page_title ' .\r
- "FROM $page,$searchindex " .\r
- 'WHERE page_id=si_page AND ' . $match;\r
- }\r
-\r
- /** @todo document */\r
- function parseQuery($filteredText, $fulltext) {\r
- global $wgContLang;\r
- $lc = SearchEngine::legalSearchChars();\r
- $this->searchTerms = array();\r
-\r
- # FIXME: This doesn't handle parenthetical expressions.\r
- $m = array();\r
- $q = array();\r
-\r
- if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',\r
- $filteredText, $m, PREG_SET_ORDER)) {\r
- foreach($m as $terms) {\r
- $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);\r
-\r
- if (!empty($terms[3])) {\r
- $regexp = preg_quote( $terms[3], '/' );\r
- if ($terms[4])\r
- $regexp .= "[0-9A-Za-z_]+";\r
- } else {\r
- $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');\r
- }\r
- $this->searchTerms[] = $regexp;\r
- }\r
- }\r
-\r
- $searchon = $this->db->strencode(join(',', $q));\r
- $field = $this->getIndexField($fulltext);\r
- \r
- // requires Net Search Extender or equivalent\r
- //return " CONTAINS($field, '$searchon') > 0 ";\r
- \r
- return " lcase($field) LIKE lcase('%$searchon%')";\r
- }\r
-\r
- /**\r
- * Create or update the search index record for the given page.\r
- * Title and text should be pre-processed.\r
- *\r
- * @param int $id\r
- * @param string $title\r
- * @param string $text\r
- */\r
- function update($id, $title, $text) {\r
- $dbw = wfGetDB(DB_MASTER);\r
- $dbw->replace('searchindex',\r
- array('si_page'),\r
- array(\r
- 'si_page' => $id,\r
- 'si_title' => $title,\r
- 'si_text' => $text\r
- ), 'SearchIBM_DB2::update' );\r
- // ?\r
- //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");\r
- //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");\r
- }\r
-\r
- /**\r
- * Update a search index record's title only.\r
- * Title should be pre-processed.\r
- *\r
- * @param int $id\r
- * @param string $title\r
- */\r
- function updateTitle($id, $title) {\r
- $dbw = wfGetDB(DB_MASTER);\r
-\r
- $dbw->update('searchindex',\r
- array('si_title' => $title),\r
- array('si_page' => $id),\r
- 'SearchIBM_DB2::updateTitle',\r
- array());\r
- }\r
-}\r
-\r
-/**\r
- * @ingroup Search\r
- */\r
-class IBM_DB2SearchResultSet extends SearchResultSet {\r
- function __construct($resultSet, $terms) {\r
- $this->mResultSet = $resultSet;\r
- $this->mTerms = $terms;\r
- }\r
-\r
- function termMatches() {\r
- return $this->mTerms;\r
- }\r
-\r
- function numRows() {\r
- return $this->mResultSet->numRows();\r
- }\r
-\r
- function next() {\r
- $row = $this->mResultSet->fetchObject();\r
- if ($row === false)\r
- return false;\r
- return new SearchResult($row);\r
- }\r
-}\r
+<?php
+# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
+# http://www.mediawiki.org/
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+# http://www.gnu.org/copyleft/gpl.html
+
+/**
+ * @file
+ * @ingroup Search
+ */
+
+/**
+ * Search engine hook base class for IBM DB2
+ * @ingroup Search
+ */
+class SearchIBM_DB2 extends SearchEngine {
+ function __construct($db) {
+ $this->db = $db;
+ }
+
+ /**
+ * Perform a full text search query and return a result set.
+ *
+ * @param $term String: raw search term
+ * @return IBM_DB2SearchResultSet
+ */
+ function searchText( $term ) {
+ $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
+ return new IBM_DB2SearchResultSet($resultSet, $this->searchTerms);
+ }
+
+ /**
+ * Perform a title-only search query and return a result set.
+ *
+ * @param $term String: taw search term
+ * @return IBM_DB2SearchResultSet
+ */
+ function searchTitle($term) {
+ $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
+ return new MySQLSearchResultSet($resultSet, $this->searchTerms);
+ }
+
+
+ /**
+ * Return a partial WHERE clause to exclude redirects, if so set
+ * @return String
+ */
+ function queryRedirect() {
+ if ($this->showRedirects) {
+ return '';
+ } else {
+ return 'AND page_is_redirect=0';
+ }
+ }
+
+ /**
+ * Return a partial WHERE clause to limit the search to the given namespaces
+ * @return String
+ */
+ function queryNamespaces() {
+ if( is_null($this->namespaces) )
+ return '';
+ $namespaces = implode(',', $this->namespaces);
+ if ($namespaces == '') {
+ $namespaces = '0';
+ }
+ return 'AND page_namespace IN (' . $namespaces . ')';
+ }
+
+ /**
+ * Return a LIMIT clause to limit results on the query.
+ * @return String
+ */
+ function queryLimit($sql) {
+ return $this->db->limitResult($sql, $this->limit, $this->offset);
+ }
+
+ /**
+ * Does not do anything for generic search engine
+ * subclasses may define this though
+ * @return String
+ */
+ function queryRanking($filteredTerm, $fulltext) {
+ // requires Net Search Extender or equivalent
+ // return ' ORDER BY score(1)';
+ return '';
+ }
+
+ /**
+ * Construct the full SQL query to do the search.
+ * The guts shoulds be constructed in queryMain()
+ * @param string $filteredTerm String
+ * @param bool $fulltext Boolean
+ */
+ function getQuery( $filteredTerm, $fulltext ) {
+ return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
+ $this->queryRedirect() . ' ' .
+ $this->queryNamespaces() . ' ' .
+ $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
+ }
+
+
+ /**
+ * Picks which field to index on, depending on what type of query.
+ * @param $fulltext Boolean
+ * @return String
+ */
+ function getIndexField($fulltext) {
+ return $fulltext ? 'si_text' : 'si_title';
+ }
+
+ /**
+ * Get the base part of the search query.
+ *
+ * @param string $filteredTerm String
+ * @param bool $fulltext Boolean
+ * @return String
+ */
+ function queryMain( $filteredTerm, $fulltext ) {
+ $match = $this->parseQuery($filteredTerm, $fulltext);
+ $page = $this->db->tableName('page');
+ $searchindex = $this->db->tableName('searchindex');
+ return 'SELECT page_id, page_namespace, page_title ' .
+ "FROM $page,$searchindex " .
+ 'WHERE page_id=si_page AND ' . $match;
+ }
+
+ /** @todo document */
+ function parseQuery($filteredText, $fulltext) {
+ global $wgContLang;
+ $lc = SearchEngine::legalSearchChars();
+ $this->searchTerms = array();
+
+ # FIXME: This doesn't handle parenthetical expressions.
+ $m = array();
+ $q = array();
+
+ if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
+ $filteredText, $m, PREG_SET_ORDER)) {
+ foreach($m as $terms) {
+ $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
+
+ if (!empty($terms[3])) {
+ $regexp = preg_quote( $terms[3], '/' );
+ if ($terms[4])
+ $regexp .= "[0-9A-Za-z_]+";
+ } else {
+ $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
+ }
+ $this->searchTerms[] = $regexp;
+ }
+ }
+
+ $searchon = $this->db->strencode(join(',', $q));
+ $field = $this->getIndexField($fulltext);
+
+ // requires Net Search Extender or equivalent
+ //return " CONTAINS($field, '$searchon') > 0 ";
+
+ return " lcase($field) LIKE lcase('%$searchon%')";
+ }
+
+ /**
+ * Create or update the search index record for the given page.
+ * Title and text should be pre-processed.
+ *
+ * @param $id Integer
+ * @param $title String
+ * @param $text String
+ */
+ function update($id, $title, $text) {
+ $dbw = wfGetDB(DB_MASTER);
+ $dbw->replace('searchindex',
+ array('si_page'),
+ array(
+ 'si_page' => $id,
+ 'si_title' => $title,
+ 'si_text' => $text
+ ), 'SearchIBM_DB2::update' );
+ // ?
+ //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
+ //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
+ }
+
+ /**
+ * Update a search index record's title only.
+ * Title should be pre-processed.
+ *
+ * @param $id Integer
+ * @param $title String
+ */
+ function updateTitle($id, $title) {
+ $dbw = wfGetDB(DB_MASTER);
+
+ $dbw->update('searchindex',
+ array('si_title' => $title),
+ array('si_page' => $id),
+ 'SearchIBM_DB2::updateTitle',
+ array());
+ }
+}
+
+/**
+ * @ingroup Search
+ */
+class IBM_DB2SearchResultSet extends SearchResultSet {
+ function __construct($resultSet, $terms) {
+ $this->mResultSet = $resultSet;
+ $this->mTerms = $terms;
+ }
+
+ function termMatches() {
+ return $this->mTerms;
+ }
+
+ function numRows() {
+ return $this->mResultSet->numRows();
+ }
+
+ function next() {
+ $row = $this->mResultSet->fetchObject();
+ if ($row === false)
+ return false;
+ return new SearchResult($row);
+ }
+}