标签 Typecho 下的文章

修改Typecho后台日期显示方式

修改前:

Typecho日期默认显示方式.png

最近的7天内,显示的字符是:“N天前”,其实我更关注的是博文的具体年月日。所以定位到\var\Typecho\I18n.php文件Typecho_I18n类的dateWord方法:

public static function dateWord($from, $now)
{
    $between = $now - $from;

    /** 如果是一天 */
    if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
        /** 如果是一小时 */
        if ($between < 3600) {
            /** 如果是一分钟 */
            if ($between < 60) {
                if (0 == $between) {
                    return _t('刚刚');
                } else {
                    return str_replace('%d', $between, _n('一秒前', '%d秒前', $between));
                }
            }

            $min = floor($between / 60);
            return str_replace('%d', $min, _n('一分钟前', '%d分钟前', $min));
        }

        $hour = floor($between / 3600);
        return str_replace('%d', $hour, _n('一小时前', '%d小时前', $hour));
    }

    /** 如果是昨天 */
    if ($between > 0 && $between < 172800 
    && (date('z', $from) + 1 == date('z', $now)                             // 在同一年的情况 
        || date('z', $from) + 1 == date('L') + 365 + date('z', $now))) {    // 跨年的情况
        return _t('昨天 %s', date('H:i', $from));
    }

    /** 如果是一个星期 */
    if ($between > 0 && $between < 604800) {
        $day = floor($between / 86400);
        return str_replace('%d', $day, _n('一天前', '%d天前', $day));
    }

    /** 如果是 */
    if (date('Y', $from) == date('Y', $now)) {
        return date(_t('n月j日'), $from);
    }

    return date(_t('Y年m月d日'), $from);
}

仅仅保留最后一行,简单粗暴:

public static function dateWord($from, $now)
{
    return date(_t('Y年m月d日'), $from);
}

修改后效果:

Typecho日期修改后显示方式.png.png

Typecho实现键盘左箭头右箭头按键切换到上一篇下一篇文章

github gist: https://gist.github.com/fr33m44/f2abc5a5204e0d14cdf915c56d5ffb10

if(document.getElementsByClassName('post-near')[0].childNodes[1].innerHTML != '上一篇: 没有了')
    var pre_link = document.getElementsByClassName('post-near')[0].childNodes[1].childNodes[1].href;
if(document.getElementsByClassName('post-near')[0].childNodes[3].innerHTML != '上一篇: 没有了')
    var next_link = document.getElementsByClassName('post-near')[0].childNodes[3].childNodes[1].href;
document.onkeydown=function(e){
    if(e.keyCode==37 && pre_link != undefined)
        window.location = pre_link;
    else if (e.keyCode ==39 && next_link != undefined)
        window.location = next_link;
};

将外链图片保存到Typecho本地

<?php 
//connect to typecho database
$curl = curl_init();
$res = mysql_connect('localhost', 'root', '1111');
$ret = mysql_select_db('typecho', $res);
$sql = "select * from jq_contents where type = 'post' order by cid asc";
$query = mysql_query($sql, $res);
while($row = mysql_fetch_assoc($query))
{
    
    preg_match_all('/src="(http:\/\/ww[1234].*?\/.*?\/(.*?))".*?"/', $row['text'], $matches);
    //url : $matches[1][x]  filename : $matches[2][x] , alt: $matches[3][x]
    foreach($matches[1] as $k=>$pic)
    {
        //print_r($matches);
        //文章日期->local date
        $local_date =  date('Y-m-d', $row['created']);
        //print_r($local_date);
        //建立图片所在日期规格的目录
        $arr_date = explode('-', $local_date);
        //@mkdir(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]); 
        //@mkdir(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]);
        //print_r(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1].'<br />');
        //下载图片
        /*
            curl_setopt($curl, CURLOPT_URL, $matches[1][$k]);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,0);
            $data = curl_exec($curl);
            $ret = file_put_contents(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k], $data);
            print_r(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k]." ret:$ret<br />");
        
        */
        //插入typecho数据库
        /*
        $type = explode('.',$matches[2][$k]);
        $text = array(
            'name'=>$matches[2][$k],
            'path'=>"/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$matches[2][$k],
            'size'=>filesize(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k]),
            'type'=> $type[1],
            'mime'=>mime_content_type(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k])
        );
        $sql2 = sprintf("INSERT INTO `jq_contents` (  `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", $matches[2][$k],str_replace('.','-', $matches[2][$k]), $row['created'],$row['created'],serialize($text), $k, 1, '', 'attachment', 'publish', '', 0, 1,1,1,$row['cid']);
        $query2 = mysql_query($sql2, $res);
        print_r($sql2.'<br />');
        
        */
        //更新文章内的图片路径
        
        $old_img_path = $matches[1][$k];
        $new_img_path = "http://tunps.com/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$matches[2][$k];
        $newtext = str_replace($old_img_path, $new_img_path, $row['text']);
        $newtext = mysql_escape_string($newtext);
        $sql3 = "UPDATE `jq_contents` SET  `text` =  '$newtext' where cid=$row[cid] ";
        $query3 = mysql_query($sql3);
        var_dump($query3);
         
    }
}
curl_close($curl);

Gist: https://gist.github.com/fr33m44/118d9f3873dbf959228f06bfe5a0253a

以上PHP脚本的作用是将外链的图片通过cURL下载到Typecho本地博客目录。然后在数据库中替换文章内容中的图片链接为本地图片链接。同时将图片信息关联到文章上。

通过contents.parent字段与contents.cid字段关联。

之所有没有做成Typecho插件形式,那是因为。。。我不懂Typecho插件机制。。。= =#