00001 <?php
00002
00016 class SRFGraph extends SMWResultPrinter {
00017
00018 public static $NODE_SHAPES = array(
00019 'box',
00020 'box3d',
00021 'circle',
00022 'component',
00023 'diamond',
00024 'doublecircle',
00025 'doubleoctagon',
00026 'egg',
00027 'ellipse',
00028 'folder',
00029 'hexagon',
00030 'house',
00031 'invhouse',
00032 'invtrapezium',
00033 'invtriangle',
00034 'Mcircle',
00035 'Mdiamond',
00036 'Msquare',
00037 'none',
00038 'note',
00039 'octagon',
00040 'parallelogram',
00041 'pentagon ',
00042 'plaintext',
00043 'point',
00044 'polygon',
00045 'rect',
00046 'rectangle',
00047 'septagon',
00048 'square',
00049 'tab',
00050 'trapezium',
00051 'triangle',
00052 'tripleoctagon',
00053 );
00054
00055 protected $m_graphName;
00056 protected $m_graphLabel;
00057 protected $m_graphColor;
00058 protected $m_graphLegend;
00059 protected $m_graphLink;
00060 protected $m_rankdir;
00061 protected $m_graphSize;
00062 protected $m_labelArray = array();
00063 protected $m_graphColors = array( 'black', 'red', 'green', 'blue', 'darkviolet', 'gold', 'deeppink', 'brown', 'bisque', 'darkgreen', 'yellow', 'darkblue', 'magenta', 'steelblue2' );
00064 protected $m_nameProperty;
00065 protected $m_nodeShape;
00066 protected $m_parentRelation;
00067 protected $m_wordWrapLimit;
00068
00073 protected function handleParameters( array $params, $outputmode ) {
00074 parent::handleParameters( $params, $outputmode );
00075
00076 $this->m_graphName = trim( $params['graphname'] );
00077 $this->m_graphSize = trim( $params['graphsize'] );
00078
00079 $this->m_graphLegend = $params['graphlegend'];
00080 $this->m_graphLabel = $params['graphlabel'];
00081
00082 $this->m_rankdir = strtoupper( trim( $params['arrowdirection'] ) );
00083
00084 $this->m_graphLink = $params['graphlink'];
00085 $this->m_graphColor =$params['graphcolor'];
00086
00087 $this->m_nameProperty = $params['nameproperty'] === false ? false : trim( $params['nameproperty'] );
00088
00089 $this->m_parentRelation = strtolower( trim( $params['relation'] ) ) == 'parent';
00090
00091 $this->m_nodeShape = $params['nodeshape'];
00092 $this->m_wordWrapLimit = $params['wordwraplimit'];
00093 }
00094
00095 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00096 if ( !is_callable( 'renderGraphviz' ) ) {
00097 wfWarn( 'The SRF Graph printer needs the GraphViz extension to be installed.' );
00098 return '';
00099 }
00100
00101 global $wgGraphVizSettings;
00102 $this->isHTML = true;
00103
00104 $graphInput = "digraph $this->m_graphName {";
00105 if ( $this->m_graphSize != '' ) $graphInput .= "size=\"$this->m_graphSize\";";
00106 if ( $this->m_nodeShape ) $graphInput .= "node [shape=$this->m_nodeShape];";
00107 $graphInput .= "rankdir=$this->m_rankdir;";
00108
00109 while ( $row = $res->getNext() ) {
00110 $graphInput .= $this->getGVForItem( $row, $outputmode );
00111 }
00112
00113 $graphInput .= "}";
00114
00115
00116 $result = renderGraphviz( $graphInput );
00117
00118 if ( $this->m_graphLegend && $this->m_graphColor ) {
00119 $arrayCount = 0;
00120 $arraySize = count( $this->m_graphColors );
00121 $result .= "<P>";
00122
00123 foreach ( $this->m_labelArray as $m_label ) {
00124 if ( $arrayCount >= $arraySize ) {
00125 $arrayCount = 0;
00126 }
00127
00128 $color = $this->m_graphColors[$arrayCount];
00129 $result .= "<font color=$color>$color: $m_label </font><br />";
00130
00131 $arrayCount += 1;
00132 }
00133
00134 $result .= "</P>";
00135 }
00136
00137 return $result;
00138 }
00139
00150 protected function getGVForItem( array $row, $outputmode ) {
00151 $segments = array();
00152
00153
00154 foreach ( $row as $i => $resultArray ) {
00155
00156
00157 while ( ( $object = $resultArray->getNextDataValue() ) !== false ) {
00158 $propName = $resultArray->getPrintRequest()->getLabel();
00159 $isName = $this->m_nameProperty ? ( $i != 0 && $this->m_nameProperty === $propName ) : $i == 0;
00160
00161 if ( $isName ) {
00162 $name = $this->getWordWrappedText( $object->getShortText( $outputmode ), $this->m_wordWrapLimit );
00163 }
00164
00165 if ( !( $this->m_nameProperty && $i == 0 ) ) {
00166 $segments[] = $this->getGVForDataValue( $object, $outputmode, $isName, $name, $propName );
00167 }
00168 }
00169 }
00170
00171 return implode( "\n", $segments );
00172 }
00173
00187 protected function getGVForDataValue( SMWDataValue $object, $outputmode, $isName, $name, $labelName ) {
00188 $graphInput = '';
00189 $text = $object->getShortText( $outputmode );
00190
00191 if ( $this->m_graphLink ) {
00192 $nodeLinkTitle = Title::newFromText( $text );
00193 $nodeLinkURL = $nodeLinkTitle->getLocalURL();
00194 }
00195
00196 $text = $this->getWordWrappedText( $text, $this->m_wordWrapLimit );
00197
00198 if ( $this->m_graphLink ) {
00199 $graphInput .= " \"$text\" [URL = \"$nodeLinkURL\"]; ";
00200 }
00201
00202 if ( !$isName ) {
00203 $graphInput .= $this->m_parentRelation ? " \"$text\" -> \"$name\" " : " \"$name\" -> \"$text\" ";
00204
00205 if ( $this->m_graphLabel && $this->m_graphColor ) {
00206 $graphInput .= ' [';
00207
00208 if ( array_search( $labelName, $this->m_labelArray, true ) === false ) {
00209 $this->m_labelArray[] = $labelName;
00210 }
00211
00212 $color = $this->m_graphColors[array_search( $labelName, $this->m_labelArray, true )];
00213
00214 if ( $this->m_graphLabel ) {
00215 $graphInput .= "label=\"$labelName\"";
00216 if ( $this->m_graphColor ) $graphInput .= ",fontcolor=$color,";
00217 }
00218
00219 if ( $this->m_graphColor ) {
00220 $graphInput .= "color=$color";
00221 }
00222
00223 $graphInput .= ']';
00224
00225 }
00226
00227 $graphInput .= ';';
00228 }
00229
00230 return $graphInput;
00231 }
00232
00243 protected function getWordWrappedText( $text, $charLimit ) {
00244 $charLimit = max( array( $charLimit, 1 ) );
00245 $segments = array();
00246
00247 while ( strlen( $text ) > $charLimit ) {
00248
00249 $splitPosition = strrpos( substr( $text, 0, $charLimit ), ' ' );
00250
00251 if ( $splitPosition === false ) {
00252
00253 $splitPosition = strpos( $text, ' ' );
00254
00255 if ( $splitPosition === false ) {
00256
00257 $splitPosition = strlen( $text ) - 1;
00258 }
00259 }
00260
00261 $segments[] = substr( $text, 0, $splitPosition + 1 );
00262 $text = substr( $text, $splitPosition + 1 );
00263 }
00264
00265 $segments[] = $text;
00266
00267 return implode( '\n', $segments );
00268 }
00269
00274 public function getName() {
00275 return wfMsg( 'srf-printername-graph' );
00276 }
00277
00278 public function getParameters() {
00279 $params = parent::getParameters();
00280
00281 $params['graphname'] = new Parameter( 'graphname', Parameter::TYPE_STRING, 'QueryResult' );
00282 $params['graphname']->setMessage( 'srf_paramdesc_graphname' );
00283
00284 $params['graphsize'] = new Parameter( 'graphsize', Parameter::TYPE_INTEGER );
00285 $params['graphsize']->setMessage( 'srf_paramdesc_graphsize' );
00286 $params['graphsize']->setDefault( '', false );
00287
00288 $params['graphlegend'] = new Parameter( 'graphlegend', Parameter::TYPE_BOOLEAN, false );
00289 $params['graphlegend']->setMessage( 'srf_paramdesc_graphlegend' );
00290
00291 $params['graphlabel'] = new Parameter( 'graphlabel', Parameter::TYPE_BOOLEAN, false );
00292 $params['graphlabel']->setMessage( 'srf_paramdesc_graphlabel' );
00293
00294 $params['graphlink'] = new Parameter( 'graphlink', Parameter::TYPE_BOOLEAN, false );
00295 $params['graphlink']->setMessage( 'srf_paramdesc_graphlink' );
00296
00297 $params['graphcolor'] = new Parameter( 'graphcolor', Parameter::TYPE_BOOLEAN, false );
00298 $params['graphcolor']->setMessage( 'srf_paramdesc_graphcolor' );
00299
00300 $params['arrowdirection'] = new Parameter( 'arrowdirection', Parameter::TYPE_STRING, 'LR', array( 'rankdir' ) );
00301 $params['arrowdirection']->setMessage( 'srf_paramdesc_rankdir' );
00302 $params['arrowdirection']->addCriteria( new CriterionInArray( 'LR', 'RL', 'TB', 'BT' ) );
00303
00304 $params['nodeshape'] = new Parameter( 'nodeshape' );
00305 $params['nodeshape']->setDefault( false, false );
00306 $params['nodeshape']->setMessage( 'srf-paramdesc-graph-nodeshape' );
00307 $params['nodeshape']->addCriteria( new CriterionInArray( self::$NODE_SHAPES ) );
00308
00309 $params['relation'] = new Parameter( 'relation' );
00310 $params['relation']->setDefault( 'child' );
00311 $params['relation']->setMessage( 'srf-paramdesc-graph-relation' );
00312 $params['relation']->addCriteria( new CriterionInArray( 'parent', 'child' ) );
00313
00314 $params['nameproperty'] = new Parameter( 'nameproperty', Parameter::TYPE_STRING, '' );
00315 $params['nameproperty']->setMessage( 'srf-paramdesc-graph-nameprop' );
00316 $params['nameproperty']->setDefault( false, false );
00317
00318 $params['wordwraplimit'] = new Parameter( 'wordwraplimit', Parameter::TYPE_INTEGER );
00319 $params['wordwraplimit']->setMessage( 'srf-paramdesc-graph-wwl' );
00320 $params['wordwraplimit']->setDefault( 25 );
00321
00322 return $params;
00323 }
00324
00325 }