Commit e948f79c authored by catch's avatar catch
Browse files

Issue #2074475 by benjy, Gábor Hojtsy: Rename {file_managed}.timestamp to...

Issue #2074475 by benjy, Gábor Hojtsy: Rename {file_managed}.timestamp to 'changed' and add a created timestamp.
parent 261fb648
...@@ -71,7 +71,8 @@ display: ...@@ -71,7 +71,8 @@ display:
filemime: filemime filemime: filemime
filesize: filesize filesize: filesize
status: status status: status
timestamp: timestamp created: created
changed: changed
count: count count: count
info: info:
fid: fid:
...@@ -109,7 +110,14 @@ display: ...@@ -109,7 +110,14 @@ display:
separator: '' separator: ''
empty_column: false empty_column: false
responsive: priority-low responsive: priority-low
timestamp: created:
sortable: true
default_sort_order: desc
align: ''
separator: ''
empty_column: false
responsive: ''
changed:
sortable: true sortable: true
default_sort_order: desc default_sort_order: desc
align: '' align: ''
...@@ -123,7 +131,7 @@ display: ...@@ -123,7 +131,7 @@ display:
separator: '' separator: ''
empty_column: false empty_column: false
responsive: priority-medium responsive: priority-medium
default: timestamp default: changed
empty_table: true empty_table: true
row: row:
type: fields type: fields
...@@ -340,10 +348,10 @@ display: ...@@ -340,10 +348,10 @@ display:
empty_zero: false empty_zero: false
hide_alter_empty: true hide_alter_empty: true
plugin_id: file_status plugin_id: file_status
timestamp: created:
id: timestamp id: created
table: file_managed table: file_managed
field: timestamp field: created
relationship: none relationship: none
group_type: group group_type: group
admin_label: '' admin_label: ''
...@@ -392,6 +400,58 @@ display: ...@@ -392,6 +400,58 @@ display:
custom_date_format: '' custom_date_format: ''
timezone: '' timezone: ''
plugin_id: date plugin_id: date
changed:
id: changed
table: file_managed
field: changed
relationship: none
group_type: group
admin_label: ''
label: 'Changed date'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
date_format: medium
custom_date_format: ''
timezone: ''
plugin_id: date
count: count:
id: count id: count
table: file_usage table: file_usage
......
...@@ -81,8 +81,8 @@ function hook_file_validate(Drupal\file\FileInterface $file) { ...@@ -81,8 +81,8 @@ function hook_file_validate(Drupal\file\FileInterface $file) {
* The file entity that is about to be created or updated. * The file entity that is about to be created or updated.
*/ */
function hook_file_presave(Drupal\file\FileInterface $file) { function hook_file_presave(Drupal\file\FileInterface $file) {
// Change the file timestamp to an hour prior. // Change the owner of the file.
$file->timestamp -= 3600; $file->uid->value = 1;
} }
/** /**
......
...@@ -78,8 +78,15 @@ function file_schema() { ...@@ -78,8 +78,15 @@ function file_schema() {
'default' => 0, 'default' => 0,
'size' => 'tiny', 'size' => 'tiny',
), ),
'timestamp' => array( 'created' => array(
'description' => 'UNIX timestamp for when the file was added.', 'description' => 'UNIX timestamp for when the file added.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'description' => 'UNIX timestamp for when the file was last changed.',
'type' => 'int', 'type' => 'int',
'unsigned' => TRUE, 'unsigned' => TRUE,
'not null' => TRUE, 'not null' => TRUE,
...@@ -89,7 +96,7 @@ function file_schema() { ...@@ -89,7 +96,7 @@ function file_schema() {
'indexes' => array( 'indexes' => array(
'uid' => array('uid'), 'uid' => array('uid'),
'status' => array('status'), 'status' => array('status'),
'timestamp' => array('timestamp'), 'changed' => array('changed'),
), ),
'unique keys' => array( 'unique keys' => array(
'uuid' => array('uuid'), 'uuid' => array('uuid'),
...@@ -320,4 +327,3 @@ function file_update_8004() { ...@@ -320,4 +327,3 @@ function file_update_8004() {
); );
db_change_field('file_managed', 'filesize', 'filesize', $spec); db_change_field('file_managed', 'filesize', 'filesize', $spec);
} }
...@@ -1036,7 +1036,11 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr ...@@ -1036,7 +1036,11 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr
break; break;
// These tokens are default variations on the chained tokens handled below. // These tokens are default variations on the chained tokens handled below.
case 'timestamp': case 'created':
$replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode); $replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
break; break;
...@@ -1047,7 +1051,11 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr ...@@ -1047,7 +1051,11 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr
} }
} }
if ($date_tokens = $token_service->findWithPrefix($tokens, 'timestamp')) { if ($date_tokens = $token_service->findWithPrefix($tokens, 'created')) {
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getCreatedTime()), $options);
}
if ($date_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getChangedTime()), $options); $replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getChangedTime()), $options);
} }
...@@ -1094,8 +1102,13 @@ function file_token_info() { ...@@ -1094,8 +1102,13 @@ function file_token_info() {
'name' => t("URL"), 'name' => t("URL"),
'description' => t("The web-accessible URL for the file."), 'description' => t("The web-accessible URL for the file."),
); );
$file['timestamp'] = array( $file['created'] = array(
'name' => t("Timestamp"), 'name' => t("Created"),
'description' => t("The date the file created."),
'type' => 'date',
);
$file['changed'] = array(
'name' => t("Changed"),
'description' => t("The date the file was most recently changed."), 'description' => t("The date the file was most recently changed."),
'type' => 'date', 'type' => 'date',
); );
......
...@@ -154,15 +154,30 @@ function file_views_data() { ...@@ -154,15 +154,30 @@ function file_views_data() {
), ),
); );
// timestamp field // The created field.
$data['file_managed']['timestamp'] = array( $data['file_managed']['created'] = array(
'title' => t('Upload date'), 'title' => t('Upload date'),
'help' => t('The date the file was uploaded.'), 'help' => t('The date the file was uploaded.'),
'field' => array( 'field' => array(
'id' => 'date', 'id' => 'date',
), ),
'sort' => array( 'sort' => array(
'id' => 'date' 'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
);
// The changed field.
$data['file_managed']['changed'] = array(
'title' => t('Modified date'),
'help' => t('The date the file was last changed.'),
'field' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'date',
), ),
'filter' => array( 'filter' => array(
'id' => 'date', 'id' => 'date',
......
...@@ -108,11 +108,18 @@ public function setSize($size) { ...@@ -108,11 +108,18 @@ public function setSize($size) {
$this->get('filesize')->value = $size; $this->get('filesize')->value = $size;
} }
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getChangedTime() { public function getChangedTime() {
return $this->get('timestamp')->value; return $this->get('changed')->value;
} }
/** /**
...@@ -178,7 +185,11 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr ...@@ -178,7 +185,11 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr
public function preSave(EntityStorageControllerInterface $storage_controller) { public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller); parent::preSave($storage_controller);
$this->timestamp = REQUEST_TIME; $this->changed->value = REQUEST_TIME;
if (empty($this->created->value)) {
$this->created->value = REQUEST_TIME;
}
$this->setSize(filesize($this->getFileUri())); $this->setSize(filesize($this->getFileUri()));
if (!isset($this->langcode->value)) { if (!isset($this->langcode->value)) {
// Default the file's language code to none, because files are language // Default the file's language code to none, because files are language
...@@ -253,9 +264,13 @@ public static function baseFieldDefinitions($entity_type) { ...@@ -253,9 +264,13 @@ public static function baseFieldDefinitions($entity_type) {
->setLabel(t('Status')) ->setLabel(t('Status'))
->setDescription(t('The status of the file, temporary (0) and permanent (1).')); ->setDescription(t('The status of the file, temporary (0) and permanent (1).'));
$fields['timestamp'] = FieldDefinition::create('integer') $fields['created'] = FieldDefinition::create('integer')
->setLabel(t('Created')) ->setLabel(t('Created'))
->setDescription(t('The time that the node was created.')); ->setDescription(t('The timestamp that the file was created.'));
$fields['changed'] = FieldDefinition::create('integer')
->setLabel(t('Changed'))
->setDescription(t('The timestamp that the file was last changed.'));
return $fields; return $fields;
} }
......
...@@ -127,4 +127,11 @@ public function getOwner(); ...@@ -127,4 +127,11 @@ public function getOwner();
*/ */
public function setOwner(UserInterface $user); public function setOwner(UserInterface $user);
/**
* Returns the node creation timestamp.
*
* @return int
* Creation timestamp of the node.
*/
public function getCreatedTime();
} }
...@@ -33,9 +33,9 @@ public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT) { ...@@ -33,9 +33,9 @@ public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT) {
public function retrieveTemporaryFiles() { public function retrieveTemporaryFiles() {
// Use separate placeholders for the status to avoid a bug in some versions // Use separate placeholders for the status to avoid a bug in some versions
// of PHP. See http://drupal.org/node/352956. // of PHP. See http://drupal.org/node/352956.
return $this->database->query('SELECT fid FROM {' . $this->entityInfo->getBaseTable() . '} WHERE status <> :permanent AND timestamp < :timestamp', array( return $this->database->query('SELECT fid FROM {' . $this->entityInfo->getBaseTable() . '} WHERE status <> :permanent AND changed < :changed', array(
':permanent' => FILE_STATUS_PERMANENT, ':permanent' => FILE_STATUS_PERMANENT,
':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE ':changed' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE
)); ));
} }
} }
...@@ -23,7 +23,7 @@ class File extends WizardPluginBase { ...@@ -23,7 +23,7 @@ class File extends WizardPluginBase {
/** /**
* Set the created column. * Set the created column.
*/ */
protected $createdColumn = 'timestamp'; protected $createdColumn = 'created';
/** /**
* Set default values for the path field options. * Set default values for the path field options.
......
...@@ -65,7 +65,7 @@ function testInUse() { ...@@ -65,7 +65,7 @@ function testInUse() {
// of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. // of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
db_update('file_managed') db_update('file_managed')
->fields(array( ->fields(array(
'timestamp' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1), 'changed' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1),
)) ))
->condition('fid', $file->id()) ->condition('fid', $file->id())
->execute(); ->execute();
......
...@@ -119,7 +119,7 @@ function testRevisions() { ...@@ -119,7 +119,7 @@ function testRevisions() {
// of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. // of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
db_update('file_managed') db_update('file_managed')
->fields(array( ->fields(array(
'timestamp' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1), 'changed' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1),
)) ))
->condition('fid', $node_file_r3->id()) ->condition('fid', $node_file_r3->id())
->execute(); ->execute();
...@@ -134,7 +134,7 @@ function testRevisions() { ...@@ -134,7 +134,7 @@ function testRevisions() {
// of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. // of the file is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
db_update('file_managed') db_update('file_managed')
->fields(array( ->fields(array(
'timestamp' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1), 'changed' => REQUEST_TIME - (DRUPAL_MAXIMUM_TEMP_FILE_AGE + 1),
)) ))
->condition('fid', $node_file_r1->id()) ->condition('fid', $node_file_r1->id())
->execute(); ->execute();
......
...@@ -158,7 +158,8 @@ protected function createFile() { ...@@ -158,7 +158,8 @@ protected function createFile() {
'filename' => 'druplicon.txt', 'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt', 'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain', 'filemime' => 'text/plain',
'timestamp' => 1, 'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT, 'status' => FILE_STATUS_PERMANENT,
)); ));
file_put_contents($file->getFileUri(), 'hello world'); file_put_contents($file->getFileUri(), 'hello world');
......
...@@ -111,7 +111,8 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) { ...@@ -111,7 +111,8 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
$file->filename = drupal_basename($file->uri); $file->filename = drupal_basename($file->uri);
$file->filemime = 'text/plain'; $file->filemime = 'text/plain';
$file->uid = 1; $file->uid = 1;
$file->timestamp = REQUEST_TIME; $file->created = REQUEST_TIME;
$file->changed = REQUEST_TIME;
$file->filesize = filesize($file->uri); $file->filesize = filesize($file->uri);
$file->status = 0; $file->status = 0;
// Write the record directly rather than using the API so we don't invoke // Write the record directly rather than using the API so we don't invoke
......
...@@ -53,8 +53,10 @@ function testFileTokenReplacement() { ...@@ -53,8 +53,10 @@ function testFileTokenReplacement() {
$tests['[file:mime]'] = check_plain($file->getMimeType()); $tests['[file:mime]'] = check_plain($file->getMimeType());
$tests['[file:size]'] = format_size($file->getSize()); $tests['[file:size]'] = format_size($file->getSize());
$tests['[file:url]'] = check_plain(file_create_url($file->getFileUri())); $tests['[file:url]'] = check_plain(file_create_url($file->getFileUri()));
$tests['[file:timestamp]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->id); $tests['[file:created]'] = format_date($file->getCreatedTime(), 'medium', '', NULL, $language_interface->id);
$tests['[file:timestamp:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->id); $tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->id);
$tests['[file:changed]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->id);
$tests['[file:changed:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->id);
$tests['[file:owner]'] = check_plain(user_format_name($this->admin_user)); $tests['[file:owner]'] = check_plain(user_format_name($this->admin_user));
$tests['[file:owner:uid]'] = $file->getOwner()->id(); $tests['[file:owner:uid]'] = $file->getOwner()->id();
......
...@@ -28,7 +28,8 @@ function testFileSave() { ...@@ -28,7 +28,8 @@ function testFileSave() {
'filename' => 'druplicon.txt', 'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt', 'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain', 'filemime' => 'text/plain',
'timestamp' => 1, 'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT, 'status' => FILE_STATUS_PERMANENT,
)); ));
file_put_contents($file->getFileUri(), 'hello world'); file_put_contents($file->getFileUri(), 'hello world');
...@@ -70,7 +71,8 @@ function testFileSave() { ...@@ -70,7 +71,8 @@ function testFileSave() {
'filename' => 'DRUPLICON.txt', 'filename' => 'DRUPLICON.txt',
'uri' => 'public://DRUPLICON.txt', 'uri' => 'public://DRUPLICON.txt',
'filemime' => 'text/plain', 'filemime' => 'text/plain',
'timestamp' => 1, 'created' => 1,
'changed' => 1,
'status' => FILE_STATUS_PERMANENT, 'status' => FILE_STATUS_PERMANENT,
)); ));
file_put_contents($file->getFileUri(), 'hello world'); file_put_contents($file->getFileUri(), 'hello world');
......
...@@ -134,7 +134,7 @@ function testTempFileCleanup() { ...@@ -134,7 +134,7 @@ function testTempFileCleanup() {
db_update('file_managed') db_update('file_managed')
->fields(array( ->fields(array(
'status' => 0, 'status' => 0,
'timestamp' => 1, 'changed' => 1,
)) ))
->condition('fid', $temp_old->id()) ->condition('fid', $temp_old->id())
->execute(); ->execute();
...@@ -151,7 +151,7 @@ function testTempFileCleanup() { ...@@ -151,7 +151,7 @@ function testTempFileCleanup() {
// Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. // Permanent file that is older than DRUPAL_MAXIMUM_TEMP_FILE_AGE.
$perm_old = file_save_data(''); $perm_old = file_save_data('');
db_update('file_managed') db_update('file_managed')
->fields(array('timestamp' => 1)) ->fields(array('changed' => 1))
->condition('fid', $temp_old->id()) ->condition('fid', $temp_old->id())
->execute(); ->execute();
$this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.'); $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.');
......
...@@ -224,7 +224,8 @@ public function testFileHooks() { ...@@ -224,7 +224,8 @@ public function testFileHooks() {
'filemime' => 'text/plain', 'filemime' => 'text/plain',
'filesize' => filesize($url), 'filesize' => filesize($url),
'status' => 1, 'status' => 1,
'timestamp' => REQUEST_TIME, 'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
)); ));
$this->assertHookMessageOrder(array( $this->assertHookMessageOrder(array(
......
...@@ -776,7 +776,8 @@ ...@@ -776,7 +776,8 @@
'filemime', 'filemime',
'filesize', 'filesize',
'status', 'status',
'timestamp' 'created',
'changed',
)) ))
->values(array( ->values(array(
'fid' => '1', 'fid' => '1',
...@@ -786,6 +787,7 @@ ...@@ -786,6 +787,7 @@
'filemime' => 'text/plain', 'filemime' => 'text/plain',
'filesize' => 0, 'filesize' => 0,
'status' => 1, 'status' => 1,