peoplesearch.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * People search action class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @author Robin Millette <millette@status.net>
  11. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  12. * @link http://status.net/
  13. *
  14. * StatusNet - the distributed open-source microblogging tool
  15. * Copyright (C) 2008, 2009, StatusNet, Inc.
  16. *
  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. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. require_once INSTALLDIR.'/lib/searchaction.php';
  34. require_once INSTALLDIR.'/lib/profilelist.php';
  35. /**
  36. * People search action class.
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Robin Millette <millette@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  43. * @link http://status.net/
  44. */
  45. class PeoplesearchAction extends SearchAction
  46. {
  47. function getInstructions()
  48. {
  49. // TRANS: Instructions for the "People search" page.
  50. // TRANS: %%site.name%% is the name of the StatusNet site.
  51. return _('Search for people on %%site.name%% by their name, location, or interests. ' .
  52. 'Separate the terms by spaces; they must be 3 characters or more.');
  53. }
  54. function title()
  55. {
  56. // TRANS: Title of a page where users can search for other users.
  57. return _('People search');
  58. }
  59. function showResults($q, $page)
  60. {
  61. $profile = new Profile();
  62. $search_engine = $profile->getSearchEngine('profile');
  63. $search_engine->set_sort_mode('chron');
  64. // Ask for an extra to see if there's more.
  65. $search_engine->limit((($page-1)*PROFILES_PER_PAGE), PROFILES_PER_PAGE + 1);
  66. if (false === $search_engine->query($q)) {
  67. $cnt = 0;
  68. }
  69. else {
  70. $cnt = $profile->find();
  71. }
  72. if ($cnt > 0) {
  73. $terms = preg_split('/[\s,]+/', $q);
  74. $results = new PeopleSearchResults($profile, $terms, $this);
  75. $results->show();
  76. $profile->free();
  77. $this->pagination($page > 1, $cnt > PROFILES_PER_PAGE,
  78. $page, 'peoplesearch', array('q' => $q));
  79. } else {
  80. // TRANS: Message on the "People search" page where a query has no results.
  81. $this->element('p', 'error', _('No results.'));
  82. $this->searchSuggestions($q);
  83. $profile->free();
  84. }
  85. }
  86. function showScripts()
  87. {
  88. parent::showScripts();
  89. $this->autofocus('q');
  90. }
  91. }
  92. /**
  93. * People search results class
  94. *
  95. * Derivative of ProfileList with specialization for highlighting search terms.
  96. *
  97. * @category Widget
  98. * @package StatusNet
  99. * @author Evan Prodromou <evan@status.net>
  100. * @author Robin Millette <millette@status.net>
  101. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  102. * @link http://status.net/
  103. *
  104. * @see PeoplesearchAction
  105. */
  106. class PeopleSearchResults extends ProfileList
  107. {
  108. var $terms = null;
  109. var $pattern = null;
  110. function __construct($profile, $terms, $action)
  111. {
  112. parent::__construct($profile, $action);
  113. $this->terms = array_map('preg_quote',
  114. array_map('htmlspecialchars', $terms));
  115. $this->pattern = '/('.implode('|',$terms).')/i';
  116. }
  117. function newProfileItem($profile)
  118. {
  119. return new PeopleSearchResultItem($profile, $this->action);
  120. }
  121. }
  122. class PeopleSearchResultItem extends ProfileListItem
  123. {
  124. function highlight($text)
  125. {
  126. return preg_replace($this->pattern, '<strong>\\1</strong>', htmlspecialchars($text));
  127. }
  128. }