SearchSubPlugin.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * A plugin to enable local tab subscription
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category SearchSubPlugin
  24. * @package StatusNet
  25. * @author Brion Vibber <brion@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * SearchSub plugin main class
  35. *
  36. * @category SearchSubPlugin
  37. * @package StatusNet
  38. * @author Brion Vibber <brionv@status.net>
  39. * @copyright 2011 StatusNet, Inc.
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  41. * @link http://status.net/
  42. */
  43. class SearchSubPlugin extends Plugin
  44. {
  45. const VERSION = '0.1';
  46. /**
  47. * Database schema setup
  48. *
  49. * @see Schema
  50. *
  51. * @return boolean hook value; true means continue processing, false means stop.
  52. */
  53. function onCheckSchema()
  54. {
  55. $schema = Schema::get();
  56. $schema->ensureTable('searchsub', SearchSub::schemaDef());
  57. return true;
  58. }
  59. /**
  60. * Map URLs to actions
  61. *
  62. * @param URLMapper $m path-to-action mapper
  63. *
  64. * @return boolean hook value; true means continue processing, false means stop.
  65. */
  66. public function onRouterInitialized(URLMapper $m)
  67. {
  68. $m->connect('search/:search/subscribe',
  69. array('action' => 'searchsub'),
  70. array('search' => Router::REGEX_TAG));
  71. $m->connect('search/:search/unsubscribe',
  72. array('action' => 'searchunsub'),
  73. array('search' => Router::REGEX_TAG));
  74. $m->connect(':nickname/search-subscriptions',
  75. array('action' => 'searchsubs'),
  76. array('nickname' => Nickname::DISPLAY_FMT));
  77. return true;
  78. }
  79. /**
  80. * Plugin version data
  81. *
  82. * @param array &$versions array of version data
  83. *
  84. * @return value
  85. */
  86. function onPluginVersion(array &$versions)
  87. {
  88. $versions[] = array('name' => 'SearchSub',
  89. 'version' => self::VERSION,
  90. 'author' => 'Brion Vibber',
  91. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SearchSub',
  92. 'rawdescription' =>
  93. // TRANS: Plugin description.
  94. _m('Plugin to allow following all messages with a given search.'));
  95. return true;
  96. }
  97. /**
  98. * Hook inbox delivery setup so search subscribers receive all
  99. * notices with that search in their inbox.
  100. *
  101. * Currently makes no distinction between local messages and
  102. * remote ones which happen to come in to the system. Remote
  103. * notices that don't come in at all won't ever reach this.
  104. *
  105. * @param Notice $notice
  106. * @param array $ni in/out map of profile IDs to inbox constants
  107. * @return boolean hook result
  108. */
  109. function onStartNoticeWhoGets(Notice $notice, array &$ni)
  110. {
  111. // Warning: this is potentially very slow
  112. // with a lot of searches!
  113. $sub = new SearchSub();
  114. $sub->groupBy('search');
  115. $sub->find();
  116. while ($sub->fetch()) {
  117. $search = $sub->search;
  118. if ($this->matchSearch($notice, $search)) {
  119. // Match? Find all those who subscribed to this
  120. // search term and get our delivery on...
  121. $searchsub = new SearchSub();
  122. $searchsub->search = $search;
  123. $searchsub->find();
  124. while ($searchsub->fetch()) {
  125. // These constants are currently not actually used, iirc
  126. $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
  127. }
  128. }
  129. }
  130. return true;
  131. }
  132. /**
  133. * Does the given notice match the given fulltext search query?
  134. *
  135. * Warning: not guaranteed to match other search engine behavior, etc.
  136. * Currently using a basic case-insensitive substring match, which
  137. * probably fits with the 'LIKE' search but not the default MySQL
  138. * or Sphinx search backends.
  139. *
  140. * @param Notice $notice
  141. * @param string $search
  142. * @return boolean
  143. */
  144. function matchSearch(Notice $notice, $search)
  145. {
  146. return (mb_stripos($notice->content, $search) !== false);
  147. }
  148. /**
  149. *
  150. * @param NoticeSearchAction $action
  151. * @param string $q
  152. * @param Notice $notice
  153. * @return boolean hook result
  154. */
  155. function onStartNoticeSearchShowResults($action, $q, $notice)
  156. {
  157. $user = common_current_user();
  158. if ($user) {
  159. $search = $q;
  160. $searchsub = SearchSub::pkeyGet(array('search' => $search,
  161. 'profile_id' => $user->id));
  162. if ($searchsub) {
  163. $form = new SearchUnsubForm($action, $search);
  164. } else {
  165. $form = new SearchSubForm($action, $search);
  166. }
  167. $action->elementStart('div', 'entity_actions');
  168. $action->elementStart('ul');
  169. $action->elementStart('li', 'entity_subscribe');
  170. $form->show();
  171. $action->elementEnd('li');
  172. $action->elementEnd('ul');
  173. $action->elementEnd('div');
  174. }
  175. return true;
  176. }
  177. /**
  178. * Menu item for personal subscriptions/groups area
  179. *
  180. * @param Widget $widget Widget being executed
  181. *
  182. * @return boolean hook return
  183. */
  184. function onEndSubGroupNav($widget)
  185. {
  186. $action = $widget->out;
  187. $action_name = $action->trimmed('action');
  188. $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
  189. // TRANS: SearchSub plugin menu item on user settings page.
  190. _m('MENU', 'Searches'),
  191. // TRANS: SearchSub plugin tooltip for user settings menu item.
  192. _m('Configure search subscriptions'),
  193. $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
  194. return true;
  195. }
  196. /**
  197. * Replace the built-in stub track commands with ones that control
  198. * search subscriptions.
  199. *
  200. * @param CommandInterpreter $cmd
  201. * @param string $arg
  202. * @param User $user
  203. * @param Command $result
  204. * @return boolean hook result
  205. */
  206. function onEndInterpretCommand($cmd, $arg, $user, &$result)
  207. {
  208. if ($result instanceof TrackCommand) {
  209. $result = new SearchSubTrackCommand($user, $arg);
  210. return false;
  211. } else if ($result instanceof TrackOffCommand) {
  212. $result = new SearchSubTrackOffCommand($user);
  213. return false;
  214. } else if ($result instanceof TrackingCommand) {
  215. $result = new SearchSubTrackingCommand($user);
  216. return false;
  217. } else if ($result instanceof UntrackCommand) {
  218. $result = new SearchSubUntrackCommand($user, $arg);
  219. return false;
  220. } else {
  221. return true;
  222. }
  223. }
  224. function onHelpCommandMessages($cmd, &$commands)
  225. {
  226. // TRANS: Help message for IM/SMS command "track <word>"
  227. $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
  228. // TRANS: Help message for IM/SMS command "untrack <word>"
  229. $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
  230. // TRANS: Help message for IM/SMS command "track off"
  231. $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
  232. // TRANS: Help message for IM/SMS command "untrack all"
  233. $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
  234. // TRANS: Help message for IM/SMS command "tracks"
  235. $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
  236. // TRANS: Help message for IM/SMS command "tracking"
  237. $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
  238. }
  239. function onEndDefaultLocalNav($menu, $user)
  240. {
  241. $user = common_current_user();
  242. if (!empty($user)) {
  243. $searches = SearchSub::forProfile($user->getProfile());
  244. if (!empty($searches) && count($searches) > 0) {
  245. $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
  246. // TRANS: Sub menu for searches.
  247. $menu->submenu(_m('MENU','Searches'), $searchSubMenu);
  248. }
  249. }
  250. return true;
  251. }
  252. }