Rework PHP and vendor check
[lhc/web/wiklou.git] / includes / PrefixSearch.php
index 955313b..55a4f49 100644 (file)
@@ -34,11 +34,12 @@ abstract class PrefixSearch {
         * @param string $search
         * @param int $limit
         * @param array $namespaces Used if query is not explicitly prefixed
+        * @param int $offset How many results to offset from the beginning
         * @return array Array of strings
         */
-       public static function titleSearch( $search, $limit, $namespaces = array() ) {
+       public static function titleSearch( $search, $limit, $namespaces = array(), $offset = 0 ) {
                $prefixSearch = new StringPrefixSearch;
-               return $prefixSearch->search( $search, $limit, $namespaces );
+               return $prefixSearch->search( $search, $limit, $namespaces, $offset );
        }
 
        /**
@@ -47,9 +48,10 @@ abstract class PrefixSearch {
         * @param string $search
         * @param int $limit
         * @param array $namespaces Used if query is not explicitly prefixed
+        * @param int $offset How many results to offset from the beginning
         * @return array Array of strings or Title objects
         */
-       public function search( $search, $limit, $namespaces = array() ) {
+       public function search( $search, $limit, $namespaces = array(), $offset = 0 ) {
                $search = trim( $search );
                if ( $search == '' ) {
                        return array(); // Return empty result
@@ -63,9 +65,9 @@ abstract class PrefixSearch {
                        $search = $title->getText();
                        if ( $ns[0] == NS_MAIN ) {
                                $ns = $namespaces; // no explicit prefix, use default namespaces
-                               wfRunHooks( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) );
+                               Hooks::run( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) );
                        }
-                       return $this->searchBackend( $ns, $search, $limit );
+                       return $this->searchBackend( $ns, $search, $limit, $offset );
                }
 
                // Is this a namespace prefix?
@@ -77,10 +79,10 @@ abstract class PrefixSearch {
                        $namespaces = array( $title->getNamespace() );
                        $search = '';
                } else {
-                       wfRunHooks( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) );
+                       Hooks::run( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) );
                }
 
-               return $this->searchBackend( $namespaces, $search, $limit );
+               return $this->searchBackend( $namespaces, $search, $limit, $offset );
        }
 
        /**
@@ -88,12 +90,12 @@ abstract class PrefixSearch {
         * @param string $search
         * @param int $limit
         * @param array $namespaces
+        * @param int $offset How many results to offset from the beginning
         *
         * @return array
         */
-       public function searchWithVariants( $search, $limit, array $namespaces ) {
-               wfProfileIn( __METHOD__ );
-               $searches = $this->search( $search, $limit, $namespaces );
+       public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
+               $searches = $this->search( $search, $limit, $namespaces, $offset );
 
                // if the content language has variants, try to retrieve fallback results
                $fallbackLimit = $limit - count( $searches );
@@ -113,7 +115,6 @@ abstract class PrefixSearch {
                                }
                        }
                }
-               wfProfileOut( __METHOD__ );
                return $searches;
        }
 
