Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
GraphVizTreeFormatter
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 getOutput
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 outputPerson
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 quoteValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 outputJunction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 outputEdge
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Genealogy;
4
5use Sanitizer;
6
7class GraphVizTreeFormatter extends TreeFormatter {
8
9    /**
10     * @inheritDoc
11     */
12    public function getOutput() {
13        // Start the tree.
14        $this->out( 'top', 'start', "digraph GenealogyTree_$this->name {" );
15        $this->out( 'top', 'graph-attrs', 'graph [rankdir=LR, ranksep=0.55]' );
16        $this->out( 'top', 'edge-attrs', 'edge [arrowhead=none, headport=w]' );
17        $this->out( 'top', 'node-attrs', 'node [shape=plaintext, fontsize=12]' );
18
19        // Combine all parts of the graph output.
20        $out = implode( "\n", $this->out['top'] ) . "\n\n"
21            . "/* People */\n"
22            . implode( "\n", $this->out['person'] ) . "\n\n";
23        if ( isset( $this->out['partner'] ) ) {
24            $out .= "/* Partners */\n"
25                . implode( "\n", $this->out['partner'] ) . "\n\n";
26        }
27        if ( isset( $this->out['child'] ) ) {
28            $out .= "/* Children */\n"
29                . implode( "\n", $this->out['child'] ) . "\n\n";
30        }
31        return $out . "}\n";
32    }
33
34    /**
35     * Output one GraphViz line for the given person.
36     * @param Person $person The person.
37     */
38    protected function outputPerson( Person $person ) {
39        if ( $person->getTitle()->exists() ) {
40            $url = '[[' . $person->getTitle()->getPrefixedText() . ']]';
41            $colour = 'black';
42        } else {
43            $queryString = [
44                'preload' => wfMessage( 'genealogy-person-preload' ),
45                'action' => 'edit',
46            ];
47            $url = '[' . $person->getTitle()->getFullURL( $queryString ) . ']';
48            $colour = 'red';
49        }
50        $personId = $this->varId( $person->getTitle()->getPrefixedText() );
51        $desc = '';
52        if ( $person->getDescription() ) {
53            $desc = '<BR/><FONT POINT-SIZE="9">'
54                . Sanitizer::stripAllTags( $person->getDescription() )
55                . '</FONT>';
56        }
57        $title = $person->getTitleHtml();
58        $line = $personId . " ["
59            . " label=<$title$desc>, "
60            . " URL=" . $this->quoteValue( $url ) . ", "
61            . " tooltip=" . $this->quoteValue( $person->getTitle()->getPrefixedText() ) . ", "
62            . " fontcolor=\"$colour\" "
63            . "]";
64        $this->out( 'person', $personId, $line );
65    }
66
67    /**
68     * Quote and escape a string.
69     * The GraphViz docs say: In quoted strings in DOT, the only escaped character is double-quote (").
70     * That is, in quoted strings, the dyad \" is converted to "; all other characters are left unchanged.
71     * In particular, \\ remains \\. Layout engines may apply additional escape sequences.
72     * @param string $val
73     * @return string The escaped value, with surrounding quotes.
74     */
75    private function quoteValue( string $val ): string {
76        return '"' . str_replace( '"', '\"', $val ) . '"';
77    }
78
79    /**
80     * @inheritDoc
81     */
82    protected function outputJunction( $peopleId ) {
83        $this->out( 'partner', $peopleId, $this->varId( $peopleId ) . ' [label="", shape="point"]' );
84    }
85
86    /**
87     * @inheritDoc
88     */
89    protected function outputEdge( $group, $key, $from, $to, $towardsJunction = false ) {
90        $line = $this->varId( $from ) . ' -> ' . $this->varId( $to );
91        if ( $towardsJunction ) {
92            $line .= " [style=\"dashed\"]";
93        }
94        $this->out( $group, $key, $line );
95    }
96}