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