3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22
* This class provides methods for creating plugin packages.
23
* @package RepositoryTools
25
* @author Jozef Selesi <selesi at gmail dot com>
26
* @version $Revision: 16021 $
28
class RepositoryPackage extends RepositoryObject {
31
* A RepositoryDescriptor object, used for getting meta data for package generation.
39
* Initializes the package generator.
41
* This method should be called after instantiating the class. It
42
* loads the package templates and sets the output directory.
44
* @param object RepositoryDescriptor $descriptor an initialized RepositoryDescriptor
45
* @param string $outputDir output directory
46
* @return object GalleryStatus a status code
48
function init($descriptor, $outputDir) {
51
$ret = $this->setOutputDir($outputDir);
55
$this->_descriptor = $descriptor;
57
$this->setTemplateFile('package.tpl');
66
* Writes all possible packages for this plugin to the repository.
68
* @param boolean $overwritePackages specifies whether to overwrite existing packages
69
* @param boolean $compressToo write a compressed version also?
70
* @return array object GalleryStatus a status code
71
* array packages created for this plugin
73
function writeAllPackagesToDisk($overwritePackages=false, $compressToo=false) {
74
$packagesWritten = array();
75
$packagesSkipped = array();
77
foreach ($this->_descriptor->getPackages() as $packageType) {
78
$ret = $this->writeSinglePackageToDisk($packageType, $overwritePackages, $compressToo);
80
return array($ret, null);
82
if ($this->_wasFileWritten) {
83
$packagesWritten[] = $packageType;
85
$packagesSkipped[] = $packageType;
89
return array(null, array('packagesWritten' => $packagesWritten,
90
'packagesSkipped' => $packagesSkipped));
94
* Writes the specified package of current plugin to the repository.
96
* @param string $packageType
97
* @param boolean $overwritePackage overwrite package on disk if it already exists
98
* @param boolean $compressToo write a compressed version also?
99
* @return object GalleryStatus a status code
101
function writeSinglePackageToDisk($packageType, $overwritePackage=false, $compressToo=false) {
104
$randomString = $this->getPackageHash($packageType, 10);
105
$this->_template->setVariable('random', $randomString);
107
/* Directory structure creation code. */
108
foreach ($this->_descriptor->getDirectoriesInPackage($packageType) as $dir) {
109
$packageDirectories[] = $dir;
112
/* File creation code. */
113
$platform =& $gallery->getPlatform();
114
$packageFiles = array();
115
foreach ($this->_descriptor->getFilesInPackage($packageType) as $file) {
116
$pluginFilePath = $this->_descriptor->getPluginDir() . $file;
117
if (false === ($fileContents = $platform->file_get_contents($pluginFilePath))) {
118
return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__, sprintf(
119
'Error reading path [%s]', $pluginFilePath));
122
if ($file == 'MANIFEST') {
123
$fileContents = $this->filterManifestForBasePackage($fileContents);
126
$packageFiles[] = array('path' => $file,
127
'data' => base64_encode($fileContents));
130
$this->_template->setVariable('directories', $packageDirectories);
131
$this->_template->setVariable('files', $packageFiles);
133
/* Set file name and write it to disk. */
134
$this->setOutputFile($this->getPackageFileName($packageType));
136
$ret = $this->writeToDisk($overwritePackage, $compressToo);
144
* Calculates the file name of the specified package of this plugin.
146
* Language packages are versioned with the strings.raw and po file revisions, other packages
147
* include the build timestamp - date and time of the last modified file in the plugin's CVS
148
* directory. Examples:
150
* Package Name File Name
151
* lang-en_EN pluginid-lang-en_EN-1.1-1.23.package
152
* base pluginid-1.0.3-20051810223141-base.package
154
* @param string $package package name
155
* @return string package file name
157
function getPackageFileName($package) {
158
if (preg_match('/^lang-(.*)$/', $package, $languageCode)) {
159
$stringsRevision = $this->_descriptor->getStringsRevision();
160
$languageCode = $languageCode[1];
162
$packageFileName = sprintf('%s-%s-%s-%s.package', $this->_descriptor->getPluginId(),
163
$package, $stringsRevision,
164
$this->_descriptor->getLanguageRevision($languageCode));
166
$packageFileName = sprintf('%s-%s-%s-%s.package', $this->_descriptor->getPluginId(),
167
$this->_descriptor->getPluginVersion(), $this->_descriptor->getBuildTimestamp(),
170
return $packageFileName;
174
* Return a hash code for the given descriptor. Not guaranteed to be unique,
175
* but at 5 characters the collision rate is 1 in 60 million.
177
* @param string $package package name
178
* @param int $length desired length
179
* @return string random string
181
function getPackageHash($packageType, $length) {
183
$phpVm = $gallery->getPhpVm();
184
$key = $this->getPackageFileName($packageType);
185
return substr($phpVm->md5($key), 0, $length);