Changeset 28153


Ignore:
Timestamp:
Apr 10, 2014, 11:26:00 PM (10 years ago)
Author:
rvelices
Message:

autocomplete allows synonims / misspellings, etc ...

Location:
extensions/rv_autocomplete
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • extensions/rv_autocomplete/admin.php

    r22988 r28153  
    1717$tabsheet->add( 'exclude', l10n('Exclude'), $my_base_url.'-exclude' );
    1818$tabsheet->add( 'custom', l10n('Custom'), $my_base_url.'-custom' );
     19$tabsheet->add( 'variants', l10n('Variants'), $my_base_url.'-variants' );
    1920$tabsheet->select($page['tab']);
    2021
  • extensions/rv_autocomplete/admin/functions.inc.php

    r27564 r28153  
    33function rvac_invalidate_cache()
    44{
    5   global $conf;
    6   conf_update_param('rvac_version', ++$conf['rvac_version'] );
    7   if (rand()%10==0)
    8   {
    9     foreach (glob(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.'acds-*.js') as $file)
    10       @unlink($file);
    11   }
     5        global $conf;
     6        conf_update_param('rvac_version', ++$conf['rvac_version'] );
     7        if (rand()%10==0)
     8        {
     9                foreach (glob(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.'acds-*.js') as $file)
     10                        @unlink($file);
     11        }
    1212}
    1313
    1414function rvac_custom_link(&$suggestion, $roots)
    1515{
    16   $url = $suggestion['url'];
    17   if (empty($url))
    18   {
    19     $q = $suggestion['name'];
    20     if ( ($pos=strpos($q,'\\')) !== false )
    21     {
    22       $q = substr($q, 0, $pos);
    23     }
    24   }
    25   elseif (strncmp($url,'q=',2)==0)
    26   {
    27     $q = substr($url,2);
    28   }
    29   else
    30   {
    31     foreach( $roots as $k => $root)
    32       $url = str_replace('$'.$k.'/', $root, $url);
    33   }
    34 
    35   if (isset($q))
    36   {
    37     if ( ($pos=strpos($q,' ')) !== false )
    38     {
    39       $q = '"'.$q.'"';
    40     }
    41     $url = get_root_url().'qsearch.php?q='.rawurlencode($q);
    42   }
    43 
    44   $suggestion['U_LINK'] = $url;
    45   return $suggestion;
     16        $url = $suggestion['url'];
     17        if (empty($url))
     18        {
     19                $q = $suggestion['name'];
     20                if ( ($pos=strpos($q,'\\')) !== false )
     21                {
     22                        $q = substr($q, 0, $pos);
     23                }
     24        }
     25        elseif (strncmp($url,'q=',2)==0)
     26        {
     27                $q = substr($url,2);
     28        }
     29        else
     30        {
     31                foreach( $roots as $k => $root)
     32                        $url = str_replace('$'.$k.'/', $root, $url);
     33        }
     34
     35        if (isset($q))
     36        {
     37                if ( ($pos=strpos($q,' ')) !== false )
     38                {
     39                        $q = '"'.$q.'"';
     40                }
     41                $url = get_root_url().'qsearch.php?q='.rawurlencode($q);
     42        }
     43
     44        $suggestion['U_LINK'] = $url;
     45        return $suggestion;
    4646}
    4747
     
    4949{
    5050        global $conf;
    51   $srv = $srv_arr[0];
    52   include_once( dirname(__FILE__).'/../functions.inc.php' );
    53   $srv->addMethod('rvac.addCustom', 'rv_ac_ws_add_custom_suggestion',
    54     array(
     51        $srv = $srv_arr[0];
     52        include_once( dirname(__FILE__).'/../functions.inc.php' );
     53        $srv->addMethod('rvac.addCustom', 'rv_ac_ws_add_custom_suggestion',
     54                array(
    5555                        'name' => array(),
    5656                        'counter' => array('default'=>0),
    5757                        'url' => array('default'=>''),
    5858                        'level' => array('default'=>min($conf['available_permission_levels']), 'maxValue'=>max($conf['available_permission_levels']),'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
    59     ),
    60     '','', array('admin_only'=>true, 'post_only'=>true));
    61 
    62   $srv->addMethod('rvac.modCustom', 'rv_ac_ws_mod_custom_suggestion',
    63     array(
    64       'id' => array(),
    65       'name' => array('flags' => WS_PARAM_OPTIONAL),
    66       'counter' => array('flags' => WS_PARAM_OPTIONAL),
    67       'url' => array('flags' => WS_PARAM_OPTIONAL),
     59                ),
     60                '','', array('admin_only'=>true, 'post_only'=>true));
     61
     62        $srv->addMethod('rvac.modCustom', 'rv_ac_ws_mod_custom_suggestion',
     63                array(
     64                        'id' => array(),
     65                        'name' => array('flags' => WS_PARAM_OPTIONAL),
     66                        'counter' => array('flags' => WS_PARAM_OPTIONAL),
     67                        'url' => array('flags' => WS_PARAM_OPTIONAL),
    6868                        'level' => array('flags' => WS_PARAM_OPTIONAL, 'maxValue'=>max($conf['available_permission_levels']),'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
    69     ),
    70     '','', array('admin_only'=>true, 'post_only'=>true));
    71 
    72   $srv->addMethod('rvac.delCustom', 'rv_ac_ws_del_custom_suggestion',
    73     array('id'),
    74     '','', array('admin_only'=>true, 'post_only'=>true));
     69                ),
     70                '','', array('admin_only'=>true, 'post_only'=>true));
     71
     72        $srv->addMethod('rvac.delCustom', 'rv_ac_ws_del_custom_suggestion',
     73                array('id'),
     74                '','', array('admin_only'=>true, 'post_only'=>true));
     75
     76
     77        $srv->addMethod('rvac.addVariant', 'rv_ac_ws_add_variant',
     78                array(
     79                        'in' => array(),
     80                        'type' => array(),
     81                        'out' => array(),
     82                        'comment' => array('flags' => WS_PARAM_OPTIONAL),
     83                ),
     84                '','', array('admin_only'=>true, 'post_only'=>true));
     85
     86        $srv->addMethod('rvac.modVariant', 'rv_ac_ws_mod_variant',
     87                array(
     88                        'key' => array(),
     89                        'in' => array(),
     90                        'type' => array(),
     91                        'out' => array(),
     92                        'comment' => array('flags' => WS_PARAM_OPTIONAL),
     93                ),
     94                '','', array('admin_only'=>true, 'post_only'=>true));
     95
     96        $srv->addMethod('rvac.delVariant', 'rv_ac_ws_del_variant',
     97                array(
     98                        'key' => array(),
     99                ),
     100                '','', array('admin_only'=>true, 'post_only'=>true));
     101
    75102}
    76103
    77104function rv_ac_ws_add_custom_suggestion($params, $service)
    78105{
    79   $name = trim($params['name']);
    80   if (empty($name))
    81     return new PwgError(400, 'Bad name');
    82 
    83   $insert = array('name'=>$name);
    84 
    85   if (isset($params['counter']))
    86     $insert['counter'] = intval($params['counter']);
    87   if ( !empty($params['url']) )
    88     $insert['url'] = $params['url'];
    89   if ( isset($params['level']) )
    90     $insert['level'] = $params['level'];
    91 
    92   mass_inserts(RVAC_SUGGESTIONS, array_keys($insert), array($insert));
    93   $id =  pwg_db_insert_id(RVAC_SUGGESTIONS);
    94 
    95   rvac_invalidate_cache();
    96   $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
    97   rvac_custom_link($row, rvac_get_url_roots());
    98   return $row;
     106        $name = trim($params['name']);
     107        if (empty($name))
     108                return new PwgError(400, 'Bad name');
     109
     110        $insert = array('name'=>$name);
     111
     112        if (isset($params['counter']))
     113                $insert['counter'] = intval($params['counter']);
     114        if ( !empty($params['url']) )
     115                $insert['url'] = $params['url'];
     116        if ( isset($params['level']) )
     117                $insert['level'] = $params['level'];
     118
     119        mass_inserts(RVAC_SUGGESTIONS, array_keys($insert), array($insert));
     120        $id =  pwg_db_insert_id(RVAC_SUGGESTIONS);
     121
     122        rvac_invalidate_cache();
     123        $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
     124        rvac_custom_link($row, rvac_get_url_roots());
     125        return $row;
    99126}
    100127
    101128function rv_ac_ws_mod_custom_suggestion($params, $service)
    102129{
    103   $id = intval($params['id']);
    104   $update = array();
    105 
    106   if (!empty($params['name']))
    107     $update['name'] = $params['name'];
    108   if (isset($params['counter']))
    109     $update['counter'] = intval($params['counter']);
    110   if (isset($params['url']))
    111     $update['url'] = $params['url'];
    112   if ( isset($params['level']) )
    113     $update['level'] = $params['level'];
    114 
    115   single_update(RVAC_SUGGESTIONS,
    116     $update,
    117     array('id' => $id)
    118     );
    119   $changes = pwg_db_changes();
    120 
    121   if ($changes)
    122     rvac_invalidate_cache();
    123 
    124   $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
    125   rvac_custom_link($row, rvac_get_url_roots());
    126   return $row;
     130        $id = intval($params['id']);
     131        $update = array();
     132
     133        if (!empty($params['name']))
     134                $update['name'] = $params['name'];
     135        if (isset($params['counter']))
     136                $update['counter'] = intval($params['counter']);
     137        if (isset($params['url']))
     138                $update['url'] = $params['url'];
     139        if ( isset($params['level']) )
     140                $update['level'] = $params['level'];
     141
     142        single_update(RVAC_SUGGESTIONS,
     143                $update,
     144                array('id' => $id)
     145                );
     146        $changes = pwg_db_changes();
     147
     148        if ($changes)
     149                rvac_invalidate_cache();
     150
     151        $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
     152        rvac_custom_link($row, rvac_get_url_roots());
     153        return $row;
    127154}
    128155
    129156function rv_ac_ws_del_custom_suggestion($params, $service)
    130157{
    131   $id = intval($params['id']);
    132   $q = 'DELETE FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id;
    133   pwg_query($q);
    134   $changes = pwg_db_changes();
    135   if ($changes)
    136     rvac_invalidate_cache();
    137   return $changes;
    138 }
    139 
     158        $id = intval($params['id']);
     159        $q = 'DELETE FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id;
     160        pwg_query($q);
     161        $changes = pwg_db_changes();
     162        if ($changes)
     163                rvac_invalidate_cache();
     164        return $changes;
     165}
     166
     167
     168
     169
     170function rvac_load_variant_rules()
     171{
     172        global $conf;
     173        $file=PHPWG_ROOT_PATH.$conf['data_location'].'plugins/autocomplete_variants.dat';
     174
     175        $data = @unserialize( file_get_contents($file));
     176        if ($data === false)
     177                return array();
     178        return $data;
     179}
     180
     181function rvac_save_variant_rules($rules, $callback = null)
     182{
     183        global $conf;
     184        $file=PHPWG_ROOT_PATH.$conf['data_location'].'plugins/autocomplete_variants.dat';
     185        $save = serialize($rules);
     186        if (@file_put_contents($file, $save)===false)
     187        {
     188                mkgetdir(dirname($file));
     189                file_put_contents($file, $save);
     190        }
     191
     192        $rmap = $amap = array();
     193        foreach($rules as $rule)
     194        {
     195                foreach($rule['in'] as $in_word)
     196                {
     197                        $in_word_t = transliterate($in_word);
     198                        $results = $rule['out'];
     199                        $processed = array();
     200                        for ($i=0; $i<count($results); $i++)
     201                        {
     202                                $outkey = $results[$i] == '$i' ? $in_word_t : transliterate($results[$i]);
     203                                if (isset($processed[$outkey]))
     204                                {
     205                                        array_splice($results, $i, 1);
     206                                        $i--;
     207                                        continue;
     208                                }
     209                                $processed[$outkey] = 1;
     210
     211                                if ('$a' == $results[$i])
     212                                {
     213                                        array_splice($results, $i, 1, $rule['in']);
     214                                        $i--;
     215                                }
     216                        }
     217
     218                        if ('r' == $rule['type'])
     219                        {
     220                                if (isset($rmap[$in_word_t]))
     221                                {
     222                                        if ($callback) $callback( array(
     223                                                'word' => $in_word,
     224                                                'wordt' => $in_word_t,
     225                                                'msg' => ' defined as input several times in replace rules'
     226                                        ));
     227                                }
     228                                else
     229                                        $rmap[$in_word_t] = $results;
     230                        }
     231                        else
     232                        {
     233                                $results = array_diff($results, array($in_word,$in_word_t, '$i'));
     234                                if (isset($amap[$in_word_t]))
     235                                        $results = array_unique( array_merge($amap[$in_word_t], $results));
     236                                $amap[$in_word_t] = $results;
     237                        }
     238                }
     239        }
     240
     241        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/autocomplete_variants.dat';
     242        $res = array('replace' => $rmap, 'add' => $amap );
     243        $save = serialize($res);
     244        if (@file_put_contents($file, $save)===false)
     245        {
     246                mkgetdir(dirname($file));
     247                file_put_contents($file, $save);
     248        }
     249        @file_put_contents($file.'.txt', var_export($res,true) );
     250}
     251
     252function rv_ac_ws_add_variant($params, $service)
     253{
     254        return rvac_ws_add_or_mod_variant($params, true);
     255}
     256
     257function rv_ac_ws_mod_variant($params, $service)
     258{
     259        return rvac_ws_add_or_mod_variant($params, false);
     260}
     261
     262function rvac_ws_add_or_mod_variant($params, $is_add)
     263{
     264        $rules = rvac_load_variant_rules();
     265
     266        foreach( array('in', 'out') as $i => $name)
     267        {
     268                $arr = preg_split("/[\n,]+/", stripslashes($params[$name]) );
     269                $arr = array_map('trim', $arr);
     270                $arr = array_values( array_filter($arr, function($word) {
     271                        return strlen($word)>0;
     272                }) );
     273                $arr = array_unique($arr);
     274                $$name = $arr;
     275        }
     276        if (empty($in))
     277                return new PwgError(WS_ERR_INVALID_PARAM, 'input word list empty');
     278        if (empty($out))
     279                return new PwgError(WS_ERR_INVALID_PARAM, 'output word list empty');
     280
     281        $in_trans = array_map('transliterate', $in);
     282        sort($in_trans);
     283        $key = implode(',', $in_trans);
     284        if (strlen($key)>16)
     285                $key = md5($key);
     286
     287        $type = $params['type'];
     288        if ($type!='r' && $type!='a')
     289                return new PwgError(WS_ERR_INVALID_PARAM, 'type');
     290
     291        if ($is_add)
     292        {
     293                if (isset($rules[$key]))
     294                        return new PwgError(WS_ERR_INVALID_PARAM, 'list already defined');
     295        }
     296        else
     297        {
     298                $okey = $params['key'];
     299                if (!isset($rules[$okey]))
     300                        return new PwgError(WS_ERR_INVALID_PARAM, 'no rule to update');
     301                unset($rules[$okey]);
     302        }
     303
     304        $rule = array(
     305                'in' => $in,
     306                'type' => $type,
     307                'out' => $out,
     308        );
     309        if (!empty($params['comment']))
     310                $rule['comment'] = $params['comment'];
     311        $rules[$key] = $rule;
     312
     313        $messages = array();
     314        $callback = function($params) use($in_trans, &$messages) {
     315                if (isset($params['word_t']))
     316                {
     317                        if (in_array($in_trans, $word_t))
     318                                $messages[] = $params['word'].' '.$params['msg'];
     319                }
     320                else
     321                        $messages[] = $params['msg'];
     322        };
     323
     324        rvac_save_variant_rules($rules, $callback);
     325        $rule['key'] = $key;
     326        return array(
     327                'messages' => $messages,
     328                'rule' => $rule,
     329        );
     330}
     331
     332function rv_ac_ws_del_variant($params, $service)
     333{
     334        $rules = rvac_load_variant_rules();
     335
     336        $key = $params['key'];
     337        if (empty($key))
     338                return new PwgError(WS_ERR_INVALID_PARAM, 'empty key');
     339
     340        if (!isset($rules[$key]))
     341                return true;
     342
     343        unset($rules[$key]);
     344
     345        rvac_save_variant_rules($rules);
     346}
    140347?>
  • extensions/rv_autocomplete/functions.inc.php

    r28088 r28153  
    33function rvac_get_conf()
    44{
    5   global $conf;
    6   if (is_array($conf['rvac_opts']))
    7     return $conf['rvac_opts'];
    8   $conf['rvac_opts'] = unserialize($conf['rvac_opts']);
    9   return $conf['rvac_opts'];
     5        global $conf;
     6        if (is_array($conf['rvac_opts']))
     7                return $conf['rvac_opts'];
     8        $conf['rvac_opts'] = unserialize($conf['rvac_opts']);
     9        return $conf['rvac_opts'];
    1010}
    1111
    1212function rvac_save_conf($cnf)
    1313{
    14   global $conf;
    15   $conf['rvac_opts'] = $cnf;
    16   conf_update_param('rvac_opts', addslashes(serialize($conf['rvac_opts'])) );
     14        global $conf;
     15        $conf['rvac_opts'] = $cnf;
     16        conf_update_param('rvac_opts', addslashes(serialize($conf['rvac_opts'])) );
    1717}
    1818
    1919function rvac_get_url_roots()
    2020{
    21   $roots = array(
    22     'r' => get_root_url()
    23   );
    24 
    25   $url = make_index_url( array('tags'=>array( array('id'=>123,'name'=>123,'url_name'=>123))) );
    26   $url = substr($url, 0, strpos($url, '123'));
    27   $roots['t'] = $url;
    28 
    29   $url = make_index_url( array('category'=>array('id'=>123,'name'=>123,'permalink'=>123)) );
    30   $url = substr($url, 0, strpos($url, '123'));
    31   $roots['a'] = $url;
    32  
    33   return $roots;
     21        $roots = array(
     22                'r' => get_root_url()
     23        );
     24
     25        $url = make_index_url( array('tags'=>array( array('id'=>123,'name'=>123,'url_name'=>123))) );
     26        $url = substr($url, 0, strpos($url, '123'));
     27        $roots['t'] = $url;
     28
     29        $url = make_index_url( array('category'=>array('id'=>123,'name'=>123,'permalink'=>123)) );
     30        $url = substr($url, 0, strpos($url, '123'));
     31        $roots['a'] = $url;
     32
     33        return $roots;
    3434}
    3535
     
    3737function rvac_normalize($name)
    3838{
    39   $invalid = '&().,-';
    40   $name = strtr($name, $invalid, str_repeat(' ', strlen($invalid)));
    41   $name = transliterate($name);
    42   for($i=0; $i<3; $i++)
    43     $name = str_replace('  ', ' ', $name);
    44   return $name;
     39        $invalid = '&().,-';
     40        $name = strtr($name, $invalid, str_repeat(' ', strlen($invalid)));
     41        $name = transliterate($name);
     42        for($i=0; $i<3; $i++)
     43                $name = str_replace('  ', ' ', $name);
     44        return $name;
    4545}
    4646
    4747function rvac_get_elem($item, $key)
    4848{
    49   $l = $item['names'][$key];
    50   $q = $item['q_names'][$key];
    51   $ret = array(
    52     'label' => $l,
    53     'value' => $item['url'],
    54     );
    55   if ($q != strtolower($l))
    56     $ret['q'] = $q;
     49        $l = $item['names'][$key];
     50        $q = $item['q_names'][$key];
     51        $ret = array(
     52                'label' => $l,
     53                'value' => $item['url'],
     54                );
     55        if ($q != strtolower($l))
     56                $ret['q'] = $q;
    5757        if ($item['counter'] > 0)
    5858                $ret['w'] = intval($item['counter']);
    59   return $ret;
     59        return $ret;
    6060}
    6161
    6262function rvac_get_index()
    6363{
    64   global $user;
    65 
    66   $rvac_conf = rvac_get_conf();
    67   $root_url = get_root_url();
    68   $items = array();
    69 
    70   $roots = rvac_get_url_roots();
    71 
    72 
    73   $query = 'SELECT id,name FROM '.TAGS_TABLE;
    74   if ( !empty($rvac_conf['excluded_tags']) )
    75     $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_tags']).')';
    76   $tag_names = query2array($query, 'id','name');
    77 
    78   $url_len = strlen( $roots['t'] );
    79   foreach( get_available_tags() as $row)
    80   {
    81     if (!isset($tag_names[$row['id']])) continue;
    82     $row['type'] = 't';
    83     $row['url'] = '$t/' . substr( make_index_url( array('tags'=>array($row)) ), $url_len);
    84     $row['name'] = $tag_names[ $row['id'] ];
    85     $items[] = $row;
    86   }
    87 
    88 
    89   $query = 'SELECT id,name,permalink,nb_images AS counter
    90   FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
    91   ON id = cat_id AND user_id = '.$user['id'];
    92   if ( !empty($rvac_conf['excluded_albums']) )
    93     $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_albums']).')';
    94 
    95   $url_len = strlen( $roots['a'] );
    96 
    97   $result = pwg_query($query);
    98   while ($row = pwg_db_fetch_assoc($result))
    99   {
    100     $row['type'] = 'a';
    101     $row['url'] = '$a/' . substr( make_index_url( array('category'=>$row) ), $url_len);
    102     $items[] = $row;
    103   }
    104 
    105   $query = 'SELECT name,counter,url FROM '.RVAC_SUGGESTIONS.' WHERE level<='.$user['level'];
    106   $result = pwg_query($query);
    107   while ($row = pwg_db_fetch_assoc($result))
    108   {
    109     $row['type'] = 's';
    110     if (empty($row['url']))
    111       $row['url'] = -1; // special use in js to submit form (cannot put 0 because jquery ui tests often item.value)
    112     $items[] = $row;
    113   }
    114 
    115   array_walk($items, function(&$obj) {
    116     $alt_names = trigger_event('get_tag_alt_names', array(), $obj['name']);
    117 
    118     if (empty($alt_names))
    119       $alt_names['default'] = $obj['name'];
    120     else
    121     {
    122       if (!isset($alt_names['default']) && isset($alt_names[0]))
    123       {
    124         $alt_names['default'] = $alt_names[0];
    125         unset($alt_names[0]);
    126       }
    127     }
    128 
    129     $obj['names'] = $alt_names;
    130     if ('s' !== $obj['type'])
    131     {
    132       $obj['q_names'] = $alt_names;
    133       foreach ($obj['q_names'] as &$q)
    134       {
    135         $q = rvac_normalize($q);
    136       }
    137     }
    138     else
    139     {
    140       foreach ($obj['names'] as $l => &$name)
    141       {
    142         $pos = strpos($name, '\\');
    143         if ($pos === false)
    144         {
    145           $q = rvac_normalize($name);
    146         }
    147         else
    148         {
    149           $q = substr($name, 0, $pos);
    150           if (-1 === $obj['url'] && 'default'==$l)
    151           {
    152             $obj['url'] = 'q='.$q;
    153           }
    154           $q = rvac_normalize($q);
    155           $name = substr_replace($name, '', $pos, 1);
    156         }
    157         $obj['q_names'][$l] = $q;
    158       }
    159     }
    160 
    161   });
    162 
    163   $type_order = array_flip( array('t','a','s') );
    164   usort($items, function($a, $b) use($type_order) {
    165     $d = $a['counter']-$b['counter'];
    166     if ($d) return -$d;
    167     $d = $type_order[$a['type']] - $type_order[$b['type']];
    168     if ($d) return $d;
    169     return strcmp($a['q_names']['default'],$b['q_names']['default']);
    170   });
    171 
    172   //var_export($items);
    173   $res = array();
    174   $res_alt = array();
    175   $lang = substr($user['language'],0,2);
    176   array_walk($items, function(&$obj) use($lang, &$res, &$res_alt) {
    177     $key = $lang;
    178     if (empty($obj['q_names'][$key]))
    179       $key = 'default';
    180     if (!empty($obj['q_names'][$key]))
    181       $res[] = rvac_get_elem($obj, $key);
    182     else
    183       $key = null;
    184     if ($key !== null) {
    185       foreach( array_keys($obj['q_names']) as $k) {
    186         if ($k==$key) continue;
    187         if (strncmp($obj['q_names'][$key],$obj['q_names'][$k],4)==0) continue;
    188         $res_alt[] = rvac_get_elem($obj, $k);
    189       }
    190     }
    191     else {
    192       foreach( $obj['q_names'] as $k => $q)
    193       {
    194         if (!empty($q))
    195           $res_alt[] = rvac_get_elem($obj, $k);
    196       }
    197     }
    198   });
    199 
    200   return array(
    201       'total' => count($res) + count($res_alt),
    202       'altLangIndex' => count($res),
    203       'src' => array_merge($res, $res_alt),
    204       'roots' => $roots
    205     );
     64        global $user;
     65
     66        $rvac_conf = rvac_get_conf();
     67        $root_url = get_root_url();
     68        $items = array();
     69
     70        $roots = rvac_get_url_roots();
     71
     72
     73        $query = 'SELECT id,name FROM '.TAGS_TABLE;
     74        if ( !empty($rvac_conf['excluded_tags']) )
     75                $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_tags']).')';
     76        $tag_names = query2array($query, 'id','name');
     77
     78        $url_len = strlen( $roots['t'] );
     79        foreach( get_available_tags() as $row)
     80        {
     81                if (!isset($tag_names[$row['id']])) continue;
     82                $row['type'] = 't';
     83                $row['url'] = '$t/' . substr( make_index_url( array('tags'=>array($row)) ), $url_len);
     84                $row['name'] = $tag_names[ $row['id'] ];
     85                $items[] = $row;
     86        }
     87
     88
     89        $query = 'SELECT id,name,permalink,nb_images AS counter
     90        FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
     91        ON id = cat_id AND user_id = '.$user['id'];
     92        if ( !empty($rvac_conf['excluded_albums']) )
     93                $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_albums']).')';
     94
     95        $url_len = strlen( $roots['a'] );
     96
     97        $result = pwg_query($query);
     98        while ($row = pwg_db_fetch_assoc($result))
     99        {
     100                $row['type'] = 'a';
     101                $row['url'] = '$a/' . substr( make_index_url( array('category'=>$row) ), $url_len);
     102                $items[] = $row;
     103        }
     104
     105        $query = 'SELECT name,counter,url FROM '.RVAC_SUGGESTIONS.' WHERE level<='.$user['level'];
     106        $result = pwg_query($query);
     107        while ($row = pwg_db_fetch_assoc($result))
     108        {
     109                $row['type'] = 's';
     110                if (empty($row['url']))
     111                        $row['url'] = -1; // special use in js to submit form (cannot put 0 because jquery ui tests often item.value)
     112                $items[] = $row;
     113        }
     114
     115        array_walk($items, function(&$obj) {
     116                $alt_names = trigger_event('get_tag_alt_names', array(), $obj['name']);
     117
     118                if (empty($alt_names))
     119                        $alt_names['default'] = $obj['name'];
     120                else
     121                {
     122                        if (!isset($alt_names['default']) && isset($alt_names[0]))
     123                        {
     124                                $alt_names['default'] = $alt_names[0];
     125                                unset($alt_names[0]);
     126                        }
     127                }
     128
     129                $obj['names'] = $alt_names;
     130                if ('s' !== $obj['type'])
     131                {
     132                        $obj['q_names'] = $alt_names;
     133                        foreach ($obj['q_names'] as &$q)
     134                        {
     135                                $q = rvac_normalize($q);
     136                        }
     137                }
     138                else
     139                {
     140                        foreach ($obj['names'] as $l => &$name)
     141                        {
     142                                $pos = strpos($name, '\\');
     143                                if ($pos === false)
     144                                {
     145                                        $q = rvac_normalize($name);
     146                                }
     147                                else
     148                                {
     149                                        $q = substr($name, 0, $pos);
     150                                        if (-1 === $obj['url'] && 'default'==$l)
     151                                        {
     152                                                $obj['url'] = 'q='.$q;
     153                                        }
     154                                        $q = rvac_normalize($q);
     155                                        $name = substr_replace($name, '', $pos, 1);
     156                                }
     157                                $obj['q_names'][$l] = $q;
     158                        }
     159                }
     160
     161        });
     162
     163        $type_order = array_flip( array('t','a','s') );
     164        usort($items, function($a, $b) use($type_order) {
     165                $d = $a['counter']-$b['counter'];
     166                if ($d) return -$d;
     167                $d = $type_order[$a['type']] - $type_order[$b['type']];
     168                if ($d) return $d;
     169                return strcmp($a['q_names']['default'],$b['q_names']['default']);
     170        });
     171
     172        //var_export($items);
     173        $res = array();
     174        $res_alt = array();
     175        $lang = substr($user['language'],0,2);
     176        array_walk($items, function(&$obj) use($lang, &$res, &$res_alt) {
     177                $key = $lang;
     178                if (empty($obj['q_names'][$key]))
     179                        $key = 'default';
     180                if (!empty($obj['q_names'][$key]))
     181                        $res[] = rvac_get_elem($obj, $key);
     182                else
     183                        $key = null;
     184                if ($key !== null) {
     185                        foreach( array_keys($obj['q_names']) as $k) {
     186                                if ($k==$key) continue;
     187                                if (strncmp($obj['q_names'][$key],$obj['q_names'][$k],4)==0) continue;
     188                                $res_alt[] = rvac_get_elem($obj, $k);
     189                        }
     190                }
     191                else {
     192                        foreach( $obj['q_names'] as $k => $q)
     193                        {
     194                                if (!empty($q))
     195                                        $res_alt[] = rvac_get_elem($obj, $k);
     196                        }
     197                }
     198        });
     199
     200        return array(
     201                        'total' => count($res) + count($res_alt),
     202                        'altLangIndex' => count($res),
     203                        'src' => array_merge($res, $res_alt),
     204                        'roots' => $roots
     205                );
     206}
     207
     208function rvac_on_qsearch_expression_parsed($expr)
     209{
     210        global $conf;
     211        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/autocomplete_variants.dat';
     212        $data = @unserialize( file_get_contents($file));
     213        if ($data === false)
     214                return;
     215
     216        $rmap = $data['replace'];
     217        $amap = $data['add'];
     218        foreach ($expr->stokens as $token)
     219        {
     220                if (isset($token->scope) && !$token->scope->is_text)
     221                        continue;
     222                if ($token->modifier & (QST_QUOTED|QST_WILDCARD))
     223                        continue;
     224
     225                $all = array_merge( array($token->term), $token->variants);
     226
     227                $in = $all[0];
     228                $in_t = transliterate($in);
     229                if (isset($rmap[$in_t]))
     230                {
     231                        $all = $rmap[$in_t];
     232                        foreach ($all as &$one)
     233                        {
     234                                if ('$i' == $one)
     235                                        $one = $in;
     236                        }
     237                        unset($one);
     238                }
     239
     240                $in_list = $processed = array();
     241                for ($i=0; $i<count($all); $i++)
     242                {
     243                        $in = $all[$i];
     244                        $in_t = transliterate($in);
     245
     246                        if (isset($in_list[$in_t]))
     247                        {
     248                                array_splice($all, $i, 1);
     249                                $i--;
     250                                continue;
     251                        }
     252
     253                        if (isset($processed[$in_t]))
     254                                continue;
     255                        $processed[$in_t] = 1;
     256
     257                        if (isset($amap[$in_t]))
     258                        {
     259                                array_splice($all, $i, 0, $amap[$in_t]);
     260                        }
     261                }
     262
     263                $token->term = array_shift($all);
     264                $token->variants = $all;
     265        }
    206266}
    207267?>
  • extensions/rv_autocomplete/main.inc.php

    r27771 r28153  
    11<?php /*
    22Plugin Name: RV autocomplete
    3 Version: 2.6.b
     3Version: 2.7.a
    44Description: Autocompletes the quick search with albums, tags or custom suggestions
    55Plugin URI: http://piwigo.org/ext/extension_view.php?eid=694
     
    7171}
    7272);
     73
     74add_event_handler('qsearch_expression_parsed', 'rvac_on_qsearch_expression_parsed', EVENT_HANDLER_PRIORITY_NEUTRAL, dirname(__FILE__).'/functions.inc.php');
    7375?>
Note: See TracChangeset for help on using the changeset viewer.