~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to lib/tools/repository/classes/RepositoryPackage.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 */
20
 
 
21
 
/**
22
 
 * This class provides methods for creating plugin packages.
23
 
 * @package RepositoryTools
24
 
 * @subpackage Classes
25
 
 * @author Jozef Selesi <selesi at gmail dot com>
26
 
 * @version $Revision: 16021 $
27
 
 */
28
 
class RepositoryPackage extends RepositoryObject {
29
 
 
30
 
    /**
31
 
     * A RepositoryDescriptor object, used for getting meta data for package generation.
32
 
     *
33
 
     * @var object
34
 
     * @access private
35
 
     */
36
 
    var $_descriptor;
37
 
 
38
 
    /**
39
 
     * Initializes the package generator.
40
 
     *
41
 
     * This method should be called after instantiating the class. It
42
 
     * loads the package templates and sets the output directory.
43
 
     *
44
 
     * @param object RepositoryDescriptor $descriptor an initialized RepositoryDescriptor
45
 
     * @param string $outputDir output directory
46
 
     * @return object GalleryStatus a status code
47
 
     */
48
 
    function init($descriptor, $outputDir) {
49
 
        parent::init();
50
 
 
51
 
        $ret = $this->setOutputDir($outputDir);
52
 
        if ($ret) {
53
 
            return $ret;
54
 
        }
55
 
        $this->_descriptor = $descriptor;
56
 
 
57
 
        $this->setTemplateFile('package.tpl');
58
 
        if ($ret) {
59
 
            return $ret;
60
 
        }
61
 
 
62
 
        return null;
63
 
    }
64
 
 
65
 
    /**
66
 
     * Writes all possible packages for this plugin to the repository.
67
 
     *
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
72
 
     */
73
 
    function writeAllPackagesToDisk($overwritePackages=false, $compressToo=false) {
74
 
        $packagesWritten = array();
75
 
        $packagesSkipped = array();
76
 
 
77
 
        foreach ($this->_descriptor->getPackages() as $packageType) {
78
 
            $ret = $this->writeSinglePackageToDisk($packageType, $overwritePackages, $compressToo);
79
 
            if ($ret) {
80
 
                return array($ret, null);
81
 
            }
82
 
            if ($this->_wasFileWritten) {
83
 
                $packagesWritten[] = $packageType;
84
 
            } else {
85
 
                $packagesSkipped[] = $packageType;
86
 
            }
87
 
        }
88
 
 
89
 
        return array(null, array('packagesWritten' => $packagesWritten,
90
 
                                 'packagesSkipped' => $packagesSkipped));
91
 
    }
92
 
 
93
 
    /**
94
 
     * Writes the specified package of current plugin to the repository.
95
 
     *
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
100
 
     */
101
 
    function writeSinglePackageToDisk($packageType, $overwritePackage=false, $compressToo=false) {
102
 
        global $gallery;
103
 
 
104
 
        $randomString = $this->getPackageHash($packageType, 10);
105
 
        $this->_template->setVariable('random', $randomString);
106
 
 
107
 
        /* Directory structure creation code. */
108
 
        foreach ($this->_descriptor->getDirectoriesInPackage($packageType) as $dir) {
109
 
            $packageDirectories[] = $dir;
110
 
        }
111
 
 
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));
120
 
            }
121
 
 
122
 
            if ($file == 'MANIFEST') {
123
 
                $fileContents = $this->filterManifestForBasePackage($fileContents);
124
 
            }
125
 
 
126
 
            $packageFiles[] = array('path' => $file,
127
 
                                    'data' => base64_encode($fileContents));
128
 
        }
129
 
 
130
 
        $this->_template->setVariable('directories', $packageDirectories);
131
 
        $this->_template->setVariable('files', $packageFiles);
132
 
 
133
 
        /* Set file name and write it to disk. */
134
 
        $this->setOutputFile($this->getPackageFileName($packageType));
135
 
 
136
 
        $ret = $this->writeToDisk($overwritePackage, $compressToo);
137
 
        if ($ret) {
138
 
            return $ret;
139
 
        }
140
 
        return null;
141
 
    }
142
 
 
143
 
    /**
144
 
     * Calculates the file name of the specified package of this plugin.
145
 
     *
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:
149
 
     *
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
153
 
     *
154
 
     * @param string $package package name
155
 
     * @return string package file name
156
 
     */
157
 
    function getPackageFileName($package) {
158
 
        if (preg_match('/^lang-(.*)$/', $package, $languageCode)) {
159
 
            $stringsRevision = $this->_descriptor->getStringsRevision();
160
 
            $languageCode = $languageCode[1];
161
 
 
162
 
            $packageFileName = sprintf('%s-%s-%s-%s.package', $this->_descriptor->getPluginId(),
163
 
                $package, $stringsRevision,
164
 
                $this->_descriptor->getLanguageRevision($languageCode));
165
 
        } else {
166
 
            $packageFileName = sprintf('%s-%s-%s-%s.package', $this->_descriptor->getPluginId(),
167
 
                $this->_descriptor->getPluginVersion(), $this->_descriptor->getBuildTimestamp(),
168
 
                $package);
169
 
        }
170
 
        return $packageFileName;
171
 
    }
172
 
 
173
 
    /**
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.
176
 
     *
177
 
     * @param string $package package name
178
 
     * @param int $length desired length
179
 
     * @return string random string
180
 
     */
181
 
    function getPackageHash($packageType, $length) {
182
 
        global $gallery;
183
 
        $phpVm = $gallery->getPhpVm();
184
 
        $key = $this->getPackageFileName($packageType);
185
 
        return substr($phpVm->md5($key), 0, $length);
186
 
    }
187
 
}
188
 
?>