/**
  * Searches the index for the given search term
  * @return SearchItem[]
  */
 public function Search(SearchQuery $query)
 {
     $terms = $query->GetSanitisedTerms();
     $len = count($terms);
     for ($i = 0; $i < $len; $i++) {
         $terms[$i] = Sql::ProtectString($this->connection, $terms[$i]);
         $terms[$i] = "LIKE '%" . trim($terms[$i], "'") . "%'";
     }
     $sql = "SELECT field_weight, weight_of_type, weight_within_type, weight, url, title, description, related_links_html \n                FROM\n                (\n                    SELECT SUM(field_weight) AS field_weight, weight_of_type, weight_within_type AS weight_within_type,\n                           SUM(field_weight) + weight_of_type + weight_within_type AS weight,\n                           url, title, description, related_links_html \n                    FROM\n                    (\n                        SELECT search_index_id, 500 as field_weight, weight_of_type, weight_within_type, url, title, description, related_links_html \n                        FROM nsa_search_index \n                        WHERE title " . implode(" AND title ", $terms) . " \n                        \n                        UNION\n                        \n                        SELECT search_index_id, 500 as field_weight, weight_of_type, weight_within_type, url, title, description, related_links_html \n                        FROM nsa_search_index \n                        WHERE keywords " . implode(" AND keywords ", $terms) . "  \n                        \n                        UNION\n                        \n                        SELECT search_index_id, 50 as field_weight, weight_of_type, weight_within_type, url, title, description, related_links_html \n                        FROM nsa_search_index \n                        WHERE description " . implode(" AND description ", $terms) . "  \n                        \n                        UNION\n                        \n                        SELECT search_index_id, 1 as field_weight, weight_of_type, weight_within_type, url, title, description, related_links_html \n                        FROM nsa_search_index \n                        WHERE full_text " . implode(" AND full_text ", $terms) . "  \n                    )\n                    AS unsorted_results\n                    GROUP BY search_index_id\n                    ORDER BY SUM(field_weight) DESC, SUM(weight_of_type) DESC, SUM(weight_within_type) DESC\n                ) \n                AS weighted_results\n                ORDER BY weight DESC";
     # Get the total results without paging
     $total = $this->connection->query("SELECT COUNT(*) AS total FROM ({$sql}) AS total");
     $row = $total->fetch();
     $this->total = $row->total;
     # Add paging and get the data
     if ($query->GetFirstResult() && $query->GetPageSize()) {
         $sql .= " LIMIT " . Sql::ProtectNumeric($query->GetFirstResult() - 1, false, false) . "," . Sql::ProtectNumeric($query->GetPageSize(), false, false);
     }
     $query_results = $this->connection->query($sql);
     require_once "search/search-item.class.php";
     $search_results = array();
     while ($row = $query_results->fetch()) {
         $result = new SearchItem();
         $result->Url($row->url);
         $result->Title($row->title);
         $result->Description($row->description);
         $result->RelatedLinksHtml($row->related_links_html);
         $result->WeightOfMatchedField($row->field_weight);
         $result->WeightOfType($row->weight_of_type);
         $result->WeightWithinType($row->weight_within_type);
         $result->Weight($row->weight);
         $search_results[] = $result;
     }
     return $search_results;
 }
 public function __construct(SearchItem $item)
 {
     if (!$item->Description()) {
         $item->Description($item->FullText());
     }
     $description = strip_tags(trim($item->Description()));
     $break = strpos($description, "\n");
     if ($break !== false and $break > 0) {
         $description = substr($description, 0, $break - 1);
     }
     $item->Description($description);
     # Assign more weight to newer articles
     $weight = intval($item->ContentDate()->format('U')) / 60 / 60 / 24 / 365;
     $item->WeightWithinType($weight);
     $this->searchable = $item;
 }
