Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php |
— | — | @@ -16,13 +16,13 @@ |
17 | 17 | protected $m_ploticusparams = ''; |
18 | 18 | protected $m_imageformat = 'png'; |
19 | 19 | protected $m_alttext = 'Ploticus chart'; |
20 | | - protected $m_showcsv = '0'; |
| 20 | + protected $m_showcsv = false; |
21 | 21 | protected $m_ploticusmode = 'prefab'; |
22 | | - protected $m_debug = '0'; |
23 | | - protected $m_liveupdating = '1'; |
| 22 | + protected $m_debug = false; |
| 23 | + protected $m_liveupdating = true; |
24 | 24 | protected $m_updatefrequency = 60; // by default, generate plot only once per minute |
25 | | - protected $m_showtimestamp = '0'; |
26 | | - protected $m_showrefresh = '0'; |
| 25 | + protected $m_showtimestamp = false; |
| 26 | + protected $m_showrefresh = false; |
27 | 27 | |
28 | 28 | protected function readParameters($params, $outputmode) { |
29 | 29 | SMWResultPrinter::readParameters($params, $outputmode); |
— | — | @@ -68,10 +68,12 @@ |
69 | 69 | |
70 | 70 | $this->isHTML = true; |
71 | 71 | |
72 | | - if(!strlen($this->m_ploticusparams)) { |
| 72 | + if(empty($this->m_ploticusparams)) |
73 | 73 | return ('<p><strong>ERROR: <em>ploticusparams</em> required.</strong></p>'); |
74 | | - } |
75 | 74 | |
| 75 | + if(empty($srfgPloticusPath)) |
| 76 | + return ('<p><strong>ERROR: Set $srfgPloticusPath in LocalSettings.php (e.g. $srfgPloticusPath=/usr/bin/ploticus).</strong></p>'); |
| 77 | + |
76 | 78 | // remove potentially dangerous keywords (prefab mode) or ploticus directives (script mode); |
77 | 79 | if ($this->m_ploticusmode == 'prefab') { |
78 | 80 | // we also remove line endings for prefab - this is done for readability so the user can specify the prefab |
— | — | @@ -85,11 +87,10 @@ |
86 | 88 | } |
87 | 89 | $sanitized_ploticusparams = preg_replace($searches, $replaces, $this->m_ploticusparams); |
88 | 90 | |
89 | | - // Create the image directory if it doesnt exist |
| 91 | + // Create the ploticus data directory if it doesn't exist |
90 | 92 | $ploticusDirectory = $wgUploadDirectory . '/ploticus/'; |
91 | | - if (!is_dir($ploticusDirectory)) { |
| 93 | + if (!is_dir($ploticusDirectory)) |
92 | 94 | mkdir($ploticusDirectory, 0777); |
93 | | - } |
94 | 95 | |
95 | 96 | // create result csv file that we pass on to ploticus |
96 | 97 | $tmpFile = tempnam($ploticusDirectory, 'srf-'); |
— | — | @@ -110,16 +111,17 @@ |
111 | 112 | fclose($fhandle); |
112 | 113 | |
113 | 114 | // we create a hash based on params and csv file. |
114 | | - // this is a great way to see if the params and/or the query result has changed |
115 | | - $hashname = md5($this->m_ploticusparams . $this->m_imageformat . $this->m_showcsv . $this->m_ploticusmode . |
| 115 | + // this is a great way to see if the params and/or the query result has changed |
| 116 | + $hashname = hash('md5',$this->m_ploticusparams . $this->m_imageformat . $this->m_showcsv . $this->m_ploticusmode . |
116 | 117 | $this->m_liveupdating . $this->m_updatefrequency . $this->m_showtimestamp); |
117 | 118 | if ($this->m_liveupdating) { |
118 | 119 | // only include contents of result csv in hash when liveupdating is on |
119 | 120 | // in this way, doing file_exists check against hash filename will fail when query result has changed |
120 | | - $hashname .= md5_file($tmpFile); |
| 121 | + $hashname .= hash_file('md5',$tmpFile); |
121 | 122 | } |
122 | 123 | |
123 | 124 | $dataFile = $ploticusDirectory . $hashname . '.csv'; |
| 125 | + @unlink($dataFile); |
124 | 126 | rename($tmpFile, $dataFile); |
125 | 127 | |
126 | 128 | $graphFile = $ploticusDirectory . $hashname . '.' . $this->m_imageformat; |
— | — | @@ -154,7 +156,8 @@ |
155 | 157 | } |
156 | 158 | |
157 | 159 | // we set GDFONTPATH if specified |
158 | | - $commandline = strlen($srfgGDFontPath) ? 'GDFONTPATH=' . $srfgGDFontPath . ' ' : ' '; |
| 160 | + $commandline = empty($srfgGDFontPath) ? ' ' : 'GDFONTPATH=' . $srfgGDFontPath . ' '; |
| 161 | + |
159 | 162 | if ($this->m_ploticusmode == 'script') { |
160 | 163 | // Script mode. Search for special strings in ploticusparam |
161 | 164 | // and replace it with actual values. (case-sensitive) |
— | — | @@ -192,7 +195,7 @@ |
193 | 196 | $errorData = file_get_contents($errorFile); |
194 | 197 | unlink($errorFile); |
195 | 198 | |
196 | | - $graphLastGenerated = filemtime($graphFile); |
| 199 | + $graphLastGenerated = time(); // faster than doing filemtime |
197 | 200 | |
198 | 201 | if($this->m_ploticusmode == 'script' && !$this->m_debug) { |
199 | 202 | unlink($scriptFile); |
— | — | @@ -201,7 +204,7 @@ |
202 | 205 | |
203 | 206 | //Prepare output |
204 | 207 | $rtnstr = '<table cols="3"><tr>'; |
205 | | - if (strlen($errorData)) { |
| 208 | + if (!empty($errorData)) { |
206 | 209 | // there was an error |
207 | 210 | $rtnstr .= '<th colspan="3"><strong>Error processing ploticus data:</strong></th></tr><tr><td colspan="3"' . |
208 | 211 | $errorData . '</td></tr>'; |
— | — | @@ -216,6 +219,7 @@ |
217 | 220 | } else { |
218 | 221 | $rtnstr .= '<td colspan="3"><img src="' . $graphURL . '" alt="' . $this->alttext .'"></td></tr>'; |
219 | 222 | } |
| 223 | + |
220 | 224 | } |
221 | 225 | $rtnstr .= '<tr>'; |
222 | 226 | // if showcsv is on, add link to data file (CSV) |
— | — | @@ -246,7 +250,7 @@ |
247 | 251 | |
248 | 252 | $rtnstr .= '</tr></table>'; |
249 | 253 | |
250 | | - // if debug is on, add link to script or display prefab cmdline |
| 254 | + // if debug is on, add link to script or display ploticus cmdline/script |
251 | 255 | if ($this->m_debug) { |
252 | 256 | if ($this->m_ploticusmode == 'script') { |
253 | 257 | $rtnstr .= '<p><strong>DEBUG: <a href="' . $scriptURL . '" target="_blank">SCRIPT</a></strong></p>'; |
— | — | @@ -254,7 +258,7 @@ |
255 | 259 | $rtnstr .= '<p><strong>DEBUG: PREFAB</strong></p><table width="100%"><tr>' . $commandline . '</tr></table>'; |
256 | 260 | } |
257 | 261 | } |
| 262 | + |
258 | 263 | return ($rtnstr); |
259 | 264 | } |
260 | 265 | } |
261 | | - |
Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus_cleanCache.php |
— | — | @@ -0,0 +1,70 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Purges old/orphan plots,maps,csvs from the ploticus cache directory. |
| 5 | + * |
| 6 | + * |
| 7 | + * Note: if SMW is not installed in its standard path under ./extensions |
| 8 | + * then the MW_INSTALL_PATH environment variable must be set. |
| 9 | + * See README in the maintenance directory. |
| 10 | + * |
| 11 | + * Usage: |
| 12 | + * php SRF_Ploticus_cleanCache.php [options...] |
| 13 | + * |
| 14 | + * -a <age in hours> Override $srfgPloticusCacheAge setting and purge files of this age and greater |
| 15 | + * -v Be verbose about the progress. |
| 16 | + * |
| 17 | + * @author Joel Natividad |
| 18 | + * @file |
| 19 | + * @ingroup SRFMaintenance |
| 20 | + */ |
| 21 | + |
| 22 | +$optionsWithArgs = array('a'); |
| 23 | + |
| 24 | +require_once ( getenv('MW_INSTALL_PATH') !== false |
| 25 | + ? getenv('MW_INSTALL_PATH')."/maintenance/commandLine.inc" |
| 26 | + : dirname( __FILE__ ) . '/../../../maintenance/commandLine.inc' ); |
| 27 | + |
| 28 | +global $wgUploadDirectory, $srfgPloticusCacheAgeHours; |
| 29 | + |
| 30 | +if ( !empty( $options['a'] ) ) { |
| 31 | + $fileAge = intval($options['a']) * 3600; // 60 secs * 60 mins |
| 32 | +} else { |
| 33 | + if ( isset($srfgPloticusCacheAgeHours) ) { |
| 34 | + $fileAge = $srfgPloticusCacheAgeHours * 3600; |
| 35 | + } else { |
| 36 | + $fileAge = 604800; // if $srfgPloticusCacheAgeHours is not set in LocalSettings.php defaults to 7 days |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +$verbose = array_key_exists( 'v', $options ); |
| 41 | + |
| 42 | +if ($fileAge <= 0) { |
| 43 | + if ($verbose) |
| 44 | + echo "Ploticus cache cleaning disabled.\n"; |
| 45 | + return; |
| 46 | +} |
| 47 | + |
| 48 | +$ploticusDirectory = $wgUploadDirectory . '/ploticus'; |
| 49 | +$deletecount = 0; |
| 50 | + |
| 51 | +if( $dirhandle = @opendir($ploticusDirectory) ) { |
| 52 | + while( false !== ($filename = readdir($dirhandle)) ) { |
| 53 | + if( $filename != '.' && $filename != '..' ) { |
| 54 | + $filename = $ploticusDirectory . '/' . $filename; |
| 55 | + |
| 56 | + if( @filemtime($filename) < (time()-$fileAge) ) { |
| 57 | + if ($verbose) |
| 58 | + echo "deleting $filename...\n"; |
| 59 | + if (@unlink($filename)) |
| 60 | + $deletecount ++; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} else { |
| 65 | + if ($verbose) |
| 66 | + echo "$ploticusDirectory not found... Aborting...\n"; |
| 67 | +} |
| 68 | + |
| 69 | +@closedir($dirhandle); |
| 70 | +if ($verbose) |
| 71 | + echo "$deletecount files successfully deleted from Ploticus cache.\n"; |
Index: trunk/extensions/SemanticResultFormats/Ploticus/README |
— | — | @@ -11,14 +11,31 @@ |
12 | 12 | any other semantic result printer (refer to the separate README for that). |
13 | 13 | |
14 | 14 | LocalSettings.php Modifications: |
15 | | - * $srfgPloticusPath (default: /usr/bin/ploticus) |
| 15 | + * $srfgPloticusPath (REQUIRED) |
| 16 | + - path to ploticus executable. (typically /usr/bin/ploticus) |
16 | 17 | |
17 | 18 | * $srfgGDFontPath (default: '') |
18 | 19 | - if you plan to use alternate TrueType fonts in your plot. |
19 | 20 | (e.g. '/usr/share/fonts/truetype/freefont:/usr/share/fonts/truetype/msttcorefonts') |
| 21 | + |
| 22 | + * $srfgPloticusCacheAgeHours (default: 168 - 7 days) |
| 23 | + - number of hours cached plot files are stored in the ploticus cache before they are forcibly purged regardless of liveupdating setting. |
| 24 | + Set to 0 if you prefer to purge the cache manually. |
| 25 | + This is required, especially with liveupdating plots, since changing query results will "orphan" plot cache files as the computed hash filename |
| 26 | + will change even if the query definition itself is NOT changed. |
20 | 27 | |
| 28 | + This setting will only work if the SRF_Ploticus_cleanCache.php script is scheduled on cron. Typically, it should only be run once a day. |
| 29 | + For example, run cleanCache at midnight, logging results to /var/log/ploticus_cleanCache.log: |
| 30 | + |
| 31 | + 0 0 * * * /usr/bin/php /var/www/wiki/extension/SemanticResultFormats/Ploticus/SRF_Ploticus_cleanCache.php -v 2>&1 > /var/log/ploticus_cleanCache.log |
| 32 | + |
| 33 | + If you're having problems running cleanCache, set the MW_INSTALL_PATH environment variable. E.g. |
| 34 | + |
| 35 | + 0 0 * * * MW_INSTALL_PATH=/var/www/wiki /usr/bin/php /var/www/wiki/extension/SemanticResultFormats/Ploticus/SRF_Ploticus_cleanCache.php -v 2>&1 > /var/log/ploticus_cleanCache.log |
| 36 | + |
21 | 37 | * $wgEnableUploads (default: false - this is a MediaWiki setting) |
22 | | - - must be true. Be sure the upload directory has the proper permissions. |
| 38 | + - must be true. Be sure the upload directory has the proper permissions. If you wish to use SRF-Ploticus without enabling uploads, |
| 39 | + you can create the 'ploticus' directory under the MW upload directory with permission 777. |
23 | 40 | (see http://www.mediawiki.org/wiki/Manual:$wgUploadPath) |
24 | 41 | |
25 | 42 | == Configuration and Usage == |
— | — | @@ -98,4 +115,4 @@ |
99 | 116 | Typically, Imagemaps "links/labels" should be set to resolve to "Article URL/Article Name". |
100 | 117 | |
101 | 118 | For up-to-date documentation and examples please refer to |
102 | | -http://semantic-mediawiki.org/wiki/Help:Ploticus_format |
| 119 | +http://semantic-mediawiki.org/wiki/Help:Ploticus_format |
\ No newline at end of file |