FeedsPlugin.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @file
  4. * Definition of FeedsPlugin class.
  5. */
  6. /**
  7. * Base class for a fetcher, parser or processor result.
  8. */
  9. class FeedsResult {}
  10. /**
  11. * Implement source interface for all plugins.
  12. *
  13. * Note how this class does not attempt to store source information locally.
  14. * Doing this would break the model where source information is represented by
  15. * an object that is being passed into a Feed object and its plugins.
  16. */
  17. abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInterface {
  18. /**
  19. * Constructor.
  20. *
  21. * Initialize class variables.
  22. */
  23. protected function __construct($id) {
  24. parent::__construct($id);
  25. $this->source_config = $this->sourceDefaults();
  26. }
  27. /**
  28. * Save changes to the configuration of this object.
  29. * Delegate saving to parent (= Feed) which will collect
  30. * information from this object by way of getConfig() and store it.
  31. */
  32. public function save() {
  33. feeds_importer($this->id)->save();
  34. }
  35. /**
  36. * Returns TRUE if $this->sourceForm() returns a form.
  37. */
  38. public function hasSourceConfig() {
  39. $form = $this->sourceForm(array());
  40. return !empty($form);
  41. }
  42. /**
  43. * Implements FeedsSourceInterface::sourceDefaults().
  44. */
  45. public function sourceDefaults() {
  46. $values = array_flip(array_keys($this->sourceForm(array())));
  47. foreach ($values as $k => $v) {
  48. $values[$k] = '';
  49. }
  50. return $values;
  51. }
  52. /**
  53. * Callback methods, exposes source form.
  54. */
  55. public function sourceForm($source_config) {
  56. return array();
  57. }
  58. /**
  59. * Validation handler for sourceForm.
  60. */
  61. public function sourceFormValidate(&$source_config) {}
  62. /**
  63. * A source is being saved.
  64. */
  65. public function sourceSave(FeedsSource $source) {}
  66. /**
  67. * A source is being deleted.
  68. */
  69. public function sourceDelete(FeedsSource $source) {}
  70. /**
  71. * Loads on-behalf implementations from mappers/ directory.
  72. *
  73. * FeedsProcessor::map() does not load from mappers/ as only node and user
  74. * processor ship with on-behalf implementations.
  75. *
  76. * @see FeedsNodeProcessor::map()
  77. * @see FeedsUserProcessor::map()
  78. *
  79. * @todo: Use CTools Plugin API.
  80. */
  81. protected static function loadMappers() {
  82. static $loaded = FALSE;
  83. if (!$loaded) {
  84. $path = drupal_get_path('module', 'feeds') . '/mappers';
  85. $files = drupal_system_listing('/.*\.inc$/', $path, 'name', 0);
  86. foreach ($files as $file) {
  87. if (strstr($file->uri, '/mappers/')) {
  88. require_once(DRUPAL_ROOT . '/' . $file->uri);
  89. }
  90. }
  91. }
  92. $loaded = TRUE;
  93. }
  94. /**
  95. * Get all available plugins.
  96. */
  97. public static function all() {
  98. ctools_include('plugins');
  99. $plugins = ctools_get_plugins('feeds', 'plugins');
  100. $result = array();
  101. foreach ($plugins as $key => $info) {
  102. if (!empty($info['hidden'])) {
  103. continue;
  104. }
  105. $result[$key] = $info;
  106. }
  107. // Sort plugins by name and return.
  108. uasort($result, 'feeds_plugin_compare');
  109. return $result;
  110. }
  111. /**
  112. * Determines whether given plugin is derived from given base plugin.
  113. *
  114. * @param $plugin_key
  115. * String that identifies a Feeds plugin key.
  116. * @param $parent_plugin
  117. * String that identifies a Feeds plugin key to be tested against.
  118. *
  119. * @return
  120. * TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
  121. * FALSE otherwise.
  122. */
  123. public static function child($plugin_key, $parent_plugin) {
  124. ctools_include('plugins');
  125. $plugins = ctools_get_plugins('feeds', 'plugins');
  126. $info = $plugins[$plugin_key];
  127. if (empty($info['handler']['parent'])) {
  128. return FALSE;
  129. }
  130. elseif ($info['handler']['parent'] == $parent_plugin) {
  131. return TRUE;
  132. }
  133. else {
  134. return self::child($info['handler']['parent'], $parent_plugin);
  135. }
  136. }
  137. /**
  138. * Determines the type of a plugin.
  139. *
  140. * @todo PHP5.3: Implement self::type() and query with $plugin_key::type().
  141. *
  142. * @param $plugin_key
  143. * String that identifies a Feeds plugin key.
  144. *
  145. * @return
  146. * One of the following values:
  147. * 'fetcher' if the plugin is a fetcher
  148. * 'parser' if the plugin is a parser
  149. * 'processor' if the plugin is a processor
  150. * FALSE otherwise.
  151. */
  152. public static function typeOf($plugin_key) {
  153. if (self::child($plugin_key, 'FeedsFetcher')) {
  154. return 'fetcher';
  155. }
  156. elseif (self::child($plugin_key, 'FeedsParser')) {
  157. return 'parser';
  158. }
  159. elseif (self::child($plugin_key, 'FeedsProcessor')) {
  160. return 'processor';
  161. }
  162. return FALSE;
  163. }
  164. /**
  165. * Gets all available plugins of a particular type.
  166. *
  167. * @param $type
  168. * 'fetcher', 'parser' or 'processor'
  169. */
  170. public static function byType($type) {
  171. $plugins = self::all();
  172. $result = array();
  173. foreach ($plugins as $key => $info) {
  174. if ($type == self::typeOf($key)) {
  175. $result[$key] = $info;
  176. }
  177. }
  178. return $result;
  179. }
  180. }
  181. /**
  182. * Used when a plugin is missing.
  183. */
  184. class FeedsMissingPlugin extends FeedsPlugin {
  185. public function menuItem() {
  186. return array();
  187. }
  188. }
  189. /**
  190. * Sort callback for FeedsPlugin::all().
  191. */
  192. function feeds_plugin_compare($a, $b) {
  193. return strcasecmp($a['name'], $b['name']);
  194. }