verso = 'a3'; $this->options = get_option('scrib'); $this->options['sphinxsearch'] = FALSE; $this->options['search_url'] = get_settings('siteurl') .'/search/'; $this->harvest_table = $wpdb->prefix . 'scrib_harvest'; $this->data_facets = $this->data_facets(); $this->search_tag = NULL; $btaggy->tags_displaynames = array( 'format' => 'Format', 'subject' => 'Subject', 'discipline' => 'Discipline', 'author' => 'Author', 'category' => 'Category', 'scope' => 'Scope', 'pubyear' => 'Publication Year', 'keyword' => 'Keyword', 'tag' => 'Tag' ); // register WordPress hooks add_filter('posts_where', array(&$this, 'posts_where'), 10); add_filter('posts_request', array(&$this, 'the_query'), 10); add_filter('bsuite_tagurl', array(&$this, 'tagurl'), 10, 2); add_filter('query_vars', array(&$this, 'query_vars'), 11); // add_filter('author_rewrite_rules', array(&$this, 'author_rewrite_rules'), 1); add_action('admin_menu', array(&$this, 'addmenus')); add_filter('bsuite_tokens', array(&$this, 'tokens_set')); add_filter('bsuite_athooks', array(&$this, 'athooks_set')); add_action('widgets_init', 'scrib_widget_init'); add_filter('the_content_rss', array(&$this, 'the_content'), 1); add_filter('get_the_excerpt', array(&$this, 'the_content'), 1); add_filter('the_content', array(&$this, 'the_content'), 1); add_filter('the_excerpt', array(&$this, 'the_content'), 1); add_action('activate_scriblio/scriblio.php', array(&$this, 'createtables')); // end register WordPress hooks } public function is_scrib($id = ''){ global $post_cache, $post, $blog_id; if(empty($post_id)) $post_id = $post->ID; if($post_cache[$blog_id][$post_id]->post_type == 'scrib'){ return true; }else{ return false; } } public function feedlink(){ return(strtolower(substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], '/'))) . '://' . $_SERVER['HTTP_HOST'] . add_query_arg('feed', 'rss')); } // // add menus // public function addmenus(){ add_options_page('Scriblio Settings', 'Scriblio', 5, __FILE__, array(&$this, 'admin_menu')); } public function admin_menu(){ require('scriblio_admin.php'); } // // // options // public function options(){ static $options = NULL; if($options){ return($options); }else{ $options = get_settings('scrib_options'); return($options); } } public function createtables() { require('scriblio_createtables.php'); } // public function tagurl($tag_array, $searchtags = array()) { global $bsuite, $btaggy, $wp_query; //print_r($searchtags); if($tag_array){ foreach($tag_array as $tag){ if(is_array($tag)){ //echo $tag['tag'] .'
'; $searchtags[$tag['fieldname']] = isset($searchtags[$tag['fieldname']]) ? $tag['tag'] .'|'. $searchtags[$tag['fieldname']] : $tag['tag'] ; } } } if(!$tag_array) { echo 'hi'; $tag_array['href'] = $this->options['browse_url'] .'?'. http_build_query($searchtags); print_r($tag_array); }else if (!empty($wp_query->query_vars['s'])) { $tag_array['href'] = $this->options['search_url'] . urlencode($wp_query->query_vars['s']) .'?'. http_build_query($searchtags); }else{ $tag_array['href'] = $this->options['browse_url'] .'?'. http_build_query($searchtags); } $tag_array['link'] = ''; return($tag_array); } public function query_vars($vars){ // filter wp's query vars, add scrib vars return(array_unique(array_merge($this->data_facets, $vars))); } public function author_rewrite_rules($rules){ reset($rules); while (list($key, $val) = each($rules)) { $newrules[str_replace('author', 'by', $key)] = $val; } return($newrules); } public function data_facets($reset = NULL) { // define the Scrib search types/facets global $wpdb, $bsuite, $btaggy; if(get_option('Scrib_data_facets') && !$reset){ $facets = get_option('Scrib_data_facets'); }else{ $facets = $wpdb->get_col("SELECT tag_type FROM $btaggy->meta_table GROUP BY tag_type"); update_option('Scrib_data_facets', $facets); } $facets[] = 'scope'; return($facets); } public function get_matching_posts($the_keywords = '', $the_search_tags = array()){ unset($the_search_tags['scope']); global $bsuite, $btaggy, $wp_query, $wpdb; static $post_id = NULL; if($post_ids) return($post_ids); // do the tag matches if($the_search_tags){ reset($this->search_tags); while (list($key, $val) = each($the_search_tags)) { $values = (explode('|', str_replace('*', '%', urldecode($val)))); foreach($values as $val){ $operator = '='; if(strpos($val, '%')) $operator = 'LIKE'; $request = "SELECT post_id FROM ( SELECT post_id FROM $btaggy->meta_table WHERE tag_type = '$key' AND tag_small $operator '". str_replace('|', '\', \'', ereg_replace('[^a-z|0-9|\||%]', '', strtolower($val))) ."' ) res LEFT JOIN $wpdb->posts ON ID=post_id ORDER BY post_date_gmt DESC"; //echo "

$request

"; if(!$post_ids){ $post_ids = $wpdb->get_col($request); }else{ $post_ids = array_intersect($post_ids, $wpdb->get_col($request)); } } } } //print_r($post_ids); // do the keyword matches if ( !empty( $the_keywords ) ) { if($this->options['sphinxsearch']){ require_once ( 'sphinxapi.php' ); $mode = SPH_MATCH_ALL; $host = 'wpopac.plymouth.edu'; $port = 3312; $index = "scrib1"; $sortby = '@rank DESC, catdate DESC, @id DESC'; $cl = new SphinxClient (); if($wp_query->query_vars['scope'] == 'catalog') $cl->SetFilter ( 'type', array ( 1 ) ); if($wp_query->query_vars['scope'] == 'blog') $cl->SetFilter ( 'type', array ( 0 ) ); $cl->SetServer ( $host, $port ); $cl->SetLimits ( 0, 1000, 0 ); $cl->SetWeights ( array ( 150, 50, 75, 500 ) ); $cl->SetMatchMode ( $mode ); $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby ); $res = $cl->Query ( $the_keywords, $index ); if ( is_array($res['matched']) ){ //print_r(array_keys($res['matched'])); //print_r($res['matched']); if(!$post_ids){ $post_ids = array_keys($res['matched']); $this->the_matching_posts_ordinals = $res['matched']; }else{ $post_ids = array_intersect(array_keys($res['matched']), $post_ids); $this->the_matching_posts_ordinals = array_intersect_key($res['matched'], array_flip($post_ids)); } if (SAVEQUERIES) { unset( $res['matches'] ); unset( $res['matched'] ); $wpdb->queries[] = $res; } //print_r($post_ids); } }else{ if($wp_query->query_vars['scope']){ if($wp_query->query_vars['scope'] == 'catalog') $content_type = "'scrib'"; if($wp_query->query_vars['scope'] == 'blog') $content_type = "'page','post'"; $query = " SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $bsuite->search_table ON ( post_id = ID ) WHERE 1=1 AND (MATCH (content, title) AGAINST ('". $wpdb->escape($the_keywords) ."')) AND (post_type IN ($content_type) AND (post_status IN ('publish', 'private'))) LIMIT 1000 "; }else{ $query = " SELECT SQL_CALC_FOUND_ROWS post_id FROM $bsuite->search_table WHERE 1=1 AND (MATCH (content, title) AGAINST ('". $wpdb->escape($the_keywords) ."')) LIMIT 1000 "; } //echo "

$query

"; if(!$post_ids){ $post_ids = $wpdb->get_col($query);; }else{ $post_ids = array_intersect($post_ids, $wpdb->get_col($query)); } } } //print_r($post_ids); if(count($post_ids)){ $this->the_matching_posts = $post_ids; // if(!$this->the_matching_posts_ordinals) $this->the_matching_posts_ordinals = array_flip($post_ids); return(TRUE); }else if(count($the_search_tags)){ $this->the_matching_posts = FALSE; $wp_query->is_404 = TRUE; return(FALSE); }else{ return(FALSE); } } public function sort_matching_posts($the_posts){ //print_r($the_posts); //print_r($this->the_matching_posts_ordinals); // insert the ordinal into each post for sorting foreach($the_posts as $post){ //echo $post->ID . '='. $this->the_matching_posts_ordinals[$post->ID] .', '; $new_order[$this->the_matching_posts_ordinals[$post->ID]] = $post; } // now that the posts are re-keyed, sort them ksort($new_order); // This shuffle resets the keys on the array. // A function later in WP expects the array keys to be sequential, // and the output here might otherwise be non-sequential. array_unshift($new_order, 'Junk'); array_shift($new_order); //print_r($new_order); return($new_order); } public function posts_where($query){ //echo "

$query

"; return(str_replace("AND (post_type = 'post')", "AND (post_type IN ('post', 'scrib'))", $query)); } public function the_query($query, $limit = NULL){ global $bsuite, $btaggy, $wp_query, $wpdb; if($wp_query->is_admin) return($query); if($wp_query->query['author_name']){ unset($query_vars['author']); }else if($wp_query->query['author']){ $wp_query->query_vars['author'] = $wp_query->query['author']; } //echo "

$query

"; //print_r($wp_query); // establish the query vars $this->search_tags = array_map('stripslashes', (array_intersect_key($wp_query->query_vars, array_flip($this->data_facets)))); // figure out what page of posts to show // $paged, $posts_per_page, and $limit are here for cases // where the query doesn't have an explicit LIMIT declaration $paged = $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1; $posts_per_page = $wp_query->query_vars['posts_per_page'] ? $wp_query->query_vars['posts_per_page'] : get_settings('posts_per_page'); // figure out if we have matching posts, and which ones they are $inids = ''; if($this->get_matching_posts($wp_query->query_vars['s'], $this->search_tags)){ $inids = 'AND ID IN ('. implode(array_slice($this->the_matching_posts, ($paged - 1) * $posts_per_page, $posts_per_page), ', ') .')'; $limit[1] = '0, '. $posts_per_page; }else{ $limit[1] = ($paged - 1) * $posts_per_page .', '. $posts_per_page; } if (!empty($wp_query->query_vars['s']) && $this->the_matching_posts) { $query = "SELECT * FROM $wpdb->posts WHERE 1=1 $inids AND post_status IN ('publish', 'private') "; add_filter('the_posts', array(&$this, 'sort_matching_posts')); add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $wp_query->found_posts = '. count($this->the_matching_posts) .' ; $wp_query->max_num_pages = ceil('. count($this->the_matching_posts) .' / '. $posts_per_page .') ; return($a);')); }else if($wp_query->query['pagename'] && ($wp_query->query['pagename'] == $this->options['browse_name'])) { $this->is_browse = TRUE; $wp_query->is_search = TRUE; $wp_query->is_singular = FALSE; $wp_query->is_page = FALSE; $content_type = "'post', 'page', 'scrib'"; if($wp_query->query_vars['scope'] == 'catalog') $content_type = "'scrib'"; if($wp_query->query_vars['scope'] == 'blog') $content_type = "'page', 'post'"; $query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.* FROM $wpdb->posts WHERE 1=1 $inids AND (post_type IN ($content_type) AND (post_status IN ('publish', 'private'))) ORDER BY post_date_gmt DESC LIMIT $limit[1] "; if(empty($inids)){ add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $count = $wpdb->get_var("SELECT FOUND_ROWS()") ; $wp_query->found_posts = $count ; $wp_query->max_num_pages = ceil($count / '. $posts_per_page .') ; return($a);')); }else{ add_filter('the_posts', create_function('$a', 'global $wpdb, $wp_query ; $wp_query->found_posts = '. count($this->the_matching_posts) .' ; $wp_query->max_num_pages = ceil('. count($this->the_matching_posts) .' / '. $posts_per_page .') ; return($a);')); } } //print_r($this->the_matching_posts); //print_r($wp_query); //echo "

$query

"; if($this->the_matching_posts === FALSE){ $query = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.* FROM $wpdb->posts WHERE 1=2 "; } // we need something to protect against somebody trying to page beyond the found set of matching posts. // if(count($this->the_matching_posts < (($paged - 1) * $posts_per_page))) $this->the_query_string = $query; return($query); } // // fetch the facets and return an array // public function fetchfacets(){ global $wpdb, $bsuite, $btaggy, $wp_query; static $result = NULL; if($result || $is_404) return($result); //print_r($wp_query->query_vars['s']); if (!empty($wp_query->query_vars['s'])) { $inids = implode(array_slice($this->the_matching_posts, 0, 150), ', '); if($inids){ $bibns = implode(', ', $rows); $request = "SELECT tag_type, tag_raw, COUNT(post_id) AS hits FROM $btaggy->meta_table WHERE post_id IN ($inids) GROUP BY tag_type, tag_small ORDER BY hits DESC "; //echo "

$request

"; $facets = $wpdb->get_results($request); } }else{ if($this->the_matching_posts){ $inids = 'WHERE post_id IN ('. implode($this->the_matching_posts, ', ') .')'; }else{ if($result = get_option('scrib_facet_cache')){ return($result); }else{ $reset_facet_cache = TRUE; } $inids = ''; } $request = "SELECT tag_type, tag_raw, COUNT(post_id) AS hits FROM $btaggy->meta_table $inids GROUP BY tag_type, tag_small ORDER BY hits DESC LIMIT 1500 "; $facets = $wpdb->get_results($request); } //echo "

$request

"; if($facets){ foreach($facets as $row){ $result[$row->tag_type][] = array('namespace' => $row->tag_space, 'fieldname' => $row->tag_type, 'tag' => $row->tag_raw, 'hits' => $row->hits); } } if ($reset_facet_cache) { update_option('scrib_facet_cache', $result); } return($result); } public function the_facets($args) { //run through an array of facets, report them global $wpdb, $bsuite, $btaggy, $wp_query; parse_str($args, $r); $include = explode(',', str_replace(' ', '', $r['include'])); if(ereg('\*', $r['exclude'])){ $exclude = '*'; }else{ $exclude = explode(',', str_replace(' ', '', $r['exclude'])); } unset($r['include']); unset($r['exclude']); $options = http_build_query($r); $facets = $this->fetchfacets(); $result = NULL; // unset the items named in $exclude if($exclude && $exclude <> '*'){ foreach($exclude as $temp){ unset($facets[$temp]); } } // print out each facet named in $include if($include){ foreach($include as $temp){ if(array_key_exists($temp, $facets)) $result .= $btaggy->tags_formatlist($facets[$temp], $options, $this->search_tags); unset($facets[$temp]); } } // print out everything that remains if($exclude <> '*'){ foreach(array_keys($facets) as $temp){ if(array_key_exists($temp, $facets)) $result .= $btaggy->tags_formatlist($facets[$temp], $options, $this->search_tags); } } if($r['return']){ return($result); }else{ echo $result; } } public function the_scope() { if(!is_search() && !$this->is_browse) return(FALSE); $url_base = trim(str_replace('scope='. $this->search_tags['scope'] , '', $_SERVER['REQUEST_URI']), '?&'); $joiner = '?'; if(strpos($url_base,'?')) $joiner = '&'; echo '

Scope

'; } public function editsearch() { global $wpdb, $wp_query, $bsuite, $btaggy; if(!empty($wp_query->query_vars['s']) || !empty($this->search_tags)){ if($wp_query->query_vars['s']){ echo '

Searched for: