Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature 519: quick search (first version)
git-svn-id: http://piwigo.org/svn/trunk@1537 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
rvelices committed Aug 15, 2006
1 parent bc1f531 commit 48c6d7e
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 42 deletions.
12 changes: 10 additions & 2 deletions include/config_default.inc.php
Expand Up @@ -206,7 +206,7 @@
$conf['send_bcc_mail_webmaster'] = false;

// enabled_format_email:
// on true email will be formatted with name and address
// on true email will be formatted with name and address
// on false email will be only address
// There are webhosting wich not allow email formatted (Lycos, ...)
$conf['enabled_format_email'] = true;
Expand Down Expand Up @@ -473,6 +473,14 @@
// cloud. Only the most represented tags will be shown
$conf['full_tag_cloud_items_number'] = 200;

// menubar_tag_cloud_items_number: number of tags to show in the tag
// cloud in the menubar. Only the most represented tags will be shown
$conf['menubar_tag_cloud_items_number'] = 100;

// content_tag_cloud_items_number: number of tags to show in the tag
// cloud on the content page. Only the most represented tags will be shown
$conf['content_tag_cloud_items_number'] = 12;

// tags_levels: number of levels to use for display. Each level is bind to a
// CSS class tagLevelX.
$conf['tags_levels'] = 5;
Expand Down Expand Up @@ -505,7 +513,7 @@
// Step 1, default_admin_layout is not defined
// null value, user_layout is used for admin layout
// defined value, this value are used for admin layout
// Next on step 2, default_admin_layout will be used
// Next on step 2, default_admin_layout will be used
// if there are not checked like admin layout
// stored on user informations
//$conf['default_admin_layout']='yoga/dark';
Expand Down
194 changes: 183 additions & 11 deletions include/functions_search.inc.php
Expand Up @@ -73,13 +73,11 @@ function get_search_array($search_id)
* Search rules are stored in search table as a serialized array. This array
* need to be transformed into an SQL clause to be used in queries.
*
* @param int search_id
* @param array search
* @return string
*/
function get_sql_search_clause($search_id)
function get_sql_search_clause($search)
{
$search = get_search_array($search_id);

// SQL where clauses are stored in $clauses array during query
// construction
$clauses = array();
Expand Down Expand Up @@ -212,17 +210,17 @@ function get_sql_search_clause($search_id)
}

/**
* returns the list of items corresponding to the search id
* returns the list of items corresponding to the advanced search array
*
* @param int search id
* @param array search
* @return array
*/
function get_search_items($search_id)
function get_regular_search_results($search)
{
$items = array();
$search_clause = get_sql_search_clause($search_id);

$search_clause = get_sql_search_clause($search);

if (!empty($search_clause))
{
$query = '
Expand Down Expand Up @@ -269,7 +267,181 @@ function get_search_items($search_id)
}
}
}

return $items;
}


if (!function_exists('array_intersect_key')) {
function array_intersect_key()
{
$arrs = func_get_args();
$result = array_shift($arrs);
foreach ($arrs as $array) {
foreach ($result as $key => $v) {
if (!array_key_exists($key, $array)) {
unset($result[$key]);
}
}
}
return $result;
}
}


function get_qsearch_like_clause($q, $field)
{
$tokens = preg_split('/[\s,.;!\?]+/', $q);
for ($i=0; $i<count($tokens); $i++)
{
$tokens[$i]=str_replace('*','%', $tokens[$i]);
if (preg_match('/^[+<>]/',$tokens[$i]) )
$tokens[$i]=substr($tokens[$i], 1);
else if (substr($tokens[$i], 0, 1)=='-')
{
unset($tokens[$i]);
$i--;
}
}

if (!empty($tokens))
{
$query = '(';
for ($i=0; $i<count($tokens); $i++)
{
if ($i>0) $query .= 'OR ';
$query .= ' '.$field.' LIKE "%'.$tokens[$i].'%" ';
}
$query .= ')';
return $query;
}
return null;
}


/**
* returns the search results corresponding to a quick search
*
* @param string q
* @return array
*/
function get_quick_search_results($q)
{
global $user, $page;
$search_results = array();

$q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' );
$by_tag_weights=array();
if (!empty($q_like_clause))
{
$query = '
SELECT id
FROM '.TAGS_TABLE.'
WHERE '.$q_like_clause;
$tag_ids = array_from_query( $query, 'id');
if (!empty($tag_ids))
{
$query = '
SELECT image_id, COUNT(tag_id) AS q
FROM '.IMAGE_TAG_TABLE.'
WHERE tag_id IN ('.implode(',',$tag_ids).')
GROUP BY image_id';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
$by_tag_weights[(int)$row['image_id']] = $row['q'];
}
}
}

$query = '
SELECT
i.id, i.file, CAST( CONCAT_WS(" ",
IFNULL(i.name,""),
IFNULL(i.comment,""),
IFNULL(GROUP_CONCAT(DISTINCT co.content),""),
IFNULL(GROUP_CONCAT(DISTINCT c.dir),""),
IFNULL(GROUP_CONCAT(DISTINCT c.name),""),
IFNULL(GROUP_CONCAT(DISTINCT c.comment),"") ) AS CHAR) AS ft
FROM (
(
'.IMAGES_TABLE.' i LEFT JOIN '.COMMENTS_TABLE.' co on i.id=co.image_id
)
INNER JOIN
'.IMAGE_CATEGORY_TABLE.' ic on ic.image_id=i.id
)
INNER JOIN
'.CATEGORIES_TABLE.' c on c.id=ic.category_id
WHERE category_id NOT IN ('.$user['forbidden_categories'].')
GROUP BY i.id';

