r84953 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84952‎ | r84953 | r84954 >
Date:12:53, 29 March 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
cleanup of graph query printer
Modified paths:
  • /trunk/extensions/SemanticResultFormats/GraphViz/SRF_Graph.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/GraphViz/SRF_Graph.php
@@ -46,92 +46,128 @@
4747 $this->m_graphColor = array_key_exists( 'graphcolor', $params ) && strtolower( trim( $params['graphcolor'] ) ) == 'yes';
4848 }
4949
50 - protected function getResultText( $res, $outputmode ) {
 50+ protected function getResultText( /* SMWQueryResult */ $res, $outputmode ) {
5151 $wgGraphVizSettings = new GraphVizSettings;
5252 $this->isHTML = true;
5353
5454 $key = 0;
55 - // Create text graph
56 - $graphInput = '';
 55+
5756 $legendInput = '';
58 -
59 - $graphInput .= "digraph $this->m_graphName {";
 57+
 58+ $graphInput = "digraph $this->m_graphName {";
6059 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 );
11664 }
117 -
 65+
11866 $graphInput .= "}";
 67+
11968 // Calls renderGraphViz function from MediaWiki GraphViz extension
12069 $result = renderGraphviz( $graphInput );
121 - if ( ( $this->m_graphLegend == true ) && ( $this->m_graphColor == true ) ) {
 70+
 71+ if ( $this->m_graphLegend && $this->m_graphColor ) {
12272 $arrayCount = 0;
12373 $result .= "<P>";
 74+
12475 foreach ( $this->m_labelArray as $m_label ) {
12576 $color = $this->m_graphColors[$arrayCount];
12677 $result .= "<font color=$color>$color: $m_label </font><br />";
12778 $arrayCount = $arrayCount + 1;
12879 }
 80+
12981 $result .= "</P>";
13082 }
13183
13284 return $result;
13385 }
 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+ }
134116
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() {
136172 return array(
137173 array( 'name' => 'graphname', 'type' => 'string', 'description' => wfMsg( 'srf_paramdesc_graphname' ) ),
138174 array( 'name' => 'graphsize', 'type' => 'int', 'description' => wfMsg( 'srf_paramdesc_graphsize' ) ),

Status & tagging log