Skip to content

Commit b766e85

Browse files
authored
Merge pull request #13 from jxmot/20220421-issues
Merge issue changes
2 parents 3b587a3 + 471322d commit b766e85

File tree

10 files changed

+184
-40
lines changed

10 files changed

+184
-40
lines changed

README.md

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<p style="text-align:center">
2+
If this repository has helped you please consider a donation to help keep this project going. Thank You!
3+
<br><br>
4+
<a href="https://www.paypal.me/jxmot" target="_blank"><img src=https://img.shields.io/badge/Contribute-PayPal.me-3b29ff.svg></a>
5+
</p>
6+
17
# Markdown Hit Counter
28

39
This counter is intended for use in GitHub README markdown files. It has the following features:
@@ -13,7 +19,7 @@ This counter is intended for use in GitHub README markdown files. It has the fol
1319

1420
You will need:
1521

16-
* A HTTP server, not HTTPS. It has not been tested with HTTPS. **If you get it to work with HTTPS please let me know and I will update this file.**
22+
* A web server, HTTP or HTTPS should be ok.
1723
* The server needs PHP >5.6.
1824
* You will need access to your server to copy files and create a folder.
1925

@@ -160,10 +166,40 @@ It is possible to retrieve the counter data for a single counter or for all of t
160166
**Return data for a specific counter -**
161167

162168
```
163-
GET http[s]://your-server/path-to-file/mdcountdata.php?id=testtest
169+
GET http[s]://your-server/path-to-file/mdcountdata.php?id=sensornet
170+
```
171+
172+
The data is returned in JSON -
173+
174+
```
175+
[
176+
{
177+
"id": "sensornet",
178+
"data": {
179+
"count": 30,
180+
"time": 1616458038,
181+
"dtime": [
182+
"20210322",
183+
"190718"
184+
]
185+
}
186+
}
187+
]
164188
```
165189

166190

191+
**Return just the count data for a specific counter -**
192+
193+
```
194+
GET http[s]://your-server/path-to-file/mdcountdata.php?id=sensornet&count
195+
```
196+
197+
A single value is returned, no JSON formatting -
198+
199+
```
200+
30
201+
```
202+
167203
**Return data for all counters -**
168204

