2011-03-16 01:45:08 +00:00
|
|
|
<?php
|
2019-09-11 08:12:49 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-03-16 01:45:08 +00:00
|
|
|
/**
|
|
|
|
* Action for showing Twitter-like JSON search results
|
|
|
|
*
|
|
|
|
* @category Search
|
2019-09-11 08:12:49 +01:00
|
|
|
* @package GNUsocial
|
2011-03-16 01:45:08 +00:00
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
2019-09-11 08:12:49 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2011-03-16 01:45:08 +00:00
|
|
|
*/
|
|
|
|
|
2019-09-11 08:12:49 +01:00
|
|
|
defined('GNUSOCIAL') || die();
|
2011-03-16 01:45:08 +00:00
|
|
|
|
|
|
|
class UserautocompleteAction extends Action
|
|
|
|
{
|
2019-08-09 15:22:55 +01:00
|
|
|
public $query;
|
2011-03-16 01:45:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization.
|
|
|
|
*
|
|
|
|
* @param array $args Web and URL arguments
|
|
|
|
*
|
|
|
|
* @return boolean true if nothing goes wrong
|
2019-08-09 15:22:55 +01:00
|
|
|
* @throws ClientException
|
2011-03-16 01:45:08 +00:00
|
|
|
*/
|
2019-09-11 08:12:49 +01:00
|
|
|
public function prepare(array $args = [])
|
2011-03-16 01:45:08 +00:00
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
$this->query = $this->trimmed('term');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-08-09 15:22:55 +01:00
|
|
|
public function handle()
|
2011-03-16 01:45:08 +00:00
|
|
|
{
|
2016-06-01 03:05:11 +01:00
|
|
|
parent::handle();
|
2011-03-16 01:45:08 +00:00
|
|
|
$this->showResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for users matching the query and spit the results out
|
|
|
|
* as a quick-n-dirty JSON document
|
|
|
|
*
|
|
|
|
* @return void
|
2019-08-09 15:22:55 +01:00
|
|
|
* @throws ServerException
|
2011-03-16 01:45:08 +00:00
|
|
|
*/
|
2019-08-09 15:22:55 +01:00
|
|
|
public function showResults()
|
2011-03-16 01:45:08 +00:00
|
|
|
{
|
|
|
|
$people = array();
|
|
|
|
|
|
|
|
$profile = new Profile();
|
|
|
|
|
|
|
|
$search_engine = $profile->getSearchEngine('profile');
|
|
|
|
$search_engine->set_sort_mode('nickname_desc');
|
|
|
|
$search_engine->limit(0, 10);
|
|
|
|
$search_engine->query(strtolower($this->query . '*'));
|
|
|
|
|
|
|
|
$cnt = $profile->find();
|
|
|
|
|
|
|
|
if ($cnt > 0) {
|
|
|
|
$sql = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id '
|
|
|
|
. ' AND LEFT(LOWER(profile.nickname), '
|
|
|
|
. strlen($this->query)
|
|
|
|
. ') = \'%s\' '
|
2019-09-11 08:12:49 +01:00
|
|
|
. ' LIMIT 10';
|
2011-03-16 01:45:08 +00:00
|
|
|
|
|
|
|
$profile->query(sprintf($sql, $this->query));
|
|
|
|
}
|
2019-08-09 15:22:55 +01:00
|
|
|
|
2011-03-16 01:45:08 +00:00
|
|
|
while ($profile->fetch()) {
|
2019-08-09 15:22:55 +01:00
|
|
|
$people[] = $profile->nickname;
|
2011-03-16 01:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
print json_encode($people);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we need to write to the database?
|
|
|
|
*
|
2019-08-09 15:22:55 +01:00
|
|
|
* @param $args
|
2011-03-16 01:45:08 +00:00
|
|
|
* @return boolean true
|
|
|
|
*/
|
2019-08-09 15:22:55 +01:00
|
|
|
public function isReadOnly($args)
|
2011-03-16 01:45:08 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|