Difference between revisions of "Extension talk:CurrentPages"

From Organic Design wiki
(Another efficiency gain would be to store article ID's rather than text)
(notes)
Line 16: Line 16:
 
# 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 views FROM $table WHERE page = $page ORDER BY total_views DESC LIMIT $n
 +
 
</php>}}
 
</php>}}

Revision as of 03:10, 25 May 2008

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 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.

Changing to DB storage

{{{1}}}