summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-12-31 11:08:47 +0000
committerDries Buytaert <dries@buytaert.net>2008-12-31 11:08:47 +0000
commitd813e3679c57da312cc0de61da90ff2c2eff70a6 (patch)
tree57b86283277ba1e4d768e5530fdb5e54d3205e4d /includes
parent82727ed8aae6dcd49b47e1893282ee36697cc9a2 (diff)
downloadbrdo-d813e3679c57da312cc0de61da90ff2c2eff70a6.tar.gz
brdo-d813e3679c57da312cc0de61da90ff2c2eff70a6.tar.bz2
- Patch #348201 by catch: make it possible to load multiple files with fewer queries.
Diffstat (limited to 'includes')
-rw-r--r--includes/file.inc77
1 files changed, 41 insertions, 36 deletions
diff --git a/includes/file.inc b/includes/file.inc
index 6a0ef2334..360b66fc4 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -267,55 +267,60 @@ function file_check_location($source, $directory = '') {
}
/**
- * Load a file object from the database.
+ * Load file objects from the database.
*
- * @param $param
- * Either the id of a file or an array of conditions to match against in the
- * database query.
- * @param $reset
- * Whether to reset the internal file_load cache.
+ * @param $fids
+ * An array of file IDs.
+ * @param $conditions
+ * An array of conditions to match against the {files} table. These
+ * should be supplied in the form array('field_name' => 'field_value').
* @return
- * A file object.
+ * An array of file objects, indexed by fid.
*
* @see hook_file_load()
+ * @see file_load()
*/
-function file_load($param, $reset = NULL) {
- static $files = array();
+function file_load_multiple($fids = array(), $conditions = array()) {
+ $query = db_select('files', 'f')->fields('f');
- if ($reset) {
- $files = array();
+ // If the $fids array is populated, add those to the query.
+ if ($fids) {
+ $query->condition('f.fid', $fids, 'IN');
}
- if (is_numeric($param)) {
- if (isset($files[(string) $param])) {
- return is_object($files[$param]) ? clone $files[$param] : $files[$param];
+ // If the conditions array is populated, add those to the query.
+ if ($conditions) {
+ foreach ($conditions as $field => $value) {
+ $query->condition('f.' . $field, $value);
}
- $result = db_query('SELECT f.* FROM {files} f WHERE f.fid = :fid', array(':fid' => $param));
}
- elseif (is_array($param)) {
- // Turn the conditions into a query.
- $cond = array();
- $arguments = array();
- foreach ($param as $key => $value) {
- $cond[] = 'f.' . db_escape_table($key) . " = '%s'";
- $arguments[] = $value;
+ $files = $query->execute()->fetchAllAssoc('fid');
+
+ // Invoke hook_file_load() on the terms loaded from the database
+ // and add them to the static cache.
+ if (!empty($files)) {
+ foreach (module_implements('file_load') as $module) {
+ $function = $module . '_file_load';
+ $function($files);
}
- $result = db_query('SELECT f.* FROM {files} f WHERE ' . implode(' AND ', $cond), $arguments);
- }
- else {
- return FALSE;
- }
- $file = $result->fetch(PDO::FETCH_OBJ);
-
- if ($file && $file->fid) {
- // Allow modules to add or change the file object.
- module_invoke_all('file_load', $file);
-
- // Cache the fully loaded value.
- $files[(string) $file->fid] = clone $file;
}
+ return $files;
+}
- return $file;
+/**
+ * Load a file object from the database.
+ *
+ * @param $fid
+ * A file ID.
+ * @return
+ * A file object.
+ *
+ * @see hook_file_load()
+ * @see file_load_multiple()
+ */
+function file_load($fid) {
+ $files = file_load_multiple(array($fid), array());
+ return reset($files);
}
/**