Changeset 1613

Show
Ignore:
Timestamp:
10/24/06 16:45:25 (2 years ago)
Author:
mbonetti
Message:

Initial work to implement OpenSearch? capabilities in Gregarius.
See:
http://developer.mozilla.org/en/docs/Supporting_search_suggestions_in_search_plugins

Location:
trunk/gregarius
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/gregarius/api.php

    r1544 r1613  
    4949        blGetItems($cid,$date,$markread); 
    5050        break; 
    51     } 
     51    case 'search': 
     52        rss_require('extlib/JSON.php'); 
     53        $json = new Services_JSON(); 
     54        $query = sanitize(@$_REQUEST['q'], RSS_SANITIZER_WORDS); 
     55        if ($query) { 
     56            $res = osSearch($query); 
     57        } else { 
     58            $res = array($query,array()); 
     59        } 
     60        header('Content-Type: application/json'); 
     61        die ($json->encode($res)); 
     62        break; 
     63    } 
     64 
    5265} 
    5366 
     
    195208 
    196209} 
     210 
     211function osSearch($q) { 
     212    rss_require('cls/search.php'); 
     213    $sil = new SearchItemList($q,5); 
     214    $results = array(); 
     215    foreach($sil -> feeds as $feed) { 
     216        foreach($feed -> items as $item) { 
     217            $results[] = $item -> title; 
     218        } 
     219    } 
     220    return array($q,$results); 
     221} 
    197222?> 
  • trunk/gregarius/cls/search.php

    r1561 r1613  
    4343define ('QUERY_MATCH_EXACT','exact'); 
    4444 
     45// This is needed for some constants 
     46rss_require('cls/wrappers/toolkit.php'); 
    4547 
    4648class SearchItemList extends ItemList { 
     
    5153 
    5254    var $currentPage; 
    53     var $resultsPerPage; 
     55    var $resultsPerPage = 0; 
    5456    var $startItem; 
    5557    var $endItem; 
     
    5860    var $logicSep; 
    5961 
    60     function SearchItemList() { 
     62    function SearchItemList($query=null,$results=0) { 
    6163        parent::ItemList(); 
     64        if ($query) { 
     65            $this -> query=$query; 
     66        } elseif(isset($_REQUEST[QUERY_PRM])) { 
     67            $this->query = $_REQUEST[QUERY_PRM]; 
     68        }else{ 
     69            $this -> query = null; 
     70        } 
     71         
     72         
     73        // Sanitize the query parameters: 
     74        // fixme: this probably breaks on queries with weird characters, depending 
     75        // on the locale.  
     76        // see: http://php.benscom.com/manual/en/reference.pcre.pattern.syntax.php 
     77        if ($this -> query) { 
     78            $this -> query = trim(preg_replace('#[^\w\s\x80-\xff]#','',$this -> query)); 
     79        } 
     80         
     81        $this->resultsPerPage = (int) $results; 
     82         
    6283        $this -> populate(); 
    6384 
     
    101122 
    102123    function populate() { 
    103         if (!isset($_REQUEST[QUERY_PRM])) { 
    104             return; 
    105         } 
    106  
    107                 // fixme: this probably breaks on queries with weird characters, depending 
    108                 // on the locale.  
    109                 // see: http://php.benscom.com/manual/en/reference.pcre.pattern.syntax.php 
    110         $this->query = trim(preg_replace('#[^\w\s\x80-\xff]#','',$_REQUEST[QUERY_PRM])); 
    111124         
    112125        if (!$this->query) { 
     
    122135            RSS_SANITIZER_NUMERIC); 
    123136 
    124         $this->resultsPerPage = sanitize( 
    125             ((array_key_exists(QUERY_RESULTS, $_REQUEST)) ? $_REQUEST[QUERY_RESULTS] : INFINE_RESULTS), 
    126             RSS_SANITIZER_NUMERIC); 
    127          
     137        if (!$this->resultsPerPage) { 
     138            $this->resultsPerPage = sanitize( 
     139                ((array_key_exists(QUERY_RESULTS, $_REQUEST)) ? $_REQUEST[QUERY_RESULTS] : INFINE_RESULTS), 
     140                RSS_SANITIZER_NUMERIC); 
     141        } 
     142 
    128143        $this->currentPage = sanitize( 
    129144            (array_key_exists(QUERY_CURRENT_PAGE, $_REQUEST) ? $_REQUEST[QUERY_CURRENT_PAGE] : 0),