xExtensionTGLinkFixer/extension.php

70 lines
2.5 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.
*/
public function init()
{
// Hook into FreshRSS just before each entry is saved to database:
$this->registerHook('entry_before_insert', array($this, 'fixLink'));
}
/**
* Our main function to rewrite the <link> from <description>.
*/
public static function fixLink($entry)
{
// 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; // skip if not a telegram link
}
try {
// output the $entry
error_log('TelegramLinkFixer: Entry: ' . json_encode($entry));
$description = $entry->description();
if (empty($description)) {
error_log('TelegramLinkFixer: Skipping - empty description');
return;
}
} catch (Exception $e) {
error_log('TelegramLinkFixer: Error processing entry: ' . $e->getMessage());
}
// Look specifically for Telegraph links
error_log('TelegramLinkFixer: Looking for Telegraph link in description: ' . $description);
try {
if (preg_match('/<a[^>]*?href="(https:\/\/telegra\.ph\/[^"]+)"[^>]*>Telegraph<\/a>/i', $description, $matches)) {
$telegraphLink = $matches[1];
if (!empty($telegraphLink)) {
Minz_Log::debug('TelegramLinkFixer: Found Telegraph link: ' . $telegraphLink);
try {
$entry->setLink($telegraphLink);
error_log('TelegramLinkFixer: Successfully updated link');
} catch (Exception $e) {
error_log('TelegramLinkFixer: Failed to set link: ' . $e->getMessage());
}
}
} else {
error_log('TelegramLinkFixer: No Telegraph link found in description');
}
} catch (Exception $e) {
error_log('TelegramLinkFixer: Error processing entry: ' . $e->getMessage());
}
}
}