Timeline.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * EasyTimeline - Timeline extension
  4. * To use, include this file from your LocalSettings.php
  5. * To configure, set members of $wgTimelineSettings after the inclusion
  6. *
  7. * @file
  8. * @ingroup Extensions
  9. * @author Erik Zachte <xxx@chello.nl (nospam: xxx=epzachte)>
  10. * @license GNU General Public License version 2
  11. * @link http://www.mediawiki.org/wiki/Extension:EasyTimeline Documentation
  12. */
  13. class TimelineSettings {
  14. var $ploticusCommand, $perlCommand;
  15. // Update this timestamp to force older rendered timelines
  16. // to be generated when the page next gets rendered.
  17. // Can help to resolve old image-generation bugs.
  18. var $epochTimestamp = '20010115000000';
  19. // Path to the EasyTimeline.pl perl file, which is used to actually generate the timelines.
  20. var $timelineFile;
  21. };
  22. $wgTimelineSettings = new TimelineSettings;
  23. $wgTimelineSettings->ploticusCommand = "/usr/bin/ploticus";
  24. $wgTimelineSettings->perlCommand = "/usr/bin/perl";
  25. $wgTimelineSettings->timelineFile = "$IP/extensions/timeline/EasyTimeline.pl";
  26. if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
  27. $wgHooks['ParserFirstCallInit'][] = 'wfTimelineExtension';
  28. } else {
  29. $wgExtensionFunctions[] = 'wfTimelineExtension';
  30. }
  31. $wgExtensionCredits['parserhook'][] = array(
  32. 'name' => 'EasyTimeline',
  33. 'author' => 'Erik Zachte',
  34. 'url' => 'http://www.mediawiki.org/wiki/Extension:EasyTimeline',
  35. 'svn-date' => '$LastChangedDate: 2009-01-26 23:46:13 +0000 (Mon, 26 Jan 2009) $',
  36. 'svn-revision' => '$LastChangedRevision: 46305 $',
  37. 'description' => 'Adds <tt>&lt;timeline&gt;</tt> tag to create timelines',
  38. 'descriptionmsg' => 'timeline-desc',
  39. );
  40. $wgExtensionMessagesFiles['Timeline'] = dirname(__FILE__) . '/Timeline.i18n.php';
  41. function wfTimelineExtension() {
  42. global $wgParser;
  43. $wgParser->setHook( 'timeline', 'renderTimeline' );
  44. return true;
  45. }
  46. function renderTimeline( $timelinesrc ){
  47. global $wgUploadDirectory, $wgUploadPath, $IP, $wgTimelineSettings, $wgArticlePath, $wgTmpDirectory;
  48. $hash = md5( $timelinesrc );
  49. $dest = $wgUploadDirectory."/timeline/";
  50. if ( ! is_dir( $dest ) ) { mkdir( $dest, 0777 ); }
  51. if ( ! is_dir( $wgTmpDirectory ) ) { mkdir( $wgTmpDirectory, 0777 ); }
  52. $fname = $dest . $hash;
  53. $previouslyFailed = file_exists( $fname.".err" );
  54. $previouslyRendered = file_exists( $fname.".png" );
  55. $expired = $previouslyRendered &&
  56. ( filemtime( $fname.".png" ) <
  57. wfTimestamp( TS_UNIX, $wgTimelineSettings->epochTimestamp ) );
  58. if ( $expired || ( !$previouslyRendered && !$previouslyFailed ) ){
  59. $handle = fopen($fname, "w");
  60. fwrite($handle, $timelinesrc);
  61. fclose($handle);
  62. $cmdline = wfEscapeShellArg( $wgTimelineSettings->perlCommand, $wgTimelineSettings->timelineFile ) .
  63. " -i " . wfEscapeShellArg( $fname ) . " -m -P " . wfEscapeShellArg( $wgTimelineSettings->ploticusCommand ) .
  64. " -T " . wfEscapeShellArg( $wgTmpDirectory ) . " -A " . wfEscapeShellArg( $wgArticlePath );
  65. $ret = `{$cmdline}`;
  66. unlink($fname);
  67. if ( $ret == "" ) {
  68. // Message not localized, only relevant during install
  69. return "<div id=\"toc\"><tt>Timeline error: Executable not found. Command line was: {$cmdline}</tt></div>";
  70. }
  71. }
  72. @$err = file_get_contents( $fname.".err" );
  73. if ( $err != "" ) {
  74. $txt = "<div id=\"toc\"><tt>$err</tt></div>";
  75. } else {
  76. @$map = file_get_contents( $fname.".map" );
  77. if( substr( php_uname(), 0, 7 ) == "Windows" ) {
  78. $ext = "gif";
  79. } else {
  80. $ext = "png";
  81. }
  82. $url = "{$wgUploadPath}/timeline/{$hash}.{$ext}";
  83. $txt = "<map name=\"$hash\">{$map}</map>".
  84. "<img usemap=\"#{$hash}\" src=\"$url\">";
  85. if( $expired ) {
  86. // Replacing an older file, we may need to purge the old one.
  87. global $wgUseSquid;
  88. if( $wgUseSquid ) {
  89. $u = new SquidUpdate( array( $url ) );
  90. $u->doUpdate();
  91. }
  92. }
  93. }
  94. return $txt;
  95. }