[Clipart] r43 - makerelease

noreply at openclipart.org noreply at openclipart.org
Sun Jun 28 02:35:50 PDT 2009


Author: rejon
Date: Sun Jun 28 09:35:49 2009
New Revision: 43

Log:
added these classes further cleaning things up

Added:
   makerelease/HTMLPreviewBuilder.php
   makerelease/opsysDependendEntity.php

Added: makerelease/HTMLPreviewBuilder.php
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ makerelease/HTMLPreviewBuilder.php	Sun Jun 28 09:35:49 2009	(r43)
@@ -0,0 +1,291 @@
+<?php
+
+/**
+ * @author Jochen Stärk <jstaerk at usegroup.de>
+ * @author Jon Phillips <jon at rejon.org>
+ * @version 1.2
+ * @date 2009-06-28
+ * @license GPL 2.0
+ * 
+ * The contents of this file are available under a CC-GNU-GPL license:
+ *
+ * http://creativecommons.org/licenses/GPL/2.0/
+ *
+ * A copy of the full license can be found as part of this
+ * distribution in the file LICENSE.TXT.
+ * 
+ * You may use this software in accordance with the
+ * terms of that license. You agree that you are solely 
+ * responsible for your use of this software and you
+ * represent and warrant to the copyright holders that your use
+ * of the software will comply with the CC-GNU-GPL.
+ *
+ * 
+ * This php shell script downloads the latest openclipart package
+ * 
+ */    
+
+/** 
+ * @class HTMLPreviewBuilder generate HTML previews.
+ */
+class HTMLPreviewBuilder extends opsysDependendEntity {
+	private $images;
+	private $logger;
+	private $destinationPath;
+	private $thumbsProPage=50;
+	private $currentPageIndex=0;
+	private $numPages=0;
+	private $templateLines=array();
+	private $version;
+	private $pagesLinked;
+	
+	public function __construct(logger $logger, $version) {
+		$this->images=array();
+		$this->logger=$logger;
+		$this->version=$version;
+		$this->checkOpsys();		
+	}
+	
+	public function addImage($filepath) {
+		$this->images[]=$filepath;
+	}
+	
+	private function dir_copy($srcdir, $dstdir, $offset = '', $verbose = false)
+	{// source: http://de3.php.net/copy#89299
+	    // A function to copy files from one directory to another one, including subdirectories and
+	    // nonexisting or newer files. Function returns number of files copied.
+	    // This function is PHP implementation of Windows xcopy  A:\dir1\* B:\dir2 /D /E /F /H /R /Y
+	    // Syntaxis: [$returnstring =] dircopy($sourcedirectory, $destinationdirectory [, $offset] [, $verbose]);
+	    // Example: $num = dircopy('A:\dir1', 'B:\dir2', 1);
+	
+	    // Original by SkyEye.  Remake by AngelKiha.
+	    // Linux compatibility by marajax.
+	    // ([danbrown AT php DOT net): *NIX-compatibility noted by Belandi.]
+	    // Offset count added for the possibilty that it somehow miscounts your files.  This is NOT required.
+	    // Remake returns an explodable string with comma differentiables, in the order of:
+	    // Number copied files, Number of files which failed to copy, Total size (in bytes) of the copied files,
+	    // and the files which fail to copy.  Example: 5,2,150000,\SOMEPATH\SOMEFILE.EXT|\SOMEPATH\SOMEOTHERFILE.EXT
+	    // If you feel adventurous, or have an error reporting system that can log the failed copy files, they can be
+	    // exploded using the | differentiable, after exploding the result string.
+	    //
+	    if(!isset($offset)) $offset=0;
+	    $num = 0;
+	    $fail = 0;
+	    $sizetotal = 0;
+	    $fifail = '';
+	    if(!is_dir($dstdir)) mkdir($dstdir);
+	    if($curdir = opendir($srcdir)) {
+	        while($file = readdir($curdir)) {
+	            if($file != '.' && $file != '..') {
+	//                $srcfile = $srcdir . '\\' . $file;    # deleted by marajax
+	//                $dstfile = $dstdir . '\\' . $file;    # deleted by marajax
+	                $srcfile = $srcdir . '/' . $file;    # added by marajax
+	                $dstfile = $dstdir . '/' . $file;    # added by marajax
+	                if(is_file($srcfile)) {
+	                    if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
+	                    if($ow > 0) {
+	                        if($verbose) echo "Copying '$srcfile' to '$dstfile'...<br />";
+	                        if(copy($srcfile, $dstfile)) {
+	                            touch($dstfile, filemtime($srcfile)); $num++;
+	                            chmod($dstfile, 0777);    # added by marajax
+	                            $sizetotal = ($sizetotal + filesize($dstfile));
+	                            if($verbose) echo "OK\n";
+	                        }
+	                        else {
+	                            echo "Error: File '$srcfile' could not be copied!<br />\n";
+	                            $fail++;
+	                            $fifail = $fifail.$srcfile.'|';
+	                        }
+	                    }
+	                }
+	                else if(is_dir($srcfile)) {
+	                    $res = explode(',',$ret);
+	//                    $ret = dircopy($srcfile, $dstfile, $verbose); # deleted by patrick
+	                    $ret = $this->dir_copy($srcfile, $dstfile, $verbose); # added by patrick
+	                    $mod = explode(',',$ret);
+	                    $imp = array($res[0] + $mod[0],$mod[1] + $res[1],$mod[2] + $res[2],$mod[3].$res[3]);
+	                    $ret = implode(',',$imp);
+	                }
+	            }
+	        }
+	        closedir($curdir);
+	    }
+	    $red = explode(',',$ret);
+	    $ret = ($num + $red[0]).','.(($fail-$offset) + $red[1]).','.($sizetotal + $red[2]).','.$fifail.$red[3];
+	    return $ret;
+	} 
+	
+	private function copyTemplateFiles() {
+		$this->dir_copy("template",$this->destinationPath);		
+	}
+	
+	private function getFilenameForIndex($idx) {
+		$filenameSuffix="_".$idx;
+		if ($idx==1) {
+			$filenameSuffix="";
+		}
+		return "index".$filenameSuffix.".html";
+	}
+
+	private function calculateLink($fromWhere, $distance, $ascending) {
+		$target=null;
+		if ($fromWhere%$distance==0) {
+			if ($ascending) {
+				$target=$fromWhere+$distance;
+			} else {
+				$target=$fromWhere-$distance;	
+			}
+				
+		} else {
+			if ($ascending) {
+				$target=ceil($fromWhere/$distance)*$distance;
+			} else {
+				$target=floor($fromWhere/$distance)*$distance;
+			}
+			
+		}
+		return $target;
+		
+	}
+	/**
+	 * will return 1 for OK, 0 for try again and -1 for not ok
+	 * */
+	private function checkAndInsertLink($toWhere) {
+		$possible=($toWhere>0)&&($toWhere<=$this->numPages);
+		if (!$possible) {
+			return -1;			
+		} else if (in_array($toWhere, $this->pagesLinked)) {
+			return 0;
+		} else {
+			$this->pagesLinked[]=$toWhere;
+			return 1;
+		}
+	}
+	
+	private function linkFarAway($distance, $ascending) {
+		//can only link to e.g. the last 10 if page number >10
+
+		$target=$this->calculateLink($this->currentPageIndex, $distance, $ascending);
+		$successful=$this->checkAndInsertLink($target);
+		while ($successful===0) {
+			// was already there
+			if ($ascending) {
+				$target+=$distance;
+			} else {
+				$target-=$distance;
+				
+			}
+			$successful=$this->checkAndInsertLink($target);
+		} 
+		
+		
+	}
+	/**
+	 * Copies the dependencies (.css, .js, .jpg files) to the directory specified
+	 * in $path and generates the index .html file and the .html files for all pages
+	 * */
+	public function writeHTML($path) {
+		$this->destinationPath=$path;
+		$this->logger->shoutNewStatus("Generating HTML");
+		$this->copyTemplateFiles();
+		$this->templateLines=file("template".$this->opsysDirSep."index.html");
+		unlink($path.$this->opsysDirSep."index.html");
+		$this->numPages	=ceil(count($this->images)/$this->thumbsProPage);
+		for($pageStartIndex=0; $pageStartIndex<count($this->images); $pageStartIndex+=$this->thumbsProPage) {
+			$this->currentPageIndex++;
+			
+			$f=fopen($path.$this->opsysDirSep.$this->getFilenameForIndex($this->currentPageIndex), "w");
+			
+			$contentHTML="";
+			for ($thumbIndex=$pageStartIndex; (($thumbIndex<count($this->images))&&($thumbIndex<$pageStartIndex+$this->thumbsProPage));$thumbIndex++) {
+				$image=$this->images[$thumbIndex];
+				$svgFile=substr($image,strlen($path));
+				$svgFile=str_replace($this->opsysDirSep,"/",$svgFile);
+				$dirParts=explode($this->opsysDirSep,$image);
+				$artist="undefined";
+				$artistDisplayName=$artist;
+				$title="undefined";
+				if (count($dirParts)>2) {
+					// the lower most directory name is also the name of the artist
+					$artist=$dirParts[count($dirParts)-2];
+					$artistDisplayName=str_replace("_"," ",$artist);
+					
+				}
+				if (count($dirParts)>1) {
+					// the lower most directory name is also the name of the artist
+					$title=substr($dirParts[count($dirParts)-1], strlen($artist)+1);
+					$title=str_replace("_"," ", $title);
+					$title=substr($title,0,strrpos($title, "."));
+					
+				}
+				$pngFile=str_replace(".svg",".png",$svgFile);
+				$contentHTML.="		
+				<div class='r-img'>
+					<div>
+						<a href='$svgFile' title='$title'><img src='$pngFile' alt='#'></a>
+					</div>
+					<h4><a href='$svgFile'>$title</a></h4>
+					<p>by <a href='http://openclipart.org/media/people/$artist'>$artistDisplayName</a></p>
+				</div> \n";
+			}
+			// page navi: e.g. for page 136 we want links like
+			// 1 100 130 134 135 *136* 137 138 140 200 236
+			// means: the current page is always visible w/o link
+			// if the page>1 insert link to page one
+			// if the page>2 insert link to previous page as well
+			// if the page>3 insert link to pre-previous page as well
+			
+			// if the page>50, insert link to last 50er (floor(page/50))*50 and, if available, to the next 50er
+			// if the page%100>0 insert link to last 10er (floor(page%100/10))*10 and, if available, to the next 10er
+
+			// if the page<last page insert link to last page
+			
+			$this->pagesLinked=array();
+			// first: link first, last, next 2 and previous 2 pages
+			$this->checkAndInsertLink(1);
+			$this->checkAndInsertLink($this->numPages);
+			$this->checkAndInsertLink($this->currentPageIndex);
+			$this->checkAndInsertLink($this->currentPageIndex-1);
+			$this->checkAndInsertLink($this->currentPageIndex-2);
+			$this->checkAndInsertLink($this->currentPageIndex+1);
+			$this->checkAndInsertLink($this->currentPageIndex+2);
+			
+			// now find out what to do with 10ers and 50ers, and keep in mind 
+			// next 10er or 50er page might already be linked via the "next two"
+			// pages e.g. on pg 48 (49,50...)
+
+			// link to next 50er have lower priority than to next 10er, i.e. on 99 link to 100 (10) and 150 
+			$this->linkFarAway(10,false); 			
+			$this->linkFarAway(10,true); 			
+			$this->linkFarAway(50,false); 			
+			$this->linkFarAway(50,true); 			
+			
+			
+			$pageNavi="";
+			sort($this->pagesLinked);
+			foreach ($this->pagesLinked as $pageLink) {
+				$prefix="<li><a href='".$this->getFilenameForIndex($pageLink)."'>";
+				$suffix="</a></li>";
+				if ($pageLink==$this->currentPageIndex) {
+					$prefix="<li id='active'>";
+					$suffix="</li>";
+				}
+				$pageNavi.=$prefix.$pageLink.$suffix;
+			}
+			
+			foreach ($this->templateLines as $indexLine) {
+				$indexLine=str_replace("[title]","OpenClipart $this->version",$indexLine);
+				$indexLine=str_replace("[subtitle]","Page {$this->currentPageIndex} of {$this->numPages}",$indexLine);
+				$indexLine=str_replace("[pages]",$pageNavi,$indexLine);
+				$indexLine=str_replace("[thumbnails]",$contentHTML,$indexLine);
+				fwrite($f, $indexLine);
+			}
+		}
+		
+		
+		
+		fclose($f);
+	}
+}
+
+?>

