Index: trunk/extensions/SemanticResultFormats/GraphViz/SRF_Graph.php |
— | — | @@ -46,92 +46,128 @@ |
47 | 47 | $this->m_graphColor = array_key_exists( 'graphcolor', $params ) && strtolower( trim( $params['graphcolor'] ) ) == 'yes'; |
48 | 48 | } |
49 | 49 | |
50 | | - protected function getResultText( $res, $outputmode ) { |
| 50 | + protected function getResultText( /* SMWQueryResult */ $res, $outputmode ) { |
51 | 51 | $wgGraphVizSettings = new GraphVizSettings; |
52 | 52 | $this->isHTML = true; |
53 | 53 | |
54 | 54 | $key = 0; |
55 | | - // Create text graph |
56 | | - $graphInput = ''; |
| 55 | + |
57 | 56 | $legendInput = ''; |
58 | | - |
59 | | - $graphInput .= "digraph $this->m_graphName {"; |
| 57 | + |
| 58 | + $graphInput = "digraph $this->m_graphName {"; |
60 | 59 | if ( $this->m_graphSize != '' ) $graphInput .= "size=\"$this->m_graphSize\";"; |
61 | | - $graphInput .= "rankdir=$this->m_rankdir;"; |
62 | | - while ( $row = $res->getNext() ) { |
63 | | - |
64 | | - $firstcol = true; |
65 | | - |
66 | | - foreach ( $row as $field ) { |
67 | | - |
68 | | - while ( ( $object = $field->getNextObject() ) !== false ) { |
69 | | - |
70 | | - $text = $object->getShortText( $outputmode ); |
71 | | - |
72 | | - if ( $firstcol ) { |
73 | | - $firstcolvalue = $object->getShortText( $outputmode ); |
74 | | - |
75 | | - } |
76 | | - |
77 | | - if ( $this->m_graphLink == true ) { |
78 | | - $nodeLinkTitle = Title::newFromText( $text ); |
79 | | - $nodeLinkURL = $nodeLinkTitle->getLocalURL(); |
80 | | - |
81 | | - $graphInput .= " \"$text\" [URL = \"$nodeLinkURL\"]; "; |
82 | | - } |
83 | | - |
84 | | - if ( !$firstcol ) { |
85 | | - $graphInput .= " \"$firstcolvalue\" -> \"$text\" "; |
86 | | - if ( ( $this->m_graphLabel == true ) || ( $this->m_graphColor == true ) ) { |
87 | | - $graphInput .= " ["; |
88 | | - $req = $field->getPrintRequest(); |
89 | | - $labelName = $req->getLabel(); |
90 | | - |
91 | | - if ( array_search( $labelName, $this->m_labelArray, true ) === false ) { |
92 | | - $this->m_labelArray[] = $labelName; |
93 | | - } |
94 | | - $key = array_search( $labelName, $this->m_labelArray, true ); |
95 | | - $color = $this->m_graphColors[$key]; |
96 | | - |
97 | | - if ( $this->m_graphLabel == true ) { |
98 | | - $graphInput .= "label=\"$labelName\""; |
99 | | - if ( $this->m_graphColor == true ) $graphInput .= ",fontcolor=$color,"; |
100 | | - } |
101 | | - if ( $this->m_graphColor == true ) { |
102 | | - |
103 | | - $graphInput .= "color=$color"; |
104 | | - } |
105 | | - $graphInput .= "]"; |
106 | | - |
107 | | - } |
108 | | - $graphInput .= ";"; |
109 | | - |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - $firstcol = false; |
114 | | - } |
115 | | - |
| 60 | + $graphInput .= "rankdir=$this->m_rankdir;"; |
| 61 | + |
| 62 | + while ( $row = $res->getNext() ) { |
| 63 | + $graphInput .= $this->getGVForItem( $row, $outputmode ); |
116 | 64 | } |
117 | | - |
| 65 | + |
118 | 66 | $graphInput .= "}"; |
| 67 | + |
119 | 68 | // Calls renderGraphViz function from MediaWiki GraphViz extension |
120 | 69 | $result = renderGraphviz( $graphInput ); |
121 | | - if ( ( $this->m_graphLegend == true ) && ( $this->m_graphColor == true ) ) { |
| 70 | + |
| 71 | + if ( $this->m_graphLegend && $this->m_graphColor ) { |
122 | 72 | $arrayCount = 0; |
123 | 73 | $result .= "<P>"; |
| 74 | + |
124 | 75 | foreach ( $this->m_labelArray as $m_label ) { |
125 | 76 | $color = $this->m_graphColors[$arrayCount]; |
126 | 77 | $result .= "<font color=$color>$color: $m_label </font><br />"; |
127 | 78 | $arrayCount = $arrayCount + 1; |
128 | 79 | } |
| 80 | + |
129 | 81 | $result .= "</P>"; |
130 | 82 | } |
131 | 83 | |
132 | 84 | return $result; |
133 | 85 | } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the GV for a single subject. |
| 89 | + * |
| 90 | + * @since 1.5.4 |
| 91 | + * |
| 92 | + * @param array $row |
| 93 | + * @param $outputmode |
| 94 | + * |
| 95 | + * @return string |
| 96 | + */ |
| 97 | + protected function getGVForItem( array /* of SMWResultArray */ $row, $outputmode ) { |
| 98 | + $segments = array(); |
| 99 | + |
| 100 | + // Loop throught all fields of the record. |
| 101 | + foreach ( $row as $i => $resultArray ) { |
| 102 | + |
| 103 | + // Loop throught all the parts of the field value. |
| 104 | + while ( ( $object = $resultArray->getNextObject() ) !== false ) { |
| 105 | + if ( $i == 0 ) { |
| 106 | + $firstColValue = $object->getShortText( $outputmode ); |
| 107 | + $labelName = $resultArray->getPrintRequest()->getLabel(); |
| 108 | + } |
| 109 | + |
| 110 | + $segments[] = $this->getGVForDataValue( $object, $outputmode, $i == 0, $firstColValue, $labelName ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return implode( "\n", $segments ); |
| 115 | + } |
134 | 116 | |
135 | | - function getParameters() { |
| 117 | + /** |
| 118 | + * Returns the GV for a single SMWDataValue. |
| 119 | + * |
| 120 | + * @since 1.5.4 |
| 121 | + * |
| 122 | + * @param SMWDataValue $object |
| 123 | + * @param $outputmode |
| 124 | + * @param boolean $isFirstColumn |
| 125 | + * @param string $firstColValue |
| 126 | + * @param string $labelName |
| 127 | + * |
| 128 | + * @return string |
| 129 | + */ |
| 130 | + protected function getGVForDataValue( SMWDataValue $object, $outputmode, $isFirstColumn, $firstColValue, $labelName ) { |
| 131 | + $graphInput = ''; |
| 132 | + $text = $object->getShortText( $outputmode ); |
| 133 | + |
| 134 | + if ( $this->m_graphLink == true ) { |
| 135 | + $nodeLinkTitle = Title::newFromText( $text ); |
| 136 | + $nodeLinkURL = $nodeLinkTitle->getLocalURL(); |
| 137 | + |
| 138 | + $graphInput .= " \"$text\" [URL = \"$nodeLinkURL\"]; "; |
| 139 | + } |
| 140 | + |
| 141 | + if ( !$isFirstColumn ) { |
| 142 | + $graphInput .= " \"$firstColValue\" -> \"$text\" "; |
| 143 | + |
| 144 | + if ( $this->m_graphLabel && $this->m_graphColor ) { |
| 145 | + $graphInput .= ' ['; |
| 146 | + |
| 147 | + if ( array_search( $labelName, $this->m_labelArray, true ) === false ) { |
| 148 | + $this->m_labelArray[] = $labelName; |
| 149 | + } |
| 150 | + $key = array_search( $labelName, $this->m_labelArray, true ); |
| 151 | + $color = $this->m_graphColors[$key]; |
| 152 | + |
| 153 | + if ( $this->m_graphLabel ) { |
| 154 | + $graphInput .= "label=\"$labelName\""; |
| 155 | + if ( $this->m_graphColor ) $graphInput .= ",fontcolor=$color,"; |
| 156 | + } |
| 157 | + if ( $this->m_graphColor ) { |
| 158 | + $graphInput .= "color=$color"; |
| 159 | + } |
| 160 | + |
| 161 | + $graphInput .= ']'; |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + $graphInput .= ';'; |
| 166 | + } |
| 167 | + |
| 168 | + return $graphInput; |
| 169 | + } |
| 170 | + |
| 171 | + public function getParameters() { |
136 | 172 | return array( |
137 | 173 | array( 'name' => 'graphname', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_graphname' ) ), |
138 | 174 | array( 'name' => 'graphsize', 'type' => 'int', 'description' => wfMsg( 'srf_paramdesc_graphsize' ) ), |