$query = 'SELECT id, MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE) AS q FROM ('.$query.') AS Y
WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)';

$q_like_clause = get_qsearch_like_clause($q, 'file' );
if (! empty($q_like_clause) )
{
$query .= ' OR '.$q_like_clause;
}

$by_weights=array();
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
$by_weights[(int)$row['id']] = $row['q'] ? $row['q'] : 0;
}

foreach ( $by_weights as $image=>$w )
{
$by_tag_weights[$image] = 2*$w+ (isset($by_tag_weights[$image])?$by_tag_weights[$image]:0);
}

if ( empty($by_tag_weights) or isset($page['super_order_by']) )
{
if (! isset($page['super_order_by']) )
{
arsort($by_tag_weights, SORT_NUMERIC);
$search_results['as_is']=1;
}
$search_results['items'] = array_keys($by_tag_weights);
}
else
{
$query = '
SELECT DISTINCT(id)
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
WHERE id IN ('.implode(',', array_keys($by_tag_weights) ).')
AND category_id NOT IN ('.$user['forbidden_categories'].')';

$allowed_image_ids = array_from_query( $query, 'id');
$by_tag_weights = array_intersect_key($by_tag_weights, array_flip($allowed_image_ids));
arsort($by_tag_weights, SORT_NUMERIC);
$search_results = array(
'items'=>array_keys($by_tag_weights),
'as_is'=>1
);
}
return $search_results;
}

/**
* returns an array of 'items' corresponding to the search id
*
* @param int search id
* @return array
*/
function get_search_results($search_id)
{
$search = get_search_array($search_id);
if ( !isset($search['q']) )
{
$result['items'] = get_regular_search_results($search);
return $result;
}
else
{
return get_quick_search_results($search['q']);
}
}
?>
43 changes: 43 additions & 0 deletions include/functions_tag.inc.php
Expand Up @@ -221,4 +221,47 @@ function get_image_ids_for_tags($tag_ids, $mode = 'AND')
}
}
}

/**
* return a list of tags corresponding to given items.
*
* @param array items
* @param array max_tags
* @param array excluded_tag_ids
* @return array
*/
function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
{
if (empty($items))
{
return array();
}
$query = '
SELECT tag_id, name, url_name, count(*) counter
FROM '.IMAGE_TAG_TABLE.'
INNER JOIN '.TAGS_TABLE.' ON tag_id = id
WHERE image_id IN ('.implode(',', $items).')';
if (!empty($excluded_tag_ids))
{
$query.='
AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
}
$query .='
GROUP BY tag_id
ORDER BY counter DESC';
if ($max_tags>0)
{
$query .= '
LIMIT 0,'.$max_tags;
}

$result = pwg_query($query);
$tags = array();
while($row = mysql_fetch_array($result))
{
array_push($tags, $row);
}
usort($tags, 'name_compare');
return $tags;
}
?>
17 changes: 3 additions & 14 deletions include/menubar.inc.php
Expand Up @@ -76,20 +76,8 @@

if ( !empty($page['items']) )
{
$query = '
SELECT tag_id, name, url_name, count(*) counter
FROM '.IMAGE_TAG_TABLE.'
INNER JOIN '.TAGS_TABLE.' ON tag_id = id
WHERE image_id IN ('.implode(',', $items).')
AND tag_id NOT IN ('.implode(',', $page['tag_ids']).')
GROUP BY tag_id
ORDER BY name ASC
;';
$result = pwg_query($query);
while($row = mysql_fetch_array($result))
{
array_push($tags, $row);
}
$tags = get_common_tags($page['items'],
$conf['menubar_tag_cloud_items_number'], $page['tag_ids']);
}

$tags = add_level_to_tags($tags);
Expand Down Expand Up @@ -269,6 +257,7 @@
'REL'=> 'rel="search"'
)
);
$template->assign_block_vars( 'summary.quick_search', array() );

// comments link
$template->assign_block_vars(
Expand Down
8 changes: 4 additions & 4 deletions include/section_init.inc.php
Expand Up @@ -450,22 +450,22 @@
{
include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );

$search_items = get_search_items($page['search']);
if ( !empty($search_items) )
$search_result = get_search_results($page['search']);
if ( !empty($search_result['items']) and !isset($search_result['as_is']) )
{
$query = '
SELECT DISTINCT(id)
FROM '.IMAGES_TABLE.'
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
WHERE id IN ('.implode(',', $search_items).')
WHERE id IN ('.implode(',', $search_result['items']).')
AND '.$forbidden.'
'.$conf['order_by'].'
;';
$page['items'] = array_from_query($query, 'id');
}
else
{
$page['items'] = array();
$page['items'] = $search_result['items'];
}

$page = array_merge(
Expand Down

0 comments on commit 48c6d7e

Please sign in to comment.