1<?php
2
3require_once __DIR__ . '/inc/ORM/AbstractModel.php';
4require_once __DIR__ . '/inc/ORM/ModelNews.php';
5require_once __DIR__ . '/inc/ORM/ModelPriority.php';
6require_once __DIR__ . '/inc/ORM/ModelStream.php';
7require_once __DIR__ . '/inc/ORM/ModelDependence.php';
8require_once __DIR__ . '/inc/ORM/AbstractService.php';
9require_once __DIR__ . '/inc/ORM/ServiceDependence.php';
10require_once __DIR__ . '/inc/ORM/ServiceNews.php';
11require_once __DIR__ . '/inc/ORM/ServicePriority.php';
12require_once __DIR__ . '/inc/ORM/ServiceStream.php';
13
14require_once __DIR__ . '/inc/renderer/AbstractRenderer.php';
15require_once __DIR__ . '/inc/renderer/VyfukRenderer.php';
16require_once __DIR__ . '/inc/renderer/FykosRenderer.php';
17require_once __DIR__ . '/inc/AbstractStream.php';
18require_once __DIR__ . '/../social/inc/OpenGraphData.php';
19
20use dokuwiki\Extension\Plugin;
21use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServiceDependence;
22use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServiceNews;
23use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServicePriority;
24use FYKOS\dokuwiki\Extension\PluginNewsFeed\ORM\ServiceStream;
25use FYKOS\dokuwiki\Extension\PluginSocial\OpenGraphData;
26use FYKOS\dokuwiki\Extension\PluginNewsFeed\Model\ModelNews;
27use FYKOS\dokuwiki\Extension\PluginNewsFeed\Model\ModelStream;
28use FYKOS\dokuwiki\Extension\PluginNewsFeed\Renderer\AbstractRenderer;
29use FYKOS\dokuwiki\Extension\PluginNewsFeed\Renderer\FykosRenderer;
30use FYKOS\dokuwiki\Extension\PluginNewsFeed\Renderer\VyfukRenderer;
31
32class helper_plugin_newsfeed extends Plugin {
33
34    const FORM_TARGET = 'plugin_newsfeed';
35
36    public static array $fields = [
37        'title',
38        'authorName',
39        'authorEmail',
40        'newsDate',
41        'image',
42        'category',
43        'linkHref',
44        'linkTitle',
45        'text',
46    ];
47    public helper_plugin_sqlite $sqlite;
48
49    public OpenGraphData $openGraphData;
50
51    public AbstractRenderer $renderer;
52
53
54    public ServiceNews $serviceNews;
55    public ServicePriority $servicePriority;
56    public ServiceDependence $serviceDependence;
57    public ServiceStream $serviceStream;
58
59    public function __construct() {
60
61        $this->openGraphData = new OpenGraphData();
62        $this->sqlite = $this->loadHelper('sqlite');
63
64        $this->serviceNews = new ServiceNews($this->sqlite);
65        $this->servicePriority = new ServicePriority($this->sqlite);
66        $this->serviceDependence = new ServiceDependence($this->sqlite);
67        $this->serviceStream = new ServiceStream($this->sqlite);
68
69        $pluginName = $this->getPluginName();
70        if (!$this->sqlite) {
71            msg($pluginName . ': This plugin requires the sqlite plugin. Please install it.');
72        }
73
74        if (!$this->sqlite->init('newsfeed', DOKU_PLUGIN . $pluginName . DIRECTORY_SEPARATOR . 'db' . DIRECTORY_SEPARATOR)
75        ) {
76            msg($pluginName . ': Cannot initialize database.');
77        }
78
79        switch ($this->getConf('contest')) {
80            default:
81            case 'fykos':
82                $this->renderer = new FykosRenderer($this);
83                break;
84            case 'vyfuk':
85                $this->renderer = new VyfukRenderer($this);
86                break;
87        }
88    }
89
90    /**
91     * @param $streamId integer
92     * @return integer[]
93     */
94    private function allParentDependence(int $streamId): array {
95        $streamIds = [];
96        $res = $this->sqlite->query('SELECT * FROM dependence WHERE parent=?', $streamId);
97        foreach ($this->sqlite->res2arr($res) as $row) {
98            $streamIds[] = $row['child'];
99        }
100        return $streamIds;
101    }
102
103    /**
104     * @param $streamId integer
105     * @param array $arr
106     * @return void
107     */
108    public function fullParentDependence(int $streamId, array &$arr): void {
109        foreach ($this->allParentDependence($streamId) as $newStreamId) {
110            if (!in_array($newStreamId, $arr)) {
111                $arr[] = $newStreamId;
112                $this->fullParentDependence($newStreamId, $arr);
113            }
114        }
115    }
116}
117