r46392 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46391‎ | r46392 | r46393 >
Date:21:46, 27 January 2009
Author:jnatividad
Status:deferred
Tags:
Comment:
used faster hash function; squashed bug causing orphan csv files; more consistent usage of booleans; added maintenance script to clean ploticus cache; expanded README
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Ploticus/README (modified) (history)
  • /trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus_cleanCache.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus.php
@@ -16,13 +16,13 @@
1717 protected $m_ploticusparams = '';
1818 protected $m_imageformat = 'png';
1919 protected $m_alttext = 'Ploticus chart';
20 - protected $m_showcsv = '0';
 20+ protected $m_showcsv = false;
2121 protected $m_ploticusmode = 'prefab';
22 - protected $m_debug = '0';
23 - protected $m_liveupdating = '1';
 22+ protected $m_debug = false;
 23+ protected $m_liveupdating = true;
2424 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;
2727
2828 protected function readParameters($params, $outputmode) {
2929 SMWResultPrinter::readParameters($params, $outputmode);
@@ -68,10 +68,12 @@
6969
7070 $this->isHTML = true;
7171
72 - if(!strlen($this->m_ploticusparams)) {
 72+ if(empty($this->m_ploticusparams))
7373 return ('<p><strong>ERROR: <em>ploticusparams</em> required.</strong></p>');
74 - }
7574
 75+ if(empty($srfgPloticusPath))
 76+ return ('<p><strong>ERROR: Set $srfgPloticusPath in LocalSettings.php (e.g. $srfgPloticusPath=/usr/bin/ploticus).</strong></p>');
 77+
7678 // remove potentially dangerous keywords (prefab mode) or ploticus directives (script mode);
7779 if ($this->m_ploticusmode == 'prefab') {
7880 // we also remove line endings for prefab - this is done for readability so the user can specify the prefab
@@ -85,11 +87,10 @@
8688 }
8789 $sanitized_ploticusparams = preg_replace($searches, $replaces, $this->m_ploticusparams);
8890
89 - // Create the image directory if it doesnt exist
 91+ // Create the ploticus data directory if it doesn't exist
9092 $ploticusDirectory = $wgUploadDirectory . '/ploticus/';
91 - if (!is_dir($ploticusDirectory)) {
 93+ if (!is_dir($ploticusDirectory))
9294 mkdir($ploticusDirectory, 0777);
93 - }
9495
9596 // create result csv file that we pass on to ploticus
9697 $tmpFile = tempnam($ploticusDirectory, 'srf-');
@@ -110,16 +111,17 @@
111112 fclose($fhandle);
112113
113114 // 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 .
116117 $this->m_liveupdating . $this->m_updatefrequency . $this->m_showtimestamp);
117118 if ($this->m_liveupdating) {
118119 // only include contents of result csv in hash when liveupdating is on
119120 // 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);
121122 }
122123
123124 $dataFile = $ploticusDirectory . $hashname . '.csv';
 125+ @unlink($dataFile);
124126 rename($tmpFile, $dataFile);
125127
126128 $graphFile = $ploticusDirectory . $hashname . '.' . $this->m_imageformat;
@@ -154,7 +156,8 @@
155157 }
156158
157159 // we set GDFONTPATH if specified
158 - $commandline = strlen($srfgGDFontPath) ? 'GDFONTPATH=' . $srfgGDFontPath . ' ' : ' ';
 160+ $commandline = empty($srfgGDFontPath) ? ' ' : 'GDFONTPATH=' . $srfgGDFontPath . ' ';
 161+
159162 if ($this->m_ploticusmode == 'script') {
160163 // Script mode. Search for special strings in ploticusparam
161164 // and replace it with actual values. (case-sensitive)
@@ -192,7 +195,7 @@
193196 $errorData = file_get_contents($errorFile);
194197 unlink($errorFile);
195198
196 - $graphLastGenerated = filemtime($graphFile);
 199+ $graphLastGenerated = time(); // faster than doing filemtime
197200
198201 if($this->m_ploticusmode == 'script' && !$this->m_debug) {
199202 unlink($scriptFile);
@@ -201,7 +204,7 @@
202205
203206 //Prepare output
204207 $rtnstr = '<table cols="3"><tr>';
205 - if (strlen($errorData)) {
 208+ if (!empty($errorData)) {
206209 // there was an error
207210 $rtnstr .= '<th colspan="3"><strong>Error processing ploticus data:</strong></th></tr><tr><td colspan="3"' .
208211 $errorData . '</td></tr>';
@@ -216,6 +219,7 @@
217220 } else {
218221 $rtnstr .= '<td colspan="3"><img src="' . $graphURL . '" alt="' . $this->alttext .'"></td></tr>';
219222 }
 223+
220224 }
221225 $rtnstr .= '<tr>';
222226 // if showcsv is on, add link to data file (CSV)
@@ -246,7 +250,7 @@
247251
248252 $rtnstr .= '</tr></table>';
249253
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
251255 if ($this->m_debug) {
252256 if ($this->m_ploticusmode == 'script') {
253257 $rtnstr .= '<p><strong>DEBUG: <a href="' . $scriptURL . '" target="_blank">SCRIPT</a></strong></p>';
@@ -254,7 +258,7 @@
255259 $rtnstr .= '<p><strong>DEBUG: PREFAB</strong></p><table width="100%"><tr>' . $commandline . '</tr></table>';
256260 }
257261 }
 262+
258263 return ($rtnstr);
259264 }
260265 }
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 @@
1212 any other semantic result printer (refer to the separate README for that).
1313
1414 LocalSettings.php Modifications:
15 - * $srfgPloticusPath (default: /usr/bin/ploticus)
 15+ * $srfgPloticusPath (REQUIRED)
 16+ - path to ploticus executable. (typically /usr/bin/ploticus)
1617
1718 * $srfgGDFontPath (default: '')
1819 - if you plan to use alternate TrueType fonts in your plot.
1920 (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.
2027
 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+
2137 * $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.
2340 (see http://www.mediawiki.org/wiki/Manual:$wgUploadPath)
2441
2542 == Configuration and Usage ==
@@ -98,4 +115,4 @@
99116 Typically, Imagemaps "links/labels" should be set to resolve to "Article URL/Article Name".
100117
101118 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

Status & tagging log