From 01515e038f382b7667245ead62a2bb8ebc28b260 Mon Sep 17 00:00:00 2001 From: Matthias Jordan Date: Tue, 1 Jul 2003 08:27:32 +0000 Subject: [PATCH] Implemented the "default namespaces for search" feature. This includes changes to the preferences form, a new key in the LanguageXX.php files (defaultns) and a new global config variable in DefaultSettings.php ($wgNamespacesToBeSearchedDefault). I also killed a bug that produced an errorneous SQL command when the user failed to check any namespaces to search in the search form. The updated implementation searched the main namespace if no checkbox is checked. --- includes/DefaultSettings.php | 3 ++ includes/SearchEngine.php | 54 +++++++++++++++++------ includes/SpecialPreferences.php | 76 ++++++++++++++++++++++++++++++++- includes/User.php | 4 ++ languages/Language.php | 1 + languages/LanguageDe.php | 1 + 6 files changed, 125 insertions(+), 14 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 40c7834a8c..60ad7a293e 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -92,4 +92,7 @@ $wgCompressByDefault = true; $wgNamespacesWithSubpages = array( -1 => 0, 0 => 0, 1 => 1, 2 => 1, 3 => 1, 4 => 0, 5 => 1, 6 => 0, 7 => 1 ); +$wgNamespacesToBeSearchedDefault = array( -1 => 0, 0 => 1, 1 => 0, + 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0 ); + ?> diff --git a/includes/SearchEngine.php b/includes/SearchEngine.php index 1960573d12..e339106015 100644 --- a/includes/SearchEngine.php +++ b/includes/SearchEngine.php @@ -23,7 +23,11 @@ class SearchEngine { function queryNamespaces() { - return "cur_namespace IN (" . implode( ",", $this->namespacesToSearch ) . ")"; + $namespaces = implode( ",", $this->namespacesToSearch ); + if ($namespaces == "") { + $namespaces = "0"; + } + return "AND cur_namespace IN (" . $namespaces . ")"; #return "1"; } @@ -33,6 +37,25 @@ class SearchEngine { return "AND cur_is_redirect=0 "; } + + + /* private */ function initNamespaceCheckbox( $i ) + { + global $wgUser, $wgNamespacesToBeSearchedDefault; + + + if ($wgUser->getRights()) { + // User is logged in so we retrieve his default namespaces + return $wgUser->getOption( "searchNs".$i ); + } + else { + // User is not logged in so we give him the global default namespaces + return $wgNamespacesToBeSearchedDefault[ $i ]; + } + } + + + function powersearch() { global $wgUser, $wgOut, $wgLang, $wgTitle; @@ -41,18 +64,25 @@ class SearchEngine { $search = $_REQUEST['search']; $searchx = $_REQUEST['searchx']; $listredirs = $_REQUEST['redirs']; - $nscb[0] = $_REQUEST['ns0']; - $nscb[1] = $_REQUEST['ns1']; - $nscb[2] = $_REQUEST['ns2']; - $nscb[3] = $_REQUEST['ns3']; - $nscb[4] = $_REQUEST['ns4']; - $nscb[5] = $_REQUEST['ns5']; - $nscb[6] = $_REQUEST['ns6']; - $nscb[7] = $_REQUEST['ns7']; + if ( ! isset ( $searchx ) ) { /* First time here */ - $nscb[0] = $listredirs = 1; /* All others should be unset */ + $listredirs = 1; + for ($i = 0; ($i <= 7); $i++) + { + $nscb[$i] = $this->initNamespaceCheckbox($i); + } + } else { + $nscb[0] = $_REQUEST['ns0']; + $nscb[1] = $_REQUEST['ns1']; + $nscb[2] = $_REQUEST['ns2']; + $nscb[3] = $_REQUEST['ns3']; + $nscb[4] = $_REQUEST['ns4']; + $nscb[5] = $_REQUEST['ns5']; + $nscb[6] = $_REQUEST['ns6']; + $nscb[7] = $_REQUEST['ns7']; } + $this->checkboxes["searchx"] = 1; $ret = wfMsg("powersearchtext"); @@ -143,7 +173,7 @@ class SearchEngine { $sql = "SELECT cur_id,cur_namespace,cur_title," . "cur_text FROM cur,searchindex " . "WHERE cur_id=si_page AND {$this->mTitlecond} " . - "AND {$searchnamespaces} {$redircond}" . + "{$searchnamespaces} {$redircond}" . "LIMIT {$offset}, {$limit}"; $res1 = wfQuery( $sql, $fname ); $num = wfNumRows($res1); @@ -154,7 +184,7 @@ class SearchEngine { $sql = "SELECT cur_id,cur_namespace,cur_title," . "cur_text FROM cur,searchindex " . "WHERE cur_id=si_page AND {$this->mTextcond} " . - "AND {$searchnamespaces} {$redircond} " . + "{$searchnamespaces} {$redircond} " . "LIMIT {$offset}, {$limit}"; $res2 = wfQuery( $sql, $fname ); $num = $num + wfNumRows($res2); diff --git a/includes/SpecialPreferences.php b/includes/SpecialPreferences.php index 48294dd3f0..18117a3e15 100644 --- a/includes/SpecialPreferences.php +++ b/includes/SpecialPreferences.php @@ -43,6 +43,21 @@ function wfSpecialPreferences() } } + +/* private */ function validateCheckbox( $cb ) +{ + if ( $cb ) + { + return 1; + } + else + { + return 0; + } +} + + + /* private */ function savePreferences() { global $wgUser, $wgLang, $wgDeferredUpdateList; @@ -50,6 +65,7 @@ function wfSpecialPreferences() global $wpSkin, $wpMath, $wpEmail, $wpEmailFlag, $wpNick, $wpSearch, $wpRecent; global $wpSearchLines, $wpSearchChars, $wpStubs; global $wpRows, $wpCols, $wpHourDiff, $HTTP_POST_VARS; + global $wpNs0, $wpNs1, $wpNs2, $wpNs3, $wpNs4, $wpNs5, $wpNs6, $wpNs7; if ( "" != $wpNewpass ) { if ( $wpNewpass != $wpRetype ) { @@ -79,8 +95,17 @@ function wfSpecialPreferences() $wgUser->setOption( "stubthreshold", validateIntOrNull( $wpStubs ) ); $wgUser->setOption( "timecorrection", validateIntOrNull( $wpHourDiff, -12, 14 ) ); - if ( $wpEmailFlag ) { $wgUser->setOption( "disablemail", 1 ); } - else { $wgUser->setOption( "disablemail", 0 ); } + $wgUser->setOption( "searchNs0", validateCheckbox( $wpNs0 ) ); + $wgUser->setOption( "searchNs1", validateCheckbox( $wpNs1 ) ); + $wgUser->setOption( "searchNs2", validateCheckbox( $wpNs2 ) ); + $wgUser->setOption( "searchNs3", validateCheckbox( $wpNs3 ) ); + $wgUser->setOption( "searchNs4", validateCheckbox( $wpNs4 ) ); + $wgUser->setOption( "searchNs5", validateCheckbox( $wpNs5 ) ); + $wgUser->setOption( "searchNs6", validateCheckbox( $wpNs6 ) ); + $wgUser->setOption( "searchNs7", validateCheckbox( $wpNs7 ) ); + + + $wgUser->setOption( "disablemail", validateCheckbox( $wpEmailFlag ) ); $togs = $wgLang->getUserToggles(); foreach ( $togs as $tname => $ttext ) { @@ -128,6 +153,45 @@ function wfSpecialPreferences() } } + + + +/* private */ function namespacesCheckboxes() +{ + global $wgLang, $wgUser; + $nscb = array(); + + + for ($i = 0; ($i < 8); $i++) + { + $nscb[$i] = $wgUser->getOption( "searchNs".$i ); + } + + # Determine namespace checkboxes + + $ns = $wgLang->getNamespaces(); + array_shift( $ns ); /* Skip "Special" */ + + $r1 = ""; + for ( $i = 0; $i < count( $ns ); ++$i ) { + $checked = ""; + if ( $nscb[$i] == 1 ) { + $checked = " checked"; + } + $name = str_replace( "_", " ", $ns[$i] ); + if ( "" == $name ) { $name = "(Main)"; } + + if ( 0 != $i ) { $r1 .= " "; } + $r1 .= "{$name}\n"; + } + + return $r1; +} + + + + /* private */ function mainPrefsForm( $err ) { global $wgUser, $wgOut, $wgLang; @@ -181,6 +245,7 @@ function wfSpecialPreferences() $scl = wfMsg( "contextlines" ); $scc = wfMsg( "contextchars" ); $rcc = wfMsg( "recentchangescount" ); + $dsn = wfMsg( "defaultns" ); $wgOut->addHTML( "
+ +
$qb:
\n" ); @@ -262,6 +327,8 @@ value=\"$i\"$checked> {$mathopts[$i]}
\n" ); if ( $wpEmailFlag ) { $emfc = "checked"; } else { $emfc = ""; } + $ps = namespacesCheckboxes(); + $wgOut->addHTML( "


@@ -273,6 +340,11 @@ value=\"$i\"$checked> {$mathopts[$i]}
\n" );
+$dsn
+$ps +
* {$tzt}
\n" ); diff --git a/includes/User.php b/includes/User.php index 691860b30a..4e38f7de9e 100644 --- a/includes/User.php +++ b/includes/User.php @@ -69,6 +69,7 @@ class User { function loadDefaults() { global $wgLang ; + global $wgNamespacesToBeSearchedDefault; $this->mId = $this->mNewtalk = 0; $this->mName = getenv( "REMOTE_ADDR" ); @@ -79,6 +80,9 @@ class User { foreach ( $defOpt as $oname => $val ) { $this->mOptions[$oname] = $val; } + foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) { + $this->mOptions["searchNs".$nsnum] = $val; + } unset( $this->mSkin ); $this->mDataLoaded = false; $this->mBlockedby = -1; # Unset diff --git a/languages/Language.php b/languages/Language.php index 6ecf19693d..7d704a15bc 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -634,6 +634,7 @@ from server time (UTC).", "servertime" => "Server time is now", "guesstimezone" => "Fill in from browser", "emailflag" => "Disable e-mail from other users", +"defaultns" => "Search in these namespaces by default:", # Recent changes # diff --git a/languages/LanguageDe.php b/languages/LanguageDe.php index 8d7376c38c..91adf0ccf0 100644 --- a/languages/LanguageDe.php +++ b/languages/LanguageDe.php @@ -568,6 +568,7 @@ Ihre interne ID-Nummer ist $2.", "servertime" => "Aktuelle Zeit auf dem Server", "guesstimezone" => "Einfügen aus dem Browser", "emailflag" => "Keine E-Mail von anderen Benutzern erhalten", +"defaultns" => "In diesen Namensräumen soll standardmäßig gesucht werden:", # Recent changes # -- 2.20.1