xref: /plugin/qc/renderer.php (revision 6afc184111ea113bfa68f1eb638a5bd926e3a742)
18fce80b1SAndreas Gohr<?php
28fce80b1SAndreas Gohr// must be run within Dokuwiki
38fce80b1SAndreas Gohrif(!defined('DOKU_INC')) die();
48fce80b1SAndreas Gohr
58fce80b1SAndreas Gohrrequire_once DOKU_INC.'inc/parser/renderer.php';
6e348ef7cSAndreas Gohrrequire_once DOKU_INC.'inc/fulltext.php';
78fce80b1SAndreas Gohr
88fce80b1SAndreas Gohr/**
98fce80b1SAndreas Gohr * The Renderer
108fce80b1SAndreas Gohr */
118fce80b1SAndreas Gohrclass renderer_plugin_qc extends Doku_Renderer {
128fce80b1SAndreas Gohr    /**
138fce80b1SAndreas Gohr     * We store all our data in an array
148fce80b1SAndreas Gohr     */
158fce80b1SAndreas Gohr    var $doc = array(
168fce80b1SAndreas Gohr        // raw statistics
178fce80b1SAndreas Gohr        'header_count'  => array(0,0,0,0,0,0),
188fce80b1SAndreas Gohr        'header_struct' => array(),
198fce80b1SAndreas Gohr        'linebreak'     => 0,
208fce80b1SAndreas Gohr        'quote_nest'    => 0,
218fce80b1SAndreas Gohr        'quote_count'   => 0,
228fce80b1SAndreas Gohr        'fixme'         => 0,
238fce80b1SAndreas Gohr        'hr'            => 0,
24d723b313SAndreas Gohr        'formatted'     => 0,
258fce80b1SAndreas Gohr
264ea373cdSAndreas Gohr        'created'       => 0,
274ea373cdSAndreas Gohr        'modified'      => 0,
284ea373cdSAndreas Gohr        'changes'       => 0,
294ea373cdSAndreas Gohr        'authors'       => array(),
304ea373cdSAndreas Gohr
31d723b313SAndreas Gohr        'internal_links'=> 0,
32d723b313SAndreas Gohr        'broken_links'  => 0,
33d723b313SAndreas Gohr        'external_links'=> 0,
348d7cf088SAndreas Gohr        'link_lengths'  => array(),
35d723b313SAndreas Gohr
364ea373cdSAndreas Gohr        'chars'         => 0,
374ea373cdSAndreas Gohr        'words'         => 0,
384ea373cdSAndreas Gohr
399068e431SAndreas Gohr        'score'         => 0,
409068e431SAndreas Gohr
418fce80b1SAndreas Gohr        // calculated error scores
428fce80b1SAndreas Gohr        'err' => array(
438fce80b1SAndreas Gohr            'fixme'      => 0,
448fce80b1SAndreas Gohr            'noh1'       => 0,
458fce80b1SAndreas Gohr            'manyh1'     => 0,
468fce80b1SAndreas Gohr            'headernest' => 0,
478fce80b1SAndreas Gohr            'manyhr'     => 0,
488fce80b1SAndreas Gohr            'manybr'     => 0,
49d723b313SAndreas Gohr            'longformat' => 0,
504bda998cSAndreas Gohr            'multiformat'=> 0,
518fce80b1SAndreas Gohr        ),
528fce80b1SAndreas Gohr    );
538fce80b1SAndreas Gohr
548fce80b1SAndreas Gohr    var $quotelevel = 0;
55d723b313SAndreas Gohr    var $formatting = 0;
56*6afc1841Sthesunrise1983    var $tableopen  = false;
578fce80b1SAndreas Gohr
584ea373cdSAndreas Gohr    function document_start() {
594ea373cdSAndreas Gohr        global $ID;
604ea373cdSAndreas Gohr        $meta = p_get_metadata($ID);
614ea373cdSAndreas Gohr
624ea373cdSAndreas Gohr        // get some dates from meta data
634ea373cdSAndreas Gohr        $this->doc['created']  = $meta['date']['created'];
644ea373cdSAndreas Gohr        $this->doc['modified'] = $meta['date']['modified'];
654ea373cdSAndreas Gohr
664ea373cdSAndreas Gohr        // get author info
674ea373cdSAndreas Gohr        $revs = getRevisions($ID,0,0);
684ea373cdSAndreas Gohr        array_push($revs,$meta['last_change']['date']);
694ea373cdSAndreas Gohr        $this->doc['changes'] = count($revs);
704ea373cdSAndreas Gohr        foreach($revs as $rev){
714ea373cdSAndreas Gohr            $info = getRevisionInfo($ID, $rev);
724ea373cdSAndreas Gohr            if($info['user']){
734ea373cdSAndreas Gohr                $this->doc['authors'][$info['user']] += 1;
744ea373cdSAndreas Gohr            }else{
754ea373cdSAndreas Gohr                $this->doc['authors']['*'] += 1;
764ea373cdSAndreas Gohr            }
774ea373cdSAndreas Gohr        }
784ea373cdSAndreas Gohr
794ea373cdSAndreas Gohr        // work on raw text
804ea373cdSAndreas Gohr        $text = rawWiki($ID);
814ea373cdSAndreas Gohr        $this->doc['chars'] = utf8_strlen($text);
82ad6cda3cSNils Georg Heinrich Reichert        $this->doc['words'] = count(array_filter(preg_split('/[^\w\-_]/u',$text)));
834ea373cdSAndreas Gohr    }
844ea373cdSAndreas Gohr
854ea373cdSAndreas Gohr
868fce80b1SAndreas Gohr    /**
878fce80b1SAndreas Gohr     * Here the score is calculated
888fce80b1SAndreas Gohr     */
898fce80b1SAndreas Gohr    function document_end() {
90e348ef7cSAndreas Gohr        global $ID;
91e348ef7cSAndreas Gohr
92e348ef7cSAndreas Gohr        // 2 points for missing backlinks
93e348ef7cSAndreas Gohr        if(!count(ft_backlinks($ID))){
94e348ef7cSAndreas Gohr            $this->doc['err']['nobacklink'] += 2;
95e348ef7cSAndreas Gohr        }
968fce80b1SAndreas Gohr
978fce80b1SAndreas Gohr        // 1 point for each FIXME
988fce80b1SAndreas Gohr        $this->doc['err']['fixme'] += $this->doc['fixme'];
998fce80b1SAndreas Gohr
1008fce80b1SAndreas Gohr        // 5 points for missing H1
1018fce80b1SAndreas Gohr        if($this->doc['header_count'][1] == 0){
1028fce80b1SAndreas Gohr            $this->doc['err']['noh1'] += 5;
1038fce80b1SAndreas Gohr        }
1048fce80b1SAndreas Gohr        // 1 point for each H1 too much
1058fce80b1SAndreas Gohr        if($this->doc['header_count'][1] > 1){
1068fce80b1SAndreas Gohr            $this->doc['err']['manyh1'] += $this->doc['header'][1];
1078fce80b1SAndreas Gohr        }
1088fce80b1SAndreas Gohr
1098fce80b1SAndreas Gohr        // 1 point for each incorrectly nested headline
1108fce80b1SAndreas Gohr        $cnt = count($this->doc['header_struct']);
1118fce80b1SAndreas Gohr        for($i = 1; $i < $cnt; $i++){
1128fce80b1SAndreas Gohr            if($this->doc['header_struct'][$i] - $this->doc['header_struct'][$i-1] > 1){
1138fce80b1SAndreas Gohr                $this->doc['err']['headernest'] += 1;
1148fce80b1SAndreas Gohr            }
1158fce80b1SAndreas Gohr        }
1168fce80b1SAndreas Gohr
1178fce80b1SAndreas Gohr        // 1/2 points for deeply nested quotations
1188fce80b1SAndreas Gohr        if($this->doc['quote_nest'] > 2){
1198fce80b1SAndreas Gohr            $this->doc['err']['deepquote'] += $this->doc['quote_nest']/2;
1208fce80b1SAndreas Gohr        }
1218fce80b1SAndreas Gohr
1228fce80b1SAndreas Gohr        // FIXME points for many quotes?
1238fce80b1SAndreas Gohr
1248fce80b1SAndreas Gohr        // 1/2 points for too many hr
1258fce80b1SAndreas Gohr        if($this->doc['hr'] > 2){
1268fce80b1SAndreas Gohr            $this->doc['err']['manyhr'] = ($this->doc['hr'] - 2)/2;
1278fce80b1SAndreas Gohr        }
1288fce80b1SAndreas Gohr
1298fce80b1SAndreas Gohr        // 1 point for too many line breaks
1308fce80b1SAndreas Gohr        if($this->doc['linebreak'] > 2){
1318fce80b1SAndreas Gohr            $this->doc['err']['manybr'] = $this->doc['linebreak'] - 2;
1328fce80b1SAndreas Gohr        }
1338fce80b1SAndreas Gohr
1344ea373cdSAndreas Gohr        // 1 point for single author only
1356a7733deSthesunrise1983        if(!$this->getConf('single_author_only') && count($this->doc['authors']) == 1){
1364ea373cdSAndreas Gohr            $this->doc['err']['singleauthor'] = 1;
1374ea373cdSAndreas Gohr        }
1384ea373cdSAndreas Gohr
139d723b313SAndreas Gohr        // 1 point for too small document
140d723b313SAndreas Gohr        if($this->doc['chars'] < 150){
141d723b313SAndreas Gohr            $this->doc['err']['toosmall'] = 1;
142d723b313SAndreas Gohr        }
143d723b313SAndreas Gohr
144d723b313SAndreas Gohr        // 1 point for too large document
145d723b313SAndreas Gohr        if($this->doc['chars'] > 100000){
146d723b313SAndreas Gohr            $this->doc['err']['toolarge'] = 1;
147d723b313SAndreas Gohr        }
148d723b313SAndreas Gohr
149d723b313SAndreas Gohr        // header to text ratio
150d723b313SAndreas Gohr        $hc = $this->doc['header_count'][1] +
151d723b313SAndreas Gohr              $this->doc['header_count'][2] +
152d723b313SAndreas Gohr              $this->doc['header_count'][3] +
153d723b313SAndreas Gohr              $this->doc['header_count'][4] +
154d723b313SAndreas Gohr              $this->doc['header_count'][5];
15513abf7ccSAndreas Gohr        $hc--; //we expect at least 1
15613abf7ccSAndreas Gohr        if($hc > 0){
157d723b313SAndreas Gohr            $hr = $this->doc['chars']/$hc;
158d723b313SAndreas Gohr
159d723b313SAndreas Gohr            // 1 point for too many headers
160d723b313SAndreas Gohr            if($hr < 200){
161d723b313SAndreas Gohr                $this->doc['err']['manyheaders'] = 1;
162d723b313SAndreas Gohr            }
163d723b313SAndreas Gohr
164d723b313SAndreas Gohr            // 1 point for too few headers
1651b085f03SAndreas Gohr            if($hr > 2000){
166d723b313SAndreas Gohr                $this->doc['err']['fewheaders'] = 1;
167d723b313SAndreas Gohr            }
168d723b313SAndreas Gohr        }
169d723b313SAndreas Gohr
170d723b313SAndreas Gohr        // 1 point when no link at all
171d723b313SAndreas Gohr        if(!$this->doc['internal_links']){
172d723b313SAndreas Gohr            $this->doc['err']['nolink'] = 1;
173d723b313SAndreas Gohr        }
174d723b313SAndreas Gohr
175d723b313SAndreas Gohr        // 0.5 for broken links when too many
176d723b313SAndreas Gohr        if($this->doc['broken_links'] > 2){
177d723b313SAndreas Gohr            $this->doc['err']['brokenlink'] = $this->doc['broken_links']*0.5;
178d723b313SAndreas Gohr        }
179d723b313SAndreas Gohr
180d723b313SAndreas Gohr        // 2 points for lot's of formatting
181d723b313SAndreas Gohr        if($this->doc['formatted'] && $this->doc['chars']/$this->doc['formatted'] < 3){
182d723b313SAndreas Gohr            $this->doc['err']['manyformat'] = 2;
183d723b313SAndreas Gohr        }
184d723b313SAndreas Gohr
1859068e431SAndreas Gohr        // add up all scores
1869068e431SAndreas Gohr        foreach($this->doc['err'] as $err => $val) $this->doc['score'] += $val;
1879068e431SAndreas Gohr
1884ea373cdSAndreas Gohr
1898fce80b1SAndreas Gohr        //we're done here
1908fce80b1SAndreas Gohr        $this->doc = serialize($this->doc);
1918fce80b1SAndreas Gohr    }
1928fce80b1SAndreas Gohr
1938fce80b1SAndreas Gohr    /**
1948fce80b1SAndreas Gohr     * the format we produce
1958fce80b1SAndreas Gohr     */
1968fce80b1SAndreas Gohr    function getFormat(){
1978fce80b1SAndreas Gohr        return 'qc';
1988fce80b1SAndreas Gohr    }
1998fce80b1SAndreas Gohr
200d723b313SAndreas Gohr    function internallink($id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content') {
201d723b313SAndreas Gohr        global $ID;
202d723b313SAndreas Gohr        resolve_pageid(getNS($ID),$id,$exists);
203d723b313SAndreas Gohr
2048d7cf088SAndreas Gohr        // calculate link width
205d78155a4SAndreas Gohr        $a = explode(':',getNS($ID));
206d78155a4SAndreas Gohr        $b = explode(':',getNS($id));
207d78155a4SAndreas Gohr        while(isset($a[0]) && $a[0] == $b[0]){
2088d7cf088SAndreas Gohr            array_shift($a);
2098d7cf088SAndreas Gohr            array_shift($b);
2108d7cf088SAndreas Gohr        }
211d78155a4SAndreas Gohr        $length = count($a)+count($b);
2128d7cf088SAndreas Gohr        $this->doc['link_lengths'][] = $length;
2138d7cf088SAndreas Gohr
2140476d180SAndreas Gohr        $this->doc['internal_links']++;
2150476d180SAndreas Gohr        if(!$exists) $this->doc['broken_links']++;
2160476d180SAndreas Gohr    }
2170476d180SAndreas Gohr
2180476d180SAndreas Gohr    function externallink($url, $name = NULL) {
2190476d180SAndreas Gohr        $this->doc['external_links']++;
220d723b313SAndreas Gohr    }
2218fce80b1SAndreas Gohr
2228fce80b1SAndreas Gohr    function header($text, $level, $pos){
2238fce80b1SAndreas Gohr        $this->doc['header_count'][$level]++;
2248fce80b1SAndreas Gohr        $this->doc['header_struct'][] = $level;
2258fce80b1SAndreas Gohr    }
2268fce80b1SAndreas Gohr
2278fce80b1SAndreas Gohr    function smiley($smiley) {
2288fce80b1SAndreas Gohr        if($smiley == 'FIXME') $this->doc['fixme']++;
2298fce80b1SAndreas Gohr    }
2308fce80b1SAndreas Gohr
2318fce80b1SAndreas Gohr    function linebreak() {
232*6afc1841Sthesunrise1983        if(!$this->tableopen){
2338fce80b1SAndreas Gohr            $this->doc['linebreak']++;
2348fce80b1SAndreas Gohr        }
235*6afc1841Sthesunrise1983    }
236*6afc1841Sthesunrise1983
237*6afc1841Sthesunrise1983    function table_open($maxcols = null, $numrows = null, $pos = null){
238*6afc1841Sthesunrise1983        $this->tableopen = true;
239*6afc1841Sthesunrise1983    }
240*6afc1841Sthesunrise1983
241*6afc1841Sthesunrise1983    function table_close($pos = null){
242*6afc1841Sthesunrise1983        $this->tableopen = false;
243*6afc1841Sthesunrise1983    }
2448fce80b1SAndreas Gohr
2458fce80b1SAndreas Gohr    function hr() {
2468fce80b1SAndreas Gohr        $this->doc['hr']++;
2478fce80b1SAndreas Gohr    }
2488fce80b1SAndreas Gohr
2498fce80b1SAndreas Gohr    function quote_open() {
2508fce80b1SAndreas Gohr        $this->doc['quote_count']++;
2518fce80b1SAndreas Gohr        $this->quotelevel++;
2528fce80b1SAndreas Gohr        $this->doc['quote_nest'] = max($this->quotelevel,$this->doc['quote_nest']);
2538fce80b1SAndreas Gohr    }
2548fce80b1SAndreas Gohr
2558fce80b1SAndreas Gohr    function quote_close() {
2568fce80b1SAndreas Gohr        $this->quotelevel--;
2578fce80b1SAndreas Gohr    }
2588fce80b1SAndreas Gohr
259d723b313SAndreas Gohr    function strong_open() {
260d723b313SAndreas Gohr        $this->formatting++;
261d723b313SAndreas Gohr    }
2628fce80b1SAndreas Gohr
263d723b313SAndreas Gohr    function strong_close() {
264d723b313SAndreas Gohr        $this->formatting--;
265d723b313SAndreas Gohr    }
266d723b313SAndreas Gohr
267d723b313SAndreas Gohr    function emphasis_open() {
268d723b313SAndreas Gohr        $this->formatting++;
269d723b313SAndreas Gohr    }
270d723b313SAndreas Gohr
271d723b313SAndreas Gohr    function emphasis_close() {
272d723b313SAndreas Gohr        $this->formatting--;
273d723b313SAndreas Gohr    }
274d723b313SAndreas Gohr
275d723b313SAndreas Gohr    function underline_open() {
276d723b313SAndreas Gohr        $this->formatting++;
277d723b313SAndreas Gohr    }
278d723b313SAndreas Gohr
279d723b313SAndreas Gohr    function underline_close() {
280d723b313SAndreas Gohr        $this->formatting--;
281d723b313SAndreas Gohr    }
282d723b313SAndreas Gohr
283d723b313SAndreas Gohr    function cdata($text) {
284d723b313SAndreas Gohr        if(!$this->formatting) return;
285d723b313SAndreas Gohr
286d723b313SAndreas Gohr        $len = utf8_strlen($text);
287d723b313SAndreas Gohr
288d723b313SAndreas Gohr        // 1 point for formattings longer than 500 chars
289d723b313SAndreas Gohr        if($len>500) $this->doc['err']['longformat']++;
290d723b313SAndreas Gohr
2914bda998cSAndreas Gohr        // 1 point for each multiformatting
2924bda998cSAndreas Gohr        if($this->formatting > 1) $this->doc['err']['multiformat'] += 1*($this->formatting - 1);
2934bda998cSAndreas Gohr
294d723b313SAndreas Gohr        $this->doc['formatted'] += $len;
295d723b313SAndreas Gohr    }
2968fce80b1SAndreas Gohr}
2978fce80b1SAndreas Gohr
2988fce80b1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
299