NotificationScraper.inc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. class NotificationScraper {
  3. public function getUserTags($text) {
  4. $matches = array();
  5. preg_match_all("/(^|)@(\w*[a-zA-Z_]+\w*)/", $text, $matches);
  6. $tags = $matches[0];
  7. foreach ($tags as $index => $tagString) {
  8. $tags[$index] = strtolower($tagString);
  9. }
  10. return $tags;
  11. }
  12. public function getNamesFromTags(array $tags) {
  13. $names = array();
  14. foreach ($tags as $tagString) {
  15. $names[] = trim($tagString, '@');
  16. }
  17. return $names;
  18. }
  19. public function getIdsFromTags(array $tags) {
  20. $userModel = new UserModel();
  21. $userIds = array();
  22. $names = $this->getNamesFromTags($tags);
  23. foreach ($names as $username) {
  24. $userIds[] = $userModel->getIdFromUsername($username);
  25. }
  26. return $userIds;
  27. }
  28. public function getUserdataFromTags(array $tags) {
  29. $userModel = new UserModel();
  30. $usersData = array();
  31. foreach ($tags as $tagString) {
  32. $usersData[$userModel->getIdFromUsername(trim($tagString, '@'))] = $tagString;
  33. }
  34. return $usersData;
  35. }
  36. public function getUserTagDataFromString($text) {
  37. $allTags = $this->getUserTags($text);
  38. $userData = $this->getUserdataFromTags($allTags);
  39. return $userData;
  40. }
  41. public function replaceNotificationsWithLinks($text) {
  42. $modifiedText = $text;
  43. $userData = $this->getUserTagDataFromString($modifiedText);
  44. foreach($userData as $userId => $userTag) {
  45. $modifiedText = str_ireplace($userTag, "<a href=\"?a=user&id=".$userId."\">".$userTag."</a>", $modifiedText);
  46. }
  47. return $modifiedText;
  48. }
  49. }