Changeset 2222:30fe556de226 for inc/core/class.dc.store.php
- Timestamp:
- 10/03/13 00:57:41 (12 years ago)
- Branch:
- dcRepo
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.store.php
r2216 r2222 15 15 @ingroup DC_CORE 16 16 @brief Repository modules XML feed reader 17 @since 2.6 17 18 18 19 Provides an object to parse XML feed of modules from repository. … … 24 25 /** @var object dcModules instance */ 25 26 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); 26 30 27 31 /** @var string User agent used to query repository */ … … 126 130 127 131 # 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 } 130 135 131 136 # For each modules 132 137 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 } 152 168 } 153 169 # Sort response by matches count … … 217 233 218 234 /** 219 * S anitize string.235 * Split and clean pattern. 220 236 * 221 237 * @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; 227 252 } 228 253
Note: See TracChangeset
for help on using the changeset viewer.