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