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