Wiki source for LinkcountAction


Show raw source

=====Linkcount Action=====

Count (and display) the number of Wikka-formatted links (##""[[http:...]]""##) in a page. Needs some optimization work.

**actions/linkcount.php**
%%(php)
<?php
/**
* Link count action
*
* Maintain a count of links on a page (could be extended to track
* other entities)
*
* Usage:
* {{linkcount preformatted="0"}}
* ...
* {{linkcount start="1"}}
* link1
* link2
* ...
* linkn
* {{linkcount stop="1"}}
*
* Optionally: {{linkcount preformatted="1"
* pretext="Some pretext"
* posttext="Some posttext"}}
*
* where preformatted = "1" produces "+++Link count: 9999 link(s)+++"
* pretext is the text to precede the word count (overrides
* preformatted)
* posttest is the text to follow the word count (overrides
* preformatted)
*
* TODO: Performance is miserable beyond a couple hundred links, so
* the next step would be to optimize it.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public Licence for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @author {@link http://wikkawiki.org/BrianKoontz Brian Koontz} <brian@pongonova.net>
* @copyright Copyright (c) 2006, Brian Koontz <brian@pongonova.net>
* @name LinkcountAction
* @package Actions
* @license http://www.gnu.org/copyleft/gpl.html
* @since Wikka 1.1.7
* @uses /actions/linkcount.php
* @version $Id: linkcount.php,v 1.1.1.1 2006/10/07 16:30:42 brian Exp brian $
*
*/

// Necessary to prevent "already defined" errors when the Wikka parser
// comes across a "start" or "stop" action tag
include_once("actions/linkcount.inc.php");

if(!isset($vars['preformatted'])) {
return;
}

$preformatted = $this->htmlspecialchars_ent($vars['preformatted']);
$pretext = $this->htmlspecialchars_ent($vars['pretext']);
$posttext = $this->htmlspecialchars_ent($vars['posttext']);

if( isset($pretext) || isset($posttext)) {
$pretext ? 1 : $pretext = '';
$posttext ? 1 : $posttext = '';
} else if(isset($preformatted)) {
$pretext = "+++Link count: ";
$posttext = " link(s)+++";
}

$body = explode("\n", $this->page['body']);
parse($body, $pretext, $posttext);

?>
%%

**actions/linkcount.inc.php**
%%(php)
<?php
/**
* Link count action
*
* Maintain a count of links on a page (could be extended to track
* other entities)
*
* @see LinkcountAction
* @see actions/linkcount.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public Licence for more details:
*
* http://www.gnu.org/copyleft/gpl.html
*
* @author {@link http://wikkawiki.org/BrianKoontz Brian Koontz} <brian@pongonova.net>
* @copyright Copyright (c) 2006, Brian Koontz <brian@pongonova.net>
* @name LinkcountAction
* @package Actions
* @license http://www.gnu.org/copyleft/gpl.html
* @since Wikka 1.1.7
* @uses /actions/linkcount.inc.php
* @version $Id: linkcount.inc.php,v 1.1.1.1 2006/10/07 16:32:17 brian Exp brian $
*
*/
function parse(&$body, $pretext, $posttext) {
$linkcount = 0;
if(!$body) {
return;
}
$state = null; #null, start
foreach($body as $line) {
switch($state) {
case null:
if(preg_match('/\{\{linkcount[\s]+start.*\}\}/', $line)) {
$state = 'start';
}
break;
case 'start':
if (preg_match('/\{\{linkcount[\s]+stop.*\}\}/', $line)) {
$state = null;
} elseif (preg_match('/\[\[http:.*\]\]/', $line)) {
$linkcount++;
}
break;
}
}
echo $pretext.$linkcount.$posttext;
}
?>
%%

----
CategoryUserContributions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki