PageRenderTime 31ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/src/phpMyFAQ/Search/SearchResultSet.php

http://github.com/thorsten/phpMyFAQ
PHP | 222 lines | 100 code | 29 blank | 93 comment | 17 complexity | d2eae6a8bd4da172510e5bf64a4a71e9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Implements result sets for phpMyFAQ search classes.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ
  10. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  11. * @copyright 2010-2021 phpMyFAQ Team
  12. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  13. * @link https://www.phpmyfaq.de
  14. * @since 2010-06-06
  15. */
  16. namespace phpMyFAQ\Search;
  17. use phpMyFAQ\Configuration;
  18. use phpMyFAQ\Faq\FaqPermission;
  19. use phpMyFAQ\User;
  20. use phpMyFAQ\User\CurrentUser;
  21. use stdClass;
  22. /**
  23. * Class SearchResultSet
  24. *
  25. * @package phpMyFAQ\Search
  26. */
  27. class SearchResultSet
  28. {
  29. /**
  30. * @var Configuration
  31. */
  32. protected $config = null;
  33. /**
  34. * "Raw" search result set without permission checks and with possible
  35. * duplicates.
  36. *
  37. * @var stdClass[]
  38. */
  39. protected $rawResultSet = [];
  40. /**
  41. * "Reviewed" search result set with checked permissions and without
  42. * duplicates.
  43. *
  44. * @var stdClass[]
  45. */
  46. protected $reviewedResultSet = [];
  47. /**
  48. * Ordering of result set.
  49. *
  50. * @var string
  51. */
  52. protected $ordering;
  53. /**
  54. * Number of search results.
  55. *
  56. * @var int
  57. */
  58. protected $numberOfResults = 0;
  59. /**
  60. * User object.
  61. *
  62. * @var User
  63. */
  64. protected $user = null;
  65. /** @var FaqPermission */
  66. private $faqPermission;
  67. /**
  68. * Constructor.
  69. *
  70. * @param CurrentUser $user User object
  71. * @param FaqPermission $faqPermission
  72. * @param Configuration $config Configuration object
  73. */
  74. public function __construct(CurrentUser $user, FaqPermission $faqPermission, Configuration $config)
  75. {
  76. $this->user = $user;
  77. $this->faqPermission = $faqPermission;
  78. $this->config = $config;
  79. }
  80. /**
  81. * Check on user and group permissions and on duplicate FAQs.
  82. *
  83. * @param stdClass[] $resultSet Array with search results
  84. */
  85. public function reviewResultSet(array $resultSet): void
  86. {
  87. $this->setResultSet($resultSet);
  88. $duplicateResults = [];
  89. if ('basic' !== $this->config->get('security.permLevel')) {
  90. // @phpstan-ignore-next-line
  91. $currentGroupIds = $this->user->perm->getUserGroups($this->user->getUserId());
  92. } else {
  93. $currentGroupIds = [-1];
  94. }
  95. foreach ($this->rawResultSet as $result) {
  96. $permission = false;
  97. // check permission for sections
  98. if ('large' === $this->config->get('security.permLevel')) {
  99. // @todo Add code for section permissions
  100. $permission = true;
  101. }
  102. // check permissions for groups
  103. if ('medium' === $this->config->get('security.permLevel')) {
  104. $groupPermissions = $this->faqPermission->get(FaqPermission::GROUP, $result->id);
  105. if (is_array($groupPermissions)) {
  106. foreach ($groupPermissions as $groupPermission) {
  107. if (in_array($groupPermission, $currentGroupIds)) {
  108. $permission = true;
  109. }
  110. }
  111. }
  112. }
  113. // check permission for user
  114. if ('basic' === $this->config->get('security.permLevel')) {
  115. $userPermission = $this->faqPermission->get(FaqPermission::USER, $result->id);
  116. if (in_array(-1, $userPermission) || in_array($this->user->getUserId(), $userPermission)) {
  117. $permission = true;
  118. } else {
  119. $permission = false;
  120. }
  121. }
  122. // check on duplicates
  123. if (!isset($duplicateResults[$result->id])) {
  124. $duplicateResults[$result->id] = 1;
  125. } else {
  126. ++$duplicateResults[$result->id];
  127. continue;
  128. }
  129. if (!isset($result->score)) {
  130. $result->score = $this->getScore($result);
  131. }
  132. if ($permission) {
  133. $this->reviewedResultSet[] = $result;
  134. }
  135. }
  136. $this->setNumberOfResults($this->reviewedResultSet);
  137. }
  138. /**
  139. * Sets the "raw" search results.
  140. *
  141. * @param stdClass[] $resultSet Array with search results
  142. */
  143. public function setResultSet(array $resultSet): void
  144. {
  145. $this->rawResultSet = $resultSet;
  146. }
  147. /**
  148. * @param stdClass $object
  149. *
  150. * @return float
  151. */
  152. public function getScore(stdClass $object): float
  153. {
  154. $score = 0;
  155. if (isset($object->relevance_thema)) {
  156. $score += $object->relevance_thema;
  157. }
  158. if (isset($object->relevance_content)) {
  159. $score += $object->relevance_thema;
  160. }
  161. if (isset($object->relevance_keywords)) {
  162. $score += $object->relevance_keywords;
  163. }
  164. return round($score / 3 * 100);
  165. }
  166. /**
  167. * Returns the "reviewed" search results.
  168. *
  169. * @return stdClass[]
  170. */
  171. public function getResultSet(): array
  172. {
  173. return $this->reviewedResultSet;
  174. }
  175. /**
  176. * Returns the number search results.
  177. *
  178. * @return int
  179. */
  180. public function getNumberOfResults(): int
  181. {
  182. return $this->numberOfResults;
  183. }
  184. /**
  185. * Sets the number of search results.
  186. *
  187. * @param stdClass[] $resultSet
  188. */
  189. public function setNumberOfResults(array $resultSet): void
  190. {
  191. $this->numberOfResults = count($resultSet);
  192. }
  193. }