noticesearch.php 5.55 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 106
        $notice        = new Notice();
        $q             = strtolower($q);
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
            $this->element('p', 'error', _('No results'));
118
            return;
119
        }
120 121 122 123
        $terms = preg_split('/[\s,]+/', $q);
        $nl = new SearchNoticeList($notice, $this, $terms);

        $cnt = $nl->show();
124

125 126
        $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
                          $this->page, 'noticesearch', array('q' => $q));
127
    }
128 129 130 131 132
    function isReadOnly()
    {
        return true;
    }
}
133

134 135
class SearchNoticeList extends NoticeList {
    function __construct($notice, $out=null, $terms)
136
    {
137 138 139
        parent::__construct($notice, $out);
        $this->terms = $terms;
    }
sarven's avatar
sarven committed
140

141 142 143 144 145
    function newListItem($notice)
    {
        return new SearchNoticeListItem($notice, $this->out, $this->terms);
    }
}
sarven's avatar
sarven committed
146

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

168 169
    }

Robin Millette's avatar
Robin Millette committed
170 171 172 173 174 175 176 177
    /**
     * Highlist query terms
     *
     * @param string $text  notice text
     * @param array  $terms terms to highlight
     *
     * @return void
     */
178 179
    function highlight($text, $terms)
    {
180
        /* Highligh search terms */
Robin Millette's avatar
Robin Millette committed
181 182
        $pattern = '/('.implode('|', array_map('htmlspecialchars', $terms)).')/i';
        $result  = preg_replace($pattern, '<strong>\\1</strong>', $text);
183 184

        /* Remove highlighting from inside links, loop incase multiple highlights in links */
Robin Millette's avatar
Robin Millette committed
185
        $pattern = '/(href="[^"]*)<strong>('.implode('|', array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
186 187 188 189 190
        do {
            $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
        } while ($count);
        return $result;
    }
Evan Prodromou's avatar
Evan Prodromou committed
191
}
Robin Millette's avatar
Robin Millette committed
192