Skip to content

Commit

Permalink
- [F] Replace DB insert with typecho custom field.
Browse files Browse the repository at this point in the history
  使用自定义字段替换数据库操作(不会破坏原有数据库结构)
- [F] fix site title position (mobile)
  • Loading branch information
shiyiya committed Nov 2, 2019
1 parent e0eedba commit 5a27779
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 52 deletions.
22 changes: 11 additions & 11 deletions component/footer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<? if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>

<footer id="footer" role="contentinfo">
<p><i class="iconfont icon-view"></i><? echo _i18n('浏览量') . ' : ' . getSiteViews(); ?></p>
<p><i class="iconfont icon-view"></i><? echo _i18n('浏览量') . ' : ' . siteViewer(); ?></p>
<p id="live-time"></p>
<p>
&copy; <? echo date('Y'); ?> <a href="<? $this->options->siteUrl(); ?>"><? $this->options->title(); ?></a>.
Expand Down Expand Up @@ -56,7 +56,7 @@
<script src="<? $this->options->themeUrl('js/index.min.js'); ?>"></script>

<!-- type -->
<? if (!empty($this->options->StyleSettings) || in_array('Banner', $this->options->StyleSettings)) : ?>
<? if (!empty($this->options->StyleSettings) && in_array('Banner', $this->options->StyleSettings)) : ?>
<script src="<? $this->options->themeUrl('js/type.min.js'); ?>"></script>
<? endif; ?>

Expand Down Expand Up @@ -105,15 +105,15 @@
<? _e($this->options->customScript) ?>

// server worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.min.js')
.then(function(reg) {
console.log('%c Sagiri serviceWorker is working ! ', 'background: #000; color:#f6f93e; padding:5px 0;');
})
.catch(function(error) {
console.log('serviceWorker failed with ' + error);
});
}
// if ('serviceWorker' in navigator) {
// navigator.serviceWorker.register('/sw.min.js')
// .then(function(reg) {
// console.log('%c Sagiri serviceWorker is working ! ', 'background: #000; color:#f6f93e; padding:5px 0;');
// })
// .catch(function(error) {
// console.log('serviceWorker failed with ' + error);
// });
// }
</script>

</div><!-- End root -->
Expand Down
2 changes: 1 addition & 1 deletion css/mix.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [A] add jp & zh_tw lang.
- [F] Replace DB insert with typecho custom field.
使用自定义字段替换数据库操作(不会破坏原有数据库结构)
- [F] fix site title position (mobile)

<!-- -->

Expand Down
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function themeInit($widget)
{
require_once 'libray/i18n/index.php';
require_once 'libray/theme-helper.php';
require_once 'libray/pv.php';
require_once 'libray/field.php';
}


Expand Down
2 changes: 1 addition & 1 deletion js/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 164 additions & 0 deletions libray/field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<? if (!defined('__TYPECHO_ROOT_DIR__')) exit;

function getFieldByName($name, $limit = 5)
{
$db = Typecho_Db::get();

$fields = $db->fetchAll(
$db->select()->from('table.fields')
->where('name = ?', $name)
->limit($limit)
);
return $fields;
}


#### Plan A -- Typecho custom field. ####
function getPostView($widget)
{
$fields = $widget->fields;
$views = 0;

if (empty($fields->views)) {
$widget->setField('views', 'int', '0', $widget->cid);
} else {
$views = $fields->views;
}

if ($widget->is('single')) {
$vieweds = Typecho_Cookie::get('extend_contents_views');
empty($vieweds) ? $vieweds = array() : $vieweds = explode(',', $vieweds);

if (!in_array($widget->cid, $vieweds)) {
$widget->incrIntField('views', '1', $widget->cid);
$vieweds[] = $widget->cid;
$vieweds = implode(',', $vieweds);
Typecho_Cookie::set("extend_contents_views", $vieweds);
}
}
echo $views;
}

