add types to parameters

This commit is contained in:
Matthew Exon 2022-10-15 18:02:43 +02:00
parent 1ed6f2b851
commit 49c86e3e8f

View file

@ -77,19 +77,19 @@ function retriever_uninstall() {
} }
/** /**
* @brief Module hook for retriever plugin * This is a statement rather than an actual function definition. The simple
* * existence of this method is checked to figure out if the addon offers a
* TODO: figure out what this should be used for * module.
*/ */
function retriever_module() {} function retriever_module() {}
/** /**
* @brief Admin page hook for retriever plugin * @brief Admin page hook for retriever plugin
* *
* @param App $a App object (by ref) * @param App $a App object (unused)
* @param string $o HTML to append content to (by ref) * @param string $o HTML to append content to (by ref)
*/ */
function retriever_addon_admin(&$a, &$o) { function retriever_addon_admin(App $a, string &$o) {
$template = Renderer::getMarkupTemplate('admin.tpl', 'addon/retriever/'); $template = Renderer::getMarkupTemplate('admin.tpl', 'addon/retriever/');
$downloads_per_cron = DI::config()->get('retriever', 'downloads_per_cron'); $downloads_per_cron = DI::config()->get('retriever', 'downloads_per_cron');
@ -141,7 +141,7 @@ $retriever_item_count = 0;
* *
* @param int $max_items Maximum number of items to retrieve in this call * @param int $max_items Maximum number of items to retrieve in this call
*/ */
function retriever_retrieve_items($max_items) { function retriever_retrieve_items(int $max_items) {
global $retriever_item_count; global $retriever_item_count;
$retriever_schedule = array(array(1,'minute'), $retriever_schedule = array(array(1,'minute'),
@ -187,7 +187,7 @@ function retriever_retrieve_items($max_items) {
* *
* @param int $max_items Maximum number of items to retrieve in this call * @param int $max_items Maximum number of items to retrieve in this call
*/ */
function retriever_clean_up_completed_resources($max_items) { function retriever_clean_up_completed_resources(int $max_items) {
// TODO: figure out how to do this with DBA module // TODO: figure out how to do this with DBA module
$r = DBA::p("SELECT retriever_resource.`id` as resource, retriever_item.`id` as item FROM retriever_resource, retriever_item, retriever_rule WHERE retriever_item.`finished` = 0 AND retriever_item.`resource` = retriever_resource.`id` AND retriever_resource.`completed` IS NOT NULL AND retriever_item.`contact-id` = retriever_rule.`contact-id` AND retriever_item.`item-uid` = retriever_rule.`uid` LIMIT $max_items"); $r = DBA::p("SELECT retriever_resource.`id` as resource, retriever_item.`id` as item FROM retriever_resource, retriever_item, retriever_rule WHERE retriever_item.`finished` = 0 AND retriever_item.`resource` = retriever_resource.`id` AND retriever_resource.`completed` IS NOT NULL AND retriever_item.`contact-id` = retriever_rule.`contact-id` AND retriever_item.`item-uid` = retriever_rule.`uid` LIMIT $max_items");
if (!DBA::isResult($r)) { if (!DBA::isResult($r)) {
@ -240,7 +240,7 @@ function retriever_tidy() {
* *
* @param array $resource The row from the retriever_resource table * @param array $resource The row from the retriever_resource table
*/ */
function retrieve_dataurl_resource($resource) { function retrieve_dataurl_resource(array $resource) {
if (!preg_match("/date:(.*);base64,(.*)/", $resource['url'], $matches)) { if (!preg_match("/date:(.*);base64,(.*)/", $resource['url'], $matches)) {
Logger::warning('retrieve_dataurl_resource: resource ' . $resource['id'] . ' does not match pattern'); Logger::warning('retrieve_dataurl_resource: resource ' . $resource['id'] . ' does not match pattern');
} else { } else {
@ -258,7 +258,7 @@ function retrieve_dataurl_resource($resource) {
* *
* @param array $resource The row from the retriever_resource table * @param array $resource The row from the retriever_resource table
*/ */
function retrieve_resource($resource) { function retrieve_resource(array $resource) {
$components = parse_url($resource['url']); $components = parse_url($resource['url']);
if (!$components) { if (!$components) {
Logger::warning('retrieve_resource: URL ' . $resource['url'] . ' could not be parsed'); Logger::warning('retrieve_resource: URL ' . $resource['url'] . ' could not be parsed');
@ -325,7 +325,7 @@ function retrieve_resource($resource) {
* @param boolean $create Whether to create a new configuration if none exists already * @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 * @return array The row from the retriever_rule database for this configuration
*/ */
function get_retriever_rule($contact_id, $uid, $create) { function get_retriever_rule(string $contact_id, string $uid, bool $create) {
$retriever_rule = DBA::selectFirst('retriever_rule', [], ['contact-id' => intval($contact_id), 'uid' => intval($uid)]); $retriever_rule = DBA::selectFirst('retriever_rule', [], ['contact-id' => intval($contact_id), 'uid' => intval($uid)]);
if ($retriever_rule) { if ($retriever_rule) {
$retriever_rule['data'] = json_decode($retriever_rule['data'], true); $retriever_rule['data'] = json_decode($retriever_rule['data'], true);
@ -344,7 +344,7 @@ function get_retriever_rule($contact_id, $uid, $create) {
* @param array $retriever_item Row from the retriever_item table * @param array $retriever_item Row from the retriever_item table
* @return array Item that was found, or undef if no item could be found * @return array Item that was found, or undef if no item could be found
*/ */
function retriever_get_item($retriever_item) { function retriever_get_item(array $retriever_item) {
$item = Post::selectFirst([], ['uri' => $retriever_item['item-uri'], 'uid' => intval($retriever_item['item-uid']), 'contact-id' => intval($retriever_item['contact-id'])]); $item = Post::selectFirst([], ['uri' => $retriever_item['item-uri'], 'uid' => intval($retriever_item['item-uid']), 'contact-id' => intval($retriever_item['contact-id'])]);
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
Logger::warning('retriever_get_item: no item found for uri ' . $retriever_item['item-uri']); Logger::warning('retriever_get_item: no item found for uri ' . $retriever_item['item-uri']);
@ -359,7 +359,7 @@ function retriever_get_item($retriever_item) {
* @param int $retriever_item_id ID of the retriever item corresponding to this resource * @param int $retriever_item_id ID of the retriever item corresponding to this resource
* @param array $resource The full details of the completed resource * @param array $resource The full details of the completed resource
*/ */
function retriever_item_completed($retriever_item_id, $resource) { function retriever_item_completed(string $retriever_item_id, array $resource) {
Logger::debug('retriever_item_completed: id ' . $retriever_item_id . ' url ' . $resource['url']); Logger::debug('retriever_item_completed: id ' . $retriever_item_id . ' url ' . $resource['url']);
$retriever_item = DBA::selectFirst('retriever_item', [], ['id' => intval($retriever_item_id)]); $retriever_item = DBA::selectFirst('retriever_item', [], ['id' => intval($retriever_item_id)]);
@ -386,7 +386,7 @@ function retriever_item_completed($retriever_item_id, $resource) {
* *
* @param array $resource The full details of the completed resource * @param array $resource The full details of the completed resource
*/ */
function retriever_resource_completed($resource) { function retriever_resource_completed(array $resource) {
Logger::debug('retriever_resource_completed: id ' . $resource['id'] . ' url ' . $resource['url']); Logger::debug('retriever_resource_completed: id ' . $resource['id'] . ' url ' . $resource['url']);
foreach (DBA::selectToArray('retriever_item', ['id'], ['resource' => intval($resource['id'])]) as $retriever_item) { foreach (DBA::selectToArray('retriever_item', ['id'], ['resource' => intval($resource['id'])]) as $retriever_item) {
retriever_item_completed($retriever_item['id'], $resource); retriever_item_completed($retriever_item['id'], $resource);
@ -399,7 +399,7 @@ function retriever_resource_completed($resource) {
* @param array $retriever The row from the retriever_rule table for the contact * @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 * @param int $num The number of existing items to queue for retrieval
*/ */
function apply_retrospective($retriever, $num) { function apply_retrospective(array $retriever, int $num) {
foreach (Post::selectToArray([], ['contact-id' => intval($retriever['contact-id'])], ['order' => ['received' => true], 'limit' => $num]) as $item) { foreach (Post::selectToArray([], ['contact-id' => intval($retriever['contact-id'])], ['order' => ['received' => true], 'limit' => $num]) as $item) {
Item::update(['visible' => 0], ['id' => intval($item['id'])]); Item::update(['visible' => 0], ['id' => intval($item['id'])]);
foreach (DBA::selectToArray('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => $item['uid'], 'contact-id' => $item['contact-id']]) as $retriever_item) { foreach (DBA::selectToArray('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => $item['uid'], 'contact-id' => $item['contact-id']]) as $retriever_item) {
@ -418,7 +418,7 @@ function apply_retrospective($retriever, $num) {
* *
* TODO: This queries then inserts. It should use some kind of lock to avoid requesting the same resource twice. * TODO: This queries then inserts. It should use some kind of lock to avoid requesting the same resource twice.
*/ */
function retriever_on_item_insert($retriever, &$item) { function retriever_on_item_insert(array $retriever, array &$item) {
if (!$retriever || !$retriever['id']) { if (!$retriever || !$retriever['id']) {
Logger::info('retriever_on_item_insert: No retriever supplied'); Logger::info('retriever_on_item_insert: No retriever supplied');
return; return;
@ -457,7 +457,7 @@ function retriever_on_item_insert($retriever, &$item) {
* @param boolean $binary Specifies if this download should be done in binary mode * @param boolean $binary Specifies if this download should be done in binary mode
* @return array The created resource * @return array The created resource
*/ */
function add_retriever_resource($url, $uid, $cid, $binary = false) { function add_retriever_resource(string $url, string $uid, string $cid, bool $binary = false) {
Logger::debug('add_retriever_resource: url ' . $url . ' uid ' . $uid . ' contact-id ' . $cid); Logger::debug('add_retriever_resource: url ' . $url . ' uid ' . $uid . ' contact-id ' . $cid);
$scheme = parse_url($url, PHP_URL_SCHEME); $scheme = parse_url($url, PHP_URL_SCHEME);
@ -505,7 +505,7 @@ function add_retriever_resource($url, $uid, $cid, $binary = false) {
* @param array $resource Resource that the item needs to wait for. This must have already been 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 * @return int ID of the retriever item that was created, or the existing one if present
*/ */
function add_retriever_item($item, $resource) { function add_retriever_item(array $item, array $resource) {
Logger::debug('add_retriever_item: ' . $resource['url'] . ' for ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']); Logger::debug('add_retriever_item: ' . $resource['url'] . ' for ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']);
if (!array_key_exists('id', $resource) || !$resource['id']) { if (!array_key_exists('id', $resource) || !$resource['id']) {
@ -532,7 +532,7 @@ function add_retriever_item($item, $resource) {
* @param array $resource The completed resource * @param array $resource The completed resource
* @return string Character encoding, e.g. "utf-8" or "iso-8859-1" * @return string Character encoding, e.g. "utf-8" or "iso-8859-1"
*/ */
function retriever_get_encoding($resource) { function retriever_get_encoding(array $resource) {
$matches = array(); $matches = array();
if (preg_match('/charset=(.*)/', $resource['type'], $matches)) { if (preg_match('/charset=(.*)/', $resource['type'], $matches)) {
return trim(array_pop($matches)); return trim(array_pop($matches));
@ -547,7 +547,7 @@ function retriever_get_encoding($resource) {
* @param DOMDocument $doc Input to the XSLT template * @param DOMDocument $doc Input to the XSLT template
* @return DOMDocument Result of applying the template * @return DOMDocument Result of applying the template
*/ */
function retriever_apply_xslt_text($xslt_text, $doc) { function retriever_apply_xslt_text(string $xslt_text, DOMDocument $doc) {
if (!$xslt_text) { if (!$xslt_text) {
Logger::info('retriever_apply_xslt_text: empty XSLT text'); Logger::info('retriever_apply_xslt_text: empty XSLT text');
return $doc; return $doc;
@ -570,7 +570,7 @@ function retriever_apply_xslt_text($xslt_text, $doc) {
* @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 &$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) * @param array $resource Newly completed resource, which should be text (HTML or XML)
*/ */
function retriever_apply_dom_filter($retriever, &$item, $resource) { function retriever_apply_dom_filter(array $retriever, array &$item, array $resource) {
Logger::debug('retriever_apply_dom_filter: applying XSLT to uri ' . $item['uri'] . ' uid ' . $item['uid'] . ' contact ' . $item['contact-id']); Logger::debug('retriever_apply_dom_filter: applying XSLT to uri ' . $item['uri'] . ' uid ' . $item['uid'] . ' contact ' . $item['contact-id']);
if (!array_key_exists('include', $retriever['data']) && !array_key_exists('customxslt', $retriever['data'])) { if (!array_key_exists('include', $retriever['data']) && !array_key_exists('customxslt', $retriever['data'])) {
@ -614,7 +614,7 @@ function retriever_apply_dom_filter($retriever, &$item, $resource) {
* *
* @param array $resource The resource containing the text content * @param array $resource The resource containing the text content
*/ */
function retriever_load_into_dom($resource) { function retriever_load_into_dom(array $resource) {
$encoding = retriever_get_encoding($resource); $encoding = retriever_get_encoding($resource);
$content = mb_convert_encoding($resource['data'], 'HTML-ENTITIES', $encoding); $content = mb_convert_encoding($resource['data'], 'HTML-ENTITIES', $encoding);
$doc = new DOMDocument('1.0', 'UTF-8'); $doc = new DOMDocument('1.0', 'UTF-8');
@ -634,7 +634,7 @@ function retriever_load_into_dom($resource) {
* @param array $retriever The retriever configuration for this contact * @param array $retriever The retriever configuration for this contact
* @return DOMDocument New DOM document containing only the desired content * @return DOMDocument New DOM document containing only the desired content
*/ */
function retriever_extract($doc, $retriever) { function retriever_extract(DOMDocument $doc, array $retriever) {
$params = array('$spec' => $retriever['data']); $params = array('$spec' => $retriever['data']);
$extract_template = Renderer::getMarkupTemplate('extract.tpl', 'addon/retriever/'); $extract_template = Renderer::getMarkupTemplate('extract.tpl', 'addon/retriever/');
$extract_xslt = Renderer::replaceMacros($extract_template, $params); $extract_xslt = Renderer::replaceMacros($extract_template, $params);
@ -656,7 +656,7 @@ function retriever_extract($doc, $retriever) {
* @param array $resource Completed resource which contains the text in the DOM document * @param array $resource Completed resource which contains the text in the DOM document
* @return DOMDocument New DOM document with global URLs * @return DOMDocument New DOM document with global URLs
*/ */
function retriever_globalise_urls($doc, $resource) { function retriever_globalise_urls(DOMDocument $doc, array $resource) {
$components = parse_url($resource['redirect-url']); $components = parse_url($resource['redirect-url']);
if (!array_key_exists('scheme', $components) || !array_key_exists('host', $components) || !array_key_exists('path', $components)) { if (!array_key_exists('scheme', $components) || !array_key_exists('host', $components) || !array_key_exists('path', $components)) {
return $doc; return $doc;
@ -675,7 +675,7 @@ function retriever_globalise_urls($doc, $resource) {
* *
* @param array $item Row from the item table * @param array $item Row from the item table
*/ */
function retriever_get_body($item) { function retriever_get_body(array $item) {
if (!array_key_exists('uri-id', $item) || !$item['uri-id']) { if (!array_key_exists('uri-id', $item) || !$item['uri-id']) {
// item has not yet been stored in database // item has not yet been stored in database
return $item['body']; return $item['body'];
@ -703,7 +703,7 @@ function retriever_get_body($item) {
* @param array &$item Item in which to set the body (by ref). This may or may not be already stored in the database. * @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 * @param string $body New body content
*/ */
function retriever_set_body(&$item, $body) { function retriever_set_body(array &$item, string $body) {
$item['body'] = $body; $item['body'] = $body;
if (!array_key_exists('id', $item) || !$item['id']) { if (!array_key_exists('id', $item) || !$item['id']) {
// item has not yet been stored in database // item has not yet been stored in database
@ -717,7 +717,7 @@ function retriever_set_body(&$item, $body) {
* *
* @param array &$item Item to be searched for images and updated (by ref). This may or may not be already stored in the database. * @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) { function retrieve_images(array &$item) {
if (!DI::config()->get('retriever', 'allow_images')) { if (!DI::config()->get('retriever', 'allow_images')) {
return; return;
} }
@ -755,7 +755,7 @@ function retrieve_images(&$item) {
* *
* @param array &$item Row from the item table (by ref) * @param array &$item Row from the item table (by ref)
*/ */
function retriever_check_item_completed(&$item) function retriever_check_item_completed(array &$item)
{ {
$waiting = DBA::selectFirst('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => intval($item['uid']), 'contact-id' => intval($item['contact-id']), 'finished' => 0]); $waiting = DBA::selectFirst('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => intval($item['uid']), 'contact-id' => intval($item['contact-id']), 'finished' => 0]);
Logger::debug('retriever_check_item_completed: item ' . $item['uri'] . ' ' . $item['uid'] . ' '. $item['contact-id'] . ' waiting for resources'); Logger::debug('retriever_check_item_completed: item ' . $item['uri'] . ' ' . $item['uid'] . ' '. $item['contact-id'] . ' waiting for resources');
@ -774,7 +774,7 @@ function retriever_check_item_completed(&$item)
* @param array &$item Row from the item table (by ref) * @param array &$item Row from the item table (by ref)
* @param array $resource The resource that has just been completed * @param array $resource The resource that has just been completed
*/ */
function retriever_apply_completed_resource_to_item($retriever, &$item, $resource) { function retriever_apply_completed_resource_to_item(array $retriever, array &$item, array $resource) {
Logger::debug('retriever_apply_completed_resource_to_item: retriever ' . ($retriever ? $retriever['id'] : 'none') . ' resource ' . $resource['url'] . ' plink ' . $item['plink']); 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) { if (strpos($resource['type'], 'image') !== false) {
retriever_transform_images($item, $resource); retriever_transform_images($item, $resource);
@ -800,7 +800,7 @@ function retriever_apply_completed_resource_to_item($retriever, &$item, $resourc
* *
* 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) { function retriever_transform_images(array &$item, array $resource) {
if (!$resource['data']) { if (!$resource['data']) {
Logger::info('retriever_transform_images: no data available for ' . $resource['id'] . ' ' . $resource['url']); Logger::info('retriever_transform_images: no data available for ' . $resource['id'] . ' ' . $resource['url']);
return; return;
@ -842,7 +842,7 @@ function retriever_transform_images(&$item, $resource) {
* *
* @param App $a The App object * @param App $a The App object
*/ */
function retriever_content($a) { function retriever_content(App $a) {
if (!local_user()) { if (!local_user()) {
$a->page['content'] .= "<p>Please log in</p>"; $a->page['content'] .= "<p>Please log in</p>";
return; return;
@ -973,7 +973,7 @@ function retriever_content($a) {
* @param App $a The App object * @param App $a The App object
* @param array $args Contact menu details to be filled in (by ref) * @param array $args Contact menu details to be filled in (by ref)
*/ */
function retriever_contact_photo_menu($a, &$args) { function retriever_contact_photo_menu(App $a, array &$args) {
if (!$args) { if (!$args) {
return; return;
} }
@ -988,7 +988,7 @@ function retriever_contact_photo_menu($a, &$args) {
* @param App $a The App object (by ref) * @param App $a The App object (by ref)
* @param array $item New item, which has not yet been inserted into database (by ref) * @param array $item New item, which has not yet been inserted into database (by ref)
*/ */
function retriever_post_remote_hook(&$a, &$item) { function retriever_post_remote_hook(App &$a, array &$item) {
Logger::info('retriever_post_remote_hook: ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']); Logger::info('retriever_post_remote_hook: ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']);
$retriever_rule = get_retriever_rule($item['contact-id'], $item["uid"], false); $retriever_rule = get_retriever_rule($item['contact-id'], $item["uid"], false);
@ -1015,7 +1015,7 @@ function retriever_post_remote_hook(&$a, &$item) {
* @param App $a The App object (by ref) * @param App $a The App object (by ref)
* @param string $s HTML string to which to append settings content (by ref) * @param string $s HTML string to which to append settings content (by ref)
*/ */
function retriever_addon_settings(&$a, &$s) { function retriever_addon_settings(App &$a, string &$s) {
$all_photos = DI::config()->get(local_user(), 'retriever', 'all_photos'); $all_photos = DI::config()->get(local_user(), 'retriever', 'all_photos');
$oembed = DI::config()->get(local_user(), 'retriever', 'oembed'); $oembed = DI::config()->get(local_user(), 'retriever', 'oembed');
$template = Renderer::getMarkupTemplate('/settings.tpl', 'addon/retriever/'); $template = Renderer::getMarkupTemplate('/settings.tpl', 'addon/retriever/');
@ -1040,7 +1040,7 @@ function retriever_addon_settings(&$a, &$s) {
* @param App $a The App object * @param App $a The App object
* @param array $post Posted content * @param array $post Posted content
*/ */
function retriever_addon_settings_post($a, $post) { function retriever_addon_settings_post(App $a, array $post) {
if ($post['retriever_all_photos']) { if ($post['retriever_all_photos']) {
DI::config()->set(local_user(), 'retriever', 'all_photos', $post['retriever_all_photos']); DI::config()->set(local_user(), 'retriever', 'all_photos', $post['retriever_all_photos']);
} }