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