[177a560] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * @file |
---|
| 5 | * Data handler used in FeedsDataProcessor. |
---|
| 6 | * |
---|
| 7 | * @todo Move to data module? |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Simple multidimensional data handler. Treats tables that join to this |
---|
| 12 | * handler's table through FeedsDatahandler::key as a cluster. Records in this |
---|
| 13 | * cluster are regarded as belonging to one multidimensional data set joined |
---|
| 14 | * by FeedsDatahandler::key. |
---|
| 15 | * |
---|
| 16 | * Limitations: |
---|
| 17 | * |
---|
| 18 | * - Records can only be tied together by a single key. Note: tables can still |
---|
| 19 | * join through other fields to this table, but these table's data won't be |
---|
| 20 | * considered of the same data set. |
---|
| 21 | * - save() is not supported. update() only supports updates on |
---|
| 22 | * FeedsDataHandler::key |
---|
| 23 | * - Note: a record in depending tables will deleted when records from the base |
---|
| 24 | * table are deleted. |
---|
| 25 | */ |
---|
| 26 | class FeedsDataHandler extends DataHandler { |
---|
| 27 | |
---|
| 28 | // An array of tables joining to the base table. |
---|
| 29 | protected $joined_tables; |
---|
| 30 | // A single field that the base table ($this->table) and depending tables |
---|
| 31 | // join on. |
---|
| 32 | protected $key; |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Constructor, call indirectly through DataHandler::instance(); |
---|
| 36 | */ |
---|
| 37 | protected function __construct($table, $key) { |
---|
| 38 | $this->table = $table; |
---|
| 39 | $this->key = $key; |
---|
| 40 | |
---|
| 41 | // Find tables joining to this table. |
---|
| 42 | // @todo DB cache. |
---|
| 43 | $this->joined_tables = array(); |
---|
| 44 | $tables = data_get_all_tables(); |
---|
| 45 | foreach ($tables as $join_table) { |
---|
| 46 | if ($join_table->get('name') == $this->table) { |
---|
| 47 | // don't bother with joins on the same table... |
---|
| 48 | continue; |
---|
| 49 | } |
---|
| 50 | $meta = $join_table->get('meta'); |
---|
| 51 | if (isset($meta['join'][$this->table]) && $meta['join'][$this->table]['left_field'] == $this->key) { |
---|
| 52 | // table has a field that joins to this table on $this->key |
---|
| 53 | $this->joined_tables[$join_table->get('name')] = $join_table->get('name'); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Instantiate a FeedsDataHandler object. We implement our own instantiator |
---|
| 60 | * method because DataHandler::instance() does not support a $key parameter. |
---|
| 61 | * |
---|
| 62 | * @param $table |
---|
| 63 | * The name of the table to access with this DataHandler object. |
---|
| 64 | * @param $key |
---|
| 65 | * The key that joins other tables. |
---|
| 66 | */ |
---|
| 67 | public static function instance($table, $key) { |
---|
| 68 | static $handlers; |
---|
| 69 | if (!isset($handlers[$table][$key])) { |
---|
| 70 | $class = 'FeedsDataHandler'; |
---|
| 71 | // @todo This is an undocumented stop gap until Data module supports |
---|
| 72 | // handler plugins. |
---|
| 73 | if ($info = variable_get($table .'_handler', NULL)) { |
---|
| 74 | $class = $info['class']; |
---|
| 75 | include_once drupal_get_path('module', $info['module']) .'/'. $info['file']; |
---|
| 76 | } |
---|
| 77 | $handlers[$table][$key] = new $class($table, $key); |
---|
| 78 | } |
---|
| 79 | return $handlers[$table][$key]; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Inserts a multi dimensional record. |
---|
| 84 | * |
---|
| 85 | * @param $record |
---|
| 86 | * An array of a record to store. Keys are the names of fields or names of |
---|
| 87 | * joining tables. Names of joining tables must start with #. Joined table |
---|
| 88 | * keys must contain an array of values to insert. |
---|
| 89 | * |
---|
| 90 | * Example: |
---|
| 91 | * This is an array that inserts a title and a description into the base |
---|
| 92 | * table and a series of tags into a depending table 'feeds_data_tags'. |
---|
| 93 | * Note how the actual serial key is missing, it will be generated and |
---|
| 94 | * passed on to depending tables by insert(). |
---|
| 95 | * |
---|
| 96 | * array( |
---|
| 97 | * 'title' => 'Example title', |
---|
| 98 | * 'description' => 'Lorem ipsum...', |
---|
| 99 | * '#feeds_data_tags' => array( |
---|
| 100 | * array( |
---|
| 101 | * 'tid' => 12, |
---|
| 102 | * ), |
---|
| 103 | * array( |
---|
| 104 | * 'tid' => 14, |
---|
| 105 | * ), |
---|
| 106 | * array( |
---|
| 107 | * 'tid' => 28, |
---|
| 108 | * ), |
---|
| 109 | * ), |
---|
| 110 | * ); |
---|
| 111 | */ |
---|
| 112 | public function insert(&$record) { |
---|
| 113 | parent::insert($record); |
---|
| 114 | |
---|
| 115 | foreach ($record as $key => $value) { |
---|
| 116 | if ($handler = $this->joinedTableHandler($key)) { |
---|
| 117 | foreach ($value as $v) { |
---|
| 118 | // parent::insert() has populated the key or key must have been passed |
---|
| 119 | // in. |
---|
| 120 | $v[$this->key] = $record[$this->key]; |
---|
| 121 | $handler->insert($v); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * Updates a multi-dimensional record. |
---|
| 129 | * |
---|
| 130 | * Assumes that updates occur on keys. Does not support any other form of |
---|
| 131 | * updates. |
---|
| 132 | * |
---|
| 133 | * @see insert(). |
---|
| 134 | * |
---|
| 135 | * @param $record |
---|
| 136 | * An array of the record to update. Keys are the names of fields or names |
---|
| 137 | * of joining tables. At least one key name in $record must match |
---|
| 138 | * $this->key. |
---|
| 139 | */ |
---|
| 140 | public function update(&$record) { |
---|
| 141 | parent::update($record, $this->key); |
---|
| 142 | |
---|
| 143 | foreach ($record as $key => $value) { |
---|
| 144 | if ($handler = $this->joinedTableHandler($key)) { |
---|
| 145 | foreach ($value as $v) { |
---|
| 146 | $handler->update($v, $this->key); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * Delete records from this handler's base table and all joined tables. |
---|
| 154 | * |
---|
| 155 | * @param $clause |
---|
| 156 | * Array that is a WHERE clause for the delete statement. The keys of the |
---|
| 157 | * array are the field of the WHERE clause. If the value for a key is a |
---|
| 158 | * simple type, then key = value is assumed. If the value is an array, then |
---|
| 159 | * the first value of that array is the operation, the second value is the |
---|
| 160 | * value to compare against. |
---|
| 161 | * |
---|
| 162 | * Example: |
---|
| 163 | * |
---|
| 164 | * This where clause would delete all records |
---|
| 165 | * WHERE feed_nid = 10 AND timestamp < 1255623780 |
---|
| 166 | * |
---|
| 167 | * array( |
---|
| 168 | * 'feed_nid' => 10, |
---|
| 169 | * 'timestamp' => array( |
---|
| 170 | * '<', |
---|
| 171 | * 1255623780, |
---|
| 172 | * ), |
---|
| 173 | * ); |
---|
| 174 | * |
---|
| 175 | * @todo Push this functionality into DataHandler. |
---|
| 176 | */ |
---|
| 177 | public function delete($clause) { |
---|
| 178 | $query = new DataQuery($this->table); |
---|
| 179 | $schema = drupal_get_schema($this->table); |
---|
| 180 | $fields = $schema['fields']; |
---|
| 181 | $this_table = db_escape_table($this->table); |
---|
| 182 | foreach ($clause as $key => $value) { |
---|
| 183 | $operator = '='; |
---|
| 184 | if (is_array($value)) { |
---|
| 185 | $operator = array_shift($value); |
---|
| 186 | $value = array_shift($value); |
---|
| 187 | } |
---|
| 188 | $query->addWhere($this_table .'.'. db_escape_string($key) ." ". $operator ." ". db_type_placeholder($fields[$key]['type']), $value); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | foreach ($this->joined_tables as $table) { |
---|
| 192 | $table = db_escape_table($table); |
---|
| 193 | $query->addSubject($table); |
---|
| 194 | $query->addJoin($table, "$table.{$this->key} = $this_table.{$this->key}", 'LEFT JOIN'); |
---|
| 195 | } |
---|
| 196 | drupal_alter('data_delete_query', $query, $this->table); |
---|
| 197 | if (!empty($query->where)) { |
---|
| 198 | $query->execute(); |
---|
| 199 | } |
---|
| 200 | return db_affected_rows(); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Helper. Returns a joined table for a given table name that starts with a #. |
---|
| 205 | * |
---|
| 206 | * @see: insert(), update(). |
---|
| 207 | * |
---|
| 208 | * @param $name |
---|
| 209 | * Potential table name. If name does not start with a #, method will always |
---|
| 210 | * return FALSE. |
---|
| 211 | * |
---|
| 212 | * @return |
---|
| 213 | * A DataHandler object if a joined table with this name could be found, |
---|
| 214 | * FALSE otherwise. Returns FALSE if $name does not start with #. |
---|
| 215 | * |
---|
| 216 | * @throws Exception |
---|
| 217 | * Throws Exception if a table name starting with # is given, but a |
---|
| 218 | * DataHandler can't be found. |
---|
| 219 | */ |
---|
| 220 | protected function joinedTableHandler($name) { |
---|
| 221 | if (strpos($name, '#') === 0) { |
---|
| 222 | $name = substr($name, 1); |
---|
| 223 | if (in_array($name, $this->joined_tables)) { |
---|
| 224 | return data_get_handler($name); |
---|
| 225 | } |
---|
| 226 | throw new Exception(t('Data handler for table !name not found.', array('!name' => $name))); |
---|
| 227 | } |
---|
| 228 | return NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * @todo Support save(). |
---|
| 233 | */ |
---|
| 234 | public function save(&$record, $update) { |
---|
| 235 | throw new Exception(t('Not implemented.')); |
---|
| 236 | } |
---|
| 237 | } |
---|