xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/UniversalFeedCreator.php (revision 3a97d936870170491bdd7d03d71143143b10191d)
1<?php
2// your local timezone, set to "" to disable or for GMT
3if (!defined('TIME_ZONE')) {
4    define("TIME_ZONE", date("O", time()));
5}
6
7/**
8 * Version string.
9 */
10define("FEEDCREATOR_VERSION", "FeedCreator 1.8");
11
12/**
13 * UniversalFeedCreator lets you choose during runtime which
14 * format to build.
15 * For general usage of a feed class, see the FeedCreator class
16 * below or the example above.
17 *
18 * @since   1.3
19 * @author  Kai Blankenhorn <kaib@bitfolge.de>
20 * @package de.bitfolge.feedcreator
21 */
22class UniversalFeedCreator extends FeedCreator
23{
24    /** @var FeedCreator */
25    protected $_feed;
26
27    /**
28     * @param string $format
29     */
30    protected function _setFormat($format)
31    {
32        switch (strtoupper($format)) {
33
34            case "BASE":
35                $this->format = $format;
36            case "2.0":
37                // fall through
38            case "RSS2.0":
39                $this->_feed = new RSSCreator20();
40                break;
41
42            case "GEOPHOTORSS":
43            case "PHOTORSS":
44            case "GEORSS":
45                $this->format = $format;
46            case "1.0":
47                // fall through
48            case "RSS1.0":
49                $this->_feed = new RSSCreator10();
50                break;
51
52            case "0.91":
53                // fall through
54            case "RSS0.91":
55                $this->_feed = new RSSCreator091();
56                break;
57
58            case "PIE0.1":
59                $this->_feed = new PIECreator01();
60                break;
61
62            case "MBOX":
63                $this->_feed = new MBOXCreator();
64                break;
65
66            case "OPML":
67                $this->_feed = new OPMLCreator();
68                break;
69
70            case "TOOLBAR":
71                $this->format = $format;
72
73            case "ATOM":
74                // fall through: always the latest ATOM version
75            case "ATOM1.0":
76                $this->_feed = new AtomCreator10();
77                break;
78
79            case "ATOM0.3":
80                $this->_feed = new AtomCreator03();
81                break;
82
83            case "HTML":
84                $this->_feed = new HTMLCreator();
85                break;
86
87            case "PHP":
88                $this->_feed = new PHPCreator();
89                break;
90            case "GPX":
91                $this->_feed = new GPXCreator();
92                break;
93            case "KML":
94                $this->_feed = new KMLCreator();
95                break;
96            case "JS":
97                // fall through
98            case "JAVASCRIPT":
99                $this->_feed = new JSCreator();
100                break;
101
102            default:
103                $this->_feed = new RSSCreator091();
104                break;
105        }
106
107        $vars = get_object_vars($this);
108        foreach ($vars as $key => $value) {
109            // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
110            if (!in_array($key, array("_feed", "contentType", "encoding"))) {
111                $this->_feed->{$key} = $this->{$key};
112            }
113        }
114    }
115
116    /**
117     * Creates a syndication feed based on the items previously added.
118     *
119     * @see FeedCreator::addItem()
120     * @param string $format format the feed should comply to. Valid values are:
121     *                       "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
122     * @return string the contents of the feed.
123     */
124    public function createFeed($format = "RSS0.91")
125    {
126        $this->_setFormat($format);
127
128        return $this->_feed->createFeed();
129    }
130
131    /**
132     * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
133     * header may be sent to redirect the use to the newly created file.
134     *
135     * @since 1.4
136     * @param string $format           format the feed should comply to. Valid values are:
137     *                                 "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM",
138     *                                 "ATOM0.3", "HTML", "JS"
139     * @param string $filename         optional    the filename where a recent version of the feed is saved. If not
140     *                                 specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to
141     *                                 .xml (see _generateFilename()).
142     * @param boolean $displayContents optional    send the content of the file or not. If true, the file will be sent
143     *                                 in the body of the response.
144     */
145    public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true)
146    {
147        $this->_setFormat($format);
148        $this->_feed->saveFeed($filename, $displayContents);
149    }
150
151    /**
152     * Turns on caching and checks if there is a recent version of this feed in the cache.
153     * If there is, an HTTP redirect header is sent.
154     * To effectively use caching, you should create the FeedCreator object and call this method
155     * before anything else, especially before you do the time consuming task to build the feed
156     * (web fetching, for example).
157     *
158     * @param string $format   format the feed should comply to. Valid values are:
159     *                         "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
160     * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the
161     *                         filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see
162     *                         _generateFilename()).
163     * @param int $timeout     optional the timeout in seconds before a cached version is refreshed (defaults to 3600 =
164     *                         1 hour)
165     */
166    public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600)
167    {
168        $this->_setFormat($format);
169        $this->_feed->useCached($filename, $timeout);
170    }
171}
172