noticesearch.php 6.96 KB
Newer Older
1
<?php
Robin Millette's avatar
Robin Millette committed
2 3 4 5 6 7
/**
 * Notice search action class.
 *
 * PHP version 5
 *
 * @category Action
8
 * @package  StatusNet
9 10 11
 * @author   Evan Prodromou <evan@status.net>
 * @author   Robin Millette <millette@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
Robin Millette's avatar
Robin Millette committed
12
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13
 * @link     http://status.net/
Robin Millette's avatar
Robin Millette committed
14
 *
15
 * StatusNet - the distributed open-source microblogging tool
16
 * Copyright (C) 2008, 2009, StatusNet, Inc.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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/>.
 */

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

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

Robin Millette's avatar
Robin Millette committed
38 39 40 41
/**
 * Notice 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
 * @todo     common parent for people and content search?
 */
49 50
class NoticesearchAction extends SearchAction
{
51 52 53 54 55 56 57 58
    function prepare($args)
    {
        parent::prepare($args);

        common_set_returnto($this->selfUrl());

        return true;
    }
59

Robin Millette's avatar
Robin Millette committed
60 61
    /**
     * Get instructions
62 63
     *
     * @return string instruction text
Robin Millette's avatar
Robin Millette committed
64 65
     */
    function getInstructions()
66
    {
67 68
        // TRANS: Instructions for Notice search page.
        // TRANS: %%site.name%% is the name of the StatusNet site.
69 70
        return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
    }
Mike Cochrane's avatar
Mike Cochrane committed
71

Robin Millette's avatar
Robin Millette committed
72 73
    /**
     * Get title
74
     *
Robin Millette's avatar
Robin Millette committed
75 76 77
     * @return string title
     */
    function title()
78
    {
79
        // TRANS: Title of the page where users can search for notices.
80 81
        return _('Text search');
    }
Mike Cochrane's avatar
Mike Cochrane committed
82

83
    function getFeeds()
84 85 86 87
    {
        $q = $this->trimmed('q');

        if (!$q) {
88
            return null;
89 90
        }

91 92
        return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
                                                           array('q' => $q)),
93 94
                              // TRANS: Test in RSS notice search.
                              // TRANS: %1$s is the query, %2$s is the StatusNet site name.
95
                              sprintf(_('Search results for "%1$s" on %2$s'),
96
                                      $q, common_config('site', 'name'))));
97 98
    }

Robin Millette's avatar
Robin Millette committed
99 100 101 102 103 104 105 106 107
    /**
     * Show results
     *
     * @param string  $q    search query
     * @param integer $page page number
     *
     * @return void
     */
    function showResults($q, $page)
108
    {
Robin Millette's avatar
Robin Millette committed
109
        $notice        = new Notice();
110

111
        $search_engine = $notice->getSearchEngine('notice');
112
        $search_engine->set_sort_mode('chron');
Robin Millette's avatar
Robin Millette committed
113
        // Ask for an extra to see if there's more.
114
        $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
115 116
        if (false === $search_engine->query($q)) {
            $cnt = 0;
Robin Millette's avatar
Robin Millette committed
117
        } else {
118
            $cnt = $notice->find();
119
        }
120
        if ($cnt === 0) {
121
            // TRANS: Text for notice search results is the query had no results.
122 123 124 125
            $this->element('p', 'error', _('No results.'));

            $this->searchSuggestions($q);
            if (common_logged_in()) {
126 127
                // TRANS: Text for logged in users making a query for notices without results.
                // TRANS: This message contains a Markdown link.
128 129 130
                $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
            }
            else {
131 132
                // TRANS: Text for not logged in users making a query for notices without results.
                // TRANS: This message contains Markdown links.
133
                $message = sprintf(_('Why not [register an account](%%%%action.register%%%%) and be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
134 135
            }

136
            $this->elementStart('div', 'guide');
137 138
            $this->raw(common_markup_to_html($message));
            $this->elementEnd('div');
139
            return;
140
        }
141 142 143
        $terms = preg_split('/[\s,]+/', $q);
        $nl = new SearchNoticeList($notice, $this, $terms);
        $cnt = $nl->show();
144 145
        $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
                          $page, 'noticesearch', array('q' => $q));
146
    }
147 148 149 150 151 152

    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('q');
    }
153
}
154

155 156
class SearchNoticeList extends NoticeList {
    function __construct($notice, $out=null, $terms)
157
    {
158 159 160
        parent::__construct($notice, $out);
        $this->terms = $terms;
    }
sarven's avatar
sarven committed
161

162 163 164 165 166
    function newListItem($notice)
    {
        return new SearchNoticeListItem($notice, $this->out, $this->terms);
    }
}
sarven's avatar
sarven committed
167

168 169 170 171 172 173 174 175 176
class SearchNoticeListItem extends NoticeListItem {
    function __construct($notice, $out=null, $terms)
    {
        parent::__construct($notice, $out);
        $this->terms = $terms;
    }

    function showContent()
    {
Robin Millette's avatar
Robin Millette committed
177
        // FIXME: URL, image, video, audio
178 179 180
        $this->out->elementStart('p', array('class' => 'entry-content'));
        if ($this->notice->rendered) {
            $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
181
        } else {
Robin Millette's avatar
Robin Millette committed
182 183 184
            // XXX: may be some uncooked notices in the DB,
            // we cook them right now. This should probably disappear in future
            // versions (>> 0.4.x)
185
            $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
186
        }
187
        $this->out->elementEnd('p');
sarven's avatar
sarven committed
188

189 190
    }

Robin Millette's avatar
Robin Millette committed
191 192 193 194 195 196 197 198
    /**
     * Highlist query terms
     *
     * @param string $text  notice text
     * @param array  $terms terms to highlight
     *
     * @return void
     */
199 200
    function highlight($text, $terms)
    {
201
        /* Highligh search terms */
Adrian Lang's avatar
Adrian Lang committed
202 203 204
        $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
                                                            array_fill(0, sizeof($terms), '/')));
        $pattern = "/($options)/i";
Robin Millette's avatar
Robin Millette committed
205
        $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
206 207

        /* Remove highlighting from inside links, loop incase multiple highlights in links */
208
        $pattern = '/(\w+="[^"]*)<strong>('.$options.')<\/strong>([^"]*")/iU';
209 210 211 212 213
        do {
            $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
        } while ($count);
        return $result;
    }
214
}