DatabaseMssql class and related changes
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index da054e0..8035e98 100644 (file)
@@ -1,21 +1,22 @@
 <?php
-# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
+/**
+ * Copyright (C) 2004 Brion Vibber <brion@pobox.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
 
 /**
  * Run text & title search and display the output
@@ -56,11 +57,10 @@ class SpecialSearch {
         * Set up basic search parameters from the request and user settings.
         * Typically you'll pass $wgRequest and $wgUser.
         *
-        * @param WebRequest $request
-        * @param User $user
-        * @public
+        * @param $request WebRequest
+        * @param $user User
         */
-       function __construct( &$request, &$user ) {
+       public function __construct( &$request, &$user ) {
                list( $this->limit, $this->offset ) = $request->getLimitOffset( 20, 'searchlimit' );
                $this->mPrefix = $request->getVal('prefix', '');
                # Extract requested namespaces
@@ -78,7 +78,8 @@ class SpecialSearch {
 
        /**
         * If an exact title match can be found, jump straight ahead to it.
-        * @param string $term
+        *
+        * @param $term String
         */
        public function goResult( $term ) {
                global $wgOut;
@@ -92,6 +93,7 @@ class SpecialSearch {
                # If there's an exact or very near match, jump right there.
                $t = SearchEngine::getNearMatch( $term );
                if( !is_null( $t ) ) {
+                       wfRunHooks( 'SpecialSearchGomatch', array( &$t ) );
                        $wgOut->redirect( $t->getFullURL() );
                        return;
                }
@@ -110,14 +112,48 @@ class SpecialSearch {
        }
 
        /**
-        * @param string $term
+        * @param $term String
         */
        public function showResults( $term ) {
                global $wgOut, $wgUser, $wgDisableTextSearch, $wgContLang, $wgScript;
                wfProfileIn( __METHOD__ );
 
                $sk = $wgUser->getSkin();
-
+               
+               $beginSearchForm = Xml::openElement(
+                       'form',
+                       array(
+                               'id' => ( $this->searchAdvanced ? 'powersearch' : 'search' ),
+                               'method' => 'get',
+                               'action' => $wgScript
+                       )
+               );
+               $mwSearchTopTable =
+                       Xml::openElement( 'table', array( 'id' => 'mw-search-top-table', 'border' => 0, 'cellpadding' => 0, 'cellspacing' => 0 ) ) .
+                       Xml::openElement( 'tr' ) .
+                       Xml::openElement( 'td' ) . "\n" .
+                       $this->shortDialog( $term ) .
+                       Xml::closeElement( 'td' ) .
+                       Xml::closeElement( 'tr' ) .
+                       Xml::closeElement( 'table' );
+               
+               // moved to check for empty or null search string before running query
+               // to prevent NULL fulltext search error in SQL Server
+               $filePrefix = $wgContLang->getFormattedNsText( NS_FILE ) . ':';
+               if ( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
+                       $wgOut->addHTML( $beginSearchForm );
+                       $wgOut->addHTML( $mwSearchTopTable );
+                       $wgOut->addHTML( $this->searchFocus() );
+                       $wgOut->addHTML( $this->formHeader( $term, 0, 0 ) );
+                       if ( $this->searchAdvanced ) {
+                               $wgOut->addHTML( $this->powerSearchBox( $term ) );
+                       }
+                       $wgOut->addHTML( Xml::closeElement( 'form' ) );
+                       // Empty query -- straight view of search form
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               
                $this->searchEngine = SearchEngine::create();
                $search =& $this->searchEngine;
                $search->setLimitOffset( $this->limit, $this->offset );
@@ -192,23 +228,10 @@ class SpecialSearch {
                }
                // start rendering the page
                $wgOut->addHtml(
-                       Xml::openElement(
-                               'form',
-                               array(
-                                       'id' => ( $this->searchAdvanced ? 'powersearch' : 'search' ),
-                                       'method' => 'get',
-                                       'action' => $wgScript
-                               )
-                       )
+                       $beginSearchForm
                );
                $wgOut->addHtml(
-                       Xml::openElement( 'table', array( 'id'=>'mw-search-top-table', 'border'=>0, 'cellpadding'=>0, 'cellspacing'=>0 ) ) .
-                       Xml::openElement( 'tr' ) .
-                       Xml::openElement( 'td' ) . "\n" .
-                       $this->shortDialog( $term ) .
-                       Xml::closeElement('td') .
-                       Xml::closeElement('tr') .
-                       Xml::closeElement('table')
+                       $mwSearchTopTable
                );
 
                // Sometimes the search engine knows there are too many hits
@@ -218,19 +241,6 @@ class SpecialSearch {
                        return;
                }
 
-               $filePrefix = $wgContLang->getFormattedNsText(NS_FILE).':';
-               if( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
-                       $wgOut->addHTML( $this->searchFocus() );
-                       $wgOut->addHTML( $this->formHeader($term, 0, 0));
-                       if( $this->searchAdvanced ) {
-                               $wgOut->addHTML( $this->powerSearchBox( $term ) );
-                       } 
-                       $wgOut->addHTML( '</form>' );
-                       // Empty query -- straight view of search form
-                       wfProfileOut( __METHOD__ );
-                       return;
-               }
-
                // Get number of results
                $titleMatchesNum = $titleMatches ? $titleMatches->numRows() : 0;
                $textMatchesNum = $textMatches ? $textMatches->numRows() : 0;
@@ -326,6 +336,8 @@ class SpecialSearch {
                                $messageName = 'searchmenu-exists';
                        } elseif( $t->userCan( 'create' ) ) {
                                $messageName = 'searchmenu-new';
+                       } else {
+                               $messageName = 'searchmenu-new-nocreate';
                        }
                } 
                if( $messageName ) {
@@ -364,14 +376,17 @@ class SpecialSearch {
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
                // add javascript specific to special:search
                $wgOut->addScriptFile( 'search.js' );
+
+               // Bug #16886: Sister projects box moves down the first extract on IE7  
+               $wgOut->addStyle( 'common/IE70Fixes.css', 'screen', 'IE 7' );
        }
 
        /**
         * Extract "power search" namespace settings from the request object,
         * returning a list of index numbers to search.
         *
-        * @param WebRequest $request
-        * @return array
+        * @param $request WebRequest
+        * @return Array
         */
        protected function powerSearch( &$request ) {
                $arr = array();
@@ -385,7 +400,8 @@ class SpecialSearch {
 
        /**
         * Reconstruct the 'power search' options for links
-        * @return array
+        *
+        * @return Array
         */
        protected function powerSearchOptions() {
                $opt = array();
@@ -402,7 +418,7 @@ class SpecialSearch {
        /**
         * Show whole set of results
         *
-        * @param SearchResultSet $matches
+        * @param $matches SearchResultSet
         */
        protected function showMatches( &$matches ) {
                global $wgContLang;
@@ -430,11 +446,12 @@ class SpecialSearch {
 
        /**
         * Format a single hit result
-        * @param SearchResult $result
-        * @param array $terms terms to highlight
+        *
+        * @param $result SearchResult
+        * @param $terms Array: terms to highlight
         */
        protected function showHit( $result, $terms ) {
-               global $wgContLang, $wgLang, $wgUser;
+               global $wgLang, $wgUser;
                wfProfileIn( __METHOD__ );
 
                if( $result->isBrokenTitle() ) {
@@ -538,6 +555,18 @@ class SpecialSearch {
                        $this->sk->formatSize( $byteSize ),
                        $wgLang->formatNum( $wordCount )
                );
+
+               if( $t->getNamespace() == NS_CATEGORY ) {
+                       $cat = Category::newFromTitle( $t );
+                       $size = wfMsgExt(
+                               'search-result-category-size',
+                               array( 'parsemag', 'escape' ),
+                               $wgLang->formatNum( $cat->getPageCount() ),
+                               $wgLang->formatNum( $cat->getSubcatCount() ),
+                               $wgLang->formatNum( $cat->getFileCount() )
+                       );
+               }
+
                $date = $wgLang->timeanddate( $timestamp );
 
                // link to related articles if supported
@@ -590,7 +619,7 @@ class SpecialSearch {
                }
 
                wfProfileOut( __METHOD__ );
-               return "<li>{$link} {$redirect} {$section} {$extract}\n" .
+               return "<li><div class='mw-search-result-heading'>{$link} {$redirect} {$section}</div> {$extract}\n" .
                        "<div class='mw-search-result-data'>{$score}{$size} - {$date}{$related}</div>" .
                        "</li>\n";
 
@@ -599,7 +628,8 @@ class SpecialSearch {
        /**
         * Show results from other wikis
         *
-        * @param SearchResultSet $matches
+        * @param $matches SearchResultSet
+        * @param $query String
         */
        protected function showInterwiki( &$matches, $query ) {
                global $wgContLang;
@@ -637,15 +667,14 @@ class SpecialSearch {
        /**
         * Show single interwiki link
         *
-        * @param SearchResult $result
-        * @param string $lastInterwiki
-        * @param array $terms
-        * @param string $query
-        * @param array $customCaptions iw prefix -> caption
+        * @param $result SearchResult
+        * @param $lastInterwiki String
+        * @param $terms Array
+        * @param $query String
+        * @param $customCaptions Array: iw prefix -> caption
         */
        protected function showInterwikiHit( $result, $lastInterwiki, $terms, $query, $customCaptions) {
                wfProfileIn( __METHOD__ );
-               global $wgContLang, $wgLang;
 
                if( $result->isBrokenTitle() ) {
                        wfProfileOut( __METHOD__ );
@@ -718,12 +747,11 @@ class SpecialSearch {
 
        /**
         * Generates the power search box at bottom of [[Special:Search]]
-        * @param $term string: search term
-        * @return $out string: HTML form
+        *
+        * @param $term String: search term
+        * @return String: HTML form
         */
        protected function powerSearchBox( $term ) {
-               global $wgScript, $wgContLang;
-               
                // Groups namespaces into rows according to subject
                $rows = array();
                foreach( SearchEngine::searchableNamespaces() as $namespace => $name ) {
@@ -871,7 +899,7 @@ class SpecialSearch {
        }
 
        protected function formHeader( $term, $resultsShown, $totalNum ) {
-               global $wgContLang, $wgLang;
+               global $wgLang;
                
                $out = Xml::openElement('div', array( 'class' =>  'mw-search-formheader' ) );
                
@@ -881,7 +909,6 @@ class SpecialSearch {
                        $bareterm = substr( $term, strpos( $term, ':' ) + 1 );
                }
 
-               
                $profiles = $this->getSearchProfiles();
                
                // Outputs XML for Search Types
@@ -957,7 +984,16 @@ class SpecialSearch {
                return $out . $this->didYouMeanHtml;            
        }
 
-       /** Make a search link with some target namespaces */
+       /**
+        * Make a search link with some target namespaces
+        *
+        * @param $term String
+        * @param $namespaces Array
+        * @param $label String: link's text
+        * @param $tooltip String: link's tooltip
+        * @param $params Array: query string parameters
+        * @return String: HTML fragment
+        */
        protected function makeSearchLink( $term, $namespaces, $label, $tooltip, $params=array() ) {
                $opt = $params;
                foreach( $namespaces as $n ) {
@@ -985,7 +1021,12 @@ class SpecialSearch {
                );
        }
 
-       /** Check if query starts with image: prefix */
+       /**
+        * Check if query starts with image: prefix
+        *
+        * @param $term String: the string to check
+        * @return Boolean
+        */
        protected function startsWithImage( $term ) {
                global $wgContLang;
 
@@ -996,7 +1037,12 @@ class SpecialSearch {
                return false;
        }
        
-       /** Check if query starts with all: prefix */
+       /**
+        * Check if query starts with all: prefix
+        *
+        * @param $term String: the string to check
+        * @return Boolean
+        */
        protected function startsWithAll( $term ) {
 
                $allkeyword = wfMsgForContent('searchall');