You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

162 lines
4.6 KiB

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