xref: /plugin/qc/renderer.php (revision 91d07bdb65f4caef3e69998c86ac3d33b5e11dd0) !
1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5/**
6 * The Renderer
7 */
8class renderer_plugin_qc extends Doku_Renderer {
9    /**
10     * We store all our data in an array
11     */
12    var $doc = array(
13        // raw statistics
14        'header_count'  => array(0,0,0,0,0,0),
15        'header_struct' => array(),
16        'linebreak'     => 0,
17        'quote_nest'    => 0,
18        'quote_count'   => 0,
19        'fixme'         => 0,
20        'hr'            => 0,
21        'formatted'     => 0,
22
23        'created'       => 0,
24        'modified'      => 0,
25        'changes'       => 0,
26        'authors'       => array(),
27
28        'internal_links'=> 0,
29        'broken_links'  => 0,
30        'external_links'=> 0,
31        'link_lengths'  => array(),
32
33        'chars'         => 0,
34        'words'         => 0,
35
36        'score'         => 0,
37
38        // calculated error scores
39        'err' => array(
40            'fixme'      => 0,
41            'noh1'       => 0,
42            'manyh1'     => 0,
43            'headernest' => 0,
44            'manyhr'     => 0,
45            'manybr'     => 0,
46            'longformat' => 0,
47            'multiformat'=> 0,
48        ),
49    );
50
51    var $quotelevel = 0;
52    var $formatting = 0;
53    var $tableopen  = false;
54
55    function document_start() {
56        global $ID;
57        $meta = p_get_metadata($ID);
58
59        // get some dates from meta data
60        $this->doc['created']  = $meta['date']['created'];
61        $this->doc['modified'] = $meta['date']['modified'];
62
63        // get author info
64        $changelog = new PageChangelog($ID);
65        $revs = $changelog->getRevisions(0,10000); //FIXME find a good solution for 'get ALL revisions'
66        array_push($revs,$meta['last_change']['date']);
67        $this->doc['changes'] = count($revs);
68        foreach($revs as $rev){
69            $info = $changelog->getRevisionInfo($rev);
70            if($info['user']){
71                $this->doc['authors'][$info['user']] += 1;
72            }else{
73                $this->doc['authors']['*'] += 1;
74            }
75        }
76
77        // work on raw text
78        $text = rawWiki($ID);
79        $this->doc['chars'] = utf8_strlen($text);
80        $this->doc['words'] = count(array_filter(preg_split('/[^\w\-_]/u',$text)));
81    }
82
83
84    /**
85     * Here the score is calculated
86     */
87    function document_end() {
88        global $ID;
89
90        // 2 points for missing backlinks
91        if(!count(ft_backlinks($ID))){
92            $this->doc['err']['nobacklink'] += 2;
93        }
94
95        // 1 point for each FIXME
96        $this->doc['err']['fixme'] += $this->doc['fixme'];
97
98        // 5 points for missing H1
99        if($this->doc['header_count'][1] == 0){
100            $this->doc['err']['noh1'] += 5;
101        }
102        // 1 point for each H1 too much
103        if($this->doc['header_count'][1] > 1){
104            $this->doc['err']['manyh1'] += $this->doc['header'][1];
105        }
106
107        // 1 point for each incorrectly nested headline
108        $cnt = count($this->doc['header_struct']);
109        for($i = 1; $i < $cnt; $i++){
110            if($this->doc['header_struct'][$i] - $this->doc['header_struct'][$i-1] > 1){
111                $this->doc['err']['headernest'] += 1;
112            }
113        }
114
115        // 1/2 points for deeply nested quotations
116        if($this->doc['quote_nest'] > 2){
117            $this->doc['err']['deepquote'] += $this->doc['quote_nest']/2;
118        }
119
120        // FIXME points for many quotes?
121
122        // 1/2 points for too many hr
123        if($this->doc['hr'] > 2){
124            $this->doc['err']['manyhr'] = ($this->doc['hr'] - 2)/2;
125        }
126
127        // 1 point for too many line breaks
128        if($this->doc['linebreak'] > 2){
129            $this->doc['err']['manybr'] = $this->doc['linebreak'] - 2;
130        }
131
132        // 1 point for single author only
133        if(!$this->getConf('single_author_only') && count($this->doc['authors']) == 1){
134            $this->doc['err']['singleauthor'] = 1;
135        }
136
137        // 1 point for too small document
138        if($this->doc['chars'] < 150){
139            $this->doc['err']['toosmall'] = 1;
140        }
141
142        // 1 point for too large document
143        if($this->doc['chars'] > 100000){
144            $this->doc['err']['toolarge'] = 1;
145        }
146
147        // header to text ratio
148        $hc = $this->doc['header_count'][1] +
149              $this->doc['header_count'][2] +
150              $this->doc['header_count'][3] +
151              $this->doc['header_count'][4] +
152              $this->doc['header_count'][5];
153        $hc--; //we expect at least 1
154        if($hc > 0){
155            $hr = $this->doc['chars']/$hc;
156
157            // 1 point for too many headers
158            if($hr < 200){
159                $this->doc['err']['manyheaders'] = 1;
160            }
161
162            // 1 point for too few headers
163            if($hr > 2000){
164                $this->doc['err']['fewheaders'] = 1;
165            }
166        }
167
168        // 1 point when no link at all
169        if(!$this->doc['internal_links']){
170            $this->doc['err']['nolink'] = 1;
171        }
172
173        // 0.5 for broken links when too many
174        if($this->doc['broken_links'] > 2){
175            $this->doc['err']['brokenlink'] = $this->doc['broken_links']*0.5;
176        }
177
178        // 2 points for lot's of formatting
179        if($this->doc['formatted'] && $this->doc['chars']/$this->doc['formatted'] < 3){
180            $this->doc['err']['manyformat'] = 2;
181        }
182
183        // add up all scores
184        foreach($this->doc['err'] as $err => $val) $this->doc['score'] += $val;
185
186
187        //we're done here
188        $this->doc = serialize($this->doc);
189    }
190
191    /**
192     * the format we produce
193     */
194    function getFormat(){
195        return 'qc';
196    }
197
198    function internallink($id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content') {
199        global $ID;
200        resolve_pageid(getNS($ID),$id,$exists);
201
202        // calculate link width
203        $a = explode(':',getNS($ID));
204        $b = explode(':',getNS($id));
205        while(isset($a[0]) && $a[0] == $b[0]){
206            array_shift($a);
207            array_shift($b);
208        }
209        $length = count($a)+count($b);
210        $this->doc['link_lengths'][] = $length;
211
212        $this->doc['internal_links']++;
213        if(!$exists) $this->doc['broken_links']++;
214    }
215
216    function externallink($url, $name = NULL) {
217        $this->doc['external_links']++;
218    }
219
220    function header($text, $level, $pos){
221        $this->doc['header_count'][$level]++;
222        $this->doc['header_struct'][] = $level;
223    }
224
225    function smiley($smiley) {
226        if($smiley == 'FIXME') $this->doc['fixme']++;
227    }
228
229    function linebreak() {
230        if(!$this->tableopen){
231            $this->doc['linebreak']++;
232        }
233    }
234
235    function table_open($maxcols = null, $numrows = null, $pos = null){
236        $this->tableopen = true;
237    }
238
239    function table_close($pos = null){
240        $this->tableopen = false;
241    }
242
243    function hr() {
244        $this->doc['hr']++;
245    }
246
247    function quote_open() {
248        $this->doc['quote_count']++;
249        $this->quotelevel++;
250        $this->doc['quote_nest'] = max($this->quotelevel,$this->doc['quote_nest']);
251    }
252
253    function quote_close() {
254        $this->quotelevel--;
255    }
256
257    function strong_open() {
258        $this->formatting++;
259    }
260
261    function strong_close() {
262        $this->formatting--;
263    }
264
265    function emphasis_open() {
266        $this->formatting++;
267    }
268
269    function emphasis_close() {
270        $this->formatting--;
271    }
272
273    function underline_open() {
274        $this->formatting++;
275    }
276
277    function underline_close() {
278        $this->formatting--;
279    }
280
281    function cdata($text) {
282        if(!$this->formatting) return;
283
284        $len = utf8_strlen($text);
285
286        // 1 point for formattings longer than 500 chars
287        if($len>500) $this->doc['err']['longformat']++;
288
289        // 1 point for each multiformatting
290        if($this->formatting > 1) $this->doc['err']['multiformat'] += 1*($this->formatting - 1);
291
292        $this->doc['formatted'] += $len;
293    }
294}
295
296//Setup VIM: ex: et ts=4 enc=utf-8 :
297