Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php |
— | — | @@ -103,19 +103,23 @@ |
104 | 104 | $this->outputmode = SMW_OUTPUT_HTML; |
105 | 105 | |
106 | 106 | // check parameters |
107 | | - if(empty($this->m_ploticusparams)) |
108 | | - return ('<p><strong>ERROR: <em>ploticusparams</em> required.</strong></p>'); |
| 107 | + $validformats = array('svg', 'svgz','swf', 'png', 'gif', 'jpeg', 'drawdump', 'drawdumpa', 'eps', 'ps', 'csv'); |
| 108 | + if (!in_array($this->m_imageformat, $validformats)) |
| 109 | + return ('<p classid="srfperror">ERROR: '. $this->m_imageformat. ' is not a supported imageformat.<br/>Valid imageformats are: ' . |
| 110 | + implode(', ', $validformats) . '</p>'); |
109 | 111 | |
110 | | - if(empty($srfgPloticusPath)) |
111 | | - return ('<p><strong>ERROR: Set $srfgPloticusPath in LocalSettings.php (e.g. $srfgPloticusPath=/usr/bin/ploticus).</strong></p>'); |
| 112 | + if($this->m_imageformat != 'csv' && empty($this->m_ploticusparams)) |
| 113 | + return ('<p classid="srfperror">ERROR: <em>ploticusparams</em> required.</p>'); |
112 | 114 | |
| 115 | + if($this->m_imageformat != 'csv' && empty($srfgPloticusPath)) |
| 116 | + return ('<p classid="srfperror">ERROR: Set $srfgPloticusPath in LocalSettings.php (e.g. $srfgPloticusPath=/usr/bin/pl).</p>'); |
| 117 | + |
| 118 | + if (!file_exists($srfgPloticusPath)) |
| 119 | + return ('<p classid=""srfperror">ERROR: Could not find ploticus in <em>' . $srfgPloticusPath . '</em></p>'); |
| 120 | + |
113 | 121 | if($this->m_ploticusmode !== 'script' && $this->m_ploticusmode !== 'prefab') |
114 | | - return ("<p><strong>ERROR: Unknown mode specified ($this->m_ploticusmode). Only 'prefab' (default) and 'script' mode supported.</strong></p>"); |
| 122 | + return ('<p classid="srfperror">ERROR: Unknown mode specified (' . $this->m_ploticusmode. '). Only "prefab" (default) and "script" mode supported.</p>'); |
115 | 123 | |
116 | | - $validformats = array('svg', 'svgz','swf', 'png', 'gif', 'jpeg', 'drawdump', 'drawdumpa', 'eps', 'ps'); |
117 | | - if (!in_array($this->m_imageformat, $validformats)) |
118 | | - return ("<p><strong>ERROR: $this->m_imageformat is not a supported format. Valid values are: svg, svg, swf, png, gif, jpeg, drawdump, drawdumpa, eps, ps</strong></p>"); |
119 | | - |
120 | 124 | // remove potentially dangerous keywords (prefab mode) or ploticus directives (script mode) |
121 | 125 | // this is an extended check, JUST IN CASE, even though we're invoking ploticus with the noshell security parameter |
122 | 126 | if ($this->m_ploticusmode === 'prefab') { |
— | — | @@ -142,7 +146,19 @@ |
143 | 147 | // create result csv file that we pass on to ploticus |
144 | 148 | $tmpFile = tempnam($ploticusDir, 'srf-'); |
145 | 149 | if (($fhandle = fopen($tmpFile, 'w')) === false ) |
146 | | - return ("<p><strong>ERROR: Cannot create data file - $tmpFile. Check permissions. </strong></p>"); |
| 150 | + return ('<p class="srfperror">ERROR: Cannot create data file - ' . $tmpFile . '. Check permissions.</p>'); |
| 151 | + // create the header row if asked - kludgy but works |
| 152 | + if ($this->mShowHeaders) { |
| 153 | + $header_row = array(); |
| 154 | + foreach ($res->getPrintRequests() as $pr) { |
| 155 | + $headertext= Sanitizer::decodeCharReferences(substr(end(explode('|', $pr->getText(SMW_OUTPUT_WIKI, $this->mLinker))),0,-2)); |
| 156 | + $header_row[] = strtr($headertext, " ,", "_|"); // ploticus cant handle embedded spaces/commas for legends |
| 157 | + } |
| 158 | + if (empty($header_row[0])) |
| 159 | + $header_row[0] = "Article"; |
| 160 | + fputcsv($fhandle, $header_row); |
| 161 | + } |
| 162 | + // write the results |
147 | 163 | while ( $row = $res->getNext() ) { |
148 | 164 | $row_items = array(); |
149 | 165 | foreach ($row as $field) { |
— | — | @@ -158,11 +174,11 @@ |
159 | 175 | } |
160 | 176 | fclose($fhandle); |
161 | 177 | |
162 | | - // we create a hash based on params and csv file. |
| 178 | + // we create a hash based on params |
163 | 179 | // this is a great way to see if the params and/or the query result has changed |
164 | 180 | $hashname = hash('md5', implode(',',$this->m_params)); |
165 | | - if ($this->m_liveupdating) { |
166 | | - // only include contents of result csv in hash when liveupdating is on |
| 181 | + if (!$this->m_imageformat != 'csv' && $this->m_liveupdating) { |
| 182 | + // only include contents of result csv in hash when liveupdating is on and imageformat != csv |
167 | 183 | // in this way, doing file_exists check against hash filename will fail when query result has changed |
168 | 184 | $hashname .= hash_file('md5',$tmpFile); |
169 | 185 | } |
— | — | @@ -174,7 +190,11 @@ |
175 | 191 | @unlink($dataFile); |
176 | 192 | @rename($tmpFile, $dataFile); |
177 | 193 | $dataURL = $wgUploadPath . '/ploticus/' . $hashname . '.csv'; |
| 194 | + $srficonPath = $wgScriptPath . '/extensions/SemanticResultFormats/Ploticus/icons/'; |
178 | 195 | |
| 196 | + if ($this->m_imageformat == 'csv') |
| 197 | + return ('<a href="' . $dataURL . '" title="CSV file"><img src="'. $srficonPath . 'csv_16.png" alt="CSV file"></a>'); |
| 198 | + |
179 | 199 | $graphFile = $ploticusDir . $hashname . '.' . $this->m_imageformat; |
180 | 200 | $graphURL = $wgUploadPath . '/ploticus/' . $hashname . '.' . $this->m_imageformat; |
181 | 201 | $errorFile = $ploticusDir . $hashname . '.err'; |
— | — | @@ -200,13 +220,7 @@ |
201 | 221 | // the need to periodically clean-up graph, csv, script and map files |
202 | 222 | $errorData = ''; |
203 | 223 | if ($this->m_debug || !file_exists($graphFile)) { |
204 | | - |
205 | | - // Verify that ploticus is installed. |
206 | | - if (!file_exists($srfgPloticusPath)) { |
207 | | - return ('<p><strong>ERROR: Could not find ploticus in <em>' . |
208 | | - $srfgPloticusPath . '</em></strong></p>'); |
209 | | - } |
210 | | - |
| 224 | + |
211 | 225 | // we set $srfgEnvSettings if specified |
212 | 226 | $commandline = empty($srfgEnvSettings) ? ' ' : $srfgEnvSettings . ' '; |
213 | 227 | |
— | — | @@ -232,7 +246,8 @@ |
233 | 247 | $commandline .= wfEscapeShellArg($srfgPloticusPath) . |
234 | 248 | ($this->m_debug ? ' -debug':' ') . |
235 | 249 | ' -noshell ' . $sanitized_ploticusparams . |
236 | | - ' data=' . wfEscapeShellArg($dataFile) . |
| 250 | + ($this->mShowHeaders ? ' header=yes':' ') . |
| 251 | + ' delim=comma data=' . wfEscapeShellArg($dataFile) . |
237 | 252 | ' -' . $this->m_imageformat; |
238 | 253 | |
239 | 254 | if ($this->m_imageformat == 'drawdump' || $this->m_imageformat == 'drawdumpa' ) { |
— | — | @@ -263,12 +278,8 @@ |
264 | 279 | } |
265 | 280 | } |
266 | 281 | |
267 | | - $srficonPath = $wgScriptPath . '/extensions/SemanticResultFormats/Ploticus/icons/'; |
268 | | - |
269 | 282 | //Prepare output. Put everything inside a table |
270 | 283 | // PLOT ROW - colspan 3 |
271 | | - // TODO: use CSS, create stylesheet |
272 | | - // TODO: generate CSS unique id for each SRF-Ploticus occurence |
273 | 284 | $rtnstr = '<table class="srfptable" id="srfptblid' . $smwgIQRunningNumber . '" cols="3"' . |
274 | 285 | (empty($this->m_tblwidth) ? ' ' : ' width="'. $this->m_tblwidth . '" ') . |
275 | 286 | (empty($this->m_tblheight) ? ' ' : ' height="'. $this->m_tblheight . '" ') . |
— | — | @@ -276,7 +287,7 @@ |
277 | 288 | if (!empty($errorData) && !$this->m_debug) { |
278 | 289 | // there was an error. We do the not debug check since ploticus by default sends the debug trace to stderr too |
279 | 290 | // so when debug is on, having a non-empty errorData does not necessarily indicate an error. |
280 | | - $rtnstr .= '<td class="srfperror" colspan="3"><strong>Error processing ploticus data:</strong></td></tr><tr><td colspan="3" align="center">' . |
| 291 | + $rtnstr .= '<td class="srfperror" colspan="3">Error processing ploticus data:</td></tr><tr><td class="srfperror" colspan="3" align="center">' . |
281 | 292 | $errorData . '</td></tr>'; |
282 | 293 | } else { |
283 | 294 | $rtnstr .= '<td class="srfpplot" colspan="3" align="center">'; |
— | — | @@ -354,7 +365,7 @@ |
355 | 366 | |
356 | 367 | // INFOROW - col 2 |
357 | 368 | // we don't display anything here for now - perhaps we can show query name in the future |
358 | | - $rtnstr .= '</td><td width="33%" colspan="1" align="center">'; |
| 369 | + $rtnstr .= '</td><td class="srfptitle" width="33%" colspan="1" align="center">'; |
359 | 370 | |
360 | 371 | |
361 | 372 | // INFOROW - TIMESTAMP - col 3 |
— | — | @@ -369,13 +380,13 @@ |
370 | 381 | // DEBUGROW - colspan 3, only display when debug is on |
371 | 382 | // add link to script or display ploticus cmdline/script |
372 | 383 | if ($this->m_debug) { |
373 | | - $rtnstr .= '<tr><td class="srfpdebug" align="center" colspan="3"><strong>DEBUG: '; |
| 384 | + $rtnstr .= '<tr><td class="srfpdebug" align="center" colspan="3">DEBUG: '; |
374 | 385 | if ($this->m_ploticusmode == 'script') { |
375 | 386 | $rtnstr .= '<a href="' . $scriptURL . '" target="_blank">SCRIPT</a> (<a href="'. |
376 | | - $errorURL . '" target="_blank">Ploticus Trace</a>)</strong></td></tr>'; |
| 387 | + $errorURL . '" target="_blank">Ploticus Trace</a>)</td></tr>'; |
377 | 388 | } else { |
378 | 389 | $rtnstr .= 'PREFAB (<a href="' . $errorURL . |
379 | | - '" target="_blank">Ploticus Trace</a>)</strong></td></tr><tr><td colspan="3">' . |
| 390 | + '" target="_blank">Ploticus Trace</a>)</td></tr><tr><td class="srfpdebug" colspan="3">' . |
380 | 391 | $commandline . '</td></tr>'; |
381 | 392 | } |
382 | 393 | } |