userautocomplete.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Action for showing Twitter-like JSON search results
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Search
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. class UserautocompleteAction extends Action
  33. {
  34. var $query;
  35. /**
  36. * Initialization.
  37. *
  38. * @param array $args Web and URL arguments
  39. *
  40. * @return boolean true if nothing goes wrong
  41. */
  42. function prepare(array $args = array())
  43. {
  44. parent::prepare($args);
  45. $this->query = $this->trimmed('term');
  46. return true;
  47. }
  48. /**
  49. * Handle a request
  50. *
  51. * @param array $args Arguments from $_REQUEST
  52. *
  53. * @return void
  54. */
  55. function handle()
  56. {
  57. parent::handle();
  58. $this->showResults();
  59. }
  60. /**
  61. * Search for users matching the query and spit the results out
  62. * as a quick-n-dirty JSON document
  63. *
  64. * @return void
  65. */
  66. function showResults()
  67. {
  68. $people = array();
  69. $profile = new Profile();
  70. $search_engine = $profile->getSearchEngine('profile');
  71. $search_engine->set_sort_mode('nickname_desc');
  72. $search_engine->limit(0, 10);
  73. $search_engine->query(strtolower($this->query . '*'));
  74. $cnt = $profile->find();
  75. if ($cnt > 0) {
  76. $sql = 'SELECT profile.* FROM profile, user WHERE profile.id = user.id '
  77. . ' AND LEFT(LOWER(profile.nickname), '
  78. . strlen($this->query)
  79. . ') = \'%s\' '
  80. . ' LIMIT 0, 10';
  81. $profile->query(sprintf($sql, $this->query));
  82. }
  83. while ($profile->fetch()) {
  84. $people[] = $profile->nickname;
  85. }
  86. header('Content-Type: application/json; charset=utf-8');
  87. print json_encode($people);
  88. }
  89. /**
  90. * Do we need to write to the database?
  91. *
  92. * @return boolean true
  93. */
  94. function isReadOnly($args)
  95. {
  96. return true;
  97. }
  98. }