NumberOfArticles

Summary: Report the number of articles in a wiki
Version: 0.3 - 18 September 2006
Prerequisites: Last tested on PmWiki version: 2.0.6
Status:
Maintainer: wanderful

Goal

Report the number of articles existing in the wiki.

Solution

1. Add the following lines to config.php:

## Add (:numberofarticles:) tag functionality
$na = NumArticles();
Markup('(:numberofarticles:)', '_begin','/\\(\\:numberofarticles\\:\\)/', "$na"); 
function NumArticles()
{
    if ($dir = opendir('wiki.d/'))
    {
	while (($file = readdir($dir)) !== false)
	{
	    if (substr($file,0,1) != '.' && strpos($file, 'Site.') === false)
            {
                $list[] = $file;
            }
        }
	closedir($dir);
    }
    $NumArticles = count($list);
    return $NumArticles;
}


2. Put (:numberofarticles:) in a page on your wiki.

You can change the tag if you wish. I initially used to correspond with the markup variables like {$Group} and {$Title}, but figured (:numberofarticles:) is more in harmony with the average user's expectation for this type of markup.

Notes

It will only count subdirectories.

~LiGuang

Request

Could this recipe be modified so that

  1. in counts the articles in a category
  2. the outcome can be included (via pagelist template)in a pagelist of categories?

So that (:pagelist #fmt=whatever link="Category.{$Name}" :) gives as output:
Category1 (5 articles)
Category2 (9 articles) 18 feb 2006, Han

See also

  • PowerTools - {(pagecount (plist ...))} expression

Comments

I've marked the recipe as obsolete, as its functionality can now be implemented using a pagelist, as follows. —Eemeli Aro July 29, 2009, at 01:04 PM

(:pagelist fmt=count:)

9707

(:pagelist fmt=count group=Cookbook list=all:)

2119

(:pagelist fmt=count link=Category.Obsolete:)

54


THX for your comment. One additional hint from my side, all articles in Site.* are not counted because of

if (substr($file,0,1) != '.' && strpos($file, 'Site.') === false)

Any ideas to prevent this kind of behavior? Holger, July 21th, 2006

Articles in Site.* are not counted by design because why should already existing functionality count as content-style articles?

That's a good idea for the category counting. I'll look into it.
wanderful

I needed to count the Number of Articles only the group seen by the public, so I reversed the logic of the selection line so that it only included those pages within the desired group:

 if (substr($file,0,1) != '.' && strpos($file, 'WantedGroup.') !== false){

so it only counts pages within the WantedGroup. Des July 23, 2007, at 09:05 AM

The original line that determines the article count includes all pages, unlike Search or PageList which can ignore core pages. By modifying the count to simply deduct the number of core pages, the count accuracy can be maintained. In my case the RecentPages and Sandbox pages increased the count by 2, so reducing final the count by 2:

 $NumArticles = (count($list) - 2);

gave the correct Number of Articles. Des July 23, 2007, at 09:05 AM

Releases

0.3 - September 18th, 2006

- fixed a problem with calling the function in PmWiki 2.1+
- thanks to John

0.2 - October 10th, 2005

- wrapped the code in a function
- shoutout to Gregor for the heads up

0.1 - September 24th, 2005

Contributors

wanderful

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.