--- ./private_upload.module 2009-10-22 12:03:36.917531344 +0100
+++ ./private_upload_alexh_20091022.module 2009-10-22 17:43:36.528655859 +0100
@@ -581,23 +581,87 @@ Deny from all", $path, FILE_EXISTS_REPLA
*/
function _private_upload_migrate_private() {
$private_path = _private_upload_path();
- $result = db_query('SELECT f.* FROM {files} f, {node_access} na '.
- ' WHERE f.nid = na.nid AND na.gid != 0 AND f.filepath NOT REGEXP "^%s" '.
- ' GROUP BY f.fid', $private_path );
- while( $file = db_fetch_object($result) ) {
+ $result = db_query('
+SELECT
+ f.*,
+ na.*,
+ u.*,
+ r.timestamp old_timestamp,
+ n.changed current_timestamp,
+ n.title node_title,
+ ncv.vid current_vid,
+ ucv.fid current_fid
+ FROM
+ {files} f
+ INNER JOIN
+ {upload} u ON (f.fid = u.fid)
+ LEFT JOIN
+ {upload} ucv ON (ucv.nid = u.nid AND ucv.vid = (SELECT MAX(n2.vid) FROM {node} n2 WHERE n2.nid = ncv.nid))
+ INNER JOIN
+ {node_revisions} r ON (r.vid = u.vid)
+ INNER JOIN
+ {node_access} na ON (u.nid = na.nid)
+ INNER JOIN
+ {node} n ON (n.nid = na.nid AND n.vid = u.vid)
+ INNER JOIN
+ {node} ncv ON (ncv.nid = u.nid AND ncv.vid = (SELECT MAX(n2.vid) FROM {node} n2 WHERE n2.nid = ncv.nid))
+ WHERE
+ na.gid != 0
+ AND f.filepath NOT REGEXP "^%s"
+ GROUP BY
+ f.fid', $private_path);
+ while($file = db_fetch_object($result)) {
// file is attached to a private node, but is a public file, so move it.
$filepath = $file->filepath;
- if( file_move($filepath, $private_path, FILE_EXISTS_REPLACE) ) {
- $output .= t("Making !filename private", array('!filename'=>$file->filename)). "
";
+ $currentversion = ($file->vid == $file->current_vid && $file->fid == $file->current_fid);
+ if(file_move($filepath, $private_path, FILE_EXISTS_REPLACE)) {
_private_upload_update_filepath($filepath, $file->fid);
+ $message_type = 'status';
+ $message = 'Successfully converted file \'%filename\' to private. The file
+ is attached to node %node_link dated %node_date.';
+ }
+ elseif (!file_exists(realpath($filepath))) {
+ /* If $currentversion is TRUE, a file listed on a current node was not found. Otherwise
+ the file's no longer listed on the current version of the node, so we can assume
+ the file is probably no longer needed. Set a message and move on.
+ */
+ $message_type = ($currentversion ? 'error' : 'warning');
+ $message = 'Unable to find ' . ($currentversion ? '' : 'the removed') . ' file \'%filename\',
+ which '. ($currentversion ? '
+
+ is attached to the current version of %node_link. Please
+ check whether the files are accessible and in the correct locations, and then
+ try running this process again.' : '
+
+ was attached to an older version of %node_link
+ dated %old_node_date. This file is no longer listed on the current version of
+ this node, dated %current_node_date, so you can safely ignore this message.') . '';
+ }
+ else {
+ $message_type = 'warning';
+ $message = 'An error occurred when moving file \'%filename\', which
+ was attached to node %node_link dated %current_node_date. More information may
+ be listed in other error messages on this page.';
+ );
+ }
+ $node_link = l($file->node_title, 'node/' . $file->nid);
+ $message_watchdog_severity_array = array(
+ 'status' => 'WATCHDOG_NOTICE',
+ 'warning' => 'WATCHDOG_WARNING',
+ 'error' => 'WATCHDOG_ERROR'
+ );
+ $message_array = array(
+ '%filename' => check_plain($file->filename),
+ '%node_link' => $node_link,
+ '%old_node_date' => format_date($file->old_timestamp),
+ '%current_node_date' => format_date($file->current_timestamp)
+ );
+ $message = t('Private upload bulk convert: ' . $message);
+ drupal_set_message($message, $message_array, $message_type);
+ watchdog('private_upload', $message, $message_array, $message_watchdog_severity_array[$message_type], $node_link);
}
- else {
- $output .= t("Could not move %filepath to private directory (fid: %fid attached to node: nid).",
- array('%filepath'=>$file->filepath, '%fid'=>$file->fid, 'nid'=>$file->nid)). "
";
- }
+ return t('The conversion process is complete. Please review the messages, above, for any errors or warnings. All messages have also been recorded in the system log.');
}
- return $output;
-}
/**
* Set the filepath for the file in the db.