xExtensionTGLinkFixer/extension.php

66 lines
2.1 KiB
PHP

<?php
/**
* Name: Telegram Link Fixer
* Description: Rewrite the <link> from the feed with one extracted from <description>.
* Author: Your Name
* Version: 1.0
*/
class TelegramLinkFixerExtension extends Minz_Extension
{
/**
* Called automatically at extension load-time. Set up hooks here.
*/
#[\Override]
public function init()
{
// Hook into FreshRSS just before each entry is saved to database:
parent::init();
$this->registerHook('entry_before_insert', array($this, 'fixLink'));
}
/**
* Our main function to rewrite the <link> from <description>.
*/
public function fixLink($entry)
{
try {
// Check if it's a telegram link
$link = $entry->link();
error_log('TelegramLinkFixer: Processing entry with link: ' . $link);
if (strpos($link, 't.me') === false) {
error_log('TelegramLinkFixer: Skipping - not a telegram link');
return $entry; // skip if not a telegram link
}
// The correct method is content() not description()
$description = $entry->content();
if (empty($description)) {
error_log('TelegramLinkFixer: Skipping - empty content');
return $entry;
}
// First try: Look for Telegraph link with "Telegraph" text
if (preg_match('/href="(https:\/\/telegra\.ph\/[^"]+)"/i', $description, $matches)) {
$telegraphLink = $matches[1];
}
if (!empty($telegraphLink)) {
error_log('TelegramLinkFixer: Found Telegraph link: ' . $telegraphLink);
$entry->_link($telegraphLink);
error_log('TelegramLinkFixer: Successfully updated link');
} else {
error_log('TelegramLinkFixer: No Telegraph link found in content');
}
return $entry;
} catch (Exception $e) {
error_log('TelegramLinkFixer: Error processing entry: ' . $e->getMessage());
return $entry; // Return the entry even if an exception occurs
}
}
}