The official, canonical postActiv repository. http://www.postactiv.com

apisearchjson.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* ============================================================================
  3. * Title: APISearchJSON
  4. * Action for showing Twitter-like JSON search results
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * Action for showing Twitter-like JSON search results
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Zach Copley
  40. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  41. * o Brion Vibber <brion@pobox.com>
  42. * o Evan Prodromou
  43. * o Mikael Nordfeldth <mmn@hethane.se>
  44. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  45. *
  46. * Web:
  47. * o postActiv <http://www.postactiv.com>
  48. * o GNU social <https://www.gnu.org/s/social/>
  49. * ============================================================================
  50. */
  51. // This file is formatted so that it provides useful documentation output in
  52. // NaturalDocs. Please be considerate of this before changing formatting.
  53. if (!defined('POSTACTIV')) { exit(1); }
  54. /**
  55. * Action handler for Twitter-compatible API search
  56. */
  57. class ApiSearchJSONAction extends ApiPrivateAuthAction
  58. {
  59. var $query;
  60. var $lang;
  61. var $rpp;
  62. var $page;
  63. var $since_id;
  64. var $limit;
  65. var $geocode;
  66. /**
  67. * Initialization.
  68. *
  69. * @param array $args Web and URL arguments
  70. *
  71. * @return boolean true if nothing goes wrong
  72. */
  73. function prepare(array $args = array())
  74. {
  75. parent::prepare($args);
  76. $this->query = $this->trimmed('q');
  77. $this->lang = $this->trimmed('lang');
  78. $this->rpp = $this->trimmed('rpp');
  79. if (!$this->rpp) {
  80. $this->rpp = 15;
  81. }
  82. if ($this->rpp > 100) {
  83. $this->rpp = 100;
  84. }
  85. $this->page = $this->trimmed('page');
  86. if (!$this->page) {
  87. $this->page = 1;
  88. }
  89. // TODO: Suppport max_id -- we need to tweak the backend
  90. // Search classes to support it.
  91. $this->since_id = $this->trimmed('since_id');
  92. $this->geocode = $this->trimmed('geocode');
  93. return true;
  94. }
  95. /**
  96. * Handle a request
  97. *
  98. * @return void
  99. */
  100. function handle()
  101. {
  102. parent::handle();
  103. $this->showResults();
  104. }
  105. /**
  106. * Show search results
  107. *
  108. * @return void
  109. */
  110. function showResults()
  111. {
  112. // TODO: Support search operators like from: and to:, boolean, etc.
  113. $notice = new Notice();
  114. $this->notices = array();
  115. $search_engine = $notice->getSearchEngine('notice');
  116. $search_engine->set_sort_mode('chron');
  117. $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1);
  118. if ($search_engine->query($this->query)) {
  119. $cnt = $notice->find();
  120. $this->notices = $notice->fetchAll();
  121. }
  122. $this->showJsonTimeline($this->notices);
  123. }
  124. /**
  125. * Do we need to write to the database?
  126. *
  127. * @return boolean true
  128. */
  129. function isReadOnly($args)
  130. {
  131. return true;
  132. }
  133. }
  134. // END OF FILE
  135. // ============================================================================
  136. ?>