Skip to content

Commit

Permalink
added ability to limit qty returned in sortings, added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jxmot committed Mar 30, 2021
1 parent 83527ed commit 2f45b0b
Showing 1 changed file with 55 additions and 10 deletions.
65 changes: 55 additions & 10 deletions mdcountdata.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
/*
See https://github.com/jxmot/markdown-hitcounter for detailed
information not contained here.
Usage:
Return data for all counters -
Expand All @@ -11,6 +15,30 @@
Return data for a specific counter -
GET http[s]://your-server/path-to-file/mdcountdata.php?id=testtest
OR
Return data for all counters, but sorted -
GET http[s]://your-server/path-to-file/mdcountdata.php?[csort|tsort|isort]=[a|d]
Where:
csort - sort by count
tsort - sort by time of last count
isort - sort by counter ID
a - sort ascending
d - sort descending
Return sorted data, but limit the quantity of counters returned
GET http[s]://your-server/path-to-file/mdcountdata.php?csort=a|[&limit=[1-n]]
Where:
limit - limit the quantity of counters to return
1-n - quantity
Report data returned:
Expand Down Expand Up @@ -46,7 +74,7 @@ function tzone() {
return $tmp->tz;
}

function countsort($sort, $out) {
function countsort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
Expand All @@ -63,10 +91,13 @@ function countsort($sort, $out) {
});
}
// done!
return json_encode($data);
if($limit !== null && ($limit > 0 && $limit < count($data))){
$data = array_slice($data, 0, $limit);
}
return json_encode($data);
}

function timesort($sort, $out) {
function timesort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
Expand All @@ -83,10 +114,13 @@ function timesort($sort, $out) {
});
}
// done!
return json_encode($data);
if($limit !== null && ($limit > 0 && $limit < count($data))){
$data = array_slice($data, 0, $limit);
}
return json_encode($data);
}

function idsort($sort, $out) {
function idsort($sort, $out, $limit = null) {
$data = json_decode($out);
// ascending
if($sort === 'a' || $sort === '') {
Expand All @@ -103,20 +137,31 @@ function idsort($sort, $out) {
});
}
// done!
return json_encode($data);
if($limit !== null && ($limit > 0 && $limit < count($data))){
$data = array_slice($data, 0, $limit);
}
return json_encode($data);
}

// MUST be done like this for PHP files that are 'linked'
$queries = array();

parse_str($_SERVER['QUERY_STRING'], $queries);
// return a single counter by ID
$id = (isset($queries['id']) ? strtolower($queries['id']) : null);
// return all counters, ordered by count
$csort = (isset($queries['csort']) ? strtolower($queries['csort']) : null);
// return all counters, ordered by time of last count
$tsort = (isset($queries['tsort']) ? strtolower($queries['tsort']) : null);
// return all counters, ordered by ID
$isort = (isset($queries['isort']) ? strtolower($queries['isort']) : null);
// for sorts, limit number of counters returned
$limit = (isset($queries['limit']) ? $queries['limit'] : null);

//$id = null;
//$csort = 'd';
//$tsort = null;
//$isort = null;
//$limit = 1;

$_idlist = json_decode(file_get_contents(VALID_COUNTERS));
$idlist = array_map('strtolower', $_idlist->valid);
Expand Down Expand Up @@ -171,17 +216,17 @@ function idsort($sort, $out) {
// is sorting selected?
if($csort !== null) {
// by count
$out = countsort($csort, $out);
$out = countsort($csort, $out, $limit);
}

if($tsort !== null) {
// by time
$out = timesort($tsort, $out);
$out = timesort($tsort, $out, $limit);
}

if($isort !== null) {
// by ID
$out = idsort($isort, $out);
$out = idsort($isort, $out, $limit);
}

$result = $out;
Expand Down

0 comments on commit 2f45b0b

Please sign in to comment.