summaryrefslogtreecommitdiff
path: root/includes/search
diff options
context:
space:
mode:
Diffstat (limited to 'includes/search')
-rw-r--r--includes/search/SearchEngine.php64
-rw-r--r--includes/search/SearchHighlighter.php26
-rw-r--r--includes/search/SearchMySQL.php4
-rw-r--r--includes/search/SearchPostgres.php2
-rw-r--r--includes/search/SearchResult.php9
5 files changed, 57 insertions, 48 deletions
diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php
index 0eb87e4a..5770276a 100644
--- a/includes/search/SearchEngine.php
+++ b/includes/search/SearchEngine.php
@@ -47,6 +47,7 @@ class SearchEngine {
/** @var bool */
protected $showSuggestion = true;
+ private $sort = 'relevance';
/** @var array Feature values */
protected $features = array();
@@ -137,7 +138,7 @@ class SearchEngine {
public static function getNearMatch( $searchterm ) {
$title = self::getNearMatchInternal( $searchterm );
- wfRunHooks( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
+ Hooks::run( 'SearchGetNearMatchComplete', array( $searchterm, &$title ) );
return $title;
}
@@ -170,7 +171,7 @@ class SearchEngine {
}
$titleResult = null;
- if ( !wfRunHooks( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
+ if ( !Hooks::run( 'SearchGetNearMatchBefore', array( $allSearchTerms, &$titleResult ) ) ) {
return $titleResult;
}
@@ -197,7 +198,7 @@ class SearchEngine {
return $title;
}
- if ( !wfRunHooks( 'SearchAfterNoDirectMatch', array( $term, &$title ) ) ) {
+ if ( !Hooks::run( 'SearchAfterNoDirectMatch', array( $term, &$title ) ) ) {
return $title;
}
@@ -227,7 +228,7 @@ class SearchEngine {
// Give hooks a chance at better match variants
$title = null;
- if ( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
+ if ( !Hooks::run( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
return $title;
}
}
@@ -310,6 +311,43 @@ class SearchEngine {
}
/**
+ * Get the valid sort directions. All search engines support 'relevance' but others
+ * might support more. The default in all implementations should be 'relevance.'
+ *
+ * @since 1.25
+ * @return array(string) the valid sort directions for setSort
+ */
+ public function getValidSorts() {
+ return array( 'relevance' );
+ }
+
+ /**
+ * Set the sort direction of the search results. Must be one returned by
+ * SearchEngine::getValidSorts()
+ *
+ * @since 1.25
+ * @throws InvalidArgumentException
+ * @param string $sort sort direction for query result
+ */
+ public function setSort( $sort ) {
+ if ( !in_array( $sort, $this->getValidSorts() ) ) {
+ throw new InvalidArgumentException( "Invalid sort: $sort. " .
+ "Must be one of: " . implode( ', ', $this->getValidSorts() ) );
+ }
+ $this->sort = $sort;
+ }
+
+ /**
+ * Get the sort direction of the search results
+ *
+ * @since 1.25
+ * @return string
+ */
+ public function getSort() {
+ return $this->sort;
+ }
+
+ /**
* Parse some common prefixes: all (search everything)
* or namespace names
*
@@ -356,7 +394,7 @@ class SearchEngine {
}
}
- wfRunHooks( 'SearchableNamespaces', array( &$arr ) );
+ Hooks::run( 'SearchableNamespaces', array( &$arr ) );
return $arr;
}
@@ -500,22 +538,12 @@ class SearchEngine {
/**
* Get OpenSearch suggestion template
*
+ * @deprecated since 1.25
* @return string
*/
public static function getOpenSearchTemplate() {
- global $wgOpenSearchTemplate, $wgCanonicalServer;
-
- if ( $wgOpenSearchTemplate ) {
- return $wgOpenSearchTemplate;
- } else {
- $ns = implode( '|', SearchEngine::defaultNamespaces() );
- if ( !$ns ) {
- $ns = "0";
- }
-
- return $wgCanonicalServer . wfScript( 'api' )
- . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
- }
+ wfDeprecated( __METHOD__, '1.25' );
+ return ApiOpenSearch::getOpenSearchTemplate( 'application/x-suggestions+json' );
}
/**
diff --git a/includes/search/SearchHighlighter.php b/includes/search/SearchHighlighter.php
index c3c3a8f8..5087e8d5 100644
--- a/includes/search/SearchHighlighter.php
+++ b/includes/search/SearchHighlighter.php
@@ -45,8 +45,6 @@ class SearchHighlighter {
public function highlightText( $text, $terms, $contextlines, $contextchars ) {
global $wgContLang, $wgSearchHighlightBoundaries;
- $fname = __METHOD__;
-
if ( $text == '' ) {
return '';
}
@@ -60,14 +58,14 @@ class SearchHighlighter {
3 => "/(\n\\{\\|)|(\n\\|\\})/" ); // table
// @todo FIXME: This should prolly be a hook or something
- if ( function_exists( 'wfCite' ) ) {
+ // instead of hardcoding a class name from the Cite extension
+ if ( class_exists( 'Cite' ) ) {
$spat .= '|(<ref>)'; // references via cite extension
$endPatterns[4] = '/(<ref>)|(<\/ref>)/';
}
$spat .= '/';
$textExt = array(); // text extracts
$otherExt = array(); // other extracts
- wfProfileIn( "$fname-split" );
$start = 0;
$textLen = strlen( $text );
$count = 0; // sequence number to maintain ordering
@@ -132,8 +130,6 @@ class SearchHighlighter {
$all = $textExt + $otherExt; // these have disjunct key sets
- wfProfileOut( "$fname-split" );
-
// prepare regexps
foreach ( $terms as $index => $term ) {
// manually do upper/lowercase stuff for utf-8 since PHP won't do it
@@ -163,8 +159,6 @@ class SearchHighlighter {
$pat1 = "/(" . $phrase . ")/ui";
$pat2 = "/$patPre(" . $anyterm . ")$patPost/ui";
- wfProfileIn( "$fname-extract" );
-
$left = $contextlines;
$snippets = array();
@@ -287,8 +281,6 @@ class SearchHighlighter {
}
}
- wfProfileOut( "$fname-extract" );
-
return $extract;
}
@@ -451,15 +443,6 @@ class SearchHighlighter {
* @return mixed
*/
function removeWiki( $text ) {
- $fname = __METHOD__;
- wfProfileIn( $fname );
-
- // $text = preg_replace( "/'{2,5}/", "", $text );
- // $text = preg_replace( "/\[[a-z]+:\/\/[^ ]+ ([^]]+)\]/", "\\2", $text );
- // $text = preg_replace( "/\[\[([^]|]+)\]\]/", "\\1", $text );
- // $text = preg_replace( "/\[\[([^]]+\|)?([^|]]+)\]\]/", "\\2", $text );
- // $text = preg_replace( "/\\{\\|(.*?)\\|\\}/", "", $text );
- // $text = preg_replace( "/\\[\\[[A-Za-z_-]+:([^|]+?)\\]\\]/", "", $text );
$text = preg_replace( "/\\{\\{([^|]+?)\\}\\}/", "", $text );
$text = preg_replace( "/\\{\\{([^|]+\\|)(.*?)\\}\\}/", "\\2", $text );
$text = preg_replace( "/\\[\\[([^|]+?)\\]\\]/", "\\1", $text );
@@ -468,13 +451,11 @@ class SearchHighlighter {
array( $this, 'linkReplace' ),
$text
);
- // $text = preg_replace("/\\[\\[([^|]+\\|)(.*?)\\]\\]/", "\\2", $text);
$text = preg_replace( "/<\/?[^>]+>/", "", $text );
$text = preg_replace( "/'''''/", "", $text );
$text = preg_replace( "/('''|<\/?[iIuUbB]>)/", "", $text );
$text = preg_replace( "/''/", "", $text );
- wfProfileOut( $fname );
return $text;
}
@@ -512,7 +493,6 @@ class SearchHighlighter {
*/
public function highlightSimple( $text, $terms, $contextlines, $contextchars ) {
global $wgContLang;
- $fname = __METHOD__;
$lines = explode( "\n", $text );
@@ -523,7 +503,6 @@ class SearchHighlighter {
$lineno = 0;
$extract = "";
- wfProfileIn( "$fname-extract" );
foreach ( $lines as $line ) {
if ( 0 == $contextlines ) {
break;
@@ -551,7 +530,6 @@ class SearchHighlighter {
$extract .= "${line}\n";
}
- wfProfileOut( "$fname-extract" );
return $extract;
}
diff --git a/includes/search/SearchMySQL.php b/includes/search/SearchMySQL.php
index 78eba2d0..485088cb 100644
--- a/includes/search/SearchMySQL.php
+++ b/includes/search/SearchMySQL.php
@@ -382,8 +382,6 @@ class SearchMySQL extends SearchDatabase {
function normalizeText( $string ) {
global $wgContLang;
- wfProfileIn( __METHOD__ );
-
$out = parent::normalizeText( $string );
// MySQL fulltext index doesn't grok utf-8, so we
@@ -416,8 +414,6 @@ class SearchMySQL extends SearchDatabase {
"$1u82e$2",
$out );
- wfProfileOut( __METHOD__ );
-
return $out;
}
diff --git a/includes/search/SearchPostgres.php b/includes/search/SearchPostgres.php
index 59b0c31c..bda10b0b 100644
--- a/includes/search/SearchPostgres.php
+++ b/includes/search/SearchPostgres.php
@@ -85,7 +85,7 @@ class SearchPostgres extends SearchDatabase {
if ( strtolower( $terms[2] ) === 'and' ) {
$searchstring .= ' & ';
}
- elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
+ elseif ( strtolower( $terms[2] ) === 'or' || $terms[2] === '|' ) {
$searchstring .= ' | ';
}
elseif ( strtolower( $terms[2] ) === 'not' ) {
diff --git a/includes/search/SearchResult.php b/includes/search/SearchResult.php
index aeaba8df..d384ae96 100644
--- a/includes/search/SearchResult.php
+++ b/includes/search/SearchResult.php
@@ -71,7 +71,7 @@ class SearchResult {
$this->mTitle = $title;
if ( !is_null( $this->mTitle ) ) {
$id = false;
- wfRunHooks( 'SearchResultInitFromTitle', array( $title, &$id ) );
+ Hooks::run( 'SearchResultInitFromTitle', array( $title, &$id ) );
$this->mRevision = Revision::newFromTitle(
$this->mTitle, $id, Revision::READ_NORMAL );
if ( $this->mTitle->getNamespace() === NS_FILE ) {
@@ -186,6 +186,13 @@ class SearchResult {
}
/**
+ * @return string Highlighted relevant category name or '' if none or not supported
+ */
+ public function getCategorySnippet() {
+ return '';
+ }
+
+ /**
* @return string Timestamp
*/
function getTimestamp() {