Browse Source

Using flock

To make sure we can read the views data before we save it.
pull/680/head
danpros 1 year ago
parent
commit
4d8cfbb733
6 changed files with 69 additions and 30 deletions
  1. +28
    -14
      system/admin/admin.php
  2. +13
    -0
      system/includes/dispatch.php
  3. +10
    -4
      system/includes/functions.php
  4. +4
    -4
      themes/doks/css/style.css
  5. +7
    -4
      themes/doks/main.html.php
  6. +7
    -4
      themes/doks/profile.html.php

+ 28
- 14
system/admin/admin.php View File

@ -451,9 +451,13 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
if ($old_url != $post_url) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'post_' . $old_url, 'post_' . $post_url);
save_json_pretty($viewsFile, $arr);
$views = json_decode(file_get_data($viewsFile), true);
$oKey = 'post_' . $old_url;
$nKey = 'post_' . $post_url;
if (isset($views[$oKey])) {
$arr = replace_key($views, $oKey, $nKey);
save_json_pretty($viewsFile, $arr);
}
}
}
@ -695,9 +699,13 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
if ($old_url != $pu) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'subpage_' . $static .'.'. $old_url, 'subpage_' . $static .'.'. $pu);
save_json_pretty($viewsFile, $arr);
$views = json_decode(file_get_data($viewsFile), true);
$oKey = 'subpage_' . $static . '.' . $old_url;
$nKey = 'subpage_' . $static . '.' . $pu;
if (isset($views[$oKey])) {
$arr = replace_key($views, $oKey, $nKey);
save_json_pretty($viewsFile, $arr);
}
}
}
@ -706,18 +714,26 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
if ($old_url != $pu) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'page_' . $old_url, 'page_' . $pu);
save_json_pretty($viewsFile, $arr);
$views = json_decode(file_get_data($viewsFile), true);
$oKey = 'page_' . $old_url;
$nKey = 'page_' . $pu;
if (isset($views[$oKey])) {
$arr = replace_key($views, $oKey, $nKey);
save_json_pretty($viewsFile, $arr);
}
}
$sPage = find_subpage($pu);
if (!empty($sPage)) {
foreach ($sPage as $sp) {
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
$arr = replace_key($views, 'subpage_' . $old_url . '.' . $sp->slug, 'subpage_' . $pu . '.' . $sp->slug);
save_json_pretty($viewsFile, $arr);
$views = json_decode(file_get_data($viewsFile), true);
$oKey = 'subpage_' . $old_url . '.' . $sp->slug;
$nKey = 'subpage_' . $pu . '.' . $sp->slug;
if (isset($views[$soKey])) {
$arr = replace_key($views, $oKey, $nKey);
save_json_pretty($viewsFile, $arr);
}
}
}
}
@ -1532,7 +1548,6 @@ function reorder_pages($pages = null)
$i = 1;
$arr = array();
$dir = 'content/static/';
$viewsFile = "content/data/views.json";
foreach ($pages as $p) {
$fn = pathinfo($p, PATHINFO_FILENAME);
$num = str_pad($i, 2, 0, STR_PAD_LEFT);
@ -1560,7 +1575,6 @@ function reorder_subpages($subpages = null)
$i = 1;
$arr = array();
$dir = 'content/static/';
$viewsFile = "content/data/views.json";
foreach ($subpages as $sp) {
$dn = $dir . pathinfo($sp, PATHINFO_DIRNAME) . '/';
$fn = pathinfo($sp, PATHINFO_FILENAME);


+ 13
- 0
system/includes/dispatch.php View File

@ -432,6 +432,19 @@ function save_json_pretty($filename, $arr)
}
}
function file_get_data($filename)
{
$thisFile = fopen($filename, 'r');
if (flock($thisFile, LOCK_SH)) {
$fileData = file_get_contents($filename);
flock($thisFile, LOCK_UN);
} else {
$fileData = json_encode(array('flock_fail' => 'reading'));
}
fclose($thisFile);
return $fileData;
}
function condition()
{
static $cb_map = array();


+ 10
- 4
system/includes/functions.php View File

@ -2677,8 +2677,8 @@ function not_found($request = null)
}
if (isset($views[$request])) {
unset($views[$request]);
save_json_pretty($filename, $views);
}
save_json_pretty($filename, $views);
}
}
@ -3253,14 +3253,19 @@ function add_view($page)
$filename = "content/data/views.json";
$views = array();
if (file_exists($filename)) {
$views = json_decode(file_get_contents($filename), true);
$views = json_decode(file_get_data($filename), true);
}
if (isset($views[$page])) {
$views[$page]++;
save_json_pretty($filename, $views);
} else {
$views[$page] = 1;
if (isset($views['flock_fail'])) {
return;
} else {
$views[$page] = 1;
save_json_pretty($filename, $views);
}
}
save_json_pretty($filename, $views);
}
// Get the page views count
@ -3271,6 +3276,7 @@ function get_views($page, $oldID = null)
if (file_exists($filename)) {
$_views = json_decode(file_get_contents($filename), true);
}
if (!is_null($oldID)) {
if (isset($_views[$oldID])) {
$arr = replace_key($_views, $oldID, $page);


+ 4
- 4
themes/doks/css/style.css View File

@ -3402,16 +3402,16 @@ body.dark .docs-links .nav > li.active li.active > a, body.dark .docs-links .n
}
#toc .h3-toc {
text-indent: .25in;
margin-left: .25in;
}
#toc .h4-toc {
text-indent: .50in;
margin-left: .50in;
}
#toc .h5-toc {
text-indent: .75in;
margin-left: .75in;
}
#toc .h6-toc {
text-indent: 1in;
margin-left: 1in;
}
#toc > div {