Added: makerelease/opsysDependendEntity.php
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ makerelease/opsysDependendEntity.php	Sun Jun 28 09:35:49 2009	(r43)
@@ -0,0 +1,116 @@
+<?php
+
+/**
+ * @author Jochen Stärk <jstaerk at usegroup.de>
+ * @author Jon Phillips <jon at rejon.org>
+ * @version 1.2
+ * @date 2009-06-28
+ * @license GPL 2.0
+ * 
+ * The contents of this file are available under a CC-GNU-GPL license:
+ *
+ * http://creativecommons.org/licenses/GPL/2.0/
+ *
+ * A copy of the full license can be found as part of this
+ * distribution in the file LICENSE.TXT.
+ * 
+ * You may use this software in accordance with the
+ * terms of that license. You agree that you are solely 
+ * responsible for your use of this software and you
+ * represent and warrant to the copyright holders that your use
+ * of the software will comply with the CC-GNU-GPL.
+ * 
+ * This php shell script downloads the latest openclipart package
+ * 
+ */    
+
+/**
+ * @class opsysDependedEntity A class to vcheck system and set settings.
+ */
+class opsysDependendEntity{
+	protected $opsys; // 1=windows, 0=linux, adjusted in checkOpsys
+	protected $opsysQuotes;//quotes around directories with spaces (and commands with multiple directories with spaces), none in linux, " in windows, adjusted in checkOpsys
+	protected $opsysDirSep;//delimiter between directories and subdirectories or files, "/" in linux, "\" in windows, adjusted in checkOpsys
+	protected $path_wget; // the full path and file name of the "wget" executable. Default assigned in checkOpsys, user will be prompted if not found there
+	protected $path_tar;  // where "wget" is found. Default assigned in..., user will be prompted...
+	protected $path_bunzip;
+	protected $path_gzip;
+	protected $path_bzip;
+	protected $path_zip;
+	protected $path_inkscape;
+	protected $path_rsync;
+	
+	public function checkOpsys() {
+		if (file_exists("/etc")) {
+			//unixoid opsys, lets assume it's linuxoid... 
+			$this->opsys=0;
+			$this->opsysQuotes="";
+			$this->opsysDirSep="/";
+
+
+			$this->path_wget="/usr/bin/wget";
+			$this->path_tar="/bin/tar";
+			$this->path_bunzip="/bin/bunzip2";
+			$this->path_gzip="/bin/gzip";
+			$this->path_bzip="/bin/bzip2";
+			$this->path_zip="/usr/bin/zip";
+			$this->path_inkscape="/usr/bin/inkscape";
+			$this->path_rsync="/usr/bin/rsync";
+		} else {
+			$this->opsys=1;
+			$this->opsysQuotes="\"";
+			$this->opsysDirSep="\\";
+
+			$this->path_wget="c:\\program files\\gnu\\gnuwin32\\bin\\wget.exe";
+			$this->path_tar="c:\\program files\\gnu\\gnuwin32\\bin\\tar.exe";
+			$this->path_bunzip="c:\\program files\\gnu\\gnuwin32\\bin\\bunzip2.exe";
+			$this->path_gzip="c:\\program files\\gnu\\gnuwin32\\bin\\gzip.exe";
+			$this->path_bzip="c:\\program files\\gnu\\gnuwin32\\bin\\bzip2.exe";
+			$this->path_zip="c:\\program files\\gnu\\gnuwin32\\bin\\zip.exe";
+			$this->path_inkscape="c:\\program files\\inkscape\\inkscape.exe";
+			$this->path_rsync="c:\\program files\\cwRsync\\bin\\rsync.exe";
+	
+		}
+	}		
+	
+	
+}
+
+/**
+ * @class logger does some logging
+ */
+class logger extends opsysDependendEntity {
+	private $logfile; // the file handle of the open lastrun.log file
+	private $startTime; // the microtime (true) when the run started. Used to calculate runtime in status messages 
+
+	public function __construct() {
+		$this->logfile=fopen("lastrun.log", "w");
+		$this->startTime=microtime(true);
+		$this->checkOpsys();
+		
+	}
+	/**
+	 * this function will give an echo and log on the logfile
+	 * */
+	function shout($message) {
+		echo $message;
+		fwrite($this->logfile, $message);
+		
+	}
+	
+	function shoutNewStatus($message) {
+		$time=microtime(true);
+		$runTime=$time-$this->startTime;
+		$this->shout("===$message===\n");
+		$this->shout("@$runTime\n");
+	}
+	public function __destruct() {
+		$this->shoutNewStatus("done");
+	
+		fclose($this->logfile);
+	}
+	
+	
+}
+
+?>



More information about the clipart mailing list