namespace OC\Collaboration\Reference;
use Fusonic\OpenGraph\Consumer;
+use OC\SystemConfig;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\IReferenceProvider;
use OCP\Http\Client\IClientService;
private IClientService $clientService;
private LoggerInterface $logger;
+ private SystemConfig $systemConfig;
- public function __construct(IClientService $clientService, LoggerInterface $logger) {
+ public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig) {
$this->clientService = $clientService;
$this->logger = $logger;
+ $this->systemConfig = $systemConfig;
}
public function resolveReference(string $referenceText): ?IReference {
+ if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
+ return null;
+ }
+
if (preg_match(self::URL_PATTERN, $referenceText)) {
$reference = new Reference($referenceText);
$this->fetchReference($reference);
return null;
}
- public function fetchReference(Reference $reference) {
+ private function fetchReference(Reference $reference) {
$client = $this->clientService->newClient();
try {
$response = $client->get($reference->getId());
class ReferenceManager implements IReferenceManager {
public const URL_PATTERN = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi';
- public const REF_PATTERN = '/#[A-z0-9_]+\/[A-z0-9_]+/i';
/** @var IReferenceProvider[] */
private array $providers = [];
private ICache $cache;
+ private LinkReferenceProvider $linkReferenceProvider;
+
public function __construct(LinkReferenceProvider $linkReferenceProvider, FileReferenceProvider $fileReferenceProvider, ICacheFactory $cacheFactory) {
$this->registerReferenceProvider($fileReferenceProvider);
- $this->registerReferenceProvider($linkReferenceProvider);
+ $this->linkReferenceProvider = $linkReferenceProvider;
$this->cache = $cacheFactory->createDistributed('reference');
}
public function extractReferences(string $text): array {
- $matches = [];
- preg_match_all(self::REF_PATTERN, $text, $matches);
- $references = $matches[0] ?? [];
-
preg_match_all(self::URL_PATTERN, $text, $matches);
- $references = array_merge($references, $matches[0] ?? []);
+ $references = $matches[0] ?? [];
return array_map(function ($reference) {
return trim($reference);
}, $references);
}
}
- return null;
+ return $this->linkReferenceProvider->resolveReference($referenceId);
}
public function registerReferenceProvider(IReferenceProvider $provider): void {