Dotclear


Ignore:
Timestamp:
10/03/13 00:57:41 (12 years ago)
Author:
Denis Jean-Chirstian <contact@…>
Branch:
dcRepo
Message:

Enhance repository search engine (and some fix)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.store.php

    r2216 r2222  
    1515@ingroup DC_CORE 
    1616@brief Repository modules XML feed reader 
     17@since 2.6 
    1718 
    1819Provides an object to parse XML feed of modules from repository. 
     
    2425     /** @var  object    dcModules instance */ 
    2526     public $modules; 
     27 
     28     /** @var  array     Modules fields to search on and their weighting */ 
     29     public static $weighting = array('id' => 10, 'name' => 8, 'author' => 6, 'tags' => 4, 'desc' => 2); 
    2630 
    2731     /** @var  string    User agent used to query repository */ 
     
    126130 
    127131          # Split query into small clean words 
    128           $patterns = explode(' ', $pattern); 
    129           array_walk($patterns, array('dcStore','sanitize')); 
     132          if (!($patterns = self::patternize($pattern))) { 
     133               return $result; 
     134          } 
    130135 
    131136          # For each modules 
    132137          foreach ($this->data['new'] as $id => $module) { 
    133  
    134                # Split modules infos into small clean word 
    135                $subjects = explode(' ', $id.' '.$module['name'].' '.$module['desc']); 
    136                array_walk($subjects, array('dcStore','sanitize')); 
    137  
    138                # Check contents 
    139                if (!($nb = preg_match_all('/('.implode('|', $patterns).')/', implode(' ', $subjects), $_))) { 
    140                     continue; 
    141                } 
    142  
    143                # Add module to result 
    144                if (!isset($sorter[$id])) { 
    145                     $sorter[$id] = 0; 
    146                     $result[$id] = $module; 
    147                } 
    148  
    149                # Increment matches count 
    150                $sorter[$id] += $nb; 
    151                $result[$id]['accuracy'] = $sorter[$id]; 
     138               $module['id'] = $id; 
     139 
     140               # Loop through required module fields 
     141               foreach(self::$weighting as $field => $weight) { 
     142 
     143                    # Skip fields which not exsist on module 
     144                    if (empty($module[$field])) { 
     145                         continue; 
     146                    } 
     147 
     148                    # Split field value into small clean word 
     149                    if (!($subjects = self::patternize($module[$field]))) { 
     150                         continue; 
     151                    } 
     152 
     153                    # Check contents 
     154                    if (!($nb = preg_match_all('/('.implode('|', $patterns).')/', implode(' ', $subjects), $_))) { 
     155                         continue; 
     156                    } 
     157 
     158                    # Add module to result 
     159                    if (!isset($sorter[$id])) { 
     160                         $sorter[$id] = 0; 
     161                         $result[$id] = $module; 
     162                    } 
     163 
     164                    # Increment score by matches count * field weight 
     165                    $sorter[$id] += $nb * $weight; 
     166                    $result[$id]['score'] = $sorter[$id]; 
     167               } 
    152168          } 
    153169          # Sort response by matches count 
     
    217233 
    218234     /** 
    219       * Sanitize string. 
     235      * Split and clean pattern. 
    220236      * 
    221237      * @param string    $str      String to sanitize 
    222       * @param null $_        Unused    param 
    223       */ 
    224      public static function sanitize(&$str, $_) 
    225      { 
    226           $str = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $str)); 
     238      * @return     array     Array of cleaned pieces of string or false if none 
     239      */ 
     240     public static function patternize($str) 
     241     { 
     242          $arr = array(); 
     243 
     244          foreach(explode(' ', $str) as $_) { 
     245               $_ = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $_)); 
     246               if (strlen($_) > 2) { 
     247                    $arr[] = $_; 
     248               } 
     249          } 
     250 
     251          return empty($arr) ? false : $arr; 
    227252     } 
    228253 
Note: See TracChangeset for help on using the changeset viewer.

Sites map