Commit 0ec23b9b authored by webchick's avatar webchick

#357403 by drewish: Make user pictures managed files.

parent cb07c999
......@@ -170,11 +170,10 @@ function user_schema() {
'description' => "User's default language.",
),
'picture' => array(
'type' => 'varchar',
'length' => 255,
'type' => 'int',
'not null' => TRUE,
'default' => '',
'description' => "Path to the user's uploaded picture.",
'default' => 0,
'description' => "Foreign key: {files}.fid of user's picture.",
),
'init' => array(
'type' => 'varchar',
......@@ -387,6 +386,83 @@ function user_update_7003() {
return $ret;
}
/**
* Add the user's pictures to the {files} table and make them managed files.
*/
function user_update_7004(&$sandbox) {
$ret = array();
$picture_field = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => t("Foriegn key: {files}.fid of user's picture."),
);
if (!isset($sandbox['progress'])) {
// Check that the field hasn't been updated in an aborted run of this
// update.
if (!db_column_exists('users', 'picture_fid')) {
// Add a new field for the fid.
db_add_field($ret, 'users', 'picture_fid', $picture_field);
}
// Initialize batch update information.
$sandbox['progress'] = 0;
$sandbox['last_user_processed'] = -1;
$sandbox['max'] = db_query("SELECT COUNT(*) FROM {users} WHERE picture <> ''")->fetchField();
}
// As a batch operation move the photos into the {files} table and update the
// {user} records.
$limit = 500;
$result = db_query_range("SELECT uid, picture FROM {users} WHERE picture <> '' AND uid > :uid ORDER BY uid", array(':uid' => $sandbox['last_user_processed']), 0, $limit);
foreach ($result as $user) {
// Don't bother adding files that don't exist.
if (!file_exists($user->picture)) {
continue;
}
// Check if the file already exists.
$files = file_load_multiple(array(), array('filepath' => $user->picture));
if (count($files)) {
$file = reset($files);
}
else {
// Create a file object.
$file = new stdClass();
$file->filepath = $user->picture;
$file->filename = basename($file->filepath);
$file->filemime = file_get_mimetype($file->filepath);
$file->uid = $user->uid;
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
}
db_update('users')
->fields(array('picture_fid' => $file->fid))
->condition('uid', $user->uid)
->execute();
// Update our progress information for the batch update.
$sandbox['progress']++;
$sandbox['last_user_processed'] = $user->uid;
}
// Indicate our current progress to the batch update system. If there's no
// max value then there's nothing to update and we're finished.
$ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
// When we're finished, drop the old picture field and rename the new one to
// replace it.
if (isset($ret['#finished']) && $ret['#finished'] == 1) {
db_drop_field($ret, 'users', 'picture');
db_change_field($ret, 'users', 'picture_fid', 'picture', $picture_field);
}
return $ret;
}
/**
* @} End of "defgroup user-updates-6.x-to-7.x"
* The next series of updates should start at 8000.
......
This diff is collapsed.
......@@ -418,7 +418,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
// Create a regular user.
$account = $this->drupalCreateUser(array());
// Create administrative user.
$admin_user = $this->drupalCreateUser(array('administer users'));
$this->drupalLogin($admin_user);
......@@ -547,7 +547,8 @@ class UserPictureTestCase extends DrupalWebTestCase {
// user's profile page.
$text = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $test_dim));
$this->assertRaw($text, t('Image was resized.'));
$this->assertRaw(file_create_url($pic_path), t("Image is displayed in user's profile page"));
$alt = t("@user's picture", array('@user' => $this->user->name));
$this->assertRaw(theme('image', $pic_path, $alt, $alt, '', FALSE), t("Image is displayed in user's profile page"));
// Check if file is located in proper directory.
$this->assertTrue(is_file($pic_path), t("File is located in proper directory"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment