/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $now = Date::toSql(); $terms = $request->get_term_ar(); $weight = '(match(be.title, be.content) against (\'' . join(' ', $terms['stemmed']) . '\'))'; $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(be.title LIKE '%{$mand}%' OR be.content LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(be.title NOT LIKE '%{$forb}%' AND be.content NOT LIKE '%{$forb}%')"; } $addtl_where[] = "(be.publish_up <= '{$now}')"; $addtl_where[] = "(be.publish_down = '0000-00-00 00:00:00' OR (be.publish_down != '0000-00-00 00:00:00' AND be.publish_down > '{$now}'))"; $addtl_where[] = '(be.access IN (0,' . implode(',', User::getAuthorisedViewLevels()) . '))'; $rows = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\tbe.id,\n\t\t\t\tbe.title,\n\t\t\t\tbe.content AS description,\n\t\t\t\t(CASE WHEN be.scope_id > 0 AND be.scope='group' THEN\n\t\t\t\t\tconcat('index.php?option=com_groups&cn=', g.cn, '&active=blog&scope=', extract(year from be.created), '/', extract(month from be.created), '/', be.alias)\n\t\t\t\tWHEN be.scope='member' AND be.scope_id > 0 THEN\n\t\t\t\t\tconcat('index.php?option=com_members&id=', be.created_by, '&active=blog&task=', extract(year from be.created), '/', extract(month from be.created), '/', be.alias)\n\t\t\t\tELSE\n\t\t\t\t\tconcat('index.php?option=com_blog&year=', extract(year from be.created), '&month=', extract(month from be.created), '&alias=', be.alias)\n\t\t\t\tEND) AS link,\n\t\t\t\t{$weight} AS weight,\n\t\t\t\t'Blog Entry' AS section,\n\t\t\t\tbe.created AS date,\n\t\t\t\tu.name AS contributors,\n\t\t\t\tbe.created_by AS contributor_ids\n\t\t\tFROM `#__blog_entries` be\n\t\t\tINNER JOIN `#__users` u ON u.id = be.created_by\n\t\t\tLEFT JOIN `#__xgroups` AS g ON g.gidNumber=be.scope_id AND be.scope='group'\n\t\t\tWHERE\n\t\t\t\tbe.state=1 AND\n\t\t\t\t{$weight} > 0" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '')); if (($rows = $rows->to_associative()) instanceof \Components\Search\Models\Basic\Result\Blank) { return; } $id_map = array(); foreach ($rows as $idx => $row) { $id_map[$row->get('id')] = $idx; } $comments = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t \tCASE WHEN bc.anonymous THEN 'Anonymous Comment' ELSE concat('Comment by ', u.name) END AS title,\n\t\t\tbc.content AS description,\n\t\t\tconcat('index.php?option=com_members&id=', be.created_by, '&active=blog&task=', extract(year from be.created), '/', extract(month from be.created), '/', be.alias) AS link,\n\t\t\tbc.created AS date,\n\t\t\t'Comments' AS section,\n\t\t\tbc.entry_id\n\t\t\tFROM `#__blog_comments` bc\n\t\t\tINNER JOIN `#__blog_entries` be\n\t\t\t\tON be.id = bc.entry_id\n\t\t\tINNER JOIN `#__users` u\n\t\t\t\tON u.id = bc.created_by\n\t\t\tWHERE bc.entry_id IN (" . implode(',', array_keys($id_map)) . ")\n\t\t\tORDER BY bc.created"); foreach ($comments->to_associative() as $comment) { $rows->at($id_map[$comment->get('entry_id')])->add_child($comment); } $results->add($rows); }
/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $terms = $request->get_term_ar(); $weight = '(match(wp.title) against (\'' . join(' ', $terms['stemmed']) . '\') + match(wv.pagetext) against (\'' . join(' ', $terms['stemmed']) . '\'))'; $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(wp.title LIKE '%{$mand}%' OR wv.pagetext LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(wp.title NOT LIKE '%{$forb}%' AND wv.pagetext NOT LIKE '%{$forb}%')"; } $viewlevels = implode(',', User::getAuthorisedViewLevels()); if ($gids = $authz->get_group_ids()) { $authorization = '(wp.access IN (0,' . $viewlevels . ') OR (wp.access = 1 AND xg.gidNumber IN (' . join(',', $gids) . ')))'; } else { $authorization = '(wp.access IN (0,' . $viewlevels . '))'; } // fml $groupAuth = array(); if ($authz->is_super_admin()) { $groupAuth[] = '1'; } else { $groupAuth[] = 'xg.plugins LIKE \'%wiki=anyone%\''; if (!$authz->is_guest()) { $groupAuth[] = 'xg.plugins LIKE \'%wiki=registered%\''; if ($gids = $authz->get_group_ids()) { $groupAuth[] = '(xg.plugins LIKE \'%wiki=members%\' AND xg.gidNumber IN (' . join(',', $gids) . '))'; } } } $rows = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\twp.title,\n\t\t\t\twp.scope,\n\t\t\t\twp.scope_id,\n\t\t\t\twv.pagehtml AS description,\n\t\t\t\tCASE\n\t\t\t\t\tWHEN wp.path != '' THEN concat(wp.path, '/', wp.pagename)\n\t\t\t\t\tELSE wp.pagename\n\t\t\t\tEND AS link,\n\t\t\t\t{$weight} AS weight,\n\t\t\t\twv.created AS date,\n\t\t\t\tCASE\n\t\t\t\t\tWHEN wp.scope='project' THEN 'Project Notes'\n\t\t\t\t\tELSE 'Wiki'\n\t\t\t\tEND AS section\n\t\t\tFROM `#__wiki_versions` wv\n\t\t\tINNER JOIN `#__wiki_pages` wp\n\t\t\t\tON wp.id = wv.page_id\n\t\t\tLEFT JOIN `#__xgroups` xg ON xg.gidNumber = wp.scope_id AND wp.scope='group'\n\t\t\tWHERE\n\t\t\t\t{$authorization} AND\n\t\t\t\t{$weight} > 0 AND\n\t\t\t\twp.state < 2 AND\n\t\t\t\twv.id = (SELECT MAX(wv2.id) FROM `#__wiki_versions` wv2 WHERE wv2.page_id = wv.page_id) " . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '') . " AND (xg.gidNumber IS NULL OR (" . implode(' OR ', $groupAuth) . "))\n\t\t\t ORDER BY {$weight} DESC"); include_once Component::path('com_wiki') . DS . 'models' . DS . 'page.php'; foreach ($rows->to_associative() as $row) { if (!$row) { continue; } $page = \Components\Wiki\Models\Page::blank(); $page->set('pagename', $row->link); $page->set('scope', $row->scope); $page->set('scope_id', $row->scope_id); $row->set_link(Route::url($page->link())); // rough de-wikifying. probably a bit faster than rendering to html and then stripping the tags, but not perfect //$row->set_description(preg_replace('/(\[+.*?\]+|\{+.*?\}+|[=*])/', '', $row->get_description())); $row->set_description(strip_tags($row->get_description())); $results->add($row); } }
/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $terms = $request->get_term_ar(); $weight = 'match(e.title, e.content) against(\'' . join(' ', $terms['stemmed']) . '\')'; $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(e.title LIKE '%{$mand}%' OR e.content LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(e.title NOT LIKE '%{$forb}%' AND e.content NOT LIKE '%{$forb}%')"; } // Commenting out Access check as it was never used and column was removed from table // during events refactoring // // @author Chris Smoak // @date 4/20/2014 // // $addtl_where[] = '(e.access IN (' . implode(',', User::getAuthorisedViewLevels()) . '))'; $rows = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\te.title,\n\t\t\t\te.content AS description,\n\t\t\t\te.scope,\n\t\t\t\te.scope_id,\n\t\t\t\tconcat('index.php?option=com_events&task=details&id=', e.id) AS link,\n\t\t\t\t{$weight} AS weight,\n\t\t\t\tpublish_up AS date,\n\t\t\t\t'Events' AS section\n\t\t\tFROM `#__events` e\n\t\t\tWHERE\n\t\t\t\tstate = 1 AND\n\t\t\t\tapproved AND {$weight} > 0" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '') . " ORDER BY {$weight} DESC"); foreach ($rows->to_associative() as $row) { if (!$row) { continue; } // check group perms if ($row->scope == 'group') { // load group $group = \Hubzero\User\Group::getInstance($row->scope_id); // make sure we found one if (!$group) { continue; } // get group calendar access $access = \Hubzero\User\Group\Helper::getPluginAccess($group, 'calendar'); // is calendar off // is calendar for registered users & not logged in // is calendar for members only and we are not a member if ($access == 'nobody' || $access == 'registered' && User::isGuest() || $access == 'members' && !in_array(User::get('id'), $group->get('members'))) { continue; } } $row->set_description(preg_replace('/(\\[+.*?\\]+|\\{+.*?\\}+|[=*])/', '', $row->get_description())); $results->add($row); } }
/** * Short description for 'onSearch' * * Long description (if any) ... * * @param object $request Parameter description (if any) ... * @param object &$results Parameter description (if any) ... * @return void */ public static function onSearch($request, &$results) { $terms = $request->get_term_ar(); $weight = 'match(wli.subject, wli.about) against(\'' . join(' ', $terms['stemmed']) . '\')'; $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(wli.subject LIKE '%{$mand}%' OR wli.about LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(wli.subject NOT LIKE '%{$forb}%' AND wli.about NOT LIKE '%{$forb}%')"; } $rows = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\twli.subject AS title,\n\t\t\t\twli.about AS description,\n\t\t\t\tconcat('index.php?option=com_wishlist&category=', wl.category, '&rid=', wl.referenceid, '&task=wish&wishid=', wli.id) AS link,\n\t\t\t\tmatch(wli.subject, wli.about) against('collaboration') AS weight,\n\t\t\t\twli.proposed AS date,\n\t\t\t\tconcat(wl.title) AS section,\n\t\t\t\tCASE\n\t\t\t\tWHEN wli.anonymous THEN NULL\n\t\t\t\tELSE (SELECT name FROM #__users ju WHERE ju.id = wli.proposed_by)\n\t\t\t\tEND AS contributors,\n\t\t\t\tCASE\n\t\t\t\tWHEN wli.anonymous THEN NULL\n\t\t\t\tELSE wli.proposed_by\n\t\t\t\tEND AS contributor_ids\n\t\t\tFROM #__wishlist_item wli\n\t\t\tINNER JOIN #__wishlist wl\n\t\t\t\tON wl.id = wli.wishlist AND wl.public = 1\n\t\t\tWHERE\n\t\t\t\tNOT wli.private AND {$weight} > 0" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '') . " ORDER BY {$weight} DESC"); foreach ($rows->to_associative() as $row) { if (!$row) { continue; } $row->set_description(preg_replace('/(\\[+.*?\\]+|\\{+.*?\\}+|[=*])/', '', $row->get_description())); $results->add($row); } }
/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $dbg = isset($_GET['dbg']); $database = App::get('db'); $groups = array_map(array($database, 'escape'), $authz->get_group_names()); $viewlevels = implode(',', User::getAuthorisedViewLevels()); /*if ($groups) { $group_list = '(\'' . join('\', \'', $groups) . '\')'; $access = '(p.access IN (' . $viewlevels . ') OR ((v.access = 4 OR access = 5) AND r.group_owner IN ' . $group_list . '))'; } else {*/ $access = '(p.access IN (0, ' . $viewlevels . '))'; //} $term_parser = $request->get_terms(); $terms = $request->get_term_ar(); $quoted_terms = array(); foreach ($terms['optional'] as $idx => $term) { if ($term_parser->is_quoted($idx)) { foreach ($terms['stemmed'] as $sidx => $stem) { if (strpos($term, $stem) === 0 || strpos($stem, $term) === 0) { unset($terms['stemmed'][$sidx]); } } $quoted_terms[] = $term; } } $tag_map = array(); foreach ($request->get_tagged_ids('publications') as $id) { if (array_key_exists($id, $tag_map)) { ++$tag_map[$id]; } else { $tag_map[$id] = 1; } } $weight_authors = 'a.name LIKE \'%' . implode(' ', $terms['optional']) . '%\''; $weight = $terms['stemmed'] ? 'match(v.title, v.description, v.abstract) against (\'' . join(' ', $terms['stemmed']) . '\')' : '0'; foreach ($quoted_terms as $term) { $weight .= " + (CASE WHEN v.title LIKE '%{$term}%' OR v.description LIKE '%{$term}%' OR v.abstract LIKE '%{$term}%' THEN 1 ELSE 0 END)"; } $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(v.title LIKE '%{$mand}%' OR v.description LIKE '%{$mand}%' OR v.abstract LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(v.title NOT LIKE '%{$forb}%' AND v.description NOT LIKE '%{$forb}%' AND v.abstract NOT LIKE '%{$forb}%')"; } $sql = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\tp.id,\n\t\t\t\tv.publication_id,\n\t\t\t\tv.title,\n\t\t\t\tv.description,\n\t\t\t\tconcat('index.php?option=com_publications&id=', coalesce(case when p.alias = '' then null else p.alias end, p.id)) AS link,\n\t\t\t\t{$weight} AS weight,\n\t\t\t\tv.published_up AS date,\n\t\t\t\tc.alias AS section,\n\t\t\t\t(SELECT group_concat(a.name order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\tAS contributors,\n\t\t\t\t(SELECT group_concat(a.user_id order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\tAS contributor_ids,\n\t\t\t\tNULL AS parents\n\t\t\tFROM #__publication_versions v\n\t\t\tINNER JOIN #__publications p\n\t\t\t\tON p.id = v.publication_id\n\t\t\tLEFT JOIN #__publication_categories c\n\t\t\t\tON c.id = p.category\n\t\t\tWHERE\n\t\t\t\tv.state = 1 AND {$access} AND (v.published_up AND NOW() > v.published_up) AND (NOT v.published_down OR NOW() < v.published_down)\n\t\t\t\tAND ({$weight} > 0)" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '') . "UNION\n\t\t\tSELECT\n\t\t\t\tp.id,\n\t\t\t\tv.publication_id,\n\t\t\t\tv.title,\n\t\t\t\tv.description,\n\t\t\t\tconcat('index.php?option=com_publications&id=', coalesce(case when p.alias = '' then null else p.alias end, p.id)) AS link,\n\t\t\t\t1 AS weight,\n\t\t\t\tv.published_up AS date,\n\t\t\t\tc.alias AS section,\n\t\t\t\t(SELECT group_concat(a.name order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\tAS contributors,\n\t\t\t\t(SELECT group_concat(a.user_id order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\tAS contributor_ids,\n\t\t\t\tNULL AS parents\n\t\t\tFROM #__publication_authors a\n\t\t\tINNER JOIN #__publication_versions v\n\t\t\t\tON v.id = a.publication_version_id\n\t\t\tINNER JOIN #__publications p\n\t\t\t\tON p.id = v.publication_id\n\t\t\tLEFT JOIN #__publication_categories c\n\t\t\t\tON c.id = p.category\n\t\t\tWHERE\n\t\t\t\tv.state = 1 AND {$access} AND (v.published_up AND NOW() > v.published_up) AND (NOT v.published_down OR NOW() < v.published_down)\n\t\t\t\tAND a.status = 1 AND {$weight_authors}"); $assoc = $sql->to_associative(); $id_assoc = array(); foreach ($assoc as $row) { $id_assoc[$row->get('id')] = $row; } $placed = array(); if (!$quoted_terms) { // Find ids of tagged resources that did not match regular fulltext searching foreach ($assoc as $row) { $id = (int) $row->get('id'); if (array_key_exists($id, $tag_map)) { $row->add_weight((1 + $tag_map[$id]) / 12, 'tag bonus from publications plugin'); unset($tag_map[$id]); } } // Fill in tagged resources that did not match on fulltext if ($tag_map) { $sql = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\t\t\tp.id,\n\t\t\t\t\t\tv.publication_id,\n\t\t\t\t\t\tv.title,\n\t\t\t\t\t\tv.description,\n\t\t\t\t\t\tconcat('index.php?option=com_publications&id=', coalesce(case when p.alias = '' then null else p.alias end, p.id)) AS link,\n\t\t\t\t\t\t0.5 as weight,\n\t\t\t\t\t\tv.published_up AS date,\n\t\t\t\t\t\tc.alias AS section,\n\t\t\t\t\t\t(SELECT group_concat(a.name order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\t\t\tAS contributors,\n\t\t\t\t\t\t(SELECT group_concat(a.user_id order by a.ordering separator '\\n') FROM #__publication_authors a WHERE a.publication_version_id = v.id AND a.status=1)\n\t\t\t\t\t\t\tAS contributor_ids,\n\t\t\t\t\t\tNULL AS parents\n\t\t\t\t\tFROM #__publication_versions v\n\t\t\t\t\tINNER JOIN #__publications p\n\t\t\t\t\t\tON p.id = v.publication_id\n\t\t\t\t\tLEFT JOIN #__publication_categories c\n\t\t\t\t\t\tON c.id = p.category\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tv.state = 1 AND {$access} AND (v.published_up AND NOW() > v.published_up) AND (NOT v.published_down OR NOW() < v.published_down)\n\t\t\t\t\t\tAND p.id in (" . implode(',', array_keys($tag_map)) . ")" . ($addtl_where ? ' AND ' . implode(' AND ', $addtl_where) : '')); foreach ($sql->to_associative() as $row) { $rows = $sql->to_associative(); foreach ($rows as $row) { if ($tag_map[$row->get('id')] > 1) { $row->adjust_weight($tag_map[$row->get('id')] / 8, 'tag bonus for non-matching but tagged publications'); } $id_assoc[$row->get('id')] = $row; } } } } $sorter = new PublicationChildSorter($placed); $rows = array(); foreach ($id_assoc as $id => $row) { if (!array_key_exists((int) $id, $placed)) { $row->sort_children(array($sorter, 'sort')); $rows[] = $row; } } usort($rows, create_function('$a, $b', 'return (($res = $a->get_weight() - $b->get_weight()) == 0 ? 0 : $res > 0 ? -1 : 1);')); foreach ($rows as $row) { $row->set_link(Route::url($row->get_raw_link())); $results->add($row); } }
/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $terms = $request->get_term_ar(); $weight = "match(f.title, f.comment) against ('" . join(' ', $terms['stemmed']) . "')"; $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(f.title LIKE '%{$mand}%' OR f.comment LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(f.title NOT LIKE '%{$forb}%' AND f.comment NOT LIKE '%{$forb}%')"; } $gids = $authz->get_group_ids(); if (!User::authorise('core.view', 'com_groups')) { $addtl_where[] = 'f.scope_id IN (0' . ($gids ? ',' . join(',', $gids) : '') . ')'; } else { $viewlevels = implode(',', User::getAuthorisedViewLevels()); if ($gids) { $addtl_where[] = '(f.access IN (0,' . $viewlevels . ') OR ((f.access = 4 OR f.access = 5) AND f.scope_id IN (0,' . join(',', $gids) . ')))'; } else { $addtl_where[] = '(f.access IN (0,' . $viewlevels . '))'; } } // fml $groupAuth = array(); if ($authz->is_super_admin()) { $groupAuth[] = '1'; } else { $groupAuth[] = "g.plugins LIKE '%forum=anyone%'"; if (!$authz->is_guest()) { $groupAuth[] = "g.plugins LIKE '%forum=registered%'"; if ($gids) { $groupAuth[] = "(g.plugins LIKE '%wiki=members%' AND g.gidNumber IN (" . join(',', $gids) . "))"; } } } $rows = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\tf.title,\n\t\t\t\tcoalesce(f.comment, '') AS description, f.scope_id, s.alias as sect, c.alias as cat, CASE WHEN f.parent > 0 THEN f.parent ELSE f.id END as `thread`,\n\t\t\t\t(CASE\n\t\t\t\t\tWHEN f.scope_id > 0 AND f.scope='group' THEN concat('index.php?option=com_groups&cn=', g.cn, '&active=forum')\n\t\t\t\t\tELSE concat('index.php?option=com_forum§ion=', coalesce(concat(s.alias, '&category=', coalesce(concat(c.alias, '&thread='), ''))), CASE WHEN f.parent > 0 THEN f.parent ELSE f.id END)\n\t\t\t\tEND) AS `link`,\n\t\t\t\t{$weight} AS `weight`,\n\t\t\t\tf.created AS `date`,\n\t\t\t\tconcat(s.alias, ', ', c.alias) AS `section`\n\t\t\tFROM `#__forum_posts` f\n\t\t\tLEFT JOIN `#__forum_categories` AS c\n\t\t\t\tON c.id = f.category_id\n\t\t\tLEFT JOIN `#__forum_sections` AS s\n\t\t\t\tON s.id = c.section_id\n\t\t\tLEFT JOIN `#__xgroups` AS g\n\t\t\t\tON g.gidNumber = f.scope_id AND f.scope='group'\n\t\t\tWHERE\n\t\t\t\tf.state = 1 AND\n\t\t\t\tf.scope != 'course' AND\n\t\t\t\t{$weight} > 0" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '') . " AND (g.gidNumber IS NULL OR (" . implode(' OR ', $groupAuth) . "))\n\t\t\tORDER BY {$weight} DESC"); foreach ($rows->to_associative() as $row) { if (!$row) { continue; } if ($row->scope_id) { $row->link .= '/' . ($row->sect ? $row->sect : 'defaultsection') . '/'; $row->link .= ($row->cat ? $row->cat : 'discussion') . '/'; $row->link .= $row->thread; } $results->add($row); } }
/** * Build search query and add it to the $results * * @param object $request \Components\Search\Models\Basic\Request * @param object &$results \Components\Search\Models\Basic\Result\Set * @param object $authz \Components\Search\Models\Basic\Authorization * @return void */ public static function onSearch($request, &$results, $authz) { $dbg = isset($_GET['dbg']); $database = App::get('db'); $groups = array_map(array($database, 'escape'), $authz->get_group_names()); $viewlevels = implode(',', User::getAuthorisedViewLevels()); if ($groups) { $group_list = '(\'' . join('\', \'', $groups) . '\')'; $access = '(access IN (0,' . $viewlevels . ') OR ((access = 4 OR access = 5) AND r.group_owner IN ' . $group_list . '))'; } else { $access = '(access IN (0,' . $viewlevels . '))'; } $term_parser = $request->get_terms(); $terms = $request->get_term_ar(); $quoted_terms = array(); foreach ($terms['optional'] as $idx => $term) { if ($term_parser->is_quoted($idx)) { foreach ($terms['stemmed'] as $sidx => $stem) { if (strpos($term, $stem) === 0 || strpos($stem, $term) === 0) { unset($terms['stemmed'][$sidx]); } } $quoted_terms[] = $term; } } $tag_map = array(); foreach ($request->get_tagged_ids('resources') as $id) { if (array_key_exists($id, $tag_map)) { ++$tag_map[$id]; } else { $tag_map[$id] = 1; } } $weight = $terms['stemmed'] ? 'match(r.title, r.introtext, r.`fulltxt`) against (\'' . join(' ', $terms['stemmed']) . '\')' : '0'; foreach ($quoted_terms as $term) { $weight .= " + (CASE WHEN r.title LIKE '%{$term}%' OR r.introtext LIKE '%{$term}%' OR r.`fulltxt` LIKE '%{$term}%' THEN 1 ELSE 0 END)"; } $addtl_where = array(); foreach ($terms['mandatory'] as $mand) { $addtl_where[] = "(r.title LIKE '%{$mand}%' OR r.introtext LIKE '%{$mand}%' OR r.`fulltxt` LIKE '%{$mand}%')"; } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(r.title NOT LIKE '%{$forb}%' AND r.introtext NOT LIKE '%{$forb}%' AND r.`fulltxt` NOT LIKE '%{$forb}%')"; } $sql = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\tr.id,\n\t\t\t\tr.title,\n\t\t\t\tcoalesce(r.`fulltxt`, r.introtext, '') AS description,\n\t\t\t\tconcat('index.php?option=com_resources&id=', coalesce(case when r.alias = '' then null else r.alias end, r.id)) AS link,\n\t\t\t\t{$weight} AS weight,\n\t\t\t\tr.publish_up AS date,\n\t\t\t\trt.type AS section,\n\t\t\t\t(SELECT group_concat(u1.name order by anames.ordering separator '\\n') FROM `#__author_assoc` anames LEFT JOIN `#__xprofiles` u1 ON u1.uidNumber = anames.authorid WHERE subtable = 'resources' AND subid = r.id)\n\t\t\t\tAS contributors,\n\t\t\t\t(SELECT group_concat(anames.authorid order by anames.ordering separator '\\n') FROM `#__author_assoc` anames WHERE subtable = 'resources' AND subid = r.id)\n\t\t\t\tAS contributor_ids,\n\t\t\t\t(select group_concat(concat(parent_id, '|', ordering))\n\t\t\t\t\tfrom `#__resource_assoc` ra2\n\t\t\t\t\tleft join `#__resources` re3 on re3.id = ra2.parent_id and re3.standalone\n\t\t\t\t\twhere ra2.child_id = r.id) AS parents\n\t\t\tFROM `#__resources` r\n\t\t\tLEFT JOIN `#__resource_types` rt\n\t\t\t\tON rt.id = r.type\n\t\t\tWHERE\n\t\t\t\tr.published = 1 AND r.standalone AND {$access} AND (r.publish_up AND UTC_TIMESTAMP() > r.publish_up) AND (NOT r.publish_down OR UTC_TIMESTAMP() < r.publish_down)\n\t\t\t\tAND ({$weight} > 0)" . ($addtl_where ? ' AND ' . join(' AND ', $addtl_where) : '')); $assoc = $sql->to_associative(); $id_assoc = array(); foreach ($assoc as $row) { $id_assoc[$row->get('id')] = $row; } $placed = array(); if (!$quoted_terms) { // Find ids of tagged resources that did not match regular fulltxt searching foreach ($assoc as $row) { $id = (int) $row->get('id'); if (array_key_exists($id, $tag_map)) { $row->add_weight((1 + $tag_map[$id]) / 12, 'tag bonus from resources plugin'); unset($tag_map[$id]); } } // Fill in tagged resources that did not match on fulltxt if ($tag_map) { $sql = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\t\t\tr.id,\n\t\t\t\t\t\tr.title,\n\t\t\t\t\t\tcoalesce(r.`fulltxt`, r.introtext, '') AS description,\n\t\t\t\t\t\tconcat('index.php?option=com_resources&id=', coalesce(case when r.alias = '' then null else r.alias end, r.id)) AS link,\n\t\t\t\t\t\tr.publish_up AS date,\n\t\t\t\t\t\t0.5 as weight,\n\t\t\t\t\t\trt.type AS section,\n\t\t\t\t\t\t(SELECT group_concat(u1.name order by anames.ordering separator '\\n') FROM `#__author_assoc` anames LEFT JOIN `#__xprofiles` u1 ON u1.uidNumber = anames.authorid WHERE subtable = 'resources' AND subid = r.id)\n\t\t\t\t\t\t\tAS contributors,\n\t\t\t\t\t\t(SELECT group_concat(anames.authorid order by anames.ordering separator '\\n') FROM `#__author_assoc` anames WHERE subtable = 'resources' AND subid = r.id)\n\t\t\t\t\t\t\tAS contributor_ids,\n\t\t\t\t\t\t(select group_concat(concat(parent_id, '|', ordering))\n\t\t\t\t\t\t\tfrom `#__resource_assoc` ra2\n\t\t\t\t\t\t\tleft join `#__resources` re3 on re3.id = ra2.parent_id and re3.standalone\n\t\t\t\t\t\t\twhere ra2.child_id = r.id) AS parents\n\t\t\t\t\tFROM `#__resources` r\n\t\t\t\t\tLEFT JOIN `#__resource_types` rt\n\t\t\t\t\tON rt.id = r.type\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tr.published = 1 AND r.standalone AND {$access} AND (r.publish_up AND NOW() > r.publish_up) AND (NOT r.publish_down OR NOW() < r.publish_down)\n\t\t\t\t\t\tAND r.id in (" . implode(',', array_keys($tag_map)) . ")" . ($addtl_where ? ' AND ' . implode(' AND ', $addtl_where) : '')); foreach ($sql->to_associative() as $row) { if ($tag_map[$row->get('id')] > 1) { $row->adjust_weight($tag_map[$row->get('id')] / 8, 'tag bonus for non-matching but tagged resources'); } $id_assoc[$row->get('id')] = $row; } } } /* // Nest child resources $section = $request->get_terms()->get_section(); foreach ($id_assoc as $id=>$row) { $parents = $row->get('parents'); if ($parents) foreach (explode(',', $parents) as $parent) { list($parent_id, $ordering) = preg_split('#\|#', $parent); if (array_key_exists((int)$parent_id, $id_assoc) && $id_assoc[(int)$parent_id]->is_in_section($section, 'resources')) { $placed[(int)$id] = $ordering; $id_assoc[(int)$parent_id]->add_child($row); $id_assoc[(int)$parent_id]->add_weight($row->get_weight()/15, 'propagating child weight'); } } } */ $sorter = new ResourceChildSorter($placed); $rows = array(); foreach ($id_assoc as $id => $row) { if (!array_key_exists((int) $id, $placed)) { $row->sort_children(array($sorter, 'sort')); $rows[] = $row; } } usort($rows, create_function('$a, $b', 'return (($res = $a->get_weight() - $b->get_weight()) == 0 ? 0 : $res > 0 ? -1 : 1);')); foreach ($rows as $row) { $results->add($row); } }
/** * Build search query and add it to the $results * * @param object $request YSearchModelRequest * @param object &$results YSearchModelResultSet * @return void */ public static function onSearchCustom($request, &$results) { if (($section = $request->get_terms()->get_section()) && $section[0] != 'members') { return; } $terms = $request->get_term_ar(); $addtl_where = array(); foreach (array($terms['mandatory'], $terms['optional']) as $pos) { foreach ($pos as $term) { $addtl_where[] = "(p.name LIKE '%{$term}%')"; } } foreach ($terms['forbidden'] as $forb) { $addtl_where[] = "(p.name NOT LIKE '%{$forb}%')"; } $sql = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\tp.uidNumber AS id,\n\t\t\t\tp.name AS title,\n\t\t\t\tcoalesce(b.bio, '') AS description,\n\t\t\t\tconcat('index.php?option=com_members&id=', CASE WHEN p.uidNumber > 0 THEN p.uidNumber ELSE concat('n', abs(p.uidNumber)) END) AS link,\n\t\t\t\tNULL AS date,\n\t\t\t\t'Members' AS section,\n\t\t\t\tCASE WHEN p.picture IS NOT NULL THEN concat('/site/members/', lpad(p.uidNumber, 5, '0'), '/', p.picture) ELSE NULL END AS img_href\n\t\t\tFROM #__xprofiles p\n\t\t\tLEFT JOIN #__xprofiles_bio b\n\t\t\t\tON b.uidNumber = p.uidNumber\n\t\t\tWHERE\n\t\t\t\tpublic AND " . join(' AND ', $addtl_where)); $assoc = $sql->to_associative(); if (!count($assoc)) { return false; } $when = "c.alias THEN\n\t\t\tconcat(\n\t\t\t\tCASE WHEN c.alias THEN concat('/', c.alias) ELSE '' END\n\t\t\t)"; $resp = array(); foreach ($assoc as $row) { $query = "SELECT\n\t\t\t\t\tCASE WHEN aa.subtable = 'resources' THEN\n\t\t\t\t\t\tr.title\n\t\t\t\t\tELSE\n\t\t\t\t\t\tc.title\n\t\t\t\t\tEND AS title,\n\t\t\t\t\tCASE\n\t\t\t\t\t\tWHEN aa.subtable = 'resources' THEN\n\t\t\t\t\t\t\tconcat(coalesce(r.introtext, ''), coalesce(r.`fulltxt`, ''))\n\t\t\t\t\t\tELSE\n\t\t\t\t\t\t\tconcat(coalesce(c.introtext, ''), coalesce(c.`fulltext`, ''))\n\t\t\t\t\tEND AS description,\n\t\t\t\t\tCASE\n\t\t\t\t\t\tWHEN aa.subtable = 'resources' THEN\n\t\t\t\t\t\t\tconcat('/resources/', r.id)\n\t\t\t\t\t\tELSE\n\t\t\t\t\t\t\tCASE\n\t\t\t\t\t\t\t\tWHEN {$when}\n\t\t\t\t\t\t\t\tELSE concat('/content/article/', c.id)\n\t\t\t\t\t\t\tEND\n\t\t\t\t\tEND AS link,\n\t\t\t\t\t1 AS weight,\n\t\t\t\t\tCASE\n\t\t\t\t\t\tWHEN aa.subtable = 'resources' THEN\n\t\t\t\t\t\t\trt.type\n\t\t\t\t\t\tELSE"; $query .= " s.alias"; $query .= " END AS section,\n\t\t\t\t\tCASE\n\t\t\t\t\t\tWHEN aa.subtable = 'resources' THEN\n\t\t\t\t\t\t\tra.ordering\n\t\t\t\t\t\tELSE\n\t\t\t\t\t\t\t-1\n\t\t\t\t\tEND AS ordering\n\t\t\t\t\tFROM #__author_assoc aa\n\t\t\t\t\tLEFT JOIN #__resources r\n\t\t\t\t\t\tON aa.subtable = 'resources' AND r.id = aa.subid AND r.published = 1\n\t\t\t\t\tLEFT JOIN #__resource_assoc ra\n\t\t\t\t\t\tON ra.child_id = r.id\n\t\t\t\t\tLEFT JOIN #__resource_types rt\n\t\t\t\t\t\tON rt.id = r.type"; $query .= " LEFT JOIN #__content c\n\t\t\t\t\t\tON aa.subtable = 'content' AND c.id = aa.subid AND c.state = 1\n\t\t\t\t\tLEFT JOIN #__categories s\n\t\t\t\t\t\tON s.id = c.sectionid\n\t\t\t\t\tLEFT JOIN #__categories ca\n\t\t\t\t\t\tON ca.id = c.catid"; $query .= " WHERE aa.authorid = " . $row->get('id'); $work = new \Components\Search\Models\Basic\Result\Sql($query); $work_assoc = $work->to_associative(); $added = array(); foreach ($work_assoc as $wrow) { $link = $wrow->get_link(); if (array_key_exists($link, $added)) { continue; } $row->add_child($wrow); $row->add_weight(1); $added[$link] = 1; } $row->sort_children(array('ContributionSorter', 'sort')); $workp = new \Components\Search\Models\Basic\Result\Sql("SELECT\n\t\t\t\t\tr.publication_id AS id,\n\t\t\t\t\tr.title AS title,\n\t\t\t\t\tconcat(coalesce(r.description, ''), coalesce(r.abstract, '')) AS description,\n\t\t\t\t\tconcat('/publications/', r.id) AS link,\n\t\t\t\t\t1 AS weight,\n\t\t\t\t\trt.alias AS section,\n\t\t\t\t\taa.ordering\n\t\t\t\t\tFROM #__publication_authors aa\n\t\t\t\t\tLEFT JOIN #__publication_versions r\n\t\t\t\t\t\tON aa.publication_version_id = r.id AND r.state = 1\n\t\t\t\t\tLEFT JOIN #__publications p\n\t\t\t\t\t\tON p.id = r.publication_id\n\t\t\t\t\tLEFT JOIN #__publication_categories rt\n\t\t\t\t\t\tON rt.id = p.category\n\t\t\t\t\tWHERE aa.user_id = " . $row->get('id')); $workp_assoc = $workp->to_associative(); foreach ($workp_assoc as $wrow) { $link = $wrow->get_link(); if (array_key_exists($link, $added)) { continue; } $row->add_child($wrow); $row->add_weight(1); $added[$link] = 1; } $row->sort_children(array('ContributionSorter', 'sort')); $resp[] = $row; } usort($resp, array('ContributionSorter', 'sort_weight')); foreach ($resp as $row) { $results->add($row); } return false; }