xref: /plugin/qc/renderer.php (revision 2e820792fb55a0f094ebe31770b4f6eb86cddd85)
1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5// we inherit from the XHTML renderer instead directly of the base renderer
6require_once DOKU_INC.'inc/parser/renderer.php';
7
8/**
9 * The Renderer
10 */
11class renderer_plugin_qc extends Doku_Renderer {
12    /**
13     * We store all our data in an array
14     */
15    var $doc = array(
16        // raw statistics
17        'header_count'  => array(0,0,0,0,0,0),
18        'header_struct' => array(),
19        'linebreak'     => 0,
20        'quote_nest'    => 0,
21        'quote_count'   => 0,
22        'fixme'         => 0,
23        'hr'            => 0,
24
25        'created'       => 0,
26        'modified'      => 0,
27        'changes'       => 0,
28        'authors'       => array(),
29
30        'chars'         => 0,
31        'words'         => 0,
32
33        'score'         => 0,
34
35        // calculated error scores
36        'err' => array(
37            'fixme'      => 0,
38            'noh1'       => 0,
39            'manyh1'     => 0,
40            'headernest' => 0,
41            'manyhr'     => 0,
42            'manybr'     => 0,
43        ),
44    );
45
46    var $quotelevel = 0;
47
48    function document_start() {
49        global $ID;
50        $meta = p_get_metadata($ID);
51
52        // get some dates from meta data
53        $this->doc['created']  = $meta['date']['created'];
54        $this->doc['modified'] = $meta['date']['modified'];
55
56        // get author info
57        $revs = getRevisions($ID,0,0);
58        array_push($revs,$meta['last_change']['date']);
59        $this->doc['changes'] = count($revs);
60        foreach($revs as $rev){
61            $info = getRevisionInfo($ID, $rev);
62            if($info['user']){
63                $this->doc['authors'][$info['user']] += 1;
64            }else{
65                $this->doc['authors']['*'] += 1;
66            }
67        }
68
69        // work on raw text
70        $text = rawWiki($ID);
71        $this->doc['chars'] = utf8_strlen($text);
72        $this->doc['words'] = count(preg_split('/[^\w\-_]/u',$text));
73    }
74
75
76    /**
77     * Here the score is calculated
78     */
79    function document_end() {
80
81        // 1 point for each FIXME
82        $this->doc['err']['fixme'] += $this->doc['fixme'];
83
84        // 5 points for missing H1
85        if($this->doc['header_count'][1] == 0){
86            $this->doc['err']['noh1'] += 5;
87        }
88        // 1 point for each H1 too much
89        if($this->doc['header_count'][1] > 1){
90            $this->doc['err']['manyh1'] += $this->doc['header'][1];
91        }
92
93        // 1 point for each incorrectly nested headline
94        $cnt = count($this->doc['header_struct']);
95        for($i = 1; $i < $cnt; $i++){
96            if($this->doc['header_struct'][$i] - $this->doc['header_struct'][$i-1] > 1){
97                $this->doc['err']['headernest'] += 1;
98            }
99        }
100
101        // 1/2 points for deeply nested quotations
102        if($this->doc['quote_nest'] > 2){
103            $this->doc['err']['deepquote'] += $this->doc['quote_nest']/2;
104        }
105
106        // FIXME points for many quotes?
107
108        // 1/2 points for too many hr
109        if($this->doc['hr'] > 2){
110            $this->doc['err']['manyhr'] = ($this->doc['hr'] - 2)/2;
111        }
112
113        // 1 point for too many line breaks
114        if($this->doc['linebreak'] > 2){
115            $this->doc['err']['manybr'] = $this->doc['linebreak'] - 2;
116        }
117
118        // 1 point for single author only
119        if(count($this->doc['authors']) == 1){
120            $this->doc['err']['singleauthor'] = 1;
121        }
122
123        // add up all scores
124        foreach($this->doc['err'] as $err => $val) $this->doc['score'] += $val;
125
126
127        //we're done here
128        $this->doc = serialize($this->doc);
129    }
130
131    /**
132     * return some info
133     */
134    function getInfo(){
135        return confToHash(dirname(__FILE__).'/info.txt');
136    }
137
138    /**
139     * the format we produce
140     */
141    function getFormat(){
142        return 'qc';
143    }
144
145
146    function header($text, $level, $pos){
147        $this->doc['header_count'][$level]++;
148        $this->doc['header_struct'][] = $level;
149    }
150
151    function smiley($smiley) {
152        if($smiley == 'FIXME') $this->doc['fixme']++;
153    }
154
155    function linebreak() {
156        $this->doc['linebreak']++;
157    }
158
159    function hr() {
160        $this->doc['hr']++;
161    }
162
163    function quote_open() {
164        $this->doc['quote_count']++;
165        $this->quotelevel++;
166        $this->doc['quote_nest'] = max($this->quotelevel,$this->doc['quote_nest']);
167    }
168
169    function quote_close() {
170        $this->quotelevel--;
171    }
172
173
174}
175
176//Setup VIM: ex: et ts=4 enc=utf-8 :
177