Enhanced ACP search provider API
authorAlexander Ebert <ebert@woltlab.com>
Sat, 18 Aug 2012 13:51:26 +0000 (15:51 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 18 Aug 2012 13:51:26 +0000 (15:51 +0200)
wcfsetup/install/files/lib/system/search/acp/ACPSearchHandler.class.php
wcfsetup/install/files/lib/system/search/acp/ACPSearchResultList.class.php
wcfsetup/install/files/lib/system/search/acp/IACPSearchResultProvider.class.php
wcfsetup/install/files/lib/system/search/acp/MenuItemACPSearchResultProvider.class.php
wcfsetup/install/files/lib/system/search/acp/OptionACPSearchResultProvider.class.php
wcfsetup/install/files/lib/system/search/acp/UserGroupOptionACPSearchResultProvider.class.php

index a13b78df2e8aabd1caca1f69e2d172cde0b3e0a4..7d77476adbdffde5d9b436579fdde968c96c5d28 100644 (file)
@@ -72,6 +72,10 @@ class ACPSearchHandler extends SingletonFactory {
                                        $resultList->addResult($result);
                                }
                                
+                               // sort list and reduce results
+                               $resultList->sort();
+                               $resultList->reduceResultsTo($maxResultsPerProvider);
+                               
                                $data[] = $resultList;
                                $totalResultCount += count($resultList);
                        }
index 75b92d9904492c3a367a56337c1d4bbcffd94f6e..12ec661ec237e783c9d7ca7ec55c061e7506fc63 100644 (file)
@@ -71,6 +71,20 @@ class ACPSearchResultList implements \Countable, \Iterator {
                $this->rewind();
        }
        
+       /**
+        * Reduces the result collection to specified size.
+        * 
+        * @param       integer         $size
+        */
+       public function reduceResultsTo($size) {
+               $count = count($this->results);
+               
+               if ($size && ($count > $size)) {
+                       $reduceBy = $count - $size;
+                       $this->reduceResults($reduceBy);
+               }
+       }
+       
        /**
         * Sorts results by title.
         */
index 8fc375b8c51e15fe09d4f13049662fb2b1f0dedc..ec17358b7b6258ed331a5745d8b92ea1afbb2213 100644 (file)
@@ -16,8 +16,7 @@ interface IACPSearchResultProvider {
         * Returns a list of seach results for given query.
         * 
         * @param       string          $query
-        * @param       integer         $limit
         * @return      array<wcf\system\search\acp\ACPSearchResult>
         */
-       public function search($query, $limit = 5);
+       public function search($query);
 }
index c7af47b1eee4dc7e148981792231259785fd2d02..571c027a8e660c692935e4fca826755f03e47382 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 namespace wcf\system\search\acp;
-use wcf\data\acp\menu\item\ACPMenuItem;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\package\PackageDependencyHandler;
 use wcf\system\request\LinkHandler;
@@ -20,7 +19,7 @@ class MenuItemACPSearchResultProvider extends AbstractACPSearchResultProvider im
        /**
         * @see wcf\system\search\acp\IACPSearchResultProvider::search()
         */
