@@ -36,245 +36,245 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Provider implements IProvider { |
| 38 | 38 | |
| 39 | - /** @var IFactory */ |
|
| 40 | - protected $languageFactory; |
|
| 41 | - |
|
| 42 | - /** @var IL10N */ |
|
| 43 | - protected $l; |
|
| 44 | - |
|
| 45 | - /** @var IURLGenerator */ |
|
| 46 | - protected $url; |
|
| 47 | - |
|
| 48 | - /** @var ICommentsManager */ |
|
| 49 | - protected $commentsManager; |
|
| 50 | - |
|
| 51 | - /** @var IUserManager */ |
|
| 52 | - protected $userManager; |
|
| 53 | - |
|
| 54 | - /** @var IManager */ |
|
| 55 | - protected $activityManager; |
|
| 56 | - |
|
| 57 | - /** @var string[] */ |
|
| 58 | - protected $displayNames = []; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param IFactory $languageFactory |
|
| 62 | - * @param IURLGenerator $url |
|
| 63 | - * @param ICommentsManager $commentsManager |
|
| 64 | - * @param IUserManager $userManager |
|
| 65 | - * @param IManager $activityManager |
|
| 66 | - */ |
|
| 67 | - public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { |
|
| 68 | - $this->languageFactory = $languageFactory; |
|
| 69 | - $this->url = $url; |
|
| 70 | - $this->commentsManager = $commentsManager; |
|
| 71 | - $this->userManager = $userManager; |
|
| 72 | - $this->activityManager = $activityManager; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $language |
|
| 77 | - * @param IEvent $event |
|
| 78 | - * @param IEvent|null $previousEvent |
|
| 79 | - * @return IEvent |
|
| 80 | - * @throws \InvalidArgumentException |
|
| 81 | - * @since 11.0.0 |
|
| 82 | - */ |
|
| 83 | - public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
| 84 | - if ($event->getApp() !== 'comments') { |
|
| 85 | - throw new \InvalidArgumentException(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $this->l = $this->languageFactory->get('comments', $language); |
|
| 89 | - |
|
| 90 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 91 | - $this->parseMessage($event); |
|
| 92 | - if ($this->activityManager->getRequirePNG()) { |
|
| 93 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png'))); |
|
| 94 | - } else { |
|
| 95 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ($this->activityManager->isFormattingFilteredObject()) { |
|
| 99 | - try { |
|
| 100 | - return $this->parseShortVersion($event); |
|
| 101 | - } catch (\InvalidArgumentException $e) { |
|
| 102 | - // Ignore and simply use the long version... |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $this->parseLongVersion($event); |
|
| 107 | - } else { |
|
| 108 | - throw new \InvalidArgumentException(); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param IEvent $event |
|
| 114 | - * @return IEvent |
|
| 115 | - * @throws \InvalidArgumentException |
|
| 116 | - */ |
|
| 117 | - protected function parseShortVersion(IEvent $event) { |
|
| 118 | - $subjectParameters = $this->getSubjectParameters($event); |
|
| 119 | - |
|
| 120 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 121 | - if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 122 | - $event->setParsedSubject($this->l->t('You commented')) |
|
| 123 | - ->setRichSubject($this->l->t('You commented'), []); |
|
| 124 | - } else { |
|
| 125 | - $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 126 | - $event->setParsedSubject($this->l->t('%1$s commented', [$author['name']])) |
|
| 127 | - ->setRichSubject($this->l->t('{author} commented'), [ |
|
| 128 | - 'author' => $author, |
|
| 129 | - ]); |
|
| 130 | - } |
|
| 131 | - } else { |
|
| 132 | - throw new \InvalidArgumentException(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return $event; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param IEvent $event |
|
| 140 | - * @return IEvent |
|
| 141 | - * @throws \InvalidArgumentException |
|
| 142 | - */ |
|
| 143 | - protected function parseLongVersion(IEvent $event) { |
|
| 144 | - $subjectParameters = $this->getSubjectParameters($event); |
|
| 145 | - |
|
| 146 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 147 | - if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 148 | - $event->setParsedSubject($this->l->t('You commented on %1$s', [ |
|
| 149 | - $subjectParameters['filePath'], |
|
| 150 | - ])) |
|
| 151 | - ->setRichSubject($this->l->t('You commented on {file}'), [ |
|
| 152 | - 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 153 | - ]); |
|
| 154 | - } else { |
|
| 155 | - $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 156 | - $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [ |
|
| 157 | - $author['name'], |
|
| 158 | - $subjectParameters['filePath'], |
|
| 159 | - ])) |
|
| 160 | - ->setRichSubject($this->l->t('{author} commented on {file}'), [ |
|
| 161 | - 'author' => $author, |
|
| 162 | - 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 163 | - ]); |
|
| 164 | - } |
|
| 165 | - } else { |
|
| 166 | - throw new \InvalidArgumentException(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $event; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - protected function getSubjectParameters(IEvent $event) { |
|
| 173 | - $subjectParameters = $event->getSubjectParameters(); |
|
| 174 | - if (isset($subjectParameters['fileId'])) { |
|
| 175 | - return $subjectParameters; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // Fix subjects from 12.0.3 and older |
|
| 179 | - // |
|
| 180 | - // Do NOT Remove unless necessary |
|
| 181 | - // Removing this will break parsing of activities that were created on |
|
| 182 | - // Nextcloud 12, so we should keep this as long as it's acceptable. |
|
| 183 | - // Otherwise if people upgrade over multiple releases in a short period, |
|
| 184 | - // they will get the dead entries in their stream. |
|
| 185 | - return [ |
|
| 186 | - 'actor' => $subjectParameters[0], |
|
| 187 | - 'fileId' => (int) $event->getObjectId(), |
|
| 188 | - 'filePath' => trim($subjectParameters[1], '/'), |
|
| 189 | - ]; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @param IEvent $event |
|
| 194 | - */ |
|
| 195 | - protected function parseMessage(IEvent $event) { |
|
| 196 | - $messageParameters = $event->getMessageParameters(); |
|
| 197 | - if (empty($messageParameters)) { |
|
| 198 | ||
| 199 | - return; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0]; |
|
| 203 | - |
|
| 204 | - try { |
|
| 205 | - $comment = $this->commentsManager->get((string) $commentId); |
|
| 206 | - $message = $comment->getMessage(); |
|
| 207 | - $message = str_replace("\n", '<br />', str_replace(['<', '>'], ['<', '>'], $message)); |
|
| 208 | - |
|
| 209 | - $mentionCount = 1; |
|
| 210 | - $mentions = []; |
|
| 211 | - foreach ($comment->getMentions() as $mention) { |
|
| 212 | - if ($mention['type'] !== 'user') { |
|
| 213 | - continue; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $pattern = '/(^|\s)(' . '@' . $mention['id'] . ')(\b)/'; |
|
| 217 | - if (strpos($mention['id'], ' ') !== false) { |
|
| 218 | - $pattern = '/(^|\s)(' . '@"' . $mention['id'] . '"' . ')(\b)?/'; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $message = preg_replace( |
|
| 222 | - $pattern, |
|
| 223 | - //'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}', |
|
| 224 | - '${1}' . '{mention' . $mentionCount . '}' . '${3}', |
|
| 225 | - $message |
|
| 226 | - ); |
|
| 227 | - $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 228 | - $mentionCount++; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - $event->setParsedMessage($comment->getMessage()) |
|
| 232 | - ->setRichMessage($message, $mentions); |
|
| 233 | - } catch (NotFoundException $e) { |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param int $id |
|
| 239 | - * @param string $path |
|
| 240 | - * @return array |
|
| 241 | - */ |
|
| 242 | - protected function generateFileParameter($id, $path) { |
|
| 243 | - return [ |
|
| 244 | - 'type' => 'file', |
|
| 245 | - 'id' => $id, |
|
| 246 | - 'name' => basename($path), |
|
| 247 | - 'path' => $path, |
|
| 248 | - 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
| 249 | - ]; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * @param string $uid |
|
| 254 | - * @return array |
|
| 255 | - */ |
|
| 256 | - protected function generateUserParameter($uid) { |
|
| 257 | - if (!isset($this->displayNames[$uid])) { |
|
| 258 | - $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - return [ |
|
| 262 | - 'type' => 'user', |
|
| 263 | - 'id' => $uid, |
|
| 264 | - 'name' => $this->displayNames[$uid], |
|
| 265 | - ]; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param string $uid |
|
| 270 | - * @return string |
|
| 271 | - */ |
|
| 272 | - protected function getDisplayName($uid) { |
|
| 273 | - $user = $this->userManager->get($uid); |
|
| 274 | - if ($user instanceof IUser) { |
|
| 275 | - return $user->getDisplayName(); |
|
| 276 | - } else { |
|
| 277 | - return $uid; |
|
| 278 | - } |
|
| 279 | - } |
|
| 39 | + /** @var IFactory */ |
|
| 40 | + protected $languageFactory; |
|
| 41 | + |
|
| 42 | + /** @var IL10N */ |
|
| 43 | + protected $l; |
|
| 44 | + |
|
| 45 | + /** @var IURLGenerator */ |
|
| 46 | + protected $url; |
|
| 47 | + |
|
| 48 | + /** @var ICommentsManager */ |
|
| 49 | + protected $commentsManager; |
|
| 50 | + |
|
| 51 | + /** @var IUserManager */ |
|
| 52 | + protected $userManager; |
|
| 53 | + |
|
| 54 | + /** @var IManager */ |
|
| 55 | + protected $activityManager; |
|
| 56 | + |
|
| 57 | + /** @var string[] */ |
|
| 58 | + protected $displayNames = []; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param IFactory $languageFactory |
|
| 62 | + * @param IURLGenerator $url |
|
| 63 | + * @param ICommentsManager $commentsManager |
|
| 64 | + * @param IUserManager $userManager |
|
| 65 | + * @param IManager $activityManager |
|
| 66 | + */ |
|
| 67 | + public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { |
|
| 68 | + $this->languageFactory = $languageFactory; |
|
| 69 | + $this->url = $url; |
|
| 70 | + $this->commentsManager = $commentsManager; |
|
| 71 | + $this->userManager = $userManager; |
|
| 72 | + $this->activityManager = $activityManager; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $language |
|
| 77 | + * @param IEvent $event |
|
| 78 | + * @param IEvent|null $previousEvent |
|
| 79 | + * @return IEvent |
|
| 80 | + * @throws \InvalidArgumentException |
|
| 81 | + * @since 11.0.0 |
|
| 82 | + */ |
|
| 83 | + public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
| 84 | + if ($event->getApp() !== 'comments') { |
|
| 85 | + throw new \InvalidArgumentException(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $this->l = $this->languageFactory->get('comments', $language); |
|
| 89 | + |
|
| 90 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 91 | + $this->parseMessage($event); |
|
| 92 | + if ($this->activityManager->getRequirePNG()) { |
|
| 93 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png'))); |
|
| 94 | + } else { |
|
| 95 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ($this->activityManager->isFormattingFilteredObject()) { |
|
| 99 | + try { |
|
| 100 | + return $this->parseShortVersion($event); |
|
| 101 | + } catch (\InvalidArgumentException $e) { |
|
| 102 | + // Ignore and simply use the long version... |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $this->parseLongVersion($event); |
|
| 107 | + } else { |
|
| 108 | + throw new \InvalidArgumentException(); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param IEvent $event |
|
| 114 | + * @return IEvent |
|
| 115 | + * @throws \InvalidArgumentException |
|
| 116 | + */ |
|
| 117 | + protected function parseShortVersion(IEvent $event) { |
|
| 118 | + $subjectParameters = $this->getSubjectParameters($event); |
|
| 119 | + |
|
| 120 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 121 | + if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 122 | + $event->setParsedSubject($this->l->t('You commented')) |
|
| 123 | + ->setRichSubject($this->l->t('You commented'), []); |
|
| 124 | + } else { |
|
| 125 | + $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 126 | + $event->setParsedSubject($this->l->t('%1$s commented', [$author['name']])) |
|
| 127 | + ->setRichSubject($this->l->t('{author} commented'), [ |
|
| 128 | + 'author' => $author, |
|
| 129 | + ]); |
|
| 130 | + } |
|
| 131 | + } else { |
|
| 132 | + throw new \InvalidArgumentException(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return $event; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param IEvent $event |
|
| 140 | + * @return IEvent |
|
| 141 | + * @throws \InvalidArgumentException |
|
| 142 | + */ |
|
| 143 | + protected function parseLongVersion(IEvent $event) { |
|
| 144 | + $subjectParameters = $this->getSubjectParameters($event); |
|
| 145 | + |
|
| 146 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 147 | + if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 148 | + $event->setParsedSubject($this->l->t('You commented on %1$s', [ |
|
| 149 | + $subjectParameters['filePath'], |
|
| 150 | + ])) |
|
| 151 | + ->setRichSubject($this->l->t('You commented on {file}'), [ |
|
| 152 | + 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 153 | + ]); |
|
| 154 | + } else { |
|
| 155 | + $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 156 | + $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [ |
|
| 157 | + $author['name'], |
|
| 158 | + $subjectParameters['filePath'], |
|
| 159 | + ])) |
|
| 160 | + ->setRichSubject($this->l->t('{author} commented on {file}'), [ |
|
| 161 | + 'author' => $author, |
|
| 162 | + 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 163 | + ]); |
|
| 164 | + } |
|
| 165 | + } else { |
|
| 166 | + throw new \InvalidArgumentException(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $event; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + protected function getSubjectParameters(IEvent $event) { |
|
| 173 | + $subjectParameters = $event->getSubjectParameters(); |
|
| 174 | + if (isset($subjectParameters['fileId'])) { |
|
| 175 | + return $subjectParameters; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // Fix subjects from 12.0.3 and older |
|
| 179 | + // |
|
| 180 | + // Do NOT Remove unless necessary |
|
| 181 | + // Removing this will break parsing of activities that were created on |
|
| 182 | + // Nextcloud 12, so we should keep this as long as it's acceptable. |
|
| 183 | + // Otherwise if people upgrade over multiple releases in a short period, |
|
| 184 | + // they will get the dead entries in their stream. |
|
| 185 | + return [ |
|
| 186 | + 'actor' => $subjectParameters[0], |
|
| 187 | + 'fileId' => (int) $event->getObjectId(), |
|
| 188 | + 'filePath' => trim($subjectParameters[1], '/'), |
|
| 189 | + ]; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @param IEvent $event |
|
| 194 | + */ |
|
| 195 | + protected function parseMessage(IEvent $event) { |
|
| 196 | + $messageParameters = $event->getMessageParameters(); |
|
| 197 | + if (empty($messageParameters)) { |
|
| 198 | ||
| 199 | + return; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0]; |
|
| 203 | + |
|
| 204 | + try { |
|
| 205 | + $comment = $this->commentsManager->get((string) $commentId); |
|
| 206 | + $message = $comment->getMessage(); |
|
| 207 | + $message = str_replace("\n", '<br />', str_replace(['<', '>'], ['<', '>'], $message)); |
|
| 208 | + |
|
| 209 | + $mentionCount = 1; |
|
| 210 | + $mentions = []; |
|
| 211 | + foreach ($comment->getMentions() as $mention) { |
|
| 212 | + if ($mention['type'] !== 'user') { |
|
| 213 | + continue; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $pattern = '/(^|\s)(' . '@' . $mention['id'] . ')(\b)/'; |
|
| 217 | + if (strpos($mention['id'], ' ') !== false) { |
|
| 218 | + $pattern = '/(^|\s)(' . '@"' . $mention['id'] . '"' . ')(\b)?/'; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $message = preg_replace( |
|
| 222 | + $pattern, |
|
| 223 | + //'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}', |
|
| 224 | + '${1}' . '{mention' . $mentionCount . '}' . '${3}', |
|
| 225 | + $message |
|
| 226 | + ); |
|
| 227 | + $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 228 | + $mentionCount++; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + $event->setParsedMessage($comment->getMessage()) |
|
| 232 | + ->setRichMessage($message, $mentions); |
|
| 233 | + } catch (NotFoundException $e) { |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param int $id |
|
| 239 | + * @param string $path |
|
| 240 | + * @return array |
|
| 241 | + */ |
|
| 242 | + protected function generateFileParameter($id, $path) { |
|
| 243 | + return [ |
|
| 244 | + 'type' => 'file', |
|
| 245 | + 'id' => $id, |
|
| 246 | + 'name' => basename($path), |
|
| 247 | + 'path' => $path, |
|
| 248 | + 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
| 249 | + ]; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * @param string $uid |
|
| 254 | + * @return array |
|
| 255 | + */ |
|
| 256 | + protected function generateUserParameter($uid) { |
|
| 257 | + if (!isset($this->displayNames[$uid])) { |
|
| 258 | + $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + return [ |
|
| 262 | + 'type' => 'user', |
|
| 263 | + 'id' => $uid, |
|
| 264 | + 'name' => $this->displayNames[$uid], |
|
| 265 | + ]; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param string $uid |
|
| 270 | + * @return string |
|
| 271 | + */ |
|
| 272 | + protected function getDisplayName($uid) { |
|
| 273 | + $user = $this->userManager->get($uid); |
|
| 274 | + if ($user instanceof IUser) { |
|
| 275 | + return $user->getDisplayName(); |
|
| 276 | + } else { |
|
| 277 | + return $uid; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | 280 | } |
@@ -213,18 +213,18 @@ |
||
| 213 | 213 | continue; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - $pattern = '/(^|\s)(' . '@' . $mention['id'] . ')(\b)/'; |
|
| 216 | + $pattern = '/(^|\s)('.'@'.$mention['id'].')(\b)/'; |
|
| 217 | 217 | if (strpos($mention['id'], ' ') !== false) { |
| 218 | - $pattern = '/(^|\s)(' . '@"' . $mention['id'] . '"' . ')(\b)?/'; |
|
| 218 | + $pattern = '/(^|\s)('.'@"'.$mention['id'].'"'.')(\b)?/'; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | $message = preg_replace( |
| 222 | 222 | $pattern, |
| 223 | 223 | //'${1}' . $this->regexSafeUser($mention['id'], $displayName) . '${3}', |
| 224 | - '${1}' . '{mention' . $mentionCount . '}' . '${3}', |
|
| 224 | + '${1}'.'{mention'.$mentionCount.'}'.'${3}', |
|
| 225 | 225 | $message |
| 226 | 226 | ); |
| 227 | - $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 227 | + $mentions['mention'.$mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 228 | 228 | $mentionCount++; |
| 229 | 229 | } |
| 230 | 230 | |
@@ -30,389 +30,389 @@ |
||
| 30 | 30 | |
| 31 | 31 | class Comment implements IComment { |
| 32 | 32 | |
| 33 | - protected $data = [ |
|
| 34 | - 'id' => '', |
|
| 35 | - 'parentId' => '0', |
|
| 36 | - 'topmostParentId' => '0', |
|
| 37 | - 'childrenCount' => '0', |
|
| 38 | - 'message' => '', |
|
| 39 | - 'verb' => '', |
|
| 40 | - 'actorType' => '', |
|
| 41 | - 'actorId' => '', |
|
| 42 | - 'objectType' => '', |
|
| 43 | - 'objectId' => '', |
|
| 44 | - 'creationDT' => null, |
|
| 45 | - 'latestChildDT' => null, |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Comment constructor. |
|
| 50 | - * |
|
| 51 | - * @param array $data optional, array with keys according to column names from |
|
| 52 | - * the comments database scheme |
|
| 53 | - */ |
|
| 54 | - public function __construct(array $data = null) { |
|
| 55 | - if(is_array($data)) { |
|
| 56 | - $this->fromArray($data); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * returns the ID of the comment |
|
| 62 | - * |
|
| 63 | - * It may return an empty string, if the comment was not stored. |
|
| 64 | - * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | - * by itself (e.g. after saving). |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - * @since 9.0.0 |
|
| 69 | - */ |
|
| 70 | - public function getId() { |
|
| 71 | - return $this->data['id']; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * sets the ID of the comment and returns itself |
|
| 76 | - * |
|
| 77 | - * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | - * string (which means it is not stored in a database, storage or whatever |
|
| 79 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | - * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | - * |
|
| 82 | - * @param string $id |
|
| 83 | - * @return IComment |
|
| 84 | - * @throws IllegalIDChangeException |
|
| 85 | - * @since 9.0.0 |
|
| 86 | - */ |
|
| 87 | - public function setId($id) { |
|
| 88 | - if(!is_string($id)) { |
|
| 89 | - throw new \InvalidArgumentException('String expected.'); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $id = trim($id); |
|
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | - $this->data['id'] = $id; |
|
| 95 | - return $this; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * returns the parent ID of the comment |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - * @since 9.0.0 |
|
| 106 | - */ |
|
| 107 | - public function getParentId() { |
|
| 108 | - return $this->data['parentId']; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * sets the parent ID and returns itself |
|
| 113 | - * |
|
| 114 | - * @param string $parentId |
|
| 115 | - * @return IComment |
|
| 116 | - * @since 9.0.0 |
|
| 117 | - */ |
|
| 118 | - public function setParentId($parentId) { |
|
| 119 | - if(!is_string($parentId)) { |
|
| 120 | - throw new \InvalidArgumentException('String expected.'); |
|
| 121 | - } |
|
| 122 | - $this->data['parentId'] = trim($parentId); |
|
| 123 | - return $this; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * returns the topmost parent ID of the comment |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - * @since 9.0.0 |
|
| 131 | - */ |
|
| 132 | - public function getTopmostParentId() { |
|
| 133 | - return $this->data['topmostParentId']; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * sets the topmost parent ID and returns itself |
|
| 139 | - * |
|
| 140 | - * @param string $id |
|
| 141 | - * @return IComment |
|
| 142 | - * @since 9.0.0 |
|
| 143 | - */ |
|
| 144 | - public function setTopmostParentId($id) { |
|
| 145 | - if(!is_string($id)) { |
|
| 146 | - throw new \InvalidArgumentException('String expected.'); |
|
| 147 | - } |
|
| 148 | - $this->data['topmostParentId'] = trim($id); |
|
| 149 | - return $this; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * returns the number of children |
|
| 154 | - * |
|
| 155 | - * @return int |
|
| 156 | - * @since 9.0.0 |
|
| 157 | - */ |
|
| 158 | - public function getChildrenCount() { |
|
| 159 | - return $this->data['childrenCount']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * sets the number of children |
|
| 164 | - * |
|
| 165 | - * @param int $count |
|
| 166 | - * @return IComment |
|
| 167 | - * @since 9.0.0 |
|
| 168 | - */ |
|
| 169 | - public function setChildrenCount($count) { |
|
| 170 | - if(!is_int($count)) { |
|
| 171 | - throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | - } |
|
| 173 | - $this->data['childrenCount'] = $count; |
|
| 174 | - return $this; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * returns the message of the comment |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - * @since 9.0.0 |
|
| 182 | - */ |
|
| 183 | - public function getMessage() { |
|
| 184 | - return $this->data['message']; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * sets the message of the comment and returns itself |
|
| 189 | - * |
|
| 190 | - * @param string $message |
|
| 191 | - * @return IComment |
|
| 192 | - * @throws MessageTooLongException |
|
| 193 | - * @since 9.0.0 |
|
| 194 | - */ |
|
| 195 | - public function setMessage($message) { |
|
| 196 | - if(!is_string($message)) { |
|
| 197 | - throw new \InvalidArgumentException('String expected.'); |
|
| 198 | - } |
|
| 199 | - $message = trim($message); |
|
| 200 | - if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
| 201 | - throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
| 202 | - } |
|
| 203 | - $this->data['message'] = $message; |
|
| 204 | - return $this; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * returns an array containing mentions that are included in the comment |
|
| 209 | - * |
|
| 210 | - * @return array each mention provides a 'type' and an 'id', see example below |
|
| 211 | - * @since 11.0.0 |
|
| 212 | - * |
|
| 213 | - * The return array looks like: |
|
| 214 | - * [ |
|
| 215 | - * [ |
|
| 216 | - * 'type' => 'user', |
|
| 217 | - * 'id' => 'citizen4' |
|
| 218 | - * ], |
|
| 219 | - * [ |
|
| 220 | - * 'type' => 'group', |
|
| 221 | - * 'id' => 'media' |
|
| 222 | - * ], |
|
| 223 | - * … |
|
| 224 | - * ] |
|
| 225 | - * |
|
| 226 | - */ |
|
| 227 | - public function getMentions() { |
|
| 228 | - $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 229 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 230 | - return []; |
|
| 231 | - } |
|
| 232 | - $uids = array_unique($mentions[0]); |
|
| 233 | - $result = []; |
|
| 234 | - foreach ($uids as $uid) { |
|
| 235 | - $result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')]; |
|
| 236 | - } |
|
| 237 | - return $result; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * returns the verb of the comment |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - * @since 9.0.0 |
|
| 245 | - */ |
|
| 246 | - public function getVerb() { |
|
| 247 | - return $this->data['verb']; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 252 | - * |
|
| 253 | - * @param string $verb |
|
| 254 | - * @return IComment |
|
| 255 | - * @since 9.0.0 |
|
| 256 | - */ |
|
| 257 | - public function setVerb($verb) { |
|
| 258 | - if(!is_string($verb) || !trim($verb)) { |
|
| 259 | - throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 260 | - } |
|
| 261 | - $this->data['verb'] = trim($verb); |
|
| 262 | - return $this; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * returns the actor type |
|
| 267 | - * |
|
| 268 | - * @return string |
|
| 269 | - * @since 9.0.0 |
|
| 270 | - */ |
|
| 271 | - public function getActorType() { |
|
| 272 | - return $this->data['actorType']; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * returns the actor ID |
|
| 277 | - * |
|
| 278 | - * @return string |
|
| 279 | - * @since 9.0.0 |
|
| 280 | - */ |
|
| 281 | - public function getActorId() { |
|
| 282 | - return $this->data['actorId']; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * sets (overwrites) the actor type and id |
|
| 287 | - * |
|
| 288 | - * @param string $actorType e.g. 'users' |
|
| 289 | - * @param string $actorId e.g. 'zombie234' |
|
| 290 | - * @return IComment |
|
| 291 | - * @since 9.0.0 |
|
| 292 | - */ |
|
| 293 | - public function setActor($actorType, $actorId) { |
|
| 294 | - if( |
|
| 295 | - !is_string($actorType) || !trim($actorType) |
|
| 296 | - || !is_string($actorId) || !trim($actorId) |
|
| 297 | - ) { |
|
| 298 | - throw new \InvalidArgumentException('String expected.'); |
|
| 299 | - } |
|
| 300 | - $this->data['actorType'] = trim($actorType); |
|
| 301 | - $this->data['actorId'] = trim($actorId); |
|
| 302 | - return $this; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * returns the creation date of the comment. |
|
| 307 | - * |
|
| 308 | - * If not explicitly set, it shall default to the time of initialization. |
|
| 309 | - * |
|
| 310 | - * @return \DateTime |
|
| 311 | - * @since 9.0.0 |
|
| 312 | - */ |
|
| 313 | - public function getCreationDateTime() { |
|
| 314 | - return $this->data['creationDT']; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * sets the creation date of the comment and returns itself |
|
| 319 | - * |
|
| 320 | - * @param \DateTime $timestamp |
|
| 321 | - * @return IComment |
|
| 322 | - * @since 9.0.0 |
|
| 323 | - */ |
|
| 324 | - public function setCreationDateTime(\DateTime $timestamp) { |
|
| 325 | - $this->data['creationDT'] = $timestamp; |
|
| 326 | - return $this; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * returns the DateTime of the most recent child, if set, otherwise null |
|
| 331 | - * |
|
| 332 | - * @return \DateTime|null |
|
| 333 | - * @since 9.0.0 |
|
| 334 | - */ |
|
| 335 | - public function getLatestChildDateTime() { |
|
| 336 | - return $this->data['latestChildDT']; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * sets the date of the most recent child |
|
| 341 | - * |
|
| 342 | - * @param \DateTime $dateTime |
|
| 343 | - * @return IComment |
|
| 344 | - * @since 9.0.0 |
|
| 345 | - */ |
|
| 346 | - public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 347 | - $this->data['latestChildDT'] = $dateTime; |
|
| 348 | - return $this; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * returns the object type the comment is attached to |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - * @since 9.0.0 |
|
| 356 | - */ |
|
| 357 | - public function getObjectType() { |
|
| 358 | - return $this->data['objectType']; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * returns the object id the comment is attached to |
|
| 363 | - * |
|
| 364 | - * @return string |
|
| 365 | - * @since 9.0.0 |
|
| 366 | - */ |
|
| 367 | - public function getObjectId() { |
|
| 368 | - return $this->data['objectId']; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * sets (overwrites) the object of the comment |
|
| 373 | - * |
|
| 374 | - * @param string $objectType e.g. 'files' |
|
| 375 | - * @param string $objectId e.g. '16435' |
|
| 376 | - * @return IComment |
|
| 377 | - * @since 9.0.0 |
|
| 378 | - */ |
|
| 379 | - public function setObject($objectType, $objectId) { |
|
| 380 | - if( |
|
| 381 | - !is_string($objectType) || !trim($objectType) |
|
| 382 | - || !is_string($objectId) || !trim($objectId) |
|
| 383 | - ) { |
|
| 384 | - throw new \InvalidArgumentException('String expected.'); |
|
| 385 | - } |
|
| 386 | - $this->data['objectType'] = trim($objectType); |
|
| 387 | - $this->data['objectId'] = trim($objectId); |
|
| 388 | - return $this; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * sets the comment data based on an array with keys as taken from the |
|
| 393 | - * database. |
|
| 394 | - * |
|
| 395 | - * @param array $data |
|
| 396 | - * @return IComment |
|
| 397 | - */ |
|
| 398 | - protected function fromArray($data) { |
|
| 399 | - foreach(array_keys($data) as $key) { |
|
| 400 | - // translate DB keys to internal setter names |
|
| 401 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 402 | - $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 403 | - |
|
| 404 | - if(method_exists($this, $setter)) { |
|
| 405 | - $this->$setter($data[$key]); |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - foreach(['actor', 'object'] as $role) { |
|
| 410 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 411 | - $setter = 'set' . ucfirst($role); |
|
| 412 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - return $this; |
|
| 417 | - } |
|
| 33 | + protected $data = [ |
|
| 34 | + 'id' => '', |
|
| 35 | + 'parentId' => '0', |
|
| 36 | + 'topmostParentId' => '0', |
|
| 37 | + 'childrenCount' => '0', |
|
| 38 | + 'message' => '', |
|
| 39 | + 'verb' => '', |
|
| 40 | + 'actorType' => '', |
|
| 41 | + 'actorId' => '', |
|
| 42 | + 'objectType' => '', |
|
| 43 | + 'objectId' => '', |
|
| 44 | + 'creationDT' => null, |
|
| 45 | + 'latestChildDT' => null, |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Comment constructor. |
|
| 50 | + * |
|
| 51 | + * @param array $data optional, array with keys according to column names from |
|
| 52 | + * the comments database scheme |
|
| 53 | + */ |
|
| 54 | + public function __construct(array $data = null) { |
|
| 55 | + if(is_array($data)) { |
|
| 56 | + $this->fromArray($data); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * returns the ID of the comment |
|
| 62 | + * |
|
| 63 | + * It may return an empty string, if the comment was not stored. |
|
| 64 | + * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | + * by itself (e.g. after saving). |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + * @since 9.0.0 |
|
| 69 | + */ |
|
| 70 | + public function getId() { |
|
| 71 | + return $this->data['id']; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * sets the ID of the comment and returns itself |
|
| 76 | + * |
|
| 77 | + * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | + * string (which means it is not stored in a database, storage or whatever |
|
| 79 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | + * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | + * |
|
| 82 | + * @param string $id |
|
| 83 | + * @return IComment |
|
| 84 | + * @throws IllegalIDChangeException |
|
| 85 | + * @since 9.0.0 |
|
| 86 | + */ |
|
| 87 | + public function setId($id) { |
|
| 88 | + if(!is_string($id)) { |
|
| 89 | + throw new \InvalidArgumentException('String expected.'); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $id = trim($id); |
|
| 93 | + if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | + $this->data['id'] = $id; |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * returns the parent ID of the comment |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + * @since 9.0.0 |
|
| 106 | + */ |
|
| 107 | + public function getParentId() { |
|
| 108 | + return $this->data['parentId']; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * sets the parent ID and returns itself |
|
| 113 | + * |
|
| 114 | + * @param string $parentId |
|
| 115 | + * @return IComment |
|
| 116 | + * @since 9.0.0 |
|
| 117 | + */ |
|
| 118 | + public function setParentId($parentId) { |
|
| 119 | + if(!is_string($parentId)) { |
|
| 120 | + throw new \InvalidArgumentException('String expected.'); |
|
| 121 | + } |
|
| 122 | + $this->data['parentId'] = trim($parentId); |
|
| 123 | + return $this; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * returns the topmost parent ID of the comment |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + * @since 9.0.0 |
|
| 131 | + */ |
|
| 132 | + public function getTopmostParentId() { |
|
| 133 | + return $this->data['topmostParentId']; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * sets the topmost parent ID and returns itself |
|
| 139 | + * |
|
| 140 | + * @param string $id |
|
| 141 | + * @return IComment |
|
| 142 | + * @since 9.0.0 |
|
| 143 | + */ |
|
| 144 | + public function setTopmostParentId($id) { |
|
| 145 | + if(!is_string($id)) { |
|
| 146 | + throw new \InvalidArgumentException('String expected.'); |
|
| 147 | + } |
|
| 148 | + $this->data['topmostParentId'] = trim($id); |
|
| 149 | + return $this; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * returns the number of children |
|
| 154 | + * |
|
| 155 | + * @return int |
|
| 156 | + * @since 9.0.0 |
|
| 157 | + */ |
|
| 158 | + public function getChildrenCount() { |
|
| 159 | + return $this->data['childrenCount']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * sets the number of children |
|
| 164 | + * |
|
| 165 | + * @param int $count |
|
| 166 | + * @return IComment |
|
| 167 | + * @since 9.0.0 |
|
| 168 | + */ |
|
| 169 | + public function setChildrenCount($count) { |
|
| 170 | + if(!is_int($count)) { |
|
| 171 | + throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | + } |
|
| 173 | + $this->data['childrenCount'] = $count; |
|
| 174 | + return $this; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * returns the message of the comment |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + * @since 9.0.0 |
|
| 182 | + */ |
|
| 183 | + public function getMessage() { |
|
| 184 | + return $this->data['message']; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * sets the message of the comment and returns itself |
|
| 189 | + * |
|
| 190 | + * @param string $message |
|
| 191 | + * @return IComment |
|
| 192 | + * @throws MessageTooLongException |
|
| 193 | + * @since 9.0.0 |
|
| 194 | + */ |
|
| 195 | + public function setMessage($message) { |
|
| 196 | + if(!is_string($message)) { |
|
| 197 | + throw new \InvalidArgumentException('String expected.'); |
|
| 198 | + } |
|
| 199 | + $message = trim($message); |
|
| 200 | + if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
| 201 | + throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
| 202 | + } |
|
| 203 | + $this->data['message'] = $message; |
|
| 204 | + return $this; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * returns an array containing mentions that are included in the comment |
|
| 209 | + * |
|
| 210 | + * @return array each mention provides a 'type' and an 'id', see example below |
|
| 211 | + * @since 11.0.0 |
|
| 212 | + * |
|
| 213 | + * The return array looks like: |
|
| 214 | + * [ |
|
| 215 | + * [ |
|
| 216 | + * 'type' => 'user', |
|
| 217 | + * 'id' => 'citizen4' |
|
| 218 | + * ], |
|
| 219 | + * [ |
|
| 220 | + * 'type' => 'group', |
|
| 221 | + * 'id' => 'media' |
|
| 222 | + * ], |
|
| 223 | + * … |
|
| 224 | + * ] |
|
| 225 | + * |
|
| 226 | + */ |
|
| 227 | + public function getMentions() { |
|
| 228 | + $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 229 | + if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 230 | + return []; |
|
| 231 | + } |
|
| 232 | + $uids = array_unique($mentions[0]); |
|
| 233 | + $result = []; |
|
| 234 | + foreach ($uids as $uid) { |
|
| 235 | + $result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')]; |
|
| 236 | + } |
|
| 237 | + return $result; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * returns the verb of the comment |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + * @since 9.0.0 |
|
| 245 | + */ |
|
| 246 | + public function getVerb() { |
|
| 247 | + return $this->data['verb']; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 252 | + * |
|
| 253 | + * @param string $verb |
|
| 254 | + * @return IComment |
|
| 255 | + * @since 9.0.0 |
|
| 256 | + */ |
|
| 257 | + public function setVerb($verb) { |
|
| 258 | + if(!is_string($verb) || !trim($verb)) { |
|
| 259 | + throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 260 | + } |
|
| 261 | + $this->data['verb'] = trim($verb); |
|
| 262 | + return $this; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * returns the actor type |
|
| 267 | + * |
|
| 268 | + * @return string |
|
| 269 | + * @since 9.0.0 |
|
| 270 | + */ |
|
| 271 | + public function getActorType() { |
|
| 272 | + return $this->data['actorType']; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * returns the actor ID |
|
| 277 | + * |
|
| 278 | + * @return string |
|
| 279 | + * @since 9.0.0 |
|
| 280 | + */ |
|
| 281 | + public function getActorId() { |
|
| 282 | + return $this->data['actorId']; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * sets (overwrites) the actor type and id |
|
| 287 | + * |
|
| 288 | + * @param string $actorType e.g. 'users' |
|
| 289 | + * @param string $actorId e.g. 'zombie234' |
|
| 290 | + * @return IComment |
|
| 291 | + * @since 9.0.0 |
|
| 292 | + */ |
|
| 293 | + public function setActor($actorType, $actorId) { |
|
| 294 | + if( |
|
| 295 | + !is_string($actorType) || !trim($actorType) |
|
| 296 | + || !is_string($actorId) || !trim($actorId) |
|
| 297 | + ) { |
|
| 298 | + throw new \InvalidArgumentException('String expected.'); |
|
| 299 | + } |
|
| 300 | + $this->data['actorType'] = trim($actorType); |
|
| 301 | + $this->data['actorId'] = trim($actorId); |
|
| 302 | + return $this; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * returns the creation date of the comment. |
|
| 307 | + * |
|
| 308 | + * If not explicitly set, it shall default to the time of initialization. |
|
| 309 | + * |
|
| 310 | + * @return \DateTime |
|
| 311 | + * @since 9.0.0 |
|
| 312 | + */ |
|
| 313 | + public function getCreationDateTime() { |
|
| 314 | + return $this->data['creationDT']; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * sets the creation date of the comment and returns itself |
|
| 319 | + * |
|
| 320 | + * @param \DateTime $timestamp |
|
| 321 | + * @return IComment |
|
| 322 | + * @since 9.0.0 |
|
| 323 | + */ |
|
| 324 | + public function setCreationDateTime(\DateTime $timestamp) { |
|
| 325 | + $this->data['creationDT'] = $timestamp; |
|
| 326 | + return $this; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * returns the DateTime of the most recent child, if set, otherwise null |
|
| 331 | + * |
|
| 332 | + * @return \DateTime|null |
|
| 333 | + * @since 9.0.0 |
|
| 334 | + */ |
|
| 335 | + public function getLatestChildDateTime() { |
|
| 336 | + return $this->data['latestChildDT']; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * sets the date of the most recent child |
|
| 341 | + * |
|
| 342 | + * @param \DateTime $dateTime |
|
| 343 | + * @return IComment |
|
| 344 | + * @since 9.0.0 |
|
| 345 | + */ |
|
| 346 | + public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 347 | + $this->data['latestChildDT'] = $dateTime; |
|
| 348 | + return $this; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * returns the object type the comment is attached to |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + * @since 9.0.0 |
|
| 356 | + */ |
|
| 357 | + public function getObjectType() { |
|
| 358 | + return $this->data['objectType']; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * returns the object id the comment is attached to |
|
| 363 | + * |
|
| 364 | + * @return string |
|
| 365 | + * @since 9.0.0 |
|
| 366 | + */ |
|
| 367 | + public function getObjectId() { |
|
| 368 | + return $this->data['objectId']; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * sets (overwrites) the object of the comment |
|
| 373 | + * |
|
| 374 | + * @param string $objectType e.g. 'files' |
|
| 375 | + * @param string $objectId e.g. '16435' |
|
| 376 | + * @return IComment |
|
| 377 | + * @since 9.0.0 |
|
| 378 | + */ |
|
| 379 | + public function setObject($objectType, $objectId) { |
|
| 380 | + if( |
|
| 381 | + !is_string($objectType) || !trim($objectType) |
|
| 382 | + || !is_string($objectId) || !trim($objectId) |
|
| 383 | + ) { |
|
| 384 | + throw new \InvalidArgumentException('String expected.'); |
|
| 385 | + } |
|
| 386 | + $this->data['objectType'] = trim($objectType); |
|
| 387 | + $this->data['objectId'] = trim($objectId); |
|
| 388 | + return $this; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * sets the comment data based on an array with keys as taken from the |
|
| 393 | + * database. |
|
| 394 | + * |
|
| 395 | + * @param array $data |
|
| 396 | + * @return IComment |
|
| 397 | + */ |
|
| 398 | + protected function fromArray($data) { |
|
| 399 | + foreach(array_keys($data) as $key) { |
|
| 400 | + // translate DB keys to internal setter names |
|
| 401 | + $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 402 | + $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 403 | + |
|
| 404 | + if(method_exists($this, $setter)) { |
|
| 405 | + $this->$setter($data[$key]); |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + foreach(['actor', 'object'] as $role) { |
|
| 410 | + if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 411 | + $setter = 'set' . ucfirst($role); |
|
| 412 | + $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + return $this; |
|
| 417 | + } |
|
| 418 | 418 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * the comments database scheme |
| 53 | 53 | */ |
| 54 | 54 | public function __construct(array $data = null) { |
| 55 | - if(is_array($data)) { |
|
| 55 | + if (is_array($data)) { |
|
| 56 | 56 | $this->fromArray($data); |
| 57 | 57 | } |
| 58 | 58 | } |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | * @since 9.0.0 |
| 86 | 86 | */ |
| 87 | 87 | public function setId($id) { |
| 88 | - if(!is_string($id)) { |
|
| 88 | + if (!is_string($id)) { |
|
| 89 | 89 | throw new \InvalidArgumentException('String expected.'); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $id = trim($id); |
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 93 | + if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | 94 | $this->data['id'] = $id; |
| 95 | 95 | return $this; |
| 96 | 96 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @since 9.0.0 |
| 117 | 117 | */ |
| 118 | 118 | public function setParentId($parentId) { |
| 119 | - if(!is_string($parentId)) { |
|
| 119 | + if (!is_string($parentId)) { |
|
| 120 | 120 | throw new \InvalidArgumentException('String expected.'); |
| 121 | 121 | } |
| 122 | 122 | $this->data['parentId'] = trim($parentId); |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @since 9.0.0 |
| 143 | 143 | */ |
| 144 | 144 | public function setTopmostParentId($id) { |
| 145 | - if(!is_string($id)) { |
|
| 145 | + if (!is_string($id)) { |
|
| 146 | 146 | throw new \InvalidArgumentException('String expected.'); |
| 147 | 147 | } |
| 148 | 148 | $this->data['topmostParentId'] = trim($id); |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @since 9.0.0 |
| 168 | 168 | */ |
| 169 | 169 | public function setChildrenCount($count) { |
| 170 | - if(!is_int($count)) { |
|
| 170 | + if (!is_int($count)) { |
|
| 171 | 171 | throw new \InvalidArgumentException('Integer expected.'); |
| 172 | 172 | } |
| 173 | 173 | $this->data['childrenCount'] = $count; |
@@ -193,12 +193,12 @@ discard block |
||
| 193 | 193 | * @since 9.0.0 |
| 194 | 194 | */ |
| 195 | 195 | public function setMessage($message) { |
| 196 | - if(!is_string($message)) { |
|
| 196 | + if (!is_string($message)) { |
|
| 197 | 197 | throw new \InvalidArgumentException('String expected.'); |
| 198 | 198 | } |
| 199 | 199 | $message = trim($message); |
| 200 | - if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
| 201 | - throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
| 200 | + if (mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
| 201 | + throw new MessageTooLongException('Comment message must not exceed '.IComment::MAX_MESSAGE_LENGTH.' characters'); |
|
| 202 | 202 | } |
| 203 | 203 | $this->data['message'] = $message; |
| 204 | 204 | return $this; |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | */ |
| 227 | 227 | public function getMentions() { |
| 228 | 228 | $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
| 229 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 229 | + if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 230 | 230 | return []; |
| 231 | 231 | } |
| 232 | 232 | $uids = array_unique($mentions[0]); |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | * @since 9.0.0 |
| 256 | 256 | */ |
| 257 | 257 | public function setVerb($verb) { |
| 258 | - if(!is_string($verb) || !trim($verb)) { |
|
| 258 | + if (!is_string($verb) || !trim($verb)) { |
|
| 259 | 259 | throw new \InvalidArgumentException('Non-empty String expected.'); |
| 260 | 260 | } |
| 261 | 261 | $this->data['verb'] = trim($verb); |
@@ -291,9 +291,9 @@ discard block |
||
| 291 | 291 | * @since 9.0.0 |
| 292 | 292 | */ |
| 293 | 293 | public function setActor($actorType, $actorId) { |
| 294 | - if( |
|
| 294 | + if ( |
|
| 295 | 295 | !is_string($actorType) || !trim($actorType) |
| 296 | - || !is_string($actorId) || !trim($actorId) |
|
| 296 | + || !is_string($actorId) || !trim($actorId) |
|
| 297 | 297 | ) { |
| 298 | 298 | throw new \InvalidArgumentException('String expected.'); |
| 299 | 299 | } |
@@ -377,9 +377,9 @@ discard block |
||
| 377 | 377 | * @since 9.0.0 |
| 378 | 378 | */ |
| 379 | 379 | public function setObject($objectType, $objectId) { |
| 380 | - if( |
|
| 380 | + if ( |
|
| 381 | 381 | !is_string($objectType) || !trim($objectType) |
| 382 | - || !is_string($objectId) || !trim($objectId) |
|
| 382 | + || !is_string($objectId) || !trim($objectId) |
|
| 383 | 383 | ) { |
| 384 | 384 | throw new \InvalidArgumentException('String expected.'); |
| 385 | 385 | } |
@@ -396,20 +396,20 @@ discard block |
||
| 396 | 396 | * @return IComment |
| 397 | 397 | */ |
| 398 | 398 | protected function fromArray($data) { |
| 399 | - foreach(array_keys($data) as $key) { |
|
| 399 | + foreach (array_keys($data) as $key) { |
|
| 400 | 400 | // translate DB keys to internal setter names |
| 401 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 401 | + $setter = 'set'.implode('', array_map('ucfirst', explode('_', $key))); |
|
| 402 | 402 | $setter = str_replace('Timestamp', 'DateTime', $setter); |
| 403 | 403 | |
| 404 | - if(method_exists($this, $setter)) { |
|
| 404 | + if (method_exists($this, $setter)) { |
|
| 405 | 405 | $this->$setter($data[$key]); |
| 406 | 406 | } |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | - foreach(['actor', 'object'] as $role) { |
|
| 410 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 411 | - $setter = 'set' . ucfirst($role); |
|
| 412 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 409 | + foreach (['actor', 'object'] as $role) { |
|
| 410 | + if (isset($data[$role.'_type']) && isset($data[$role.'_id'])) { |
|
| 411 | + $setter = 'set'.ucfirst($role); |
|
| 412 | + $this->$setter($data[$role.'_type'], $data[$role.'_id']); |
|
| 413 | 413 | } |
| 414 | 414 | } |
| 415 | 415 | |