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

noticesearch.php 7.6 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
/**
 * 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.
Evan Prodromou's avatar
Evan Prodromou committed
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);
}
Evan Prodromou's avatar
Evan Prodromou committed
35

Robin Millette's avatar
Robin Millette committed
36
require_once INSTALLDIR.'/lib/searchaction.php';
Evan Prodromou's avatar
Evan Prodromou committed
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
    protected $q = null;

53 54 55 56
    function prepare($args)
    {
        parent::prepare($args);

57 58 59 60 61 62 63 64 65
        $this->q = $this->trimmed('q');

        // FIXME: very dependent on tag format
        if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) {
            common_redirect(common_local_url('tag',
                                             array('tag' => common_canonical_tag(substr($this->q, 1)))),
                            303);
        }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        if (!empty($this->q)) {

            $profile = Profile::current();
            $stream  = new SearchNoticeStream($this->q, $profile);
            $page    = $this->trimmed('page');

            if (empty($page)) {
                $page = 1;
            } else {
                $page = (int)$page;
            }

            $this->notice = $stream->getNotices((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
        }

81 82 83 84
        common_set_returnto($this->selfUrl());

        return true;
    }
85

Robin Millette's avatar
Robin Millette committed
86 87
    /**
     * Get instructions
88 89
     *
     * @return string instruction text
Robin Millette's avatar
Robin Millette committed
90 91
     */
    function getInstructions()
92
    {
93 94
        // TRANS: Instructions for Notice search page.
        // TRANS: %%site.name%% is the name of the StatusNet site.
95 96
        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
97

Robin Millette's avatar
Robin Millette committed
98 99
    /**
     * Get title
100
     *
Robin Millette's avatar
Robin Millette committed
101 102 103
     * @return string title
     */
    function title()
104
    {
105
        // TRANS: Title of the page where users can search for notices.
106 107
        return _('Text search');
    }
Mike Cochrane's avatar
Mike Cochrane committed
108

109
    function getFeeds()
110 111 112 113
    {
        $q = $this->trimmed('q');

        if (!$q) {
114
            return null;
115 116
        }

117 118
        return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
                                                           array('q' => $q)),
119 120
                              // TRANS: Test in RSS notice search.
                              // TRANS: %1$s is the query, %2$s is the StatusNet site name.
121
                              sprintf(_('Search results for "%1$s" on %2$s'),
122
                                      $q, common_config('site', 'name'))));
123 124
    }

Robin Millette's avatar
Robin Millette committed
125 126 127 128 129 130 131 132 133
    /**
     * Show results
     *
     * @param string  $q    search query
     * @param integer $page page number
     *
     * @return void
     */
    function showResults($q, $page)
134
    {
135
        if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $this->notice))) {
136 137 138 139 140 141 142 143 144 145 146 147
            if ($this->notice->N === 0) {
                $this->showEmptyResults($q, $page);
            } else {
                $terms = preg_split('/[\s,]+/', $q);
                $nl = new SearchNoticeList($this->notice, $this, $terms);
                $cnt = $nl->show();
                $this->pagination($page > 1,
                                  $cnt > NOTICES_PER_PAGE,
                                  $page,
                                  'noticesearch',
                                  array('q' => $q));
            }
148 149 150 151 152 153
            Event::handle('EndNoticeSearchShowResults', array($this, $q, $this->notice));
        }
    }

    function showEmptyResults($q, $page)
    {
154
            // TRANS: Text for notice search results is the query had no results.
155 156 157 158
            $this->element('p', 'error', _('No results.'));

            $this->searchSuggestions($q);
            if (common_logged_in()) {
159 160
                // TRANS: Text for logged in users making a query for notices without results.
                // TRANS: This message contains a Markdown link.
161 162 163
                $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
            }
            else {
164 165
                // TRANS: Text for not logged in users making a query for notices without results.
                // TRANS: This message contains Markdown links.
166
                $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));
167 168
            }

169
            $this->elementStart('div', 'guide');
170 171
            $this->raw(common_markup_to_html($message));
            $this->elementEnd('div');
172
            return;
173
    }
174 175 176 177 178 179

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

182 183
class SearchNoticeList extends NoticeList {
    function __construct($notice, $out=null, $terms)
184
    {
185 186 187
        parent::__construct($notice, $out);
        $this->terms = $terms;
    }
sarven's avatar
sarven committed
188

189 190 191 192 193
    function newListItem($notice)
    {
        return new SearchNoticeListItem($notice, $this->out, $this->terms);
    }
}
sarven's avatar
sarven committed
194

195 196 197 198 199 200 201 202 203
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
204
        // FIXME: URL, image, video, audio
mattl's avatar
mattl committed
205
        $this->out->elementStart('p', array('class' => 'e-content'));
206
        $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms));
207
        $this->out->elementEnd('p');
sarven's avatar
sarven committed
208

209 210
    }

Robin Millette's avatar
Robin Millette committed
211 212 213 214 215 216 217 218
    /**
     * Highlist query terms
     *
     * @param string $text  notice text
     * @param array  $terms terms to highlight
     *
     * @return void
     */
219 220
    function highlight($text, $terms)
    {
221
        /* Highligh search terms */
Adrian Lang's avatar
Adrian Lang committed
222 223 224
        $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
                                                            array_fill(0, sizeof($terms), '/')));
        $pattern = "/($options)/i";
225 226 227 228 229 230 231 232 233 234 235 236 237
        $result = '';

        /* Divide up into text (highlight me) and tags (don't touch) */
        $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($chunks as $i => $chunk) {
            if ($i % 2 == 1) {
                // odd: delimiter (tag)
                $result .= $chunk;
            } else {
                // even: freetext between tags
                $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);
            }
        }
238 239 240

        return $result;
    }
Evan Prodromou's avatar
Evan Prodromou committed
241
}