We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

peoplesearch.php 4.25 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
Robin Millette's avatar
Robin Millette committed
2 3 4 5 6 7
/**
 * People search action class.
 *
 * PHP version 5
 *
 * @category Action
8
 * @package  StatusNet
9 10
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
Robin Millette's avatar
Robin Millette committed
11
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12
 * @link     http://status.net/
Robin Millette's avatar
Robin Millette committed
13
 *
14
 * StatusNet - the distributed open-source microblogging tool
15
 * Copyright (C) 2008, 2009, StatusNet, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
Robin Millette's avatar
Robin Millette committed
32 33
    exit(1);
}
Evan Prodromou's avatar
Evan Prodromou committed
34

Robin Millette's avatar
Robin Millette committed
35 36
require_once INSTALLDIR.'/lib/searchaction.php';
require_once INSTALLDIR.'/lib/profilelist.php';
Evan Prodromou's avatar
Evan Prodromou committed
37

Robin Millette's avatar
Robin Millette committed
38 39 40 41
/**
 * People search action class.
 *
 * @category Action
42
 * @package  StatusNet
43 44
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
Robin Millette's avatar
Robin Millette committed
45
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46
 * @link     http://status.net/
Robin Millette's avatar
Robin Millette committed
47
 */
48 49
class PeoplesearchAction extends SearchAction
{
Robin Millette's avatar
Robin Millette committed
50
    function getInstructions()
51
    {
52 53
        // TRANS: Instructions for the "People search" page.
        // TRANS: %%site.name%% is the name of the StatusNet site.
54 55 56
        return _('Search for people on %%site.name%% by their name, location, or interests. ' .
                  'Separate the terms by spaces; they must be 3 characters or more.');
    }
57

Robin Millette's avatar
Robin Millette committed
58
    function title()
59
    {
60
        // TRANS: Title of a page where users can search for other users.
61 62
        return _('People search');
    }
Mike Cochrane's avatar
Mike Cochrane committed
63

Robin Millette's avatar
Robin Millette committed
64
    function showResults($q, $page)
65
    {
66
        $profile = new Profile();
67
        $search_engine = $profile->getSearchEngine('profile');
68
        $search_engine->set_sort_mode('chron');
69
        // Ask for an extra to see if there's more.
millette's avatar
millette committed
70
        $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
71 72 73 74
        if (false === $search_engine->query($q)) {
            $cnt = 0;
        }
        else {
75 76 77 78
            $cnt = $profile->find();
        }
        if ($cnt > 0) {
            $terms = preg_split('/[\s,]+/', $q);
Robin Millette's avatar
Robin Millette committed
79 80
            $results = new PeopleSearchResults($profile, $terms, $this);
            $results->show();
81 82 83 84
            $profile->free();
            $this->pagination($page > 1, $cnt > PROFILES_PER_PAGE,
                          $page, 'peoplesearch', array('q' => $q));

85
        } else {
86
            // TRANS: Message on the "People search" page where a query has no results.
87
            $this->element('p', 'error', _('No results.'));
88
            $this->searchSuggestions($q);
89 90
            $profile->free();
        }
91
    }
92 93 94 95 96 97

    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('q');
    }
98
}
Mike Cochrane's avatar
Mike Cochrane committed
99

100 101 102 103 104 105
/**
 * People search results class
 *
 * Derivative of ProfileList with specialization for highlighting search terms.
 *
 * @category Widget
106
 * @package  StatusNet
107 108
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
109
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
110
 * @link     http://status.net/
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
 *
 * @see PeoplesearchAction
 */

class PeopleSearchResults extends ProfileList
{
    var $terms = null;
    var $pattern = null;

    function __construct($profile, $terms, $action)
    {
        parent::__construct($profile, $action);

        $this->terms = array_map('preg_quote',
                                 array_map('htmlspecialchars', $terms));

        $this->pattern = '/('.implode('|',$terms).')/i';
    }

    function newProfileItem($profile)
    {
        return new PeopleSearchResultItem($profile, $this->action);
    }
}

class PeopleSearchResultItem extends ProfileListItem
{
    function highlight($text)
    {
        return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
    }
}