r47505 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47504‎ | r47505 | r47506 >
Date:19:12, 19 February 2009
Author:jnatividad
Status:deferred
Tags:
Comment:
implemented CSV format (bug 17490); use CSS throughout and removed hardcoded html formatting (bug 17491); made delim=comma default for prefabs (shorter queries); support headers=show|hide when generating CSV/plots. When headers=yes, most prefab plots will also automatically show a legend; expanded README
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php
@@ -103,19 +103,23 @@
104104 $this->outputmode = SMW_OUTPUT_HTML;
105105
106106 // 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>');
109111
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>');
112114
 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+
113121 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>');
115123
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 -
120124 // remove potentially dangerous keywords (prefab mode) or ploticus directives (script mode)
121125 // this is an extended check, JUST IN CASE, even though we're invoking ploticus with the noshell security parameter
122126 if ($this->m_ploticusmode === 'prefab') {
@@ -142,7 +146,19 @@
143147 // create result csv file that we pass on to ploticus
144148 $tmpFile = tempnam($ploticusDir, 'srf-');
145149 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
147163 while ( $row = $res->getNext() ) {
148164 $row_items = array();
149165 foreach ($row as $field) {
@@ -158,11 +174,11 @@
159175 }
160176 fclose($fhandle);
161177
162 - // we create a hash based on params and csv file.
 178+ // we create a hash based on params
163179 // this is a great way to see if the params and/or the query result has changed
164180 $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
167183 // in this way, doing file_exists check against hash filename will fail when query result has changed
168184 $hashname .= hash_file('md5',$tmpFile);
169185 }
@@ -174,7 +190,11 @@
175191 @unlink($dataFile);
176192 @rename($tmpFile, $dataFile);
177193 $dataURL = $wgUploadPath . '/ploticus/' . $hashname . '.csv';
 194+ $srficonPath = $wgScriptPath . '/extensions/SemanticResultFormats/Ploticus/icons/';
178195
 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+
179199 $graphFile = $ploticusDir . $hashname . '.' . $this->m_imageformat;
180200 $graphURL = $wgUploadPath . '/ploticus/' . $hashname . '.' . $this->m_imageformat;
181201 $errorFile = $ploticusDir . $hashname . '.err';
@@ -200,13 +220,7 @@
201221 // the need to periodically clean-up graph, csv, script and map files
202222 $errorData = '';
203223 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+
211225 // we set $srfgEnvSettings if specified
212226 $commandline = empty($srfgEnvSettings) ? ' ' : $srfgEnvSettings . ' ';
213227
@@ -232,7 +246,8 @@
233247 $commandline .= wfEscapeShellArg($srfgPloticusPath) .
234248 ($this->m_debug ? ' -debug':' ') .
235249 ' -noshell ' . $sanitized_ploticusparams .
236 - ' data=' . wfEscapeShellArg($dataFile) .
 250+ ($this->mShowHeaders ? ' header=yes':' ') .
 251+ ' delim=comma data=' . wfEscapeShellArg($dataFile) .
237252 ' -' . $this->m_imageformat;
238253
239254 if ($this->m_imageformat == 'drawdump' || $this->m_imageformat == 'drawdumpa' ) {
@@ -263,12 +278,8 @@
264279 }
265280 }
266281
267 - $srficonPath = $wgScriptPath . '/extensions/SemanticResultFormats/Ploticus/icons/';
268 -
269282 //Prepare output. Put everything inside a table
270283 // PLOT ROW - colspan 3
271 - // TODO: use CSS, create stylesheet
272 - // TODO: generate CSS unique id for each SRF-Ploticus occurence
273284 $rtnstr = '<table class="srfptable" id="srfptblid' . $smwgIQRunningNumber . '" cols="3"' .
274285 (empty($this->m_tblwidth) ? ' ' : ' width="'. $this->m_tblwidth . '" ') .
275286 (empty($this->m_tblheight) ? ' ' : ' height="'. $this->m_tblheight . '" ') .
@@ -276,7 +287,7 @@
277288 if (!empty($errorData) && !$this->m_debug) {
278289 // there was an error. We do the not debug check since ploticus by default sends the debug trace to stderr too
279290 // 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">' .
281292 $errorData . '</td></tr>';
282293 } else {
283294 $rtnstr .= '<td class="srfpplot" colspan="3" align="center">';
@@ -354,7 +365,7 @@
355366
356367 // INFOROW - col 2
357368 // 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">';
359370
360371
361372 // INFOROW - TIMESTAMP - col 3
@@ -369,13 +380,13 @@
370381 // DEBUGROW - colspan 3, only display when debug is on
371382 // add link to script or display ploticus cmdline/script
372383 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: ';
374385 if ($this->m_ploticusmode == 'script') {
375386 $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>';
377388 } else {
378389 $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">' .
380391 $commandline . '</td></tr>';
381392 }
382393 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r47263implements bug 17491 - add CSS supportjnatividad14:26, 14 February 2009

Status & tagging log