Almost finished, maybe not working

This commit is contained in:
Matthew Exon 2019-10-13 10:40:24 +02:00 committed by Matthew Exon
parent 78ae25d054
commit 4be5839afe
4 changed files with 834 additions and 680 deletions

View file

@ -1,5 +1,5 @@
<?php
/**
/**
* Name: Retriever
* Description: Follow the permalink of RSS/Atom feed items and replace the summary with the full content.
* Version: 1.0
@ -27,33 +27,12 @@ use Friendica\Util\DateTimeFormat;
* @brief Installation hook for retriever plugin
*/
function retriever_install() {
Addon::registerHook('plugin_settings', 'addon/retriever/retriever.php', 'retriever_plugin_settings');
Addon::registerHook('plugin_settings_post', 'addon/retriever/retriever.php', 'retriever_plugin_settings_post');
Addon::registerHook('addon_settings', 'addon/retriever/retriever.php', 'retriever_addon_settings');
Addon::registerHook('addon_settings_post', 'addon/retriever/retriever.php', 'retriever_addon_settings_post');
Addon::registerHook('post_remote', 'addon/retriever/retriever.php', 'retriever_post_remote_hook');
Addon::registerHook('contact_photo_menu', 'addon/retriever/retriever.php', 'retriever_contact_photo_menu');
Addon::registerHook('cron', 'addon/retriever/retriever.php', 'retriever_cron');
if (Config::get('retriever', 'dbversion') == '0.10') {
q('ALTER TABLE `retriever_resource` MODIFY COLUMN `type` char(255) NULL DEFAULT NULL');
q('ALTER TABLE `retriever_resource` MODIFY COLUMN `data` mediumblob NULL DEFAULT NULL');
q('ALTER TABLE `retriever_rule` MODIFY COLUMN `data` mediumtext NULL DEFAULT NULL');
Config::set('retriever', 'dbversion', '0.11');
}
if (Config::get('retriever', 'dbversion') == '0.11') {
q('ALTER TABLE `retriever_resource` ADD INDEX `url` (`url`)');
q('ALTER TABLE `retriever_resource` ADD INDEX `completed` (`completed`)');
q('ALTER TABLE `retriever_item` ADD INDEX `finished` (`finished`)');
q('ALTER TABLE `retriever_item` ADD INDEX `item-uid` (`item-uid`)');
Config::set('retriever', 'dbversion', '0.12');
}
if (Config::get('retriever', 'dbversion') == '0.12') {
q("ALTER TABLE `retriever_resource` ADD COLUMN `contact-id` int(10) unsigned NOT NULL DEFAULT '0' AFTER `id`");
q("ALTER TABLE `retriever_resource` ADD COLUMN `item-uid` int(10) unsigned NOT NULL DEFAULT '0' AFTER `id`");
Config::set('retriever', 'dbversion', '0.13');
}
if (Config::get('retriever', 'dbversion') == '0.13') {
Config::set('retriever', 'downloads_per_cron', '100');
}
if (Config::get('retriever', 'dbversion') != '0.14') {
$schema = file_get_contents(dirname(__file__).'/database.sql');
$tables = explode(';', $schema);
@ -72,11 +51,11 @@ function retriever_install() {
* @brief Uninstallation hook for retriever plugin
*/
function retriever_uninstall() {
Addon::unregisterHook('plugin_settings', 'addon/retriever/retriever.php', 'retriever_plugin_settings');
Addon::unregisterHook('plugin_settings_post', 'addon/retriever/retriever.php', 'retriever_plugin_settings_post');
Addon::unregisterHook('addon_settings', 'addon/retriever/retriever.php', 'retriever_addon_settings');
Addon::unregisterHook('addon_settings_post', 'addon/retriever/retriever.php', 'retriever_addon_settings_post');
Addon::unregisterHook('post_remote', 'addon/retriever/retriever.php', 'retriever_post_remote_hook');
Addon::unregisterHook('plugin_settings', 'addon/retriever/retriever.php', 'retriever_plugin_settings');
Addon::unregisterHook('plugin_settings_post', 'addon/retriever/retriever.php', 'retriever_plugin_settings_post');
Addon::unregisterHook('addon_settings', 'addon/retriever/retriever.php', 'retriever_addon_settings');
Addon::unregisterHook('addon_settings_post', 'addon/retriever/retriever.php', 'retriever_addon_settings_post');
Addon::unregisterHook('contact_photo_menu', 'addon/retriever/retriever.php', 'retriever_contact_photo_menu');
Addon::unregisterHook('cron', 'addon/retriever/retriever.php', 'retriever_cron');
}
@ -90,16 +69,28 @@ function retriever_module() {}
/**
* @brief Admin page hook for retriever plugin
*
* @param App $a App object (by ref)
* @param string $o HTML to append content to (by ref)
*/
function retriever_addon_admin() {
$downloads_per_cron = Config::get('retriever', 'downloads_per_cron');
function retriever_addon_admin(&$a, &$o) {
$template = Renderer::getMarkupTemplate('admin.tpl', 'addon/retriever/');
$config = ['downloads_per_cron',
$downloads_per_cron = Config::get('retriever', 'downloads_per_cron');
$downloads_per_cron_config = ['downloads_per_cron',
L10n::t('Downloads per Cron'),
$downloads_per_cron,
L10n::t('Maximum number of downloads to attempt during each run of the cron job.')];
$allow_images = Config::get('retriever', 'allow_images');
$allow_images_config = ['allow_images',
L10n::t('Allow Retrieving Images'),
$allow_images,
L10n::t('Allow users to request images be downloaded as well as text.<br><b>Warning: the images are not automatically deleted and may fill up your database.</b>')];
$o .= Renderer::replaceMacros($template, [
'$downloads_per_cron' => $config,
'$downloads_per_cron' => $downloads_per_cron_config,
'$allow_images' => $allow_images_config,
'$submit' => L10n::t('Save Settings')]);
}
@ -110,6 +101,7 @@ function retriever_addon_admin_post () {
if (!empty($_POST['downloads_per_cron'])) {
Config::set('retriever', 'downloads_per_cron', $_POST['downloads_per_cron']);
}
Config::set('retriever', 'allow_images', $_POST['allow_images']);
}
/**
@ -125,6 +117,7 @@ function retriever_cron() {
retriever_tidy();
}
// This global variable is used to track the number of items that have been retrieved during the course of this process
$retriever_item_count = 0;
/**
@ -187,7 +180,7 @@ function retriever_clean_up_completed_resources($max_items) {
}
Logger::debug('retriever_clean_up_completed_resources: items waiting even though resource has completed: ' . count($r));
foreach ($r as $rr) {
$retriever_item = retriever_get_retriever_item($rr['item']);
$retriever_item = DBA::selectFirst('retriever_item', [], ['id' => intval($rr['item'])]);
if (!DBA::isResult($retriever_item)) {
Logger::warning('retriever_clean_up_completed_resources: no retriever item with id ' . $rr['item']);
continue;
@ -197,13 +190,14 @@ function retriever_clean_up_completed_resources($max_items) {
Logger::warning('retriever_clean_up_completed_resources: no item ' . $retriever_item['item-uri']);
continue;
}
$retriever_rule = get_retriever_rule($retriever_item['contact-id'], $item['uid']);
$retriever_rule = get_retriever_rule($retriever_item['contact-id'], $item['uid'], false);
if (!$retriever_rule) {
Logger::warning('retriever_clean_up_completed_resources: no retriever for uri ' . $retriever_item['item-uri'] . ' uid ' . $retriever_item['uid'] . ' ' . $retriever_item['contact-id']);
continue;
}
$resource = DBA::selectFirst('retriever_resource', [], ['id' => intval($rr['resource'])]);
retriever_apply_completed_resource_to_item($retriever_rule, $item, $resource);
// TODO: I don't really get how the $old_fields argument to DBA::update works
DBA::update('retriever_item', ['finished' => 1], ['id' => intval($retriever_item['id'])], ['finished' => 0]);
retriever_check_item_completed($item);
}
@ -223,6 +217,11 @@ function retriever_tidy() {
}
}
/**
* @brief Special case of retrieving a resource: if the URL is a data URL, do not use cURL, decode the URL directly
*
* @param array $resource The row from the retriever_resource table
*/
function retrieve_dataurl_resource($resource) {
if (!preg_match("/date:(.*);base64,(.*)/", $resource['url'], $matches)) {
Logger::warning('retrieve_dataurl_resource: resource ' . $resource['id'] . ' does not match pattern');
@ -232,26 +231,39 @@ function retrieve_dataurl_resource($resource) {
}
// Succeed or fail, there's no point retrying
q("UPDATE `retriever_resource` SET `last-try` = now(), `num-tries` = `num-tries` + 1, `completed` = now(), `data` = '%s', `type` = '%s' WHERE id = %d",
DBA::escape($resource['data']),
DBA::escape($resource['type']),
intval($resource['id']));
DBA::update('retriever_resource', ['id' => intval($resource['id'])], ['last-try' => DateTimeFormat::utcNow(), 'num-tries' => intval($resource['num-tries']) + 1, 'completed' => DateTimeFormat::utcNow(), 'data' => $resource['data'], 'type' => $resource['type']], ['last-try' => false]);
retriever_resource_completed($resource);
}
/**
* @brief Makes an attempt to retrieve the supplied resource, and updates the row in the table with the results
*
* @param array $resource The row from the retriever_resource table
*/
function retrieve_resource($resource) {
if (substr($resource['url'], 0, 5) == "data:") {
$components = parse_url($resource['url']);
if ($components['scheme'] == "data") {
return retrieve_dataurl_resource($resource);
}
if (($components['scheme'] != "http") && ($components['scheme'] != "https")) {
Logger::warning('retrieve_resource: URL scheme not supported for ' . $resource['url']);
DBA::update('retriever_resource', ['completed' => DateTimeFormat::utcNow()], ['id' => intval($resource['id'])], ['completed' => false]);
retriever_resource_completed($resource);
return;
}
$retriever_rule = get_retriever_rule($resource['contact-id'], $resource['item-uid']);
$retriever_rule = get_retriever_rule($resource['contact-id'], $resource['item-uid'], false);
if (!$retriever_rule) {
Logger::warning('retrieve_resource: no rule found for contact ' . $resource['contact-id'] . ' item ' . $resource['item-uid']);
Logger::warning('retrieve_resource: no rule found for resource id ' . $resource['id'] . ' contact ' . $resource['contact-id'] . ' item ' . $resource['item-uid']);
DBA::update('retriever_resource', ['completed' => DateTimeFormat::utcNow()], ['id' => intval($resource['id'])], ['completed' => false]);
retriever_resource_completed($resource);
return;
}
$rule_data = $retriever_rule['data'];
if (!$rule_data) {
Logger::warning('retrieve_resource: no rule data found for contact ' . $resource['contact-id'] . ' item ' . $resource['item-uid']);
Logger::warning('retrieve_resource: no rule data found for resource id ' . $resource['id'] . ' contact ' . $resource['contact-id'] . ' item ' . $resource['item-uid']);
DBA::update('retriever_resource', ['completed' => DateTimeFormat::utcNow()], ['id' => intval($resource['id'])], ['completed' => false]);
retriever_resource_completed($resource);
return;
}
@ -277,14 +289,22 @@ function retrieve_resource($resource) {
} catch (Exception $e) {
Logger::info('retrieve_resource: unable to retrieve ' . $resource['url'] . ' - ' . $e->getMessage());
}
DBA::update('retriever_resource', ['id' => intval($resource['id'])], ['last-try' => DateTimeFormat::utcNow(), 'num-tries' => intval($resource['num-tries']) + 1, 'http-code' => intval($resource['http-code']), 'redirect-url' => $resource['redirect-url']], ['last-try' => false]);
DBA::update('retriever_resource', ['last-try' => DateTimeFormat::utcNow(), 'num-tries' => intval($resource['num-tries']) + 1, 'http-code' => intval($resource['http-code']), 'redirect-url' => $resource['redirect-url']], ['id' => intval($resource['id'])], ['last-try' => false]);
if ($resource['data']) {
DBA::update('retriever_resource', ['id' => intval($resource['id'])], ['completed' => DateTimeFormat::utcNow(), 'data' => $resource['data'], 'type' => $resource['type']], ['completed' => false]);
DBA::update('retriever_resource', ['completed' => DateTimeFormat::utcNow(), 'data' => $resource['data'], 'type' => $resource['type']], ['id' => intval($resource['id'])], ['completed' => false]);
retriever_resource_completed($resource);
}
}
function get_retriever_rule($contact_id, $uid, $create = false) {
/**
* @brief Gets the retriever configuration for a particular contact. Optionally, will create a blank configuration.
*
* @param int $contact_id The Contact ID of the retriever configuration
* @param int $uid The User ID of the retriever configuration
* @param boolean $create Whether to create a new configuration if none exists already
* @return array The row from the retriever_rule database for this configuration
*/
function get_retriever_rule($contact_id, $uid, $create) {
$retriever_rule = DBA::selectFirst('retriever_rule', [], ['contact-id' => intval($contact_id), 'uid' => intval($uid)]);
if ($retriever_rule) {
$retriever_rule['data'] = json_decode($retriever_rule['data'], true);
@ -297,10 +317,12 @@ function get_retriever_rule($contact_id, $uid, $create = false) {
}
}
function retriever_get_retriever_item($id) {
return DBA::selectFirst('retriever_item', [], ['id' => intval($id)]);
}
/**
* @brief Looks up the item from the database that corresponds to the retriever_item
*
* @param array $retriever_item Row from the retriever_item table
* @return array Item that was found, or undef if no item could be found
*/
function retriever_get_item($retriever_item) {
$item = Item::selectFirst([], ['uri' => $retriever_item['item-uri'], 'uid' => intval($retriever_item['item-uid']), 'contact-id' => intval($retriever_item['contact-id'])]);
if (!DBA::isResult($item)) {
@ -310,10 +332,16 @@ function retriever_get_item($retriever_item) {
return $item;
}
/**
* @brief This function should be called when a resource is completed to trigger all next steps, based on the corresponding retriever item
*
* @param int $retriever_item_id ID of the retriever item corresponding to this resource
* @param array $resource The full details of the completed resource
*/
function retriever_item_completed($retriever_item_id, $resource) {
Logger::debug('retriever_item_completed: id ' . $retriever_item_id . ' url ' . $resource['url']);
$retriever_item = retriever_get_retriever_item($retriever_item_id);
$retriever_item = DBA::selectFirst('retriever_item', [], ['id' => intval($retriever_item_id)]);
if (!DBA::isResult($retriever_item)) {
Logger::info('retriever_item_completed: no retriever item with id ' . $retriever_item_id);
return;
@ -324,7 +352,7 @@ function retriever_item_completed($retriever_item_id, $resource) {
return;
}
// Note: the retriever might be null. Doesn't matter.
$retriever_rule = get_retriever_rule($retriever_item['contact-id'], $retriever_item['item-uid']);
$retriever_rule = get_retriever_rule($retriever_item['contact-id'], $retriever_item['item-uid'], false);
retriever_apply_completed_resource_to_item($retriever_rule, $item, $resource);
@ -332,6 +360,11 @@ function retriever_item_completed($retriever_item_id, $resource) {
retriever_check_item_completed($item);
}
/**
* @brief This function should be called when a resource is completed to trigger all next steps
*
* @param array $resource The full details of the completed resource
*/
function retriever_resource_completed($resource) {
Logger::debug('retriever_resource_completed: id ' . $resource['id'] . ' url ' . $resource['url']);
foreach (DBA::selectToArray('retriever_item', ['id'], ['resource' => intval($resource['id'])]) as $retriever_item) {
@ -339,6 +372,12 @@ function retriever_resource_completed($resource) {
}
}
/**
* @brief For a retriever config for a particular contact, remove existing artifacts for a number of completed items and queue them to be tried again. Will make the items invisible until they are again completed. The items chosen will be the most recently received.
*
* @param array $retriever The row from the retriever_rule table for the contact
* @param int $num The number of existing items to queue for retrieval
*/
function apply_retrospective($retriever, $num) {
foreach (Item::selectToArray([], ['contact-id' => intval($retriever['contact-id'])], ['order' => ['received' => true], 'limit' => $num]) as $item) {
Item::update(['visible' => 0], ['id' => intval($item['id'])]);
@ -388,6 +427,15 @@ function retriever_on_item_insert($retriever, &$item) {
$retriever_item_id = add_retriever_item($item, $resource);
}
/**
* @brief Creates a new resource to be downloaded from the supplied URL. Unique resources are created for each URL, UID and contact ID, because different contact IDs may have different rules for how to retrieve them. If the URL is actually a data URL, the resource is completed immediately.
*
* @param string $url URL of the resource to be downloaded
* @param int $uid User ID that this resource is being downloaded fore
* @param int $cid Contact ID of the item that triggered the downloading of this resource
* @param boolean $binary Specifies if this download should be done in binary mode
* @return array The created resource
*/
function add_retriever_resource($url, $uid, $cid, $binary = false) {
Logger::debug('add_retriever_resource: url ' . $url . ' uid ' . $uid . ' contact-id ' . $cid);
@ -429,12 +477,18 @@ function add_retriever_resource($url, $uid, $cid, $binary = false) {
return DBA::selectFirst('retriever_resource', [], ['url' => $url, 'item-uid' => intval($uid), 'contact-id' => intval($cid)]);
}
function add_retriever_item(&$item, $resource) {
/**
* @brief Adds a retriever item for the supplied resource and item, to mark that this item should wait for the resource to be completed. Does not create a retriever item if a matching one already exists.
*
* @param array $item Item that is waiting for the resource. This may or may not have been already stored in the database.
* @param array $resource Resource that the item needs to wait for. This must have already been stored in the database.
* @return int ID of the retriever item that was created, or the existing one if present
*/
function add_retriever_item($item, $resource) {
Logger::debug('add_retriever_item: ' . $resource['url'] . ' for ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']);
if (!array_key_exists('id', $resource) || !$resource['id']) {
Logger::warning('add_retriever_item: resource is empty');
//@@@ check that this does not happen
return;
}
if (DBA::selectFirst('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => intval($item['uid']), 'resource' => intval($resource['id'])])) {
@ -451,6 +505,12 @@ function add_retriever_item(&$item, $resource) {
return $retriever_item['id'];
}
/**
* @brief Analyse a completed text resource (such as HTML) for the character encoding used
*
* @param array $resource The completed resource
* @return string Character encoding, e.g. "utf-8" or "iso-8859-1"
*/
function retriever_get_encoding($resource) {
$matches = array();
if (preg_match('/charset=(.*)/', $resource['type'], $matches)) {
@ -459,6 +519,13 @@ function retriever_get_encoding($resource) {
return 'utf-8';
}
/**
* @brief Apply the XSLT template to the DOM document
*
* @param string $xslt_text Text of the XSLT template
* @param DOMDocument $doc Input to the XSLT template
* @return DOMDocument Result of applying the template
*/
function retriever_apply_xslt_text($xslt_text, $doc) {
if (!$xslt_text) {
Logger::info('retriever_apply_xslt_text: empty XSLT text');
@ -475,6 +542,13 @@ function retriever_apply_xslt_text($xslt_text, $doc) {
return $result;
}
/**
* @brief Applies the retriever rules to the downloaded resource, and stores the results as the new body text of the item
*
* @param array $retriever Retriever rules as stored in the database, with the "data" element already decoded from JSON
* @param array &$item Item to be in which to store the new body (by ref). This may or may not be already stored in the database.
* @param array $resource Newly completed resource, which should be text (HTML or XML)
*/
function retriever_apply_dom_filter($retriever, &$item, $resource) {
Logger::debug('retriever_apply_dom_filter: applying XSLT to uri ' . $item['uri'] . ' uid ' . $item['uid'] . ' contact ' . $item['contact-id']);
@ -514,6 +588,11 @@ function retriever_apply_dom_filter($retriever, &$item, $resource) {
retriever_set_body($item, $body);
}
/**
* @brief Converts the completed resource, which must be HTML or XML, into a DOM document
*
* @param array $resource The resource containing the text content
*/
function retriever_load_into_dom($resource) {
$encoding = retriever_get_encoding($resource);
$content = mb_convert_encoding($resource['data'], 'HTML-ENTITIES', $encoding);
@ -527,6 +606,13 @@ function retriever_load_into_dom($resource) {
return $doc;
}
/**
* @brief Applies the retriever rules, including configuration for included and excluded portions, to the DOM document
*
* @param DOMDocument $doc The original DOM document downloaded from the link
* @param array $retriever The retriever configuration for this contact
* @return DOMDocument New DOM document containing only the desired content
*/
function retriever_extract($doc, $retriever) {
$params = array('$spec' => $retriever['data']);
$extract_template = Renderer::getMarkupTemplate('extract.tpl', 'addon/retriever/');
@ -542,6 +628,13 @@ function retriever_extract($doc, $retriever) {
return $doc;
}
/**
* @brief Converts local URLs in the DOM document to global URLs
*
* @param DOMDocument $doc DOM document potentially containing links
* @param array $resource Completed resource which contains the text in the DOM document
* @return DOMDocument New DOM document with global URLs
*/
function retriever_globalise_urls($doc, $resource) {
$components = parse_url($resource['redirect-url']);
$rooturl = $components['scheme'] . "://" . $components['host'];
@ -553,6 +646,11 @@ function retriever_globalise_urls($doc, $resource) {
return $doc;
}
/**
* @brief Returns the body text for the supplied item. If the item has already been stored in the database, this will fetch the content from the database rather than from the supplied array.
*
* @param array $item Row from the item table
*/
function retriever_get_body($item) {
if (!array_key_exists('uri-id', $item) || !$item['uri-id']) {
// item has not yet been stored in database
@ -567,21 +665,21 @@ function retriever_get_body($item) {
}
if (!$content['body']) {
Logger::warning('retriever_get_body: item-content uri-id ' . $item['uri-id'] . ' has no body');
//@@@ check never happens
return $item['body'];
}
if ($content['body'] != $item['body']) {
Logger::warning('@@@ this is probably bad @@@ content: ' . $content['body'] . ' @@@ item: ' . $item['body']);
//@@@ check for this.
}
return $content['body'];
}
function retriever_set_body(&$item, $body, $allow_empty = false) {
if (!$body && !$allow_empty) {
Logger::debug('retriever_set_body: will not set empty body in item id ' . $item['id'] . ' uri ' . $item['uri']);
return;
}
/**
* @brief Updates the item with the supplied body text. If the item has already been stored in the database, this will update the database too.
*
* @param array &$item Item in which to set the body (by ref). This may or may not be already stored in the database.
* @param string $body New body content
*/
function retriever_set_body(&$item, $body) {
$item['body'] = $body;
if (!array_key_exists('id', $item) || !$item['id']) {
// item has not yet been stored in database
@ -593,9 +691,13 @@ function retriever_set_body(&$item, $body, $allow_empty = false) {
/**
* @brief Searches for images in the item and adds corresponding retriever_items. If the images have already been downloaded, updates the body in the supplied item array.
*
* @param array &$item Row from the item table (by ref)
* @param array &$item Item to be searched for images and updated (by ref). This may or may not be already stored in the database.
*/
function retrieve_images(&$item) {
if (!Config::get('retriever', 'allow_images')) {
return;
}
$body = retriever_get_body($item);
if (!strlen($body)) {
Logger::warning('retrieve_images: no body for item ' . $item['uri']);
@ -612,7 +714,7 @@ function retrieve_images(&$item) {
if (!$url) {
continue;
}
if (strpos($url, get_app()->getBaseUrl()) === FALSE) {
if (strpos($url, System::baseUrl()) === FALSE) {
$resource = add_retriever_resource($url, $item['uid'], $item['contact-id'], true);
if (!$resource['completed']) {
add_retriever_item($item, $resource);
@ -624,6 +726,11 @@ function retrieve_images(&$item) {
}
}
/**
* @brief Checks if an item has been completed, i.e. all its associated retriever_item rows have been retrieved. If so, update the item to be visible again.
*
* @param array &$item Row from the item table (by ref)
*/
function retriever_check_item_completed(&$item)
{
$waiting = DBA::selectFirst('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => intval($item['uid']), 'contact-id' => intval($item['contact-id']), 'finished' => 0]);
@ -636,6 +743,13 @@ function retriever_check_item_completed(&$item)
}
}
/**
* @brief Updates an item with a completed resource. If the resource was text, update the body with the new content. If the resource was an image, replace remote images in the body with a local version.
*
* @param array $retriever Rule configuration for this contact
* @param array &$item Row from the item table (by ref)
* @param array $resource The resource that has just been completed
*/
function retriever_apply_completed_resource_to_item($retriever, &$item, $resource) {
Logger::debug('retriever_apply_completed_resource_to_item: retriever ' . ($retriever ? $retriever['id'] : 'none') . ' resource ' . $resource['url'] . ' plink ' . $item['plink']);
if (strpos($resource['type'], 'image') !== false) {
@ -659,8 +773,9 @@ function retriever_apply_completed_resource_to_item($retriever, &$item, $resourc
*
* @param array &$item Row from the item table (by ref)
* @param array $resource Row from the resource table containing successfully downloaded image
*
* TODO: split this into two functions, one to store the image, the other to change the item body
*/
// TODO: split this into two functions, one to store the image, the other to change the item body
function retriever_transform_images(&$item, $resource) {
if (!$resource['data']) {
Logger::info('retriever_transform_images: no data available for ' . $resource['id'] . ' ' . $resource['url']);
@ -698,6 +813,11 @@ function retriever_transform_images(&$item, $resource) {
retriever_set_body($item, $body);
}
/**
* @brief Displays the retriever configuration page for a contact. Alternatively, if the user clicked the "help" button, display the help content.
*
* @param App $a The App object
*/
function retriever_content($a) {
if (!local_user()) {
$a->page['content'] .= "<p>Please log in</p>";
@ -706,11 +826,11 @@ function retriever_content($a) {
if ($a->argv[1] === 'help') {
$feeds = DBA::selectToArray('contact', ['id', 'name', 'thumb'], ['uid' => local_user(), 'network' => 'feed']);
for ($i = 0; $i < count($feeds); ++$i) {
$feeds[$i]['url'] = $a->getBaseUrl() . '/retriever/' . $feeds[$i]['id'];
$feeds[$i]['url'] = System::baseUrl() . '/retriever/' . $feeds[$i]['id'];
}
$template = Renderer::getMarkupTemplate('/help.tpl', 'addon/retriever/');
$a->page['content'] .= Renderer::replaceMacros($template, array(
'$config' => $a->getBaseUrl() . '/settings/addon',
'$config' => $a->getBaseUrl . '/settings/addon',
'$feeds' => $feeds));
return;
}
@ -774,6 +894,7 @@ function retriever_content($a) {
L10n::t('URL Replace'),
$retriever_rule['data']['replace'],
L10n::t('Text to replace matching part of above regular expression')),
'$allow_images' => Config::get('retriever', 'allow_images'),
'$images' => array(
'retriever_images',
L10n::t('Download Images'),
@ -799,7 +920,7 @@ function retriever_content($a) {
$retriever_rule['data']['customxslt'],
L10n::t("When standard rules aren't enough, apply custom XSLT to the article")),
'$title' => L10n::t('Retrieve Feed Content'),
'$help' => $a->getBaseUrl() . '/retriever/help',
'$help' => $a->getBaseUrl . '/retriever/help',
'$help_t' => L10n::t('Get Help'),
'$submit_t' => L10n::t('Submit'),
'$submit' => L10n::t('Save Settings'),
@ -817,15 +938,27 @@ function retriever_content($a) {
}
}
/**
* @brief Hook that adds the retriever option to the contact menu
*
* @param App $a The App object
* @param array $args Contact menu details to be filled in (by ref)
*/
function retriever_contact_photo_menu($a, &$args) {
if (!$args) {
return;
}
if ($args["contact"]["network"] == "feed") {
$args["menu"][ 'retriever' ] = array(L10n::t('Retriever'), $a->getBaseUrl() . '/retriever/' . $args["contact"]['id']);
$args["menu"]['retriever'] = array(L10n::t('Retriever'), System::baseUrl() . '/retriever/' . $args["contact"]['id']);
}
}
/**
* @brief Hook for processing new incoming items
*
* @param App $a The App object (by ref)
* @param array $item New item, which has not yet been inserted into database (by ref)
*/
function retriever_post_remote_hook(&$a, &$item) {
Logger::info('retriever_post_remote_hook: ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']);
@ -847,37 +980,48 @@ function retriever_post_remote_hook(&$a, &$item) {
retriever_check_item_completed($item);
}
function retriever_plugin_settings(&$a, &$s) {
/**
* @brief Hook for adding per-user retriever settings to the user's settings page
*
* @param App $a The App object (by ref)
* @param string $s HTML string to which to append settings content (by ref)
*/
function retriever_addon_settings(&$a, &$s) {
$all_photos = PConfig::get(local_user(), 'retriever', 'all_photos');
$oembed = PConfig::get(local_user(), 'retriever', 'oembed');
$template = Renderer::getMarkupTemplate('/settings.tpl', 'addon/retriever/');
$s .= Renderer::replaceMacros($template, array(
'$allphotos' => array(
'retriever_all_photos',
$config = array('$submit' => L10n::t('Save Settings'),
'$title' => L10n::t('Retriever Settings'),
'$help' => $a->getBaseUrl . '/retriever/help',
'$allow_images' => Config::get('retriever', 'allow_images'));
$config['$allphotos'] = array('retriever_all_photos',
L10n::t('All Photos'),
$all_photos,
L10n::t('Check this to retrieve photos for all posts')),
'$oembed' => array(
'retriever_oembed',
L10n::t('Check this to retrieve photos for all posts'));
$config['$oembed'] = array('retriever_oembed',
L10n::t('Resolve OEmbed'),
$oembed,
L10n::t('Check this to attempt to retrieve embedded content for all posts - useful e.g. for Facebook posts')),
'$submit' => L10n::t('Save Settings'),
'$title' => L10n::t('Retriever Settings'),
'$help' => $a->getBaseUrl() . '/retriever/help'));
L10n::t('Check this to attempt to retrieve embedded content for all posts'));
$s .= Renderer::replaceMacros($template, $config);
}
function retriever_plugin_settings_post($a,$post) {
if ($_POST['retriever_all_photos']) {
PConfig::set(local_user(), 'retriever', 'all_photos', $_POST['retriever_all_photos']);
/**
* @brief Hook for processing post results from user's settings page
*
* @param App $a The App object
* @param array $post Posted content
*/
function retriever_addon_settings_post($a, $post) {
if ($post['retriever_all_photos']) {
PConfig::set(local_user(), 'retriever', 'all_photos', $post['retriever_all_photos']);
}
else {
PConfig::del(local_user(), 'retriever', 'all_photos');
PConfig::delete(local_user(), 'retriever', 'all_photos');
}
if ($_POST['retriever_oembed']) {
PConfig::set(local_user(), 'retriever', 'oembed', $_POST['retriever_oembed']);
if ($post['retriever_oembed']) {
PConfig::set(local_user(), 'retriever', 'oembed', $post['retriever_oembed']);
}
else {
PConfig::del(local_user(), 'retriever', 'oembed');
PConfig::delete(local_user(), 'retriever', 'oembed');
}
}

View file

@ -4,5 +4,6 @@
*
*}}
{{include file="field_input.tpl" field=$downloads_per_cron}}
{{include file="field_checkbox.tpl" field=$allow_images}}
<div class="submit"><input type="submit" name="page_site" value="{{$submit}}"></div>

View file

@ -142,7 +142,9 @@ document.addEventListener('DOMContentLoaded', function() {
{{include file="field_checkbox.tpl" field=$modurl}}
{{include file="field_input.tpl" field=$pattern}}
{{include file="field_input.tpl" field=$replace}}
{{if $allow_images}}
{{include file="field_checkbox.tpl" field=$images}}
{{/if}}
{{include file="field_textarea.tpl" field=$customxslt}}
{{include file="field_checkbox.tpl" field=$storecookies}}
{{include file="field_textarea.tpl" field=$cookiedata}}

View file

@ -1,9 +1,16 @@
<div class="settings-block">
<span id="settings_retriever_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose('settings_retriever_expanded'); openClose('settings_retriever_inflated');">
<h3>{{$title}}</h3>
</span>
<div id="settings_retriever_expanded" class="settings-block" style="display: none;">
<span class="fakelink" onclick="openClose('settings_retriever_expanded'); openClose('settings_retriever_inflated');">
<h3>{{$title}}</h3>
</span>
<p>
<a href="{{$help}}">Get Help</a>
</p>
{{if $allow_images}}
{{include file="field_checkbox.tpl" field=$allphotos}}
{{/if}}
{{include file="field_checkbox.tpl" field=$oembed}}
<input type="submit" value="{{$submit}}">
</div>