Manybubbles has uploaded a new change for review.
https://gerrit.wikimedia.org/r/161020
Change subject: Support strings in namespace weights
......................................................................
Support strings in namespace weights
This is much more convenient when specifying configuration across many wikis
that do not share consistent namespace numbering but do consistently alias
namespaces. For example: most wikisources alias the 'Author' namespace
to the word 'Author' in their language but that namespace is sometimes 100,
sometimes 102, somtimes 104, and sometimes 106.
Bug: 69771
Change-Id: Ice3febecd8cd39c551d2809f1a88a6c8c91fae26
---
M CirrusSearch.php
M includes/Searcher.php
2 files changed, 36 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch
refs/changes/20/161020/1
diff --git a/CirrusSearch.php b/CirrusSearch.php
index 08a63fb..00e9beb 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -260,6 +260,8 @@
// NS_MAIN can be overriden with this then 1 just represents what NS_MAIN
would have been....
// If you override NS_MAIN here then NS_TALK will still default to:
// $wgCirrusSearchNamespaceWeights[ NS_MAIN ] *
wgCirrusSearchTalkNamespaceWeight
+// You can specify namespace by number or string. Strings are converted to
numbers using the
+// content language including aliases.
$wgCirrusSearchNamespaceWeights = array(
NS_USER => 0.05,
NS_PROJECT => 0.1,
diff --git a/includes/Searcher.php b/includes/Searcher.php
index bd78012..c0f57a0 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -8,6 +8,7 @@
use \CirrusSearch\Search\FullTextResultsType;
use \CirrusSearch\Search\IdResultsType;
use \CirrusSearch\Search\ResultsType;
+use \Language;
use \MWNamespace;
use \PoolCounterWorkViaCallback;
use \ProfileSection;
@@ -71,6 +72,11 @@
* @var array(integer) namespaces in which to search
*/
protected $namespaces;
+
+ /**
+ * @var Language language of the wiki
+ */
+ private $language;
/**
* @var ResultsType|null type of results. null defaults to
FullTextResultsType
@@ -169,6 +175,12 @@
private $returnQuery = false;
/**
+ * @var null|array lazily initialized version of
$wgCirrusSearchNamespaceWeights with all string keys
+ * translated into integer namespace codes using $this->language.
+ */
+ private $normalizedNamespaceWeights = null;
+
+ /**
* Constructor
* @param int $offset Offset the results by this much
* @param int $limit Limit the results to this many
@@ -178,13 +190,15 @@
*/
public function __construct( $offset, $limit, $namespaces, $user,
$index = false ) {
global $wgCirrusSearchSlowSearch,
- $wgLanguageCode;
+ $wgLanguageCode,
+ $wgContLang;
parent::__construct( $user, $wgCirrusSearchSlowSearch );
$this->offset = min( $offset, self::MAX_OFFSET );
$this->limit = $limit;
$this->namespaces = $namespaces;
$this->indexBaseName = $index ?: wfWikiId();
+ $this->language = $wgContLang;
$this->escaper = new Escaper( $wgLanguageCode );
}
@@ -1379,8 +1393,23 @@
$wgCirrusSearchDefaultNamespaceWeight,
$wgCirrusSearchTalkNamespaceWeight;
- if ( isset( $wgCirrusSearchNamespaceWeights[ $namespace ] ) ) {
- return $wgCirrusSearchNamespaceWeights[ $namespace ];
+ if ( $this->normalizedNamespaceWeights === null ) {
+ $this->normalizedNamespaceWeights = array();
+ foreach ( $wgCirrusSearchNamespaceWeights as $ns =>
$weight ) {
+ if ( is_string( $ns ) ) {
+ $ns = $this->language->getNsIndex( $ns
);
+ // Ignore namespaces that don't exist.
+ if ( $ns === false ) {
+ continue;
+ }
+ }
+ // Now $ns should always be an integer.
+ $this->normalizedNamespaceWeights[ $ns ] =
$weight;
+ }
+ }
+
+ if ( isset( $this->normalizedNamespaceWeights[ $namespace ] ) )
{
+ return $this->normalizedNamespaceWeights[ $namespace ];
}
if ( MWNamespace::isSubject( $namespace ) ) {
if ( $namespace === NS_MAIN ) {
@@ -1389,8 +1418,8 @@
return $wgCirrusSearchDefaultNamespaceWeight;
}
$subjectNs = MWNamespace::getSubject( $namespace );
- if ( isset( $wgCirrusSearchNamespaceWeights[ $subjectNs ] ) ) {
- return $wgCirrusSearchTalkNamespaceWeight *
$wgCirrusSearchNamespaceWeights[ $subjectNs ];
+ if ( isset( $this->normalizedNamespaceWeights[ $subjectNs ] ) )
{
+ return $wgCirrusSearchTalkNamespaceWeight *
$this->normalizedNamespaceWeights[ $subjectNs ];
}
if ( $namespace === NS_TALK ) {
return $wgCirrusSearchTalkNamespaceWeight;
--
To view, visit https://gerrit.wikimedia.org/r/161020
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ice3febecd8cd39c551d2809f1a88a6c8c91fae26
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits