Exemple #1
0
function s4wp_search_results()
{
    $qry = filter_input(INPUT_GET, 'ssearch', FILTER_SANITIZE_STRING);
    $offset = filter_input(INPUT_GET, 'offset', FILTER_SANITIZE_STRING);
    $count = filter_input(INPUT_GET, 'count', FILTER_SANITIZE_STRING);
    $fq = filter_input(INPUT_GET, 'fq', FILTER_SANITIZE_STRING);
    $sort = filter_input(INPUT_GET, 'sort', FILTER_SANITIZE_STRING);
    $order = filter_input(INPUT_GET, 'order', FILTER_SANITIZE_STRING);
    $isdym = filter_input(INPUT_GET, 'isdym', FILTER_SANITIZE_STRING);
    $server = filter_input(INPUT_GET, 'server', FILTER_SANITIZE_STRING);
    $plugin_s4wp_settings = s4wp_get_option();
    $output_info = $plugin_s4wp_settings['s4wp_output_info'];
    $output_pager = $plugin_s4wp_settings['s4wp_output_pager'];
    $output_facets = $plugin_s4wp_settings['s4wp_output_facets'];
    $results_per_page = $plugin_s4wp_settings['s4wp_num_results'];
    $categoy_as_taxonomy = $plugin_s4wp_settings['s4wp_cat_as_taxo'];
    $out = array();
    $out['hits'] = "0";
    $qry = html_entity_decode($qry);
    $qry = trim($qry, '"');
    $qry = trim($qry, "'");
    if (!$qry) {
        $qry = '';
    }
    global $wpdb;
    //Sql Injection Prevention
    $qry = $wpdb->_real_escape(trim($qry));
    //if server value has been set lets set it up here
    // and add it to all the search urls henceforth
    $serverval = isset($server) ? '&server=' . $server : '';
    # set some default values
    if (!$offset) {
        $offset = 0;
    }
    # only use default if not specified in post information
    if (!$count) {
        $count = $results_per_page;
    }
    if (!$fq) {
        $fq = '';
    }
    if ($sort && $order) {
        $sortby = $sort;
    } else {
        $sortby = '';
        $order = '';
    }
    if (!$isdym) {
        $isdym = 0;
    }
    $fqstr = '';
    $fqitms = explode('||', $fq);
    $selectedfacets = array();
    foreach ($fqitms as $fqitem) {
        if ($fqitem && $fqitem !== 'any') {
            $splititm = explode(':', $fqitem);
            $selectedfacet = array();
            $selectedfacet['name'] = sprintf(__("%s:%s"), ucwords(preg_replace('/_str$/i', '', $splititm[0])), str_replace("^^", "/", $splititm[1]));
            $removelink = '';
            foreach ($fqitms as $fqitem2) {
                if ($fqitem2 && !($fqitem2 === $fqitem)) {
                    $splititm2 = explode(':', $fqitem2);
                    $removelink = $removelink . urlencode('||') . $splititm2[0] . ':' . urlencode($splititm2[1]);
                }
            }
            if ($removelink) {
                $selectedfacet['removelink'] = htmlspecialchars(sprintf(__("?ssearch=%s&fq=%s"), urlencode($qry), $removelink));
            } else {
                $selectedfacet['removelink'] = htmlspecialchars(sprintf(__("?ssearch=%s"), urlencode($qry)));
            }
            //if server is set add it on the end of the url
            $selectedfacet['removelink'] .= $serverval;
            $fqstr = $fqstr . urlencode('||') . $splititm[0] . ':' . urlencode($splititm[1]);
            $selectedfacets[] = $selectedfacet;
        }
    }
    if ($qry) {
        $results = s4wp_query($qry, $offset, $count, $fqitms, $sortby, $order, $server);
        if ($results) {
            $data = $results->getData();
            $response = $data['response'];
            $header = $data['responseHeader'];
            $teasers = $results->getHighlighting()->getResults();
            if ($output_info) {
                $out['hits'] = $response['numFound'];
                $out['qtime'] = sprintf(__("%.3f"), $header['QTime'] / 1000);
            } else {
                $out['hits'] = 0;
            }
            if ($output_pager) {
                # calculate the number of pages
                $numpages = ceil($response['numFound'] / $count);
                $currentpage = ceil($offset / $count) + 1;
                $pagerout = array();
                if ($numpages == 0) {
                    $numpages = 1;
                }
                foreach (range(1, $numpages) as $pagenum) {
                    if ($pagenum != $currentpage) {
                        $offsetnum = ($pagenum - 1) * $count;
                        $pageritm = array();
                        $pageritm['page'] = sprintf(__("%d"), $pagenum);
                        if (!isset($sortby) || $sortby == "") {
                            $pagersortby = "date";
                            $pagerorder = "desc";
                        } else {
                            $pagersortby = $sortby;
                            $pagerorder = $order;
                        }
                        $pagerlink = sprintf(__("?ssearch=%s&offset=%d&count=%d&sort=%s&order=%s"), urlencode($qry), $offsetnum, $count, $pagersortby, $pagerorder);
                        if ($fqstr) {
                            $pagerlink .= '&fq=' . $fqstr;
                        }
                        $pageritm['link'] = htmlspecialchars($pagerlink);
                        //if server is set add it on the end of the url
                        $pageritm['link'] .= isset($pageritm['link']) ? $serverval : '';
                        $pagerout[] = $pageritm;
                    } else {
                        $pageritm = array();
                        $pageritm['page'] = sprintf(__("%d"), $pagenum);
                        $pageritm['link'] = "";
                        $pagerout[] = $pageritm;
                    }
                }
                $out['pager'] = $pagerout;
            }
            if ($output_facets) {
                # handle facets
                $facetout = array();
                if ($results->getFacetSet()) {
                    foreach ($results->getFacetSet()->getFacets() as $facetfield => $facet) {
                        if (!get_object_vars($facet)) {
                            //continue;
                        }
                        $facetinfo = array();
                        $facetitms = array();
                        $facetinfo['name'] = ucwords(preg_replace('/_str$/i', '', $facetfield));
                        # categories is a taxonomy
                        if ($categoy_as_taxonomy && $facetfield == 'categories') {
                            # generate taxonomy and counts
                            $taxo = array();
                            foreach ($facet as $facetval => $facetcnt) {
                                $taxovals = explode('^^', rtrim($facetval, '^^'));
                                $taxo = s4wp_gen_taxo_array($taxo, $taxovals);
                            }
                            $facetitms = s4wp_get_output_taxo($facet, $taxo, '', $fqstr . $serverval, $facetfield);
                        } else {
                            foreach ($facet as $facetval => $facetcnt) {
                                $facetitm = array();
                                $facetitm['count'] = sprintf(__("%d"), $facetcnt);
                                $facetitm['link'] = htmlspecialchars(sprintf(__('?ssearch=%s&fq=%s:%s%s', 'solr4wp'), urlencode($qry), $facetfield, urlencode('"' . $facetval . '"'), $fqstr));
                                //if server is set add it on the end of the url
                                $facetitm['link'] .= $serverval;
                                $facetitm['name'] = $facetval;
                                $facetitms[] = $facetitm;
                            }
                        }
                        $facetinfo['items'] = $facetitms;
                        $facetout[$facetfield] = $facetinfo;
                    }
                }
                $facetout['selected'] = $selectedfacets;
                $out['facets'] = $facetout;
            }
            $resultout = array();
            if ($response['numFound'] != 0) {
                foreach ($response['docs'] as $doc) {
                    $resultinfo = array();
                    $docid = strval($doc['id']);
                    $resultinfo['permalink'] = $doc['permalink'];
                    $resultinfo['title'] = $doc['title'];
                    if (isset($doc['author'])) {
                        $resultinfo['author'] = $doc['author'];
                    }
                    if (isset($doc['author_s'])) {
                        $resultinfo['authorlink'] = htmlspecialchars($doc['author_s']);
                    }
                    $resultinfo['numcomments'] = $doc['numcomments'];
                    $resultinfo['date'] = $doc['displaydate'];
                    if ($doc['numcomments'] === 0) {
                        $resultinfo['comment_link'] = $doc['permalink'] . "#respond";
                    } else {
                        $resultinfo['comment_link'] = $doc['permalink'] . "#comments";
                    }
                    $resultinfo['score'] = $doc['score'];
                    $resultinfo['id'] = $docid;
                    $docteaser = $teasers[$docid];
                    $docteaser = $docteaser->getFields();
                    if ($docteaser) {
                        $resultinfo['teaser'] = sprintf(__("...%s..."), implode("...", $docteaser['content']));
                    } else {
                        $words = explode(' ', $doc['content']);
                        $teaser = implode(' ', array_slice($words, 0, 30));
                        $resultinfo['teaser'] = sprintf(__("%s..."), $teaser);
                    }
                    $resultout[] = $resultinfo;
                }
            }
            $out['results'] = $resultout;
        }
    } else {
        $out['hits'] = "0";
    }
    # pager and results count helpers
    $out['query'] = htmlspecialchars($qry);
    $out['offset'] = strval($offset);
    $out['count'] = strval($count);
    $out['firstresult'] = strval($offset + 1);
    $out['lastresult'] = strval(min($offset + $count, $out['hits']));
    // hits doesn't exist
    $out['sortby'] = $sortby;
    $out['order'] = $order;
    $out['sorting'] = array('scoreasc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=score&order=asc%s', urlencode($qry), stripslashes($fq), $serverval)), 'scoredesc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=score&order=desc%s', urlencode($qry), stripslashes($fq), $serverval)), 'dateasc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=date&order=asc%s', urlencode($qry), stripslashes($fq), $serverval)), 'datedesc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=date&order=desc%s', urlencode($qry), stripslashes($fq), $serverval)), 'modifiedasc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=modified&order=asc%s', urlencode($qry), stripslashes($fq), $serverval)), 'modifieddesc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=modified&order=desc%s', urlencode($qry), stripslashes($fq), $serverval)), 'commentsasc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=numcomments&order=asc%s', urlencode($qry), stripslashes($fq), $serverval)), 'commentsdesc' => htmlspecialchars(sprintf('?ssearch=%s&fq=%s&sort=numcomments&order=desc%s', urlencode($qry), stripslashes($fq), $serverval)));
    return $out;
}
function s4wp_apply_config_to_blog($blogid)
{
    syslog(LOG_ERR, "applying config to blog with id {$blogid}");
    if (!is_multisite()) {
        return;
    }
    wp_cache_flush();
    $plugin_s4wp_settings = s4wp_get_option();
    switch_to_blog($blogid);
    wp_cache_flush();
    s4wp_update_option($plugin_s4wp_settings);
    restore_current_blog();
    wp_cache_flush();
}
":</strong>
    <br />Hits:<?php 
                        echo esc_html($out['hits']);
                        ?>
    <br />Query Time:<?php 
                        echo esc_html($out['qtime']);
                        ?>
    </p></div>
<?php 
                    }
                }
            }
        }
    }
}
$s4wp_settings = s4wp_get_option('plugin_s4wp_settings');
?>


<div class="wrap">
<h2><?php 
_e('Solr Power', 'solr4wp');
?>
</h2>


<h2 class="nav-tab-wrapper" id="solr-tabs">
	<a class="nav-tab nav-tab-active" id="solr_info-tab"
		   href="#top#solr_info">Info</a>
		<a class="nav-tab" id="solr_indexing-tab"
		   href="#top#solr_indexing">Indexing</a>
</strong></p></div>
<?php 
            } else {
                if (isset($_POST['s4wp_init_blogs']) and $_POST['s4wp_init_blogs']) {
                    s4wp_copy_config_to_all_blogs();
                } else {
                    if (isset($_POST['s4wp_query']) && $_POST['solrQuery']) {
                        $qry = filter_input(INPUT_POST, 'solrQuery', FILTER_SANITIZE_STRING);
                        $offset = null;
                        $count = null;
                        $fq = null;
                        $sortby = null;
                        $order = null;
                        $results = s4wp_query($qry, $offset, $count, $fq, $sortby, $order);
                        if (isset($results)) {
                            $plugin_s4wp_settings = s4wp_get_option();
                            $output_info = $plugin_s4wp_settings['s4wp_output_info'];
                            $data = $results->getData();
                            $response = $data['response'];
                            $header = $data['responseHeader'];
                            if ($output_info) {
                                $out['hits'] = $response['numFound'];
                                $out['qtime'] = sprintf(__("%.3f"), $header['QTime'] / 1000);
                            } else {
                                $out['hits'] = 0;
                                $out['qtime'] = 0;
                            }
                        } else {
                            $data = $results;
                        }
                        ?>