-       public function search($query, $limit = 5) {
+       public function search($query) {
                $results = array();
                
                // search by language item
@@ -64,19 +63,12 @@ class MenuItemACPSearchResultProvider extends AbstractACPSearchResultProvider im
                $statement = WCF::getDB()->prepareStatement($sql); // don't use a limit here
                $statement->execute($conditions->getParameters());
                
-               $count = 0;
-               while ($row = $statement->fetchArray()) {
-                       if ($count == $limit) {
-                               break;
-                       }
-                       
-                       $menuItem = new ACPMenuItem(null, $row);
+               while ($menuItem = $statement->fetchObject('wcf\data\acp\menu\item\ACPMenuItem')) {
                        if (!$this->validate($menuItem)) {
                                continue;
                        }
                        
-                       $results[] = new ACPSearchResult($languageItems[$row['menuItem']], $row['menuItemLink'] . SID_ARG_1ST);
-                       $count++;
+                       $results[] = new ACPSearchResult($languageItems[$menuItem->menuItem], $menuItem->getLink());
                }
                
                return $results;
index d7ef7ed330f3fb9966b86cd562a4f77917c36001..39a5eaf0dafd99011c75c5458d752d0512d8b36e 100644 (file)
@@ -1,9 +1,6 @@
 <?php
 namespace wcf\system\search\acp;
-use wcf\data\option\Option;
-use wcf\data\option\category\OptionCategoryList;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
-use wcf\system\exception\SystemException;
 use wcf\system\package\PackageDependencyHandler;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
@@ -27,7 +24,7 @@ class OptionACPSearchResultProvider extends AbstractCategorizedACPSearchResultPr
        /**
         * @see wcf\system\search\acp\IACPSearchResultProvider::search()
         */
-       public function search($query, $limit = 5) {
+       public function search($query) {
                $results = array();
                
                // search by language item
@@ -50,7 +47,7 @@ class OptionACPSearchResultProvider extends AbstractCategorizedACPSearchResultPr
                        FROM            wcf".WCF_N."_language_item
                        ".$conditions."
                        ORDER BY        languageItemValue ASC";
-               $statement = WCF::getDB()->prepareStatement($sql, $limit);
+               $statement = WCF::getDB()->prepareStatement($sql); // don't use a limit here
                $statement->execute($conditions->getParameters());
                $languageItems = array();
                $optionNames = array();
@@ -71,23 +68,22 @@ class OptionACPSearchResultProvider extends AbstractCategorizedACPSearchResultPr
                $sql = "SELECT  optionName, categoryName, options, permissions
                        FROM    wcf".WCF_N."_option
                        ".$conditions;
-               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement = WCF::getDB()->prepareStatement($sql); // don't use a limit here
                $statement->execute($conditions->getParameters());
                
-               while ($row = $statement->fetchArray()) {
+               while ($option = $statement->fetchObject('wcf\data\option\Option')) {
                        // category is not accessible
-                       if (!$this->isValid($row['categoryName'])) {
+                       if (!$this->isValid($option->categoryName)) {
                                continue;
                        }
                        
                        // option is not accessible
-                       $option = new Option(null, $row);
                        if (!$this->validate($option)) {
                                continue;
                        }
                        
-                       $link = LinkHandler::getInstance()->getLink('Option', array('id' => $this->getCategoryID($row['categoryName'])), 'optionName='.$row['optionName'].'#'.$this->getCategoryName($row['categoryName']));
-                       $results[] = new ACPSearchResult($languageItems[$row['optionName']], $link);
+                       $link = LinkHandler::getInstance()->getLink('Option', array('id' => $this->getCategoryID($option->categoryName)), 'optionName='.$option->optionName.'#'.$this->getCategoryName($option->categoryName));
+                       $results[] = new ACPSearchResult($languageItems[$option->optionName], $link);
                }
                
                return $results;
index 9fa9af9b6ef356ee6db80bc85ab07e466f58c64f..0b153b29db9b4abd8955a6226a3146c1b4b80760 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 namespace wcf\system\search\acp;
-use wcf\data\user\group\option\UserGroupOption;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\package\PackageDependencyHandler;
 use wcf\system\request\LinkHandler;
@@ -25,7 +24,7 @@ class UserGroupOptionACPSearchResultProvider extends AbstractCategorizedACPSearc
        /**
         * @see wcf\system\search\acp\IACPSearchResultProvider::search()
         */
-       public function search($query, $limit = 5) {
+       public function search($query) {
                $results = array();
                
                // search by language item
@@ -74,26 +73,19 @@ class UserGroupOptionACPSearchResultProvider extends AbstractCategorizedACPSearc
                $statement = WCF::getDB()->prepareStatement($sql); // don't use a limit here
                $statement->execute($conditions->getParameters());
                
-               $count = 0;
-               while ($row = $statement->fetchArray()) {
-                       if ($count == $limit) {
-                               break;
-                       }
-                       
+               while ($userGroupOption = $statement->fetchObject('wcf\data\user\group\option\UserGroupOption')) {
                        // category is not accessible
-                       if (!$this->isValid($row['categoryName'])) {
+                       if (!$this->isValid($userGroupOption->categoryName)) {
                                continue;
                        }
                        
                        // option is not accessible
-                       $userGroupOption = new UserGroupOption(null, $row);
                        if (!$this->validate($userGroupOption)) {
                                continue;
                        }
                        
-                       $link = LinkHandler::getInstance()->getLink('UserGroupOption', array('id' => $row['optionID']));
-                       $results[] = new ACPSearchResult($languageItems[$row['optionName']], $link);
-                       $count++;
+                       $link = LinkHandler::getInstance()->getLink('UserGroupOption', array('id' => $userGroupOption->optionID));
+                       $results[] = new ACPSearchResult($languageItems[$userGroupOption->optionName], $link);
                }
                
                return $results;