Extension:CurrentPages

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 creates a database table called currentpages_hourlyviews to store the data in, the structure of which is shown below.

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.

mysql> describe currentpages_hourlyviews; +---+-+--+-+-+---+ +---+-+--+-+-+---+ +---+-+--+-+-+---+ 3 rows in set (0.00 sec)
 * Field | Type   | Null | Key | Default | Extra |
 * hour | int(11) | YES  |     | NULL    |       |
 * page | int(11) | YES  |     | NULL    |       |
 * views | int(11) | YES |     | NULL    |       |

Installation
Create a directory called CurrentPages in your wiki's extensions directory. Then, copy the source code from OrganicDesign:Extension:CurrentPages.php and save it as a file called CurrentPages.php into the newly created directory. Add an include statement into your LocalSettings.php file to include the downloaded CurrentPages.php file as usual, for example:

Usage
To add the list to an article, use the following parser function syntax:

There are also two parameters that can be added, the first is the maximum number of items to show in the list which defaults to 10 if not supplied. The second specifies the format to sue for each item where $1 is replaced with the article link and $2 with the number of views of that article. The default is to start with an asterisk to start a bullet-list item, and the default ending is a newline character. The following example creates a 50 item list with the number of views in brackets after the title link.

Efficiency
When I first made this extension it used a file to store the hit data in, but I didn't realise how much data would need to be read in on each request. On our site which is not that popular the file would be over a megabyte, so I changed the storage method to use a database table instead, which is much more efficient.

For a normal page request there are only two single-row SELECT queries and one single-row UPDATE necessary to add the new title request to the counter table. Once per hour an additional single-row UPDATE and multi-row DELETE will occur to clear out the titles from the 24hr old data. To render the list, a multi-row SELECT query limited to the number of items in the list is performed.

It could be made more efficient again by doing processing once per hour and calculating the hourly totals from the current hit counter table. The numbers would only change each hour, and the rendered list could be cached.

Bugs

 * For some reason most page requests increase the count by two (perhaps hooking in before redirect?)
 * If there are no requests for more than an hour the information will not be cleared for the hours during that period, but in reality no site connected to the internet would have no requests in any given hour.