Difference between revisions of "Extension talk:CurrentPages"

From Organic Design wiki
m (Changing to DB storage)
Line 1: Line 1:
The main functionality is all in the function called by ExtensionSetup. It's not very efficient because the file is read and written for every request and due to the large number of articles read over a 24 hour period, the file can get up to a 1 megabyte or so. The best way would be to create an additional database table to store the entries by. The [[Extension:PayPal.php|PayPal extension]] has example code for adding and using a new table. Another efficiency gain would be to store article ID's rather than text.
+
== About ==
 +
This extension is basically a hit counter recording the number of normal page views to each article. The difference between this extension and the normal [[Special:Popularpages|popular pages special-page]] is that this only records the hits for the last 24 hours. It does this by storing the current hour along with each title and its view count.
 +
 
 +
It also stores the current hour in the database and when the current hour changes it deletes any existing rows exhibiting the new hour. It does this so that items more than 24 hours old are purged.
 +
 
 +
== Bugs ==
 +
*For some reason most page requests increase the count by two
 +
 
 +
== Storing the data in a file ==
 +
The main code was like this before, where it serialised and deserialised the data array into a file. The problem was that the file started getting quite large which is a big problem considering that its loaded and saved for every request. Due to this, the storage mechanism was changed to use the database instead.
 
{{code|<php>
 
{{code|<php>
 
# Read the $egCurrentPagesData array from file
 
# Read the $egCurrentPagesData array from file
Line 16: Line 25:
 
# Write the $egCurrentPagesData array back to file
 
# Write the $egCurrentPagesData array back to file
 
file_put_contents($egCurrentPagesFile, serialize($egCurrentPagesData));
 
file_put_contents($egCurrentPagesFile, serialize($egCurrentPagesData));
</php>}}
 
== Changing to DB storage ==
 
{{code|<php>
 
# Create the DB table if it doesn't exist
 
$table = $db->tableName('currentpages_hourlyviews');
 
if (!$db->tableExists($table)) {
 
$query = "CREATE TABLE $table (hour INTEGER, page INTEGER, views INTEGER);";
 
$result = $db->query($query);
 
$db->freeResult($result);
 
}
 
 
# Increment a title
 
 
SELECT * FROM $table WHERE hour = $hour AND page = $page
 
if ($row = fetch...) {
 
$views++
 
UPDATE...
 
}
 
else {
 
INSERT INTO $table VALUES($hour,$page,1)
 
}
 
 
# Render list
 
SELECT page, SUM(views) AS totals FROM $table GROUP BY page ORDER BY totals DESC LIMIT $n
 
 
 
</php>}}
 
</php>}}

Revision as of 08:02, 25 May 2008

About

This extension is basically a hit counter recording the number of normal page views to each article. The difference between this extension and the normal popular pages special-page is that this only records the hits for the last 24 hours. It does this by storing the current hour along with each title and its view count.

It also stores the current hour in the database and when the current hour changes it deletes any existing rows exhibiting the new hour. It does this so that items more than 24 hours old are purged.

Bugs

  • For some reason most page requests increase the count by two

Storing the data in a file

The main code was like this before, where it serialised and deserialised the data array into a file. The problem was that the file started getting quite large which is a big problem considering that its loaded and saved for every request. Due to this, the storage mechanism was changed to use the database instead.