diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-12-31 11:08:47 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-12-31 11:08:47 +0000 |
commit | d813e3679c57da312cc0de61da90ff2c2eff70a6 (patch) | |
tree | 57b86283277ba1e4d768e5530fdb5e54d3205e4d /modules/upload/upload.module | |
parent | 82727ed8aae6dcd49b47e1893282ee36697cc9a2 (diff) | |
download | brdo-d813e3679c57da312cc0de61da90ff2c2eff70a6.tar.gz brdo-d813e3679c57da312cc0de61da90ff2c2eff70a6.tar.bz2 |
- Patch #348201 by catch: make it possible to load multiple files with fewer queries.
Diffstat (limited to 'modules/upload/upload.module')
-rw-r--r-- | modules/upload/upload.module | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/modules/upload/upload.module b/modules/upload/upload.module index 74f95783d..3b9a87b6b 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -269,11 +269,13 @@ function upload_form_alter(&$form, $form_state, $form_id) { /** * Implementation of hook_file_load(). */ -function upload_file_load(&$file) { +function upload_file_load($files) { // Add the upload specific data into the file object. - $values = db_query('SELECT * FROM {upload} u WHERE u.fid = :fid', array(':fid' => $file->fid))->fetch(PDO::FETCH_ASSOC); - foreach ((array)$values as $key => $value) { - $file->{$key} = $value; + $result = db_query('SELECT * FROM {upload} u WHERE u.fid IN (:fids)', array(':fids' => array_keys($files)))->fetchAll(PDO::FETCH_ASSOC); + foreach ($result as $record) { + foreach ($record as $key => $value) { + $files[$record['fid']]->$key = $value; + } } } @@ -297,16 +299,42 @@ function upload_file_delete(&$file) { db_delete('upload')->condition('fid', $file->fid)->execute(); } - /** * Implementation of hook_nodeapi_load(). */ function upload_nodeapi_load($nodes, $types) { + // Collect all the revision ids for nodes with upload enabled. + $node_vids = array(); foreach ($nodes as $node) { if (variable_get("upload_$node->type", 1) == 1) { - $node->files = upload_load($node); + $node_vids[$node->vid] = $node->vid; + $node->files = array(); } } + // If there are no vids then there's no point trying to load files. + if (empty($node_vids)) { + return; + } + + // Fetch the fids associated with these node revisions. + $result = db_query('SELECT u.fid, u.nid, u.vid FROM {upload} u WHERE u.vid IN (:node_vids) ORDER BY u.weight, u.fid', array(':node_vids' => $node_vids)); + + // The same file may be attached to several nodes (e.g. translated nodes) so + // simply calling db_query()->fetchAllAssoc('fid') would return one node + // per file. Instead we build one array with the file ids for + // file_load_multiple() and another array with upload records so we can match + // files back to the nodes. + $fids = array(); + $uploads = array(); + foreach ($result as $record) { + $fids[] = $record->fid; + $uploads[] = $record; + } + + $files = file_load_multiple($fids); + foreach ($uploads as $upload) { + $nodes[$upload->nid]->files[$upload->fid] = $files[$upload->fid]; + } } /** @@ -324,7 +352,7 @@ function upload_nodeapi_view($node, $teaser, $page) { ); } } - + upload_nodeapi_links($node, $teaser); } } @@ -609,19 +637,6 @@ function theme_upload_form_new($form) { return $output; } -function upload_load($node) { - $files = array(); - - if ($node->vid) { - $result = db_query('SELECT u.fid FROM {upload} u WHERE u.vid = :vid ORDER BY u.weight, u.fid', array(':vid' => $node->vid)); - foreach ($result as $file) { - $files[$file->fid] = file_load($file->fid); - } - } - - return $files; -} - /** * Menu-callback for JavaScript-based uploads. */ |