Example #3
0
     if ($db->num_rows($result)) {
         list($id_list) = $db->fetch_row($result);
         $sortby = 'cache';
     }
 }
 $results = array();
 $plain = false;
 switch ($sortby) {
     case 'relevance':
         //sort by relevance
         $result = $db->query('SELECT p.content,p.id AS post_id,p.posted,i.locations,i.word FROM `#^search_index` AS i LEFT JOIN `#^posts` AS p ON p.id=i.post_id LEFT JOIN `#^topics` AS t ON t.id=p.topic_id LEFT JOIN `#^users` AS u ON u.id=p.poster WHERE i.word IN(' . implode(',', $terms) . ') AND ' . implode(' AND ', $addl_where)) or enhanced_error('Failed to get search information', true);
         while ($match = $db->fetch_assoc($result)) {
             if (isset($results[$match['post_id']])) {
                 $results[$match['post_id']]->addKeyword($match['word'], $match['locations']);
             } else {
                 $item = new SearchItem($match['content'], $match['posted'], $match['post_id']);
                 $item->addKeyword($match['word'], $match['locations']);
                 $results[$match['post_id']] = $item;
             }
         }
         usort($results, function ($m1, $m2) {
             return $m1->compareTo($m2);
         });
         if (!isset($_GET['direction']) || $_GET['direction'] == 'desc') {
             $results = array_reverse($results);
         }
         break;
     case 'posttime':
         $plain = true;
         $order = 'p.posted';
         break;
 private function IndexOtherPages()
 {
     require_once "search/search-item.class.php";
     $this->SearchIndexer()->DeleteFromIndexByType("other");
     $docs = array();
     $contact = new SearchItem("other", "/contact/", "/contact/", "Contact us", "Phone or email us about anything to do with stoolball, or contact us on Facebook or Twitter.", "phone email twitter facebook contact address");
     $contact->RelatedLinksHtml('<ul><li><a href="https://facebook.com/stoolball">Stoolball England on Facebook</a></li><li><a href="https://twitter.com/stoolball">Stoolball England on Twitter</a></li></ul>');
     $docs[] = $contact;
     $docs[] = new SearchItem("other", "/teams/map", "/teams/map", "Map of stoolball teams", "See a map of all the stoolball teams currently playing.", "where clubs teams map");
     $docs[] = new SearchItem("other", "/tournaments", "/tournaments/all", "Go to a tournament", "See all the stoolball tournaments taking place in the next year.", "where map events");
     $docs[] = new SearchItem("other", "/play/yourteam.php", "/play/yourteam.php", "Tell us about your team", "Ask us to add your stoolball team if it's not listed, or update your page if it is.", "club listing list");
     $docs[] = new SearchItem("other", "/you/emails.php", "/you/emails.php", "Email alerts", "Change the email alerts you get when someone adds a comment.", "spam unsubscribe");
     $docs[] = new SearchItem("other", "/you/essential.php", "/you/essential.php", "Essential information", "Change your name, email address or password.");
     $docs[] = new SearchItem("other", "/you/personal.php", "/you/personal.php", "More about you", "Tell others where you're from, who you are and what you like.");
     $docs[] = new SearchItem("other", "play/statistics/individual-scores", "/play/statistics/individual-scores", "Highest individual scores", "See the highest scores by individuals in a single stoolball innings. Filter by team, ground, date and more.", "hundreds centuries batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-runs", "/play/statistics/most-runs", "Most runs", "Find out who has scored the most runs overall in all stoolball matches. Filter by team, ground, date and more.", "hundreds centuries batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-scores-of-100", "/play/statistics/most-scores-of-100", "Most scores of 100 or more", "Find out who has scored the most hundreds in all stoolball matches. Filter by team, ground, date and more.", "fifties hundreds centuries batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-scores-of-50", "/play/statistics/most-scores-of-50", "Most scores of 50 or more", "Find out who has scored the most fifties in all stoolball matches. Filter by team, ground, date and more.", "fifties hundreds centuries batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/batting-average", "/play/statistics/batting-average", "Batting averages statistics", "A batsman's average measures how many runs he or she typically scores before getting out. Filter by team, ground, date and more.", "average batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/batting-strike-rate", "/play/statistics/batting-strike-rate", "Batting strike rates", "A batsman's strike rate measures how many runs he or she typically scores per 100 balls faced. Filter by team, ground, date and more.", "strike rate batting batsmen batters statistics");
     $docs[] = new SearchItem("other", "/play/statistics/bowling-performances", "/play/statistics/bowling-performances", "All bowling performances", "See the best wicket-taking performances in all stoolball matches. Filter by team, ground, date and more.", "bowling bowler figures statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-wickets", "/play/statistics/most-wickets", "Most wickets", "If a player is out caught, caught and bowled, bowled, body before wicket or for hitting the ball twice the wicket is credited to the bowler. Filter by team, ground, date and more.", "wickets bowling figures bowler statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-5-wickets", "/play/statistics/most-5-wickets", "Most times taking 5 wickets in an innings", "If a player is out caught, caught and bowled, bowled, body before wicket or for hitting the ball twice the wicket is credited to the bowler. Filter by team, ground, date and more.", "five wicket wickets haul bowling figures bowler statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-wickets-by-bowler-and-catcher", "/play/statistics/most-wickets-by-bowler-and-catcher", "Most wickets by a bowling and catching combination", "This measures which combination of bowler and catcher has taken the most wickets. Catches taken by a bowler off their own bowling are not counted. Filter by team, ground, date and more.", "wickets bowling figures bowler statistics catching catcher");
     $docs[] = new SearchItem("other", "/play/statistics/bowling-average", "/play/statistics/bowling-average", "Bowling averages", "A bowler's average measures how many runs he or she typically concedes before taking a wicket. Filter by team, ground, date and more.", "average bowling figures bowler statistics");
     $docs[] = new SearchItem("other", "/play/statistics/economy-rate", "/play/statistics/economy-rate", "Economy rates", "A bowler's economy rate measures how many runs he or she typically concedes in each over. Filter by team, ground, date and more.", "economy bowling figures bowler statistics");
     $docs[] = new SearchItem("other", "/play/statistics/bowling-strike-rate", "/play/statistics/bowling-strike-rate", "Bowling strike rates", "A bowler's strike rate measures how many deliveries he or she typically bowls before taking a wicket. Filter by team, ground, date and more.", "economy wickets bowling figures bowler statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-catches", "/play/statistics/most-catches", "Most catches", "This measures the number of catches taken by a fielder, not how often a batsman has been caught out. Filter by team, ground, date and more.", "fielding catching catches statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-catches-in-innings", "/play/statistics/most-catches-in-innings", "Most catches in an innings", "This measures the number of catches taken by a fielder, not how often a batsman has been caught out. Filter by team, ground, date and more.", "fielding catching catches statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-run-outs", "/play/statistics/most-run-outs", "Most run-outs", "This measures the number of run-outs completed by a fielder, not how often a batsman has been run-out. Filter by team, ground, date and more.", "run outs runouts run-outs fielding statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-run-outs-in-innings", "/play/statistics/most-run-outs-in-innings", "Most run-outs in an innings", "This measures the number of run-outs completed by a fielder, not how often a batsman has been run-out. Filter by team, ground, date and more.", "run outs runouts run-outs fielding statistics");
     $docs[] = new SearchItem("other", "/play/statistics/most-player-of-match", "/play/statistics/most-player-of-match", "Most player of the match nominations", "Find out who has won the most player of the match awards for their outstanding performances on the pitch. Filter by team, ground, date and more.", "man of the match mom awards batting bowling fielding catches statistics tables");
     $docs[] = new SearchItem("other", "/play/statistics/player-of-match", "/play/statistics/player-of-match", "Player of the match nominations", "All of the matches where players were awarded player of the match for their outstanding performances on the pitch. Filter by team, ground, date and more.", "man of the match mom awards batting bowling fielding catches statistics tables");
     $docs[] = new SearchItem("other", "/play/statistics/player-performances", "/play/statistics/player-performances", "Player performances", "All of the match performances by a stoolball player, summarising their batting, bowling and fielding in the match. Filter by team, ground, date and more.", "batting bowling fielding catches statistics tables");
     $docs[] = new SearchItem("other", "spreadshirt", "https://shop.spreadshirt.co.uk/stoolball", "Gift shop - hoodies, t-shirts, hats, bags and more", "Buy hoodies, t-shirts, hats, bags, umbrellas, teddy bears and a lot more in our stoolball gift shop. Like us on Facebook or follow us on Twitter to find out about special offers.", "gifts presents clothing buy bags t-shirts polo hats merchandise wear clothes shopping shop");
     $facebook = new SearchItem("other", "facebook", "https://facebook.com/stoolball", "Stoolball England on Facebook", "Find us on Facebook to keep up with stoolball news, and get extra updates and special offers from our gift shop.", "twitter news like photo picture contact");
     $facebook->RelatedLinksHtml('<ul><li><a href="https://twitter.com/stoolball">Stoolball England on Twitter</a></li><li><a href="https://youtube.com/stoolballengland">Stoolball England on YouTube</a></li></ul>');
     $docs[] = $facebook;
     $twitter = new SearchItem("other", "twitter", "https://twitter.com/stoolball", "Stoolball England on Twitter", "Follow us on Twitter to keep up with stoolball news, and get extra updates and special offers from our gift shop.", "facebook follow tweet contact");
     $twitter->RelatedLinksHtml('<ul><li><a href="https://facebook.com/stoolball">Stoolball England on Facebook</a></li><li><a href="https://youtube.com/stoolballengland">Stoolball England on YouTube</a></li></ul>');
     $docs[] = $twitter;
     $youtube = new SearchItem("other", "youtube", "https://youtube.com/stoolballengland", "Stoolball England on YouTube", "Subscribe to our YouTube channel to see the best stoolball videos.", "video youtube");
     $youtube->RelatedLinksHtml('<ul><li><a href="https://facebook.com/stoolball">Stoolball England on Facebook</a></li><li><a href="https://twitter.com/stoolball">Stoolball England on Twitter</a></li></ul>');
     $docs[] = $youtube;
     foreach ($docs as $doc) {
         /* @var $doc SearchItem */
         $doc->WeightOfType(1200);
         $this->SearchIndexer()->Index($doc);
     }
     $this->SearchIndexer()->CommitChanges();
 }
 /**
  * Queues the item to be added to the index when CommitChanges is called
  */
 public function Index(SearchItem $item)
 {
     $this->index_queue[] = "INSERT INTO nsa_search_index SET \n            search_index_id = " . Sql::ProtectString($this->connection, $item->SearchItemId()) . ",\n            indexed_item_type = " . Sql::ProtectString($this->connection, $item->SearchItemType()) . ",\n            url = " . Sql::ProtectString($this->connection, $item->Url()) . ",\n            title = " . Sql::ProtectString($this->connection, $item->Title()) . ",\n            keywords = " . Sql::ProtectString($this->connection, $item->Keywords()) . ",\n            description = " . Sql::ProtectString($this->connection, $item->Description()) . ",\n            related_links_html = " . Sql::ProtectString($this->connection, $item->RelatedLinksHtml()) . ",\n            full_text = " . Sql::ProtectString($this->connection, $item->FullText()) . ",\n            weight_of_type = " . Sql::ProtectNumeric($item->WeightOfType(), false, false) . ",\n            weight_within_type = " . Sql::ProtectNumeric($item->WeightWithinType(), false, false);
 }
 /**
  * Add a WordPress post to search results when it is published
  * @param $post_id int
  */
 public function PublishPost($post_id)
 {
     $post = get_post($post_id);
     require_once "search/mysql-search-indexer.class.php";
     require_once "search/blog-post-search-adapter.class.php";
     require_once "search/search-item.class.php";
     require_once "data/mysql-connection.class.php";
     $search = new MySqlSearchIndexer(new MySqlConnection($this->settings->DatabaseHost(), $this->settings->DatabaseUser(), $this->settings->DatabasePassword(), $this->settings->DatabaseName()));
     $search->DeleteFromIndexById("post" . $post_id);
     $item = new SearchItem("post", $post_id, get_permalink($post_id), $post->post_title);
     $item->ContentDate(new DateTime($post->post_date));
     $item->FullText($post->post_content);
     $adapter = new BlogPostSearchAdapter($item);
     $search->Index($adapter->GetSearchableItem());
     $search->CommitChanges();
 }