aboutsummaryrefslogtreecommitdiff
path: root/bridges/FeedReducerBridge.php
blob: 37bf9809e679743753d1a1dc1c0c0a7897bf9597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

class FeedReducerBridge extends FeedExpander
{
    const MAINTAINER = 'mdemoss';
    const NAME = 'Feed Reducer';
    const URI = 'http://github.com/RSS-Bridge/rss-bridge/';
    const DESCRIPTION = 'Choose a percentage of a feed you want to see.';
    const PARAMETERS = [ [
        'url' => [
            'name' => 'Feed URI',
            'exampleValue' => 'https://lorem-rss.herokuapp.com/feed?length=42',
            'required' => true
        ],
        'percentage' => [
            'name' => 'percentage',
            'type' => 'number',
            'exampleValue' => 50,
            'required' => true
        ]
    ]];
    const CACHE_TIMEOUT = 3600;

    public function collectData()
    {
        $url = $this->getInput('url');
        if (preg_match('#^http(s?)://#i', $url)) {
            $this->collectExpandableDatas($url);
        } else {
            throw new Exception('URI must begin with http(s)://');
        }
    }

    public function getItems()
    {
        $filteredItems = [];
        $intPercentage = (int)preg_replace('/[^0-9]/', '', $this->getInput('percentage'));

        foreach ($this->items as $item) {
            // The URL is included in the hash:
            //  - so you can change the output by adding a local-part to the URL
            //  - so items with the same URI in different feeds won't be correlated

            // $pseudoRandomInteger will be a 16 bit unsigned int mod 100.
            // This won't be uniformly distributed 1-100, but should be close enough.

            $data = $item['uri'] . '::' . $this->getInput('url');
            $hash = hash('sha256', $data, true);
            // S = unsigned 16-bit int
            $pseudoRandomInteger = unpack('S', $hash)[1] % 100;

            if ($pseudoRandomInteger < $intPercentage) {
                $filteredItems[] = $item;
            }
        }

        return $filteredItems;
    }

    public function getName()
    {
        $trimmedPercentage = preg_replace('/[^0-9]/', '', $this->getInput('percentage') ?? '');
        return parent::getName() . ' [' . $trimmedPercentage . '%]';
    }
}