* Added fields to list=search output: size, wordcount, timestamp, snippet
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 28 Jul 2009 21:13:48 +0000 (21:13 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 28 Jul 2009 21:13:48 +0000 (21:13 +0000)
* Where supported by backend, list=search adds a 'searchinfo' element with
  optional info: 'totalhits' count and 'suggestion' alternate query term

Snippets added to result items earlier by Roan; extended this with the other
byte size, word count, and timestamp available on the result items and exposed
through the regular UI.

Had to work out a backwards-compatible method for the search meta-information
with Roan; added a second 'searchinfo' element since adding attributes to
'search' would break compatibility for JSON output (despite being safe in XML).

'searchinfo' is present only if the backend supports the extra info and has
something available; 'totalhits' with a total hit count and 'suggestion' for
an alternate query suggestion (exposed as "Did you mean X?" link in UI).

Note that total hit counts can be enabled for MySQL backend now by setting
the experimental option $wgSearchMySQLTotalHits, but did-you-mean suggestions
are not yet supported and need to be tested with a hack or another backend.

Sample XML and JSON output with the new searchinfo items (which can be
present whether or not there are any result items):

<?xml version="1.0"?>
<api>
  <query>
    <searchinfo totalhits="0" suggestion="joe momma" />
    <search />
  </query>
</api>

{
"query": {
"searchinfo": {
"totalhits": 0,
"suggestion": "joe momma"
},
"search": [

]
}
}

The suggestion value is suitable for plugging back in as a search term,
if present.

RELEASE-NOTES
includes/api/ApiQuerySearch.php

index c730500..b0fd39d 100644 (file)
@@ -384,7 +384,6 @@ this. Was used when mwEmbed was going to be an extension.
 * (bug 18720) Add anchor field to action=parse&prop=sections output
 * (bug 19423) The initial file description page used caption in user lang
   rather than UI lang
-* Added snippet field to list=search output
 * (bug 17809) Add number of users in user groups to meta=siteinfo
 * (bug 18533) Add readonly reason to readonly exception
 * (bug 19528) Added XSLT parameter to API queries in format=xml
@@ -392,6 +391,9 @@ this. Was used when mwEmbed was going to be an extension.
   parameter in action=edit
 * (bug 19090) Added watchlist parameter, deprecated watch and unwatch 
   parameter in action=edit
+* Added fields to list=search output: size, wordcount, timestamp, snippet
+* Where supported by backend, list=search adds a 'searchinfo' element with
+  optional info: 'totalhits' count and 'suggestion' alternate query term
 
 === Languages updated in 1.16 ===
 
index 4a3b892..1d1a44f 100644 (file)
@@ -86,6 +86,15 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                if (is_null($matches))
                        $this->dieUsage("{$what} search is disabled",
                                        "search-{$what}-disabled");
+               
+               $totalhits = $matches->getTotalHits();
+               if( $totalhits !== null ) {
+                       $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'totalhits', $totalhits );
+               }
+               if( $matches->hasSuggestion() ) {
+                       $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'suggestion',
+                               $matches->getSuggestionQuery() );
+               }
 
                $terms = $wgContLang->convertForSearchResult($matches->termMatches());
                $titles = array ();
@@ -106,6 +115,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                                $vals = array();
                                ApiQueryBase::addTitleInfo($vals, $title);
                                $vals['snippet'] = $result->getTextSnippet($terms);
+                               $vals['size'] = $result->getByteSize();
+                               $vals['wordcount'] = $result->getWordCount();
+                               $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
                                $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
                                if(!$fit)
                                {