+ 7
- 4
themes/doks/main.html.php View File

@ -70,13 +70,16 @@
<?php if (!empty($post->image)) {?>
<img src="<?php echo $post->image;?>" width="100%">
<?php } elseif (!empty($post->video)) {?>
<img src="//img.youtube.com/vi/<?php echo get_video_id($post->video);?>/sddefault.jpg" width="100%">
<?php } elseif (!empty($post->audio)) {?>
<img src="<?php echo theme_path();?>img/soundcloud.jpg" width="100%">
<?php } elseif (!empty($img)) {?>
<img src="<?php echo $img;?>" width="100%">
<?php } ?>
<?php if(!empty($post->video)):?>
<iframe width="100%" height="315px" class="embed-responsive-item media-wrapper" src="https://www.youtube.com/embed/<?php echo get_video_id($post->video); ?>" frameborder="0" allowfullscreen></iframe>
<?php endif;?>
<?php if(!empty($post->audio)):?>
<iframe width="100%" height="200px" class="embed-responsive-item media-wrapper" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php echo $post->audio;?>&amp;auto_play=false&amp;visual=true"></iframe>
<?php endif;?>
<?php if(!empty($post->quote)):?>
<div class="quote">


+ 7
- 4
themes/doks/profile.html.php View File

@ -17,13 +17,16 @@
<?php if (!empty($post->image)) {?>
<img src="<?php echo $post->image;?>" width="100%">
<?php } elseif (!empty($post->video)) {?>
<img src="//img.youtube.com/vi/<?php echo get_video_id($post->video);?>/sddefault.jpg" width="100%">
<?php } elseif (!empty($post->audio)) {?>
<img src="<?php echo theme_path();?>img/soundcloud.jpg" width="100%">
<?php } elseif (!empty($img)) {?>
<img src="<?php echo $img;?>" width="100%">
<?php } ?>
<?php if(!empty($post->video)):?>
<iframe width="100%" height="315px" class="embed-responsive-item media-wrapper" src="https://www.youtube.com/embed/<?php echo get_video_id($post->video); ?>" frameborder="0" allowfullscreen></iframe>
<?php endif;?>
<?php if(!empty($post->audio)):?>
<iframe width="100%" height="200px" class="embed-responsive-item media-wrapper" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=<?php echo $post->audio;?>&amp;auto_play=false&amp;visual=true"></iframe>
<?php endif;?>
<?php if(!empty($post->quote)):?>
<div class="quote">


Loading…
Cancel
Save