/**
* Parse some common prefixes: all (search everything)
- * or namespace names
+ * or namespace names and set the list of namespaces
+ * of this class accordingly.
*
* @param string $query
* @return string
*/
function replacePrefixes( $query ) {
+ $queryAndNs = self::parseNamespacePrefixes( $query );
+ if ( $queryAndNs === false ) {
+ return $query;
+ }
+ $this->namespaces = $queryAndNs[1];
+ return $queryAndNs[0];
+ }
+
+ /**
+ * Parse some common prefixes: all (search everything)
+ * or namespace names
+ *
+ * @param string $query
+ * @return false|array false if no namespace was extracted, an array
+ * with the parsed query at index 0 and an array of namespaces at index
+ * 1 (or null for all namespaces).
+ */
+ public static function parseNamespacePrefixes( $query ) {
global $wgContLang;
$parsed = $query;
if ( strpos( $query, ':' ) === false ) { // nothing to do
- return $parsed;
+ return false;
}
+ $extractedNamespace = null;
$allkeyword = wfMessage( 'searchall' )->inContentLanguage()->text() . ":";
if ( strncmp( $query, $allkeyword, strlen( $allkeyword ) ) == 0 ) {
- $this->namespaces = null;
+ $extractedNamespace = null;
$parsed = substr( $query, strlen( $allkeyword ) );
} elseif ( strpos( $query, ':' ) !== false ) {
+ // TODO: should we unify with PrefixSearch::extractNamespace ?
$prefix = str_replace( ' ', '_', substr( $query, 0, strpos( $query, ':' ) ) );
$index = $wgContLang->getNsIndex( $prefix );
if ( $index !== false ) {
- $this->namespaces = [ $index ];
+ $extractedNamespace = [ $index ];
$parsed = substr( $query, strlen( $prefix ) + 1 );
+ } else {
+ return false;
}
}
+
if ( trim( $parsed ) == '' ) {
$parsed = $query; // prefix was the whole query
}
- return $parsed;
+ return [ $parsed, $extractedNamespace ];
}
/**
Hooks::run( 'SearchIndexFields', [ &$fields, $this ] );
return $fields;
}
+
+ /**
+ * Augment search results with extra data.
+ *
+ * @param SearchResultSet $resultSet
+ */
+ public function augmentSearchResults( SearchResultSet $resultSet ) {
+ $setAugmentors = [];
+ $rowAugmentors = [];
+ Hooks::run( "SearchResultsAugment", [ &$setAugmentors, &$rowAugmentors ] );
+
+ if ( !$setAugmentors && !$rowAugmentors ) {
+ // We're done here
+ return;
+ }
+
+ // Convert row augmentors to set augmentor
+ foreach ( $rowAugmentors as $name => $row ) {
+ if ( isset( $setAugmentors[$name] ) ) {
+ throw new InvalidArgumentException( "Both row and set augmentors are defined for $name" );
+ }
+ $setAugmentors[$name] = new PerRowAugmentor( $row );
+ }
+
+ foreach ( $setAugmentors as $name => $augmentor ) {
+ $data = $augmentor->augmentAll( $resultSet );
+ if ( $data ) {
+ $resultSet->setAugmentedData( $name, $data );
+ }
+ }
+ }
}
/**