function getTopView()
{
$db = Typecho_Db::get();

$viewsFields = getFieldByName('views');

$cids = [];
$cidsMap = [];
foreach ($viewsFields as $val) {
array_push($cids, $val['cid']);
$cidsMap[$val['cid']] = $val['int_value'];
}

$cid = implode(',', $cids);
$days = 99999999;
$time = time() - (24 * 60 * 60 * $days);

$query = $db->select()->from('table.contents')
. ' where cid in (' . $cid . ')'
. ' and created >= ' . $time
. ' and created <= ' . time()
. ' and type = "post" and status = "publish"'
. ' order by views ASC';

$result = $db->fetchAll($query);

echo '<ul class="top-view-archive list">';
foreach ($result as $val) {
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="' . $permalink . '" title="' . $cidsMap[$val['cid']] . '人看过">' . $post_title . '</a></li>';
}
echo '</ul>';
}

function siteViewer()
{
$viewsFields = getFieldByName('views', 9999999999);
$count = 0;

foreach ($viewsFields as $val) {
$count += $val['int_value'];
}
return $count;
}


#### Plan B -- Will destroy the original structure of the Typecho database. ####

## BackUp
/*
```sql
-- postview(mysql)
ALTER TABLE `typecho_contents` ADD `views` INT(10) NULL DEFAULT '0' AFTER `parent`;
```
*/
/*
function getPostView($archive)
{
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$views = Typecho_Cookie::get('extend_contents_views');
if (empty($views)) {
$views = array();
} else {
$views = explode(',', $views);
}
if (!in_array($cid, $views)) {
$db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
array_push($views, $cid);
$views = implode(',', $views);
Typecho_Cookie::set('extend_contents_views', $views);
}
}
echo $row['views'];
}
function getTopView($limit = 5)
{
$days = 99999999999999;
$time = time() - (24 * 60 * 60 * $days);
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select()->from('table.contents')
->where('created >= ?', $time)
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created <= ?', time())
->limit($limit)
->order('views', Typecho_Db::SORT_DESC));
if ($result) {
echo '<ul class="top-view-archive list">';
foreach ($result as $val) {
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="' . $permalink . '" title="' . $val['views'] . '人看过">' . $post_title . '</a></li>';
}
echo '</ul>';
}
}
function siteViewer()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$pom = $db->fetchAll("SELECT SUM(VIEWS) FROM `" . $prefix . "contents` WHERE `type`='page' or `type`='post'");
$num = number_format($pom[0]['SUM(VIEWS)'], 0, '', '');
return $num;
} else {
return 0;
}
}
*/
38 changes: 0 additions & 38 deletions libray/theme-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function isPc()
return true;
}


function showThumb($obj, $themeUrl)
{
$config = Typecho_Widget::widget('Widget_Options')->feature;
Expand Down Expand Up @@ -164,19 +163,6 @@ function getOs($agent)
echo $OSVersion;
}

function getSiteViews()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$pom = $db->fetchAll("SELECT SUM(VIEWS) FROM `" . $prefix . "contents` WHERE `type`='page' or `type`='post'");
$num = number_format($pom[0]['SUM(VIEWS)'], 0, '', '');
return $num;
} else {
return 0;
}
}

function getRandomPosts($limit = 5)
{
$db = Typecho_Db::get();
Expand Down Expand Up @@ -206,30 +192,6 @@ function getRandomPosts($limit = 5)
}
}

function getTopView($limit = 5)
{
$days = 99999999999999;
$time = time() - (24 * 60 * 60 * $days);
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select()->from('table.contents')
->where('created >= ?', $time)
->where('type = ?', 'post')
->where('status = ?', 'publish')
->where('created <= ?', time())
->limit($limit)
->order('views', Typecho_Db::SORT_DESC));
if ($result) {
echo '<ul class="top-view-archive list">';
foreach ($result as $val) {
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="' . $permalink . '" title="' . $val['views'] . '人看过">' . $post_title . '</a></li>';
}
echo '</ul>';
}
}

function getTopCommentPosts($limit = 5)
{
$days = 99999999999999;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"vinyl-source-stream": "^2.0.0"
},
"dependencies": {
"browser-sync": "^2.26.7",
"core-js": "^3.1.4",
"gsap": "^2.1.3"
}
Expand Down

0 comments on commit 5a27779

Please sign in to comment.