169205
```
@@ -254,15 +290,18 @@ GET http[s]://your-server/path-to-file/mdcountdata.php?id=BLAH
254290

255291
The following files are used in report generation and viewing:
256292

257-
* `mdreport.php` - Retrieves the counter data and renders a Bootstrap 4.x table.
258-
* `mdreport.css` - Additional required CSS for the table
293+
* `mdreport.php` - Retrieves the counter data and renders a Bootstrap 4.x table
294+
* `mdhcreport.css` - Additional required CSS for the table
295+
* `embedreport.css` - CSS required for an embedded report
296+
* `reporthead.html` - For non-embedded reports, this contains the page title and table heading
297+
* `reportcaption.html` - For non-embedded reports, this contains the table caption
259298
* `mdreport-th.txt` - Column heading text
260-
* `stddefines.php` - A collection of `define()` that make a number of PHP `$_SERVER[]` values available to the application. It contains components used for creating URLs to resources.
261-
* `report.html` - The minimum required HTML/CSS and JavaScript/jQuery to render and display the table.
299+
* `stddefines.php` - A collection of `define()` that make a number of PHP `$_SERVER[]` values available to the application. It contains components used for creating URLs to resources
300+
* `report.html` - The minimum required HTML/CSS and JavaScript/jQuery to render and display the table
262301

263302
**Retrieving the Report -**
264303

265-
The code that retreives the report and handles clicks on the column headings is contained in `report.html`.
304+
The code that retreives the report and handles clicks on the column headings is contained in `report.html`. The necessary CSS can be found in `mdhcreport.css`.
266305

267306
```
268307
<body>
@@ -338,9 +377,9 @@ document.addEventListener("DOMContentLoaded", () => {
338377
</body>
339378
```
340379

341-
**Sample Report Screen Shot -**
380+
**Sample Report Screen Shot - (via `report.html`)**
342381

343-
<img src="./mdimg/report_sshot.png" style="border: 2px dashed">
382+
<img src="./mdimg/report_sshot.png" style="border: 2px dashed;max-width:50%;">
344383

345384
The "Hit Count", "Repository" and "Last Counted" headings can be clicked to select sorting criteria and direction (*ascending vs descending*). When a column heading is clicked the report caption will change to reflect the choice.
346385

@@ -350,7 +389,23 @@ The "Hit Count", "Repository" and "Last Counted" headings can be clicked to sele
350389
* The links in the "Repository" column are built from the IDs found in `counters.json`. The ID text I used there is the *name of the repository* that the counter is intended for. The corresponding counter data files are named `repo-name`**`_counter.json`**.
351390
* When the report is viewed the data shown is *current*.
352391
* If the "Last Counted" column date and time seem to be incorrect then the time zone may need to be changed. Edit the `timezone.json` file to match your time zone.
353-
392+
393+
## "Embedded" Report
394+
395+
It is possible to retreive counter data without using `report.html`. The purpose is to *embed* the table in another page and to be able to apply some alternative CSS.
396+
397+
To retrieve it -
398+
399+
```
400+
GET http[s]://your-server/path-to-file/mdreport.php?tsort=d&limit=5&embed
401+
```
402+
403+
The response is going to contain some CSS for the table, and *just* the table itself. You can use a browser and go to `http[s]://your-server/path-to-file/mdreport.php?tsort=d&limit=5&embed`. You will see something like this-
404+
405+
<img src="./mdimg/report_embed_sshot.png" style="border: 2px dashed;max-width:50%;">
406+
407+
**NOTE:** The table is not sortable like the one rendered with `report.html`.
408+
354409
## Other Uses
355410

356411
You could count just about anything. All you need is to do a GET of `mdcount.php` with a proper query and you got a counter!
@@ -359,7 +414,7 @@ You could count just about anything. All you need is to do a GET of `mdcount.php
359414

360415
This started out as an interesting afternoon project. I created it because I could not find *exactly* what I needed in the many hit counters out there.
361416

362-
And this project has evolved since then with the addition (and changes) of JSON formatted counter files, and sorted counter data retrieval.
417+
And this project has evolved since then with the addition (and changes) of JSON formatted counter files, and sorted counter data retrieval. And the latest change was adding a 5th column to contain some GitHub statistics.
363418

364419
---
365420
<img src="http://webexperiment.info/extcounter/mdcount.php?id=markdown-hitcounter">

embedreport.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* embedreport.css */
2+
.hit-table {
3+
border-collapse: collapse;
4+
}
5+
6+
.hit-table thead {
7+
border-bottom: 3px solid black;
8+
}
9+
10+
.hit-table tr {
11+
border-bottom: 1px solid black;
12+
}
13+
/*
14+
Bootstrap classes:
15+
16+
table-responsive
17+
table
18+
table-sm
19+
20+
Not needed when the report is embedded.
21+
*/

mdcountdata.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,16 @@ function idsort($sort, $out, $limit = null) {
157157
$isort = (isset($queries['isort']) ? strtolower($queries['isort']) : null);
158158
// for sorts, limit number of counters returned
159159
$limit = (isset($queries['limit']) ? $queries['limit'] : null);
160+
// if $count is true then only the count value will be returned
161+
$count = (isset($queries['count']) ? true : false);
160162
} else {
161163
// set as needed for testing
162164
$id = null;
163165
$csort = 'a';
164166
$tsort = null;
165167
$isort = null;
166168
$limit = 2;
169+
$count = false;
167170
}
168171

169172
// get the valid counter ID list
@@ -185,8 +188,12 @@ function idsort($sort, $out, $limit = null) {
185188
// would get that big.
186189
$json = fgets($filecnt,128);
187190
fclose($filecnt);
188-
//$result = '['.$json.']';
189-
$result = '[{"id":"'.$id.'","data":'.$json.'}]';
191+
if($count === false) {
192+
$result = '[{"id":"'.$id.'","data":'.$json.'}]';
193+
} else {
194+
$tmp = json_decode($json);
195+
$result = $tmp->{'count'};
196+
}
190197
} else {
191198
$result = '[{"error":true,"msg":"file ['.$cntfile.'] does not exist"}]';
192199
}

mdreport.css renamed to mdhcreport.css

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
/* mdhcreport.css */
22
div.table-container {
33
max-width:30%;
44
margin:auto;
@@ -119,3 +119,32 @@ table.hit-table caption {
119119
background-color: rgba(189, 183, 137, 1);
120120
border-radius: 15px 50px;
121121
}
122+
123+
.stats-cell {
124+
background-color: lightgrey;
125+
}
126+
127+
.stats-cell-stars {
128+
margin: 0 0.15rem 0.15rem 0.15rem;
129+
}
130+
131+
.stats-cell-forks {
132+
margin: 0 0.15rem 0.15rem 0.15rem;
133+
}
134+
135+
.stats-cell-watchers {
136+
margin: 0 0.15rem 0.15rem 0.15rem;
137+
}
138+
139+
.stats-cell-issues {
140+
margin: 0 0.15rem 0.15rem 0.15rem;
141+
}
142+
143+
.stats-cell-first {
144+
margin-top: 0.25rem!important;
145+
}
146+
147+
.stats-cell-last {
148+
margin-bottom: 0.25rem!important;
149+
}
150+

mdimg/report_embed_sshot.png

16.5 KB
Loading

mdimg/report_sshot.png

-13.7 KB
Loading

mdreport-th.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#,Views,Repository,Last Viewed
1+
#,Views,Repository,Last Viewed,Stats

mdreport.php

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ function tzone() {
1717

1818
// check for debug/test mode
1919
if(!defined('_DEBUG') || _DEBUG === false) {
20-
21-
// $mdcountdata = THISSRVR.'/mdcountdata.php';
22-
2320
// MUST be done like this for PHP files that are 'linked'
2421
$queries = array();
2522
if(QRYSTR !== null) {
@@ -33,15 +30,14 @@ function tzone() {
3330
$isort = (isset($queries['isort']) ? strtolower($queries['isort']) : null);
3431
// for sorts, limit number of counters returned
3532
$limit = (isset($queries['limit']) ? $queries['limit'] : null);
33+
// if embed is true then there will be no report heading or table caption
34+
$embed = (isset($queries['embed']) ? true : false);
3635
} else {
3736
// for testing the query string while _DEBUG is true
3837
if(QRYSTR !== null) {
3938
$q = QRYSTR;
4039
echo "<p>$q</p>\n";
4140
}
42-
43-
// $mdcountdata = THISSRVR.'/mdcountdata.php';
44-
4541
// set as needed for testing
4642
$csort = 'd';
4743
$tsort = null;
@@ -52,7 +48,8 @@ function tzone() {
5248
$thfile = './mdreport-th.txt';
5349
$arrup = 'sort-arrow-up';
5450
$arrdn = 'sort-arrow-dn';
55-
$repohome = 'https://github.com/jxmot/';
51+
$owner = 'jxmot';
52+
$repohome = 'https://github.com/'.$owner.'/';
5653
$linktitle = 'Open Link in New Tab or Window';
5754
$thtitle = 'Click to select or to change the sorting order.';
5855

@@ -74,27 +71,34 @@ function tzone() {
7471
?>
7572
<style>
7673
<?php
77-
echo file_get_contents('./mdreport.css');
74+
echo file_get_contents('./mdhcreport.css');
75+
if($embed === true) echo file_get_contents('./embedreport.css');
7876
?>
7977
</style>
8078
<div class="table-responsive table-container">
81-
<p style="text-align:center!important;">
82-
<br>
83-
<strong>
84-
This is a demonstration of the code found in
85-
<br>
86-
<a href="https://github.com/jxmot/markdown-hitcounter" target="_blank" title="Open in new tab">markdown-hitcounter</a>.
87-
</strong>
88-
</p>
79+
<?php
80+
if($embed === false) include 'reporthead.html';
81+
?>
8982
<table id="hit-table" class="table table-sm hit-table">
9083
<thead>
9184
<tr>
9285
<?php
9386
for($ix = 0; $ix < count($thitems); $ix++) {
94-
if($ix !== $sortidx) {
95-
echo ' <th'.($ix !== 0 ? ' id="hit-table-col'.$ix.'" title="'.$thtitle.'" class="orderhover" data-ix="'.$ix.'"' : '').'>'.$thitems[$ix].'</th>'."\n";
87+
if($embed === false) {
88+
if($ix !== $sortidx) {
89+
// the last column is not sortable, this
90+
// is intentional. it's used as the "Stats"
91+
// column and the data cannot be sorted.
92+
if($ix === (count($thitems) - 1)) {
93+
echo ' <th'.($ix !== 0 ? ' id="hit-table-col'.$ix.'" title="" class="" data-ix="'.$ix.'"' : '').'>'.$thitems[$ix].'</th>'."\n";
94+
} else {
95+
echo ' <th'.($ix !== 0 ? ' id="hit-table-col'.$ix.'" title="'.$thtitle.'" class="orderhover" data-ix="'.$ix.'"' : '').'>'.$thitems[$ix].'</th>'."\n";
96+
}
97+
} else {
98+
echo ' <th id="hit-table-col'.$ix.'" title="'.$thtitle.'" class="orderhover" data-order="'.$sortdir.'" data-ix="'.$ix.'">'.$thitems[$ix].'<span id="hit-table-order'.$ix.'" data-order="'.$sortdir.'" data-ix="'.$ix.'" class="'.$dircss.'">&nbsp;</span></th>'."\n";
99+
}
96100
} else {
97-
echo ' <th id="hit-table-col'.$ix.'" title="'.$thtitle.'" class="orderhover" data-order="'.$sortdir.'" data-ix="'.$ix.'">'.$thitems[$ix].'<span id="hit-table-order'.$ix.'" data-order="'.$sortdir.'" data-ix="'.$ix.'" class="'.$dircss.'">&nbsp;</span></th>'."\n";
101+
echo ' <th'.($ix !== 0 ? ' id="hit-table-col'.$ix.'" title="" class="" data-ix="'.$ix.'"' : '').'>'.$thitems[$ix].'</th>'."\n";
98102
}
99103
}
100104
?>
@@ -185,14 +189,30 @@ function tzone() {
185189
$date = $counters[$ix]->data->dtime[0] . '<br>' . $counters[$ix]->data->dtime[1];
186190
}
187191

188-
echo " <td>".$date."</td>\n";
189-
echo " </tr>\n";
192+
echo " <td>".$date."</td>\n";
193+
194+
echo " <td>\n";
195+
echo ' <div class="stats-cell">' . "\n";
196+
echo ' <img class="stats-cell-stars stats-cell-first" src="https://img.shields.io/github/stars/'.$owner.'/'.$counters[$ix]->id.'">' . "\n";
197+
echo " <br>\n";
198+
echo ' <img class="stats-cell-forks stats-cell-last" src="https://img.shields.io/github/forks/'.$owner.'/'.$counters[$ix]->id.'">' . "\n";
199+
// uncomment the next two lines to have the number of watchers
200+
// if this is the last badge then add the stats-cell-last class
201+
// and remove it above
202+
// echo " <br>\n";
203+
// echo ' <img class="stats-cell-watchers" src="https://img.shields.io/github/watchers/'.$owner.'/'.$counters[$ix]->id.'">' . "\n";
204+
// uncomment the next two lines to have the issue count
205+
// if this is the last badge then add the stats-cell-last class
206+
// and remove it above
207+
// echo " <br>\n";
208+
// echo ' <img class="stats-cell-issues" src="https://img.shields.io/github/issues/'.$owner.'/'.$counters[$ix]->id.'">' . "\n";
209+
echo " </div>\n";
210+
echo " </td>\n";
211+
echo " </tr>\n";
190212
}
213+
214+
if($embed === false) include 'reportcaption.html';
191215
?>
192-
<!-- BS4 likes to render this on the bottom of the table, but we have CSS that
193-
moves it to the top.
194-
-->
195-
<caption><?php echo $tablecaption; ?></caption>
196216
</tbody>
197217
</table>
198218
</div>

reportcaption.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- BS4 likes to render this on the bottom of the table, but we have CSS that
2+
moves it to the top.
3+
-->
4+
<caption><?php echo $tablecaption; ?></caption>

reporthead.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<p style="text-align:center!important;">
2+
<br>
3+
<strong>
4+
This is a demonstration of the code found in
5+
<br>
6+
<a href="https://github.com/jxmot/markdown-hitcounter" target="_blank" title="Open in new tab">markdown-hitcounter</a>.
7+
</strong>
8+
</p>

0 commit comments

Comments
 (0)