Dotclear

Changeset 2222:30fe556de226 for inc


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)

Location:
inc
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/lib.moduleslist.php

    r2221 r2222  
    269269                    'tags'                   => '', 
    270270                    'details'                => '', 
    271                     'sshot'             => '' 
     271                    'sshot'             => '', 
     272                    'score'                  => 0 
    272273               ), 
    273274               # Module's values 
     
    314315          } 
    315316 
     317          if (in_array('score', $cols) && $this->getSearchQuery() !== null && defined('DC_DEBUG') && DC_DEBUG) { 
     318               echo  
     319               '<th class="nowrap">'.__('Score').'</th>'; 
     320          } 
     321 
    316322          if (in_array('version', $cols)) { 
    317323               echo  
     
    381387                    html::escapeHTML($module['name']) 
    382388               ).'</td>'; 
     389 
     390               # Display score only for debug purpose 
     391               if (in_array('score', $cols) && $this->getSearchQuery() !== null && defined('DC_DEBUG') && DC_DEBUG) { 
     392                    echo  
     393                    '<td class="module-version nowrap count"><span class="debug">'.$module['score'].'</span></td>'; 
     394               } 
    383395 
    384396               if (in_array('version', $cols)) { 
     
    863875               } 
    864876 
     877               # Display score only for debug purpose 
     878               if (in_array('score', $cols) && $this->getSearchQuery() !== null && defined('DC_DEBUG') && DC_DEBUG) { 
     879                    $line .=  
     880                    '<p class="module-score debug">'.sprintf(__('Score: %s'), $module['score']).'</p>'; 
     881               } 
     882 
    865883               if (in_array('sshot', $cols)) { 
    866884                    # Screenshot from url 
     
    901919               } 
    902920 
     921               if (in_array('current_version', $cols)) { 
     922                    $line .=  
     923                    '<span class="module-current-version">'.sprintf(__('(current version %s)'),html::escapeHTML($module['current_version'])).'</span> '; 
     924               } 
     925 
    903926               if (in_array('parent', $cols) && $has_parent) { 
    904927                    if ($is_parent_present) { 
     
    910933                         '<span class="module-parent-missing">'.sprintf(__('(requires "%s")'),html::escapeHTML($parent)).'</span> '; 
    911934                    } 
    912                } 
    913  
    914                if (in_array('version', $cols)) { 
    915                     $line .=  
    916                     '<span class="module-version">'.sprintf(__('version %s'),html::escapeHTML($module['version'])).'</span> '; 
    917935               } 
    918936 
  • inc/core/class.dc.store.parser.php

    r2216 r2222  
    1515@ingroup DC_CORE 
    1616@brief Repository modules XML feed parser 
     17@since 2.6 
    1718 
    1819Provides an object to parse XML feed of modules from a repository. 
  • 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 
  • inc/core/class.dc.store.reader.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. 
Note: See TracChangeset for help on using the changeset viewer.

Sites map