xref: /dokuwiki/inc/Feed/FeedCreatorOptions.php (revision 7e23bd088acc9a711a4d14ca8e1bc21b87709866)
1fe9d054bSAndreas Gohr<?php
2fe9d054bSAndreas Gohr
3fe9d054bSAndreas Gohrnamespace dokuwiki\Feed;
4fe9d054bSAndreas Gohr
5fe9d054bSAndreas Gohruse dokuwiki\Extension\Event;
6fe9d054bSAndreas Gohr
7fe9d054bSAndreas Gohr/**
8fe9d054bSAndreas Gohr * Hold the options for feed generation
9fe9d054bSAndreas Gohr */
10fe9d054bSAndreas Gohrclass FeedCreatorOptions
11fe9d054bSAndreas Gohr{
12fe9d054bSAndreas Gohr    /** @var array[] supported feed types */
13fe9d054bSAndreas Gohr    protected $types = [
14fe9d054bSAndreas Gohr        'rss' => [
15fe9d054bSAndreas Gohr            'name' => 'RSS0.91',
16fe9d054bSAndreas Gohr            'mime' => 'text/xml; charset=utf-8',
17fe9d054bSAndreas Gohr        ],
18*7e23bd08SAndreas Gohr        'rss1' => [
19*7e23bd08SAndreas Gohr            'name' => 'RSS1.0',
20*7e23bd08SAndreas Gohr            'mime' => 'text/xml; charset=utf-8',
21*7e23bd08SAndreas Gohr        ],
22fe9d054bSAndreas Gohr        'rss2' => [
23fe9d054bSAndreas Gohr            'name' => 'RSS2.0',
24fe9d054bSAndreas Gohr            'mime' => 'text/xml; charset=utf-8',
25fe9d054bSAndreas Gohr        ],
26fe9d054bSAndreas Gohr        'atom' => [
27fe9d054bSAndreas Gohr            'name' => 'ATOM0.3',
28fe9d054bSAndreas Gohr            'mime' => 'application/xml; charset=utf-8',
29fe9d054bSAndreas Gohr        ],
30fe9d054bSAndreas Gohr        'atom1' => [
31fe9d054bSAndreas Gohr            'name' => 'ATOM1.0',
32fe9d054bSAndreas Gohr            'mime' => 'application/atom+xml; charset=utf-8',
33fe9d054bSAndreas Gohr        ],
34fe9d054bSAndreas Gohr    ];
35fe9d054bSAndreas Gohr
36fe9d054bSAndreas Gohr    /** @var array[] the set options */
37fe9d054bSAndreas Gohr    public $options = [
38fe9d054bSAndreas Gohr        'type' => 'rss',
39fe9d054bSAndreas Gohr        'feed_mode' => 'recent',
40fe9d054bSAndreas Gohr        'link_to' => 'page',
41fe9d054bSAndreas Gohr        'item_content' => 'diff',
42fe9d054bSAndreas Gohr        'namespace' => '',
43fe9d054bSAndreas Gohr        'items' => 15,
44fe9d054bSAndreas Gohr        'show_minor' => false,
45fe9d054bSAndreas Gohr        'show_deleted' => false,
46fe9d054bSAndreas Gohr        'show_summary' => false,
47fe9d054bSAndreas Gohr        'only_new' => false,
48fe9d054bSAndreas Gohr        'sort' => 'natural',
49fe9d054bSAndreas Gohr        'search_query' => '',
50fe9d054bSAndreas Gohr        'content_type' => 'pages',
51fe9d054bSAndreas Gohr        'guardmail' => 'none',
52fe9d054bSAndreas Gohr        'title' => '',
53fe9d054bSAndreas Gohr    ];
54fe9d054bSAndreas Gohr
55fe9d054bSAndreas Gohr    /**
56fe9d054bSAndreas Gohr     * Initialize the options from the request, falling back to config defaults
57fe9d054bSAndreas Gohr     *
58fe9d054bSAndreas Gohr     * @triggers FEED_OPTS_POSTPROCESS
59fe9d054bSAndreas Gohr     * @param array $options additional options to set (for testing)
60fe9d054bSAndreas Gohr     */
61fe9d054bSAndreas Gohr    public function __construct($options = [])
62fe9d054bSAndreas Gohr    {
63fe9d054bSAndreas Gohr        global $conf;
64fe9d054bSAndreas Gohr        global $INPUT;
65fe9d054bSAndreas Gohr
66fe9d054bSAndreas Gohr        $this->options['type'] = $INPUT->valid(
67fe9d054bSAndreas Gohr            'type',
68fe9d054bSAndreas Gohr            array_keys($this->types),
69fe9d054bSAndreas Gohr            $conf['rss_type']
70fe9d054bSAndreas Gohr        );
71fe9d054bSAndreas Gohr        // we only support 'list', 'search', 'recent' but accept anything so plugins can take over
72fe9d054bSAndreas Gohr        $this->options['feed_mode'] = $INPUT->str('mode', 'recent');
73fe9d054bSAndreas Gohr        $this->options['link_to'] = $INPUT->valid(
74fe9d054bSAndreas Gohr            'linkto',
75fe9d054bSAndreas Gohr            ['diff', 'page', 'rev', 'current'],
76fe9d054bSAndreas Gohr            $conf['rss_linkto']
77fe9d054bSAndreas Gohr        );
78fe9d054bSAndreas Gohr        $this->options['item_content'] = $INPUT->valid(
79fe9d054bSAndreas Gohr            'content',
80fe9d054bSAndreas Gohr            ['abstract', 'diff', 'htmldiff', 'html'],
81fe9d054bSAndreas Gohr            $conf['rss_content']
82fe9d054bSAndreas Gohr        );
83fe9d054bSAndreas Gohr        $this->options['namespace'] = $INPUT->filter('cleanID')->str('ns');
84fe9d054bSAndreas Gohr        $this->options['items'] = max(0, $INPUT->int('num', $conf['recent']));
85fe9d054bSAndreas Gohr        $this->options['show_minor'] = $INPUT->bool('minor');
86fe9d054bSAndreas Gohr        $this->options['show_deleted'] = $conf['rss_show_deleted'];
87fe9d054bSAndreas Gohr        $this->options['show_summary'] = $conf['rss_show_summary'];
88fe9d054bSAndreas Gohr        $this->options['only_new'] = $INPUT->bool('onlynewpages');
89fe9d054bSAndreas Gohr        $this->options['sort'] = $INPUT->valid(
90fe9d054bSAndreas Gohr            'sort',
91fe9d054bSAndreas Gohr            ['natural', 'date'],
92fe9d054bSAndreas Gohr            'natural'
93fe9d054bSAndreas Gohr        );
94fe9d054bSAndreas Gohr        $this->options['search_query'] = $INPUT->str('q');
95fe9d054bSAndreas Gohr        $this->options['content_type'] = $INPUT->valid(
96fe9d054bSAndreas Gohr            'view',
97fe9d054bSAndreas Gohr            ['pages', 'media', 'both'],
98fe9d054bSAndreas Gohr            $conf['rss_media']
99fe9d054bSAndreas Gohr        );
100fe9d054bSAndreas Gohr        $this->options['guardmail'] = $conf['mailguard'];
101fe9d054bSAndreas Gohr        $this->options['title'] = $conf['title'];
102fe9d054bSAndreas Gohr        if ($this->options['namespace']) {
103fe9d054bSAndreas Gohr            $this->options['title'] .= ' - ' . $this->options['namespace'];
104fe9d054bSAndreas Gohr        }
1051136941dSAndreas Gohr        $this->options['subtitle'] = $conf['tagline'];
106fe9d054bSAndreas Gohr
107fe9d054bSAndreas Gohr        $this->options = array_merge($this->options, $options);
108fe9d054bSAndreas Gohr
109fe9d054bSAndreas Gohr        // initialization finished, let plugins know
110fe9d054bSAndreas Gohr        $eventData = [
111fe9d054bSAndreas Gohr            'opt' => &$this->options,
112fe9d054bSAndreas Gohr        ];
113fe9d054bSAndreas Gohr        Event::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData);
114fe9d054bSAndreas Gohr    }
115fe9d054bSAndreas Gohr
116fe9d054bSAndreas Gohr    /**
117fe9d054bSAndreas Gohr     * The cache key to use for a feed with these options
118fe9d054bSAndreas Gohr     *
119fe9d054bSAndreas Gohr     * Does not contain user or host specific information yet
120fe9d054bSAndreas Gohr     *
121fe9d054bSAndreas Gohr     * @return string
122fe9d054bSAndreas Gohr     */
123fe9d054bSAndreas Gohr    public function getCacheKey()
124fe9d054bSAndreas Gohr    {
125fe9d054bSAndreas Gohr        return implode('', array_values($this->options));
126fe9d054bSAndreas Gohr    }
127fe9d054bSAndreas Gohr
128fe9d054bSAndreas Gohr    /**
129fe9d054bSAndreas Gohr     * Return a feed option by name
130fe9d054bSAndreas Gohr     *
131fe9d054bSAndreas Gohr     * @param string $option The name of the option
132fe9d054bSAndreas Gohr     * @param mixed $default default value if option is not set (should usually not happen)
133fe9d054bSAndreas Gohr     * @return mixed
134fe9d054bSAndreas Gohr     */
135fe9d054bSAndreas Gohr    public function get($option, $default = null)
136fe9d054bSAndreas Gohr    {
13772c714a3Ssplitbrain        return $this->options[$option] ?? $default;
138fe9d054bSAndreas Gohr    }
139fe9d054bSAndreas Gohr
140fe9d054bSAndreas Gohr    /**
141fe9d054bSAndreas Gohr     * Return the feed type for UniversalFeedCreator
142fe9d054bSAndreas Gohr     *
143fe9d054bSAndreas Gohr     * This returns the apropriate type for UniversalFeedCreator
144fe9d054bSAndreas Gohr     *
145fe9d054bSAndreas Gohr     * @return string
146fe9d054bSAndreas Gohr     */
147fe9d054bSAndreas Gohr    public function getType()
148fe9d054bSAndreas Gohr    {
149fe9d054bSAndreas Gohr        return $this->types[$this->options['type']]['name'];
150fe9d054bSAndreas Gohr    }
151fe9d054bSAndreas Gohr
152fe9d054bSAndreas Gohr    /**
153fe9d054bSAndreas Gohr     * Return the feed mime type
154fe9d054bSAndreas Gohr     *
155fe9d054bSAndreas Gohr     * @return string
156fe9d054bSAndreas Gohr     */
157fe9d054bSAndreas Gohr    public function getMimeType()
158fe9d054bSAndreas Gohr    {
159fe9d054bSAndreas Gohr        return $this->types[$this->options['type']]['mime'];
160fe9d054bSAndreas Gohr    }
161fe9d054bSAndreas Gohr}
162