aboutsummaryrefslogtreecommitdiff
path: root/bridges/GoogleGroupsBridge.php
blob: 5bd7df4798335262f312834eadadaa6f412de54b (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
66
67
68
69
70
71
<?php

class GoogleGroupsBridge extends XPathAbstract
{
    const NAME = 'Google Groups Bridge';
    const DESCRIPTION = 'Returns the latest posts on a Google Group';
    const URI = 'https://groups.google.com';
    const PARAMETERS = [ [
        'group' => [
            'name' => 'Group id',
            'title' => 'The string that follows /g/ in the URL',
            'exampleValue' => 'governance',
            'required' => true
        ],
        'account' => [
            'name' => 'Account id',
            'title' => 'Some Google groups have an additional id following /a/ in the URL',
            'exampleValue' => 'mozilla.org',
            'required' => false
        ]
    ]];
    const CACHE_TIMEOUT = 3600;

    const TEST_DETECT_PARAMETERS = [
        'https://groups.google.com/a/mozilla.org/g/announce' => [
            'account' => 'mozilla.org', 'group' => 'announce'
        ],
        'https://groups.google.com/g/ansible-project' => [
            'account' => null, 'group' => 'ansible-project'
        ],
    ];

    const XPATH_EXPRESSION_ITEM = '//div[@class="yhgbKd"]';
    const XPATH_EXPRESSION_ITEM_TITLE = './/span[@class="o1DPKc"]';
    const XPATH_EXPRESSION_ITEM_CONTENT = './/span[@class="WzoK"]';
    const XPATH_EXPRESSION_ITEM_URI = './/a[@class="ZLl54"]/@href';
    const XPATH_EXPRESSION_ITEM_AUTHOR = './/span[@class="z0zUgf"][last()]';
    const XPATH_EXPRESSION_ITEM_TIMESTAMP = './/div[@class="tRlaM"]';
    const XPATH_EXPRESSION_ITEM_ENCLOSURES = '';
    const XPATH_EXPRESSION_ITEM_CATEGORIES = '';
    const SETTING_FIX_ENCODING = true;

    protected function getSourceUrl()
    {
        $source = self::URI;

        $account = $this->getInput('account');
        if ($account) {
            $source = $source . '/a/' . $account;
        }
        return $source . '/g/' . $this->getInput('group');
    }

    protected function provideWebsiteContent()
    {
        return defaultLinkTo(getContents($this->getSourceUrl()), self::URI);
    }

    const URL_REGEX = '#^https://groups.google.com(?:/a/(?<account>\S+))?(?:/g/(?<group>\S+))#';

    public function detectParameters($url)
    {
        $params = [];
        if (preg_match(self::URL_REGEX, $url, $matches)) {
            $params['group'] = $matches['group'];
            $params['account'] = $matches['account'];
            return $params;
        }
        return null;
    }
}