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