]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/peoplesearch.php
6d3f6a73f48b72d28cdf66af54697466d0a9b0d4
[quix0rs-gnu-social.git] / actions / peoplesearch.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23 require_once(INSTALLDIR.'/lib/profilelist.php');
24
25 class PeoplesearchAction extends SearchAction {
26
27     function get_instructions()
28     {
29         return _('Search for people on %%site.name%% by their name, location, or interests. ' .
30                   'Separate the terms by spaces; they must be 3 characters or more.');
31     }
32
33     function get_title()
34     {
35         return _('People search');
36     }
37
38     function show_results($q, $page)
39     {
40
41         $profile = new Profile();
42
43         # lcase it for comparison
44         $q = strtolower($q);
45
46         $search_engine = $profile->getSearchEngine('identica_people');
47
48         $search_engine->set_sort_mode('chron');
49         # Ask for an extra to see if there's more.
50         $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
51         if (false === $search_engine->query($q)) {
52             $cnt = 0;
53         }
54         else {
55             $cnt = $profile->find();
56         }
57         if ($cnt > 0) {
58             $terms = preg_split('/[\s,]+/', $q);
59             $results = new PeopleSearchResults($profile, $terms);
60             $results->show_list();
61         } else {
62             common_element('p', 'error', _('No results'));
63         }
64
65         $profile->free();
66         
67         common_pagination($page > 1, $cnt > PROFILES_PER_PAGE,
68                           $page, 'peoplesearch', array('q' => $q));
69     }
70 }
71
72 class PeopleSearchResults extends ProfileList {
73
74     var $terms = null;
75     var $pattern = null;
76     
77     function __construct($profile, $terms)
78     {
79         parent::__construct($profile);
80         $this->terms = array_map('preg_quote', 
81                                  array_map('htmlspecialchars', $terms));
82         $this->pattern = '/('.implode('|',$terms).')/i';
83     }
84     
85     function highlight($text)
86     {
87         return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
88     }
89 }