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

noticesearch.php 6.14 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 8 9 10
/**
 * Notice search action class.
 *
 * PHP version 5
 *
 * @category Action
 * @package  Laconica
 * @author   Evan Prodromou <evan@controlyourself.ca>
 * @author   Robin Millette <millette@controlyourself.ca>
sarven's avatar
sarven committed
11
 * @author   Sarven Capadisli <csarven@controlyourself.ca>
Robin Millette's avatar
Robin Millette committed
12 13 14
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://laconi.ca/
 *
Evan Prodromou's avatar
Evan Prodromou committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
 *
 * 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/>.
 */

Robin Millette's avatar
Robin Millette committed
32 33 34
if (!defined('LACONICA')) {
    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 42 43 44 45 46 47 48
/**
 * Notice search action class.
 *
 * @category Action
 * @package  Laconica
 * @author   Evan Prodromou <evan@controlyourself.ca>
 * @author   Robin Millette <millette@controlyourself.ca>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://laconi.ca/
 * @todo     common parent for people and content search?
 */
49 50
class NoticesearchAction extends SearchAction
{
51 52 53 54 55 56 57 58 59

    function prepare($args)
    {
        parent::prepare($args);

        common_set_returnto($this->selfUrl());

        return true;
    }
60

Robin Millette's avatar
Robin Millette committed
61 62
    /**
     * Get instructions
63 64
     *
     * @return string instruction text
Robin Millette's avatar
Robin Millette committed
65 66
     */
    function getInstructions()
67
    {
68 69
        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
70

Robin Millette's avatar
Robin Millette committed
71 72
    /**
     * Get title
73
     *
Robin Millette's avatar
Robin Millette committed
74 75 76
     * @return string title
     */
    function title()
77
    {
78 79
        return _('Text search');
    }
Mike Cochrane's avatar
Mike Cochrane committed
80

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

        if (!$q) {
86
            return null;
87 88
        }

89 90 91 92
        return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
                                                           array('q' => $q)),
                              sprintf(_('Search results for "%s" on %s'),
                                      $q, common_config('site', 'name'))));
93 94
    }

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

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

            $this->searchSuggestions($q);
            if (common_logged_in()) {
                $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
            }
            else {
                $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));
            }

127
            $this->elementStart('div', 'guide');
128 129
            $this->raw(common_markup_to_html($message));
            $this->elementEnd('div');
130
            return;
131
        }
132 133 134
        $terms = preg_split('/[\s,]+/', $q);
        $nl = new SearchNoticeList($notice, $this, $terms);
        $cnt = $nl->show();
135 136
        $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE,
                          $page, 'noticesearch', array('q' => $q));
137
    }
138
}
139

140 141
class SearchNoticeList extends NoticeList {
    function __construct($notice, $out=null, $terms)
142
    {
143 144 145
        parent::__construct($notice, $out);
        $this->terms = $terms;
    }
sarven's avatar
sarven committed
146

147 148 149 150 151
    function newListItem($notice)
    {
        return new SearchNoticeListItem($notice, $this->out, $this->terms);
    }
}
sarven's avatar
sarven committed
152

153 154 155 156 157 158 159 160 161
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
162
        // FIXME: URL, image, video, audio
163 164 165
        $this->out->elementStart('p', array('class' => 'entry-content'));
        if ($this->notice->rendered) {
            $this->out->raw($this->highlight($this->notice->rendered, $this->terms));
166
        } else {
Robin Millette's avatar
Robin Millette committed
167 168 169
            // XXX: may be some uncooked notices in the DB,
            // we cook them right now. This should probably disappear in future
            // versions (>> 0.4.x)
170
            $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms));
171
        }
172
        $this->out->elementEnd('p');
sarven's avatar
sarven committed
173

174 175
    }

Robin Millette's avatar
Robin Millette committed
176 177 178 179 180 181 182 183
    /**
     * Highlist query terms
     *
     * @param string $text  notice text
     * @param array  $terms terms to highlight
     *
     * @return void
     */
184 185
    function highlight($text, $terms)
    {
186
        /* Highligh search terms */
Adrian Lang's avatar
Adrian Lang committed
187 188 189
        $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
190
        $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
191 192

        /* Remove highlighting from inside links, loop incase multiple highlights in links */
Adrian Lang's avatar
Adrian Lang committed
193
        $pattern = '/(href="[^"]*)<strong>('.$options.')<\/strong>([^"]*")/iU';
194 195 196 197 198
        do {
            $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
        } while ($count);
        return $result;
    }
Evan Prodromou's avatar
Evan Prodromou committed
199
}
Robin Millette's avatar
Robin Millette committed
200