@@ -141,20 +142,24 @@ abstract class PrefixSearch {
         * @param array $namespaces
         * @param string $search
         * @param int $limit
+        * @param int $offset How many results to offset from the beginning
         * @return array Array of strings
         */
-       protected function searchBackend( $namespaces, $search, $limit ) {
+       protected function searchBackend( $namespaces, $search, $limit, $offset ) {
                if ( count( $namespaces ) == 1 ) {
                        $ns = $namespaces[0];
                        if ( $ns == NS_MEDIA ) {
                                $namespaces = array( NS_FILE );
                        } elseif ( $ns == NS_SPECIAL ) {
-                               return $this->titles( $this->specialSearch( $search, $limit ) );
+                               return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
                        }
                }
                $srchres = array();
-               if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
-                       return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
+               if ( Hooks::run(
+                       'PrefixSearchBackend',
+                       array( $namespaces, $search, $limit, &$srchres, $offset )
+               ) ) {
+                       return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
                }
                return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
        }
@@ -189,25 +194,25 @@ abstract class PrefixSearch {
                                // returned match to the front.  This might look odd but the alternative
                                // is to put the redirect in front and drop the match.  The name of the
                                // found match is often more descriptive/better formed than the name of
-                               // the redirec AND by definition they share a prefix.  Hopefully this
-                               // choice is less confusing and more helpful.  But it might now be.  But
+                               // the redirect AND by definition they share a prefix.  Hopefully this
+                               // choice is less confusing and more helpful.  But it might not be.  But
                                // it is the choice we're going with for now.
                                return $this->pullFront( $key, $srchres );
                        }
                        $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
-                       if ( isset( $redirectTargetsToRedirect[ $target ] ) ) {
+                       if ( isset( $redirectTargetsToRedirect[$target] ) ) {
                                // The exact match and something in the results list are both redirects
                                // to the same thing!  In this case we'll pull the returned match to the
                                // top following the same logic above.  Again, it might not be a perfect
                                // choice but it'll do.
-                               return $this->pullFront( $redirectTargetsToRedirect[ $target ], $srchres );
+                               return $this->pullFront( $redirectTargetsToRedirect[$target], $srchres );
                        }
                } else {
                        $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
-                       if ( isset( $redirectTargetsToRedirect[ $string ] ) ) {
+                       if ( isset( $redirectTargetsToRedirect[$string] ) ) {
                                // The exact match is the target of a redirect already in the results list so remove
                                // the redirect from the results list and push the exact match to the front
-                               array_splice( $srchres, $redirectTargetsToRedirect[ $string ], 1 );
+                               array_splice( $srchres, $redirectTargetsToRedirect[$string], 1 );
                                array_unshift( $srchres, $string );
                                return $srchres;
                        }
@@ -238,7 +243,7 @@ abstract class PrefixSearch {
                        if ( !$target ) {
                                continue;
                        }
-                       $result[ $target ] = $key;
+                       $result[$target] = $key;
                }
                return $result;
        }
@@ -266,9 +271,10 @@ abstract class PrefixSearch {
         *
         * @param string $search Term
         * @param int $limit Max number of items to return
+        * @param int $offset Number of items to offset
         * @return array
         */
-       protected function specialSearch( $search, $limit ) {
+       protected function specialSearch( $search, $limit, $offset ) {
                global $wgContLang;
 
                $searchParts = explode( '/', $search, 2 );
@@ -284,7 +290,7 @@ abstract class PrefixSearch {
                        }
                        $special = SpecialPageFactory::getPage( $specialTitle->getText() );
                        if ( $special ) {
-                               $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit );
+                               $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
                                return array_map( function ( $sub ) use ( $specialTitle ) {
                                        return $specialTitle->getSubpage( $sub );
                                }, $subpages );
@@ -316,12 +322,17 @@ abstract class PrefixSearch {
                ksort( $keys );
 
                $srchres = array();
+               $skipped = 0;
                foreach ( $keys as $pageKey => $page ) {
                        if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
                                // bug 27671: Don't use SpecialPage::getTitleFor() here because it
                                // localizes its input leading to searches for e.g. Special:All
                                // returning Spezial:MediaWiki-Systemnachrichten and returning
                                // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
+                               if ( $offset > 0 && $skipped < $offset ) {
+                                       $skipped++;
+                                       continue;
+                               }
                                $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
                        }
 
@@ -342,9 +353,10 @@ abstract class PrefixSearch {
         * @param array $namespaces Namespaces to search in
         * @param string $search Term
         * @param int $limit Max number of items to return
+        * @param int $offset Number of items to skip
         * @return array Array of Title objects
         */
-       protected function defaultSearchBackend( $namespaces, $search, $limit ) {
+       protected function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
                $ns = array_shift( $namespaces ); // support only one namespace
                if ( in_array( NS_MAIN, $namespaces ) ) {
                        $ns = NS_MAIN; // if searching on many always default to main
@@ -360,7 +372,11 @@ abstract class PrefixSearch {
                                'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
                        ),
                        __METHOD__,
-                       array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
+                       array(
+                               'LIMIT' => $limit,
+                               'ORDER BY' => 'page_title',
+                               'OFFSET' => $offset
+                       )
                );
                $srchres = array();
                foreach ( $res as $row ) {