Not logged in. · Lost password · Register
Forum: General Help and Support Plugins RSS
Counting the number of pages WITHIN a namespace. How to return a 0?
I want my site to say "this section has 0 pages". Unfortunately, when no pages are present it says "this section has pages"
Avatar
mrwills #1
Member for 2 weeks · 3 posts
Group memberships: Members
Show profile · Link to this post
Subject: Counting the number of pages WITHIN a namespace. How to return a 0?
I'm using the "numberof" plugin right now. It is excellent bar one small issue.
When the number of pages in a given section is 0, it does not output.

This means a sentence which in markdown reads
This section has {{NUMBEROFPAGES>:this_section}} pages
If there were six pages then it would read
This section has 6 pages
If there were no pages then it would read
This section has pages

The plugin is only 40 lines long and it is my guess that when searching for pages to populate the array, if no pages are found then the array is empty and nothing gets outputted.
If I knew what I was doing I'm sure I could just pop an if statement somewhere to make it output 0 when the array is empty.

Any suggestions on how to overcome this problem would be greatly appreciated  :-)

This is the numberofplugin php page which is the only functional file in the plugin.
 
<?php
/**
 * DokuWiki Plugin numberof (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  S.C. Yoo <dryoo@live.com>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

class syntax_plugin_numberof extends DokuWiki_Syntax_Plugin {
    public function getType() { return 'substition'; }
    public function getSort() { return 32; }

    public function connectTo($mode) {
      $this->Lexer->addSpecialPattern('\{\{NUMBEROF[^\}]*\}\}',$mode,'plugin_numberof');
    }
 
    public function handle($match, $state, $pos, Doku_Handler $handler){
    global $conf;
        $list = array();
        $data=0;
        $match=substr($match,10,-2);
        $matches=explode(">",$match);
        $matches[1]=str_replace(":","/",$matches[1]);
        switch ($matches[0]) {
            case "PAGES":
                search($list,$conf['datadir'].$matches[1],array($this,'_search_count'),array('all'=>false),'');
                $data = $list['file_count'];
                break;
              
            case "MEDIAS":
                search($list,$conf['mediadir'].$matches[1],array($this,'_search_count'),array('all'=>true));
                $data    = $list['file_count'];
                break;
          }
        return $data;
    }

    public function render($mode, Doku_Renderer $renderer, $data) {
        if($mode != 'xhtml') return false;
        $renderer->doc.= $data;
        return true;
    }

    function _search_count(&$data,$base,$file,$type,$lvl,$opts){
        if($type == 'd'){
            if($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
            $data['dir_count']++;
            return true;
        }
        if($opts['all'] || substr($file,-4) == '.txt'){
            $data['file_count']++;
        }
        return false;
    }
}

// vim:ts=4:sw=4:et:
Avatar
turnermm (Moderator) #2
Member since Oct 2009 · 4675 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
You can report this to the author, but he seems not to  have responded to the one issue posted there:  https://github.com/dryoo/numberof/issues.

If he doesn't answer, you can try this:

$data = $list['file_count'] ? $list['file_count'] : '0';
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
Avatar
mrwills #3
Member for 2 weeks · 3 posts
Group memberships: Members
Show profile · Link to this post
Thank you turnermm! Yeah, I saw that the author didn't seem to have responded to that other issue  :-(

Where in the code would you insert the code you suggested?
I'm not very familiar with php unfortunately.
Thanks again.
Avatar
turnermm (Moderator) #4
Member since Oct 2009 · 4675 posts · Location: Canada
Group memberships: Global Moderators, Members, Super Mods
Show profile · Link to this post
            case "PAGES":
                search($list,$conf['datadir'].$matches[1],array($this,'_search_count'),array('all'=>false),'');
                $data = $list['file_count'] ? $list['file_count'] : '0';
                break;
              
            case "MEDIAS":
                search($list,$conf['mediadir'].$matches[1],array($this,'_search_count'),array('all'=>true));
                $data  = $list['file_count'] ? $list['file_count'] : '0';
                break;
Myron Turner
github: https://github.com/turnermm
plugins, templates: http://www.mturner.org/devel
This post was edited on 2019-07-11, 04:21 by turnermm.
Avatar
mrwills #5
Member for 2 weeks · 3 posts
Group memberships: Members
Show profile · Link to this post
Thank you turnermm.
That has worked absolutely perfectly.
Very much appreciated  :-)
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Imprint
This board is powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2015 by Yves Goergen
Current time: 2019-07-23, 01:10:25 (UTC +02:00)