apisearchjson.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 GNUsocial
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2008-2010 StatusNet, Inc.
  26. * @copyright 2013 Free Software Foundation, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://www.gnu.org/software/social/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Action handler for Twitter-compatible API search
  33. *
  34. * @category Search
  35. * @package StatusNet
  36. * @author Zach Copley <zach@status.net>
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://status.net/
  39. * @see ApiAction
  40. */
  41. class ApiSearchJSONAction extends ApiPrivateAuthAction
  42. {
  43. var $query;
  44. var $lang;
  45. var $rpp;
  46. var $page;
  47. var $since_id;
  48. var $limit;
  49. var $geocode;
  50. /**
  51. * Initialization.
  52. *
  53. * @param array $args Web and URL arguments
  54. *
  55. * @return boolean true if nothing goes wrong
  56. */
  57. function prepare(array $args = array())
  58. {
  59. parent::prepare($args);
  60. $this->query = $this->trimmed('q');
  61. $this->lang = $this->trimmed('lang');
  62. $this->rpp = $this->trimmed('rpp');
  63. if (!$this->rpp) {
  64. $this->rpp = 15;
  65. }
  66. if ($this->rpp > 100) {
  67. $this->rpp = 100;
  68. }
  69. $this->page = $this->trimmed('page');
  70. if (!$this->page) {
  71. $this->page = 1;
  72. }
  73. // TODO: Suppport max_id -- we need to tweak the backend
  74. // Search classes to support it.
  75. $this->since_id = $this->trimmed('since_id');
  76. $this->geocode = $this->trimmed('geocode');
  77. return true;
  78. }
  79. /**
  80. * Handle a request
  81. *
  82. * @param array $args Arguments from $_REQUEST
  83. *
  84. * @return void
  85. */
  86. function handle()
  87. {
  88. parent::handle();
  89. $this->showResults();
  90. }
  91. /**
  92. * Show search results
  93. *
  94. * @return void
  95. */
  96. function showResults()
  97. {
  98. // TODO: Support search operators like from: and to:, boolean, etc.
  99. $notice = new Notice();
  100. $this->notices = array();
  101. $search_engine = $notice->getSearchEngine('notice');
  102. $search_engine->set_sort_mode('chron');
  103. $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1);
  104. if ($search_engine->query($this->query)) {
  105. $cnt = $notice->find();
  106. $this->notices = $notice->fetchAll();
  107. }
  108. $this->showJsonTimeline($this->notices);
  109. }
  110. /**
  111. * Do we need to write to the database?
  112. *
  113. * @return boolean true
  114. */
  115. function isReadOnly($args)
  116. {
  117. return true;
  118. }
  119. }