From f22635779b10fe3735a11bc1af5c88653e580534 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Wed, 7 Oct 2009 15:36:14 +0200 Subject: New dformat() function for fuzzy date support Ignore-this: 7193cd788fee2c05a9068bf6edd5dc17 It is now possible to use the %f placeholder in $conf['dformat'] to add a fuzzy age string. Template developers and plugin authors should replace their strftime + $conf['dformat'] calls with calls to the new dformat() function. Example: %Y/%m/%d %H:%M (%f) produces dates like this: 2009/09/16 10:36 (3 weeks ago) darcs-hash:20091007133614-6e07b-677108d1b43928ef8fd886813e43514507b5e073.gz --- inc/common.php | 22 +++++++++++++++++++++- inc/html.php | 20 ++++++++++---------- inc/lang/de/lang.php | 7 +++++++ inc/lang/en/lang.php | 12 ++++++------ inc/media.php | 4 ++-- inc/template.php | 2 +- inc/toolbar.php | 2 +- lib/exe/ajax.php | 2 +- lib/tpl/default/detail.php | 2 +- 9 files changed, 50 insertions(+), 23 deletions(-) diff --git a/inc/common.php b/inc/common.php index 82d5f69ee..a15cb5c62 100644 --- a/inc/common.php +++ b/inc/common.php @@ -1082,7 +1082,7 @@ function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ } $ip = clientIP(); - $text = str_replace('@DATE@',strftime($conf['dformat']),$text); + $text = str_replace('@DATE@',dformat(),$text); $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); $text = str_replace('@IPADDRESS@',$ip,$text); $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text); @@ -1223,6 +1223,26 @@ function datetime_h($dt){ } +/** + * Wraps around strftime but provides support for fuzzy dates + * + * The format default to $conf['dformat']. It is passed to + * strftime - %f can be used to get the value from datetime_h() + * + * @see datetime_h + * @author Andreas Gohr + */ +function dformat($dt=null,$format=''){ + global $conf; + + if(is_null($dt)) $dt = time(); + $dt = (int) $dt; + if(!$format) $format = $conf['dformat']; + + $format = str_replace('%f',datetime_h($dt),$format); + return strftime($format,$dt); +} + /** * return an obfuscated email address in line with $conf['mailguard'] setting * diff --git a/inc/html.php b/inc/html.php index 46ced1de1..0ea72c06c 100644 --- a/inc/html.php +++ b/inc/html.php @@ -249,7 +249,7 @@ function html_draft(){ $form->addHidden('date', $draft['date']); $form->addElement(form_makeWikiText($text, array('readonly'=>'readonly'))); $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status'))); - $form->addElement($lang['draftdate'].' '. strftime($conf['dformat'],filemtime($INFO['draft']))); + $form->addElement($lang['draftdate'].' '. dformat(filemtime($INFO['draft']))); $form->addElement(form_makeCloseTag('div')); $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1'))); $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2'))); @@ -382,7 +382,7 @@ function html_locked(){ global $INFO; $locktime = filemtime(wikiLockFN($ID)); - $expire = @strftime($conf['dformat'], $locktime + $conf['locktime'] ); + $expire = dformat($locktime + $conf['locktime']); $min = round(($conf['locktime'] - (time() - $locktime) )/60); print p_locale_xhtml('locked'); @@ -418,7 +418,7 @@ function html_revisions($first=0){ array_pop($revisions); // remove extra log entry } - $date = @strftime($conf['dformat'],$INFO['lastmod']); + $date = dformat($INFO['lastmod']); print p_locale_xhtml('revisions'); @@ -466,7 +466,7 @@ function html_revisions($first=0){ } foreach($revisions as $rev){ - $date = strftime($conf['dformat'],$rev); + $date = dformat($rev); $info = getRevisionInfo($ID,$rev,true); $exists = page_exists($ID,$rev); @@ -593,7 +593,7 @@ function html_recent($first=0){ $form->addElement(form_makeOpenTag('ul')); foreach($recents as $recent){ - $date = strftime($conf['dformat'],$recent['date']); + $date = dformat($recent['date']); if ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $form->addElement(form_makeOpenTag('li', array('class' => 'minor'))); else @@ -878,7 +878,7 @@ function html_diff($text='',$intro=true){ $l_rev = ''; $l_text = rawWiki($ID,''); $l_head = ''. - $ID.' '.strftime($conf['dformat'],@filemtime(wikiFN($ID))).' '. + $ID.' '.dformat((int) @filemtime(wikiFN($ID))).' '. $lang['current']; $r_rev = ''; @@ -925,7 +925,7 @@ function html_diff($text='',$intro=true){ if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"'; $l_head = ''. - $ID.' ['.strftime($conf['dformat'],$l_rev).']'. + $ID.' ['.dformat($l_rev).']'. '
'.$l_user.' '.$l_sum; } @@ -939,7 +939,7 @@ function html_diff($text='',$intro=true){ if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"'; $r_head = ''. - $ID.' ['.strftime($conf['dformat'],$r_rev).']'. + $ID.' ['.dformat($r_rev).']'. '
'.$r_user.' '.$r_sum; }elseif($_rev = @filemtime(wikiFN($ID))){ $_info = getRevisionInfo($ID,$_rev,true); @@ -951,7 +951,7 @@ function html_diff($text='',$intro=true){ if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"'; $r_head = ''. - $ID.' ['.strftime($conf['dformat'],$_rev).'] '. + $ID.' ['.dformat($_rev).'] '. '('.$lang['current'].')'. '
'.$_user.' '.$_sum; }else{ @@ -1173,7 +1173,7 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
-
+
diff --git a/inc/lang/de/lang.php b/inc/lang/de/lang.php index 530b0bc24..1ab03e4c1 100644 --- a/inc/lang/de/lang.php +++ b/inc/lang/de/lang.php @@ -247,3 +247,10 @@ $lang['mu_filetypes'] = 'Erlaubte Dateitypen'; $lang['mu_info'] = 'Dateien hochgeladen!'; $lang['mu_lasterr'] = 'Letzter Fehler:'; $lang['recent_global'] = 'Im Moment sehen Sie die Änderungen im Namensraum %s. Sie können auch die Änderungen im gesamten Wiki sehen.'; + +$lang['years'] = 'vor %d Jahren'; +$lang['months'] = 'vor %d Monaten'; +$lang['weeks'] = 'vor %d Wochen'; +$lang['days'] = 'vor %d Tagen'; +$lang['hours'] = 'vor %d Stunden'; +$lang['seconds'] = 'vor %d Sekunden'; diff --git a/inc/lang/en/lang.php b/inc/lang/en/lang.php index fbde8cab0..dfe45bf83 100644 --- a/inc/lang/en/lang.php +++ b/inc/lang/en/lang.php @@ -269,11 +269,11 @@ $lang['mu_lasterr'] = 'Last error:'; $lang['recent_global'] = 'You\'re currently watching the changes inside the %s namespace. You can also view the recent changes of the whole wiki.'; -$lang['years'] = '%d years'; -$lang['months'] = '%d months'; -$lang['weeks'] = '%d weeks'; -$lang['days'] = '%d days'; -$lang['hours'] = '%d hours'; -$lang['seconds'] = '%d seconds'; +$lang['years'] = '%d years ago'; +$lang['months'] = '%d months ago'; +$lang['weeks'] = '%d weeks ago'; +$lang['days'] = '%d days ago'; +$lang['hours'] = '%d hours ago'; +$lang['seconds'] = '%d seconds ago'; //Setup VIM: ex: et ts=2 enc=utf-8 : diff --git a/inc/media.php b/inc/media.php index 4ad7047b2..2a45b6d80 100644 --- a/inc/media.php +++ b/inc/media.php @@ -404,7 +404,7 @@ function media_notify($id,$file,$mime){ $ip = clientIP(); $text = rawLocale('uploadmail'); - $text = str_replace('@DATE@',strftime($conf['dformat']),$text); + $text = str_replace('@DATE@',dformat(),$text); $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); $text = str_replace('@IPADDRESS@',$ip,$text); $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text); @@ -570,7 +570,7 @@ function media_printfile($item,$auth,$jump,$display_namespace=false){ $info .= (int) $item['meta']->getField('File.Height'); $info .= ' '; } - $info .= ''.strftime($conf['dformat'],$item['mtime']).''; + $info .= ''.dformat($item['mtime']).''; $info .= ' '; $info .= filesize_h($item['size']); diff --git a/inc/template.php b/inc/template.php index c181c164c..e26c5202a 100644 --- a/inc/template.php +++ b/inc/template.php @@ -958,7 +958,7 @@ function tpl_pageinfo($ret=false){ } } $fn = utf8_decodeFN($fn); - $date = strftime($conf['dformat'],$INFO['lastmod']); + $date = dformat($INFO['lastmod']); // print it if($INFO['exists']){ diff --git a/inc/toolbar.php b/inc/toolbar.php index a1851c746..701723b58 100644 --- a/inc/toolbar.php +++ b/inc/toolbar.php @@ -236,7 +236,7 @@ function toolbar_signature(){ $sig = str_replace('@USER@',$_SERVER['REMOTE_USER'],$sig); $sig = str_replace('@NAME@',$INFO['userinfo']['name'],$sig); $sig = str_replace('@MAIL@',$INFO['userinfo']['mail'],$sig); - $sig = str_replace('@DATE@',strftime($conf['dformat']),$sig); + $sig = str_replace('@DATE@',dformat(),$sig); $sig = str_replace('\\\\n','\\n',addslashes($sig)); return $sig; } diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php index 4d9a8105a..4618abd71 100644 --- a/lib/exe/ajax.php +++ b/lib/exe/ajax.php @@ -152,7 +152,7 @@ function ajax_lock(){ ); $cname = getCacheName($draft['client'].$id,'.draft'); if(io_saveFile($cname,serialize($draft))){ - echo $lang['draftdate'].' '.strftime($conf['dformat']); + echo $lang['draftdate'].' '.dformat(); } } diff --git a/lib/tpl/default/detail.php b/lib/tpl/default/detail.php index 765358568..4f42b116e 100644 --- a/lib/tpl/default/detail.php +++ b/lib/tpl/default/detail.php @@ -53,7 +53,7 @@ if (!defined('DOKU_INC')) die();
'.$lang['img_date'].':
'.strftime($conf['dformat'],$t).'
'; + if($t) print '
'.$lang['img_date'].':
'.dformat($t).'
'; $t = tpl_img_getTag('File.Name'); if($t) print '
'.$lang['img_fname'].':
'.hsc($t).'
'; -- cgit v1.2.3