Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/models/version.php
blob: e323ac240b382b30213a64f12be6b01758f5cc58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is addons.mozilla.org site.
 *
 * The Initial Developer of the Original Code is
 * The Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Andrei Hajdukewycz <sancus@off.net> (Original Author)
 *   Justin Scott <fligtar@gmail.com>
 *   Mike Morgan <morgamic@mozilla.com>
 *   Frederic Wenzel <fwenzel@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

class Version extends AppModel
{
    var $name = 'Version';
    var $belongsTo_full = array('Addon' =>
                                array('className' => 'Addon'),
                                'License' =>
                                array('className'  => 'License',
                                      'conditions' => '',
                                      'order'      => '',
                                      'foreignKey' => 'license_id'
                                )
                          );

    var $hasMany = array('Review' =>
                         array('className'   => 'Review',
                               'conditions'  => '',
                               'order'       => '',
                               'limit'       => '',
                               'foreignKey'  => 'version_id',
                               'dependent'   => true,
                               'exclusive'   => false,
                               'finderSql'   => ''
                         ),
                       'File' =>
                         array('className'   => 'File',
                               'conditions'  => '',
                               'order'       => '',
                               'limit'       => '',
                               'foreignKey'  => 'version_id',
                               'dependent'   => true,
                               'exclusive'   => false,
                               'finderSql'   => ''
                         )
                  );
    var $hasAndBelongsToMany = array('Application' =>
                                      array('className'  => 'Application',
                                            'joinTable'  => 'applications_versions',
                                            'foreignKey' => 'version_id',
                                            'associationForeignKey'=> 'application_id',
                                            'conditions' => '',
                                            'order'      => '',
                                            'limit'      => '',
                                            'unique'     => false,
                                            'finderSql'  => '',
                                            'deleteQuery'=> ''
                                      )
                               );

    var $translated_fields = array(
                'releasenotes'
            );    

    var $validate = array(
                          'addon_id' => VALID_NUMBER,
                          'version' => VALID_NOT_EMPTY
                    );

    /**
     * Return the id or ids of valid versions by add-on id.
     *
     * @param mixed $id
     * @param array $status non-empty array
     * @return aray|boolean array of valid version_ids on success or false on failure
     */
    function getVersionIdsByAddonId($id, $status = array(STATUS_PUBLIC)) {

        // Implode our status array
        $status_sql = implode(',',$status);

        $id_sql = is_array($id) ? implode(',',$id) : $id;

        $sql = "
            SELECT DISTINCT
                Version.id
            FROM
                versions AS Version
            INNER JOIN
                files AS File ON File.status IN ({$status_sql}) AND File.version_id = Version.id 
            WHERE
                Version.addon_id IN ({$id_sql})
            ORDER BY
                Version.created DESC
        ";

        return $this->query($sql);
    }

    /**
     * Return the latest version id by add-on id.
     *
     * @param int $id
     * @param array $status non-empty array
     * @return int $id of the latest version or 0
     */
    function getVersionByAddonId($id, $status = array(STATUS_PUBLIC)) {
        if (!is_array($status)) $status = array($status);
        $status_sql = implode(',',$status);

        $sql = "
            SELECT 
                Version.id
            FROM
                versions AS Version
            INNER JOIN
                files AS File ON File.status IN ({$status_sql}) AND File.version_id = Version.id 
            WHERE
                Version.addon_id = {$id}
            ORDER BY
                Version.created DESC
            LIMIT 1
        ";

        $buf = $this->query($sql);

        if (!empty($buf[0]['Version']['id'])) {
            return $buf[0]['Version']['id'];
        }

        return 0;
    }
    
    /**
     * Get the apps compatible with a given addon version
     */
    function getCompatibleApps($id) {
        global $app_shortnames;
        
        $supported_app_ids = implode(',',array_values($app_shortnames));
        $sql = "
            SELECT
                Application.application_id,
                Min_Version.version,
                Max_Version.version
            FROM
                applications_versions AS Application
            INNER JOIN
                appversions AS Min_Version ON (Min_Version.id = Application.`min`)
            INNER JOIN
                appversions AS Max_Version ON (Max_Version.id = Application.`max`)
            WHERE
                Application.version_id = '{$id}'
            AND
                Application.application_id IN ({$supported_app_ids})
            ORDER BY
                (Application.application_id = '".APP_ID."') DESC,
                FIELD(Application.application_id,{$supported_app_ids})
        ";
        return $this->query($sql, true);
    }
    
    /**
     * Gets the apps compatible with a given add-on version in the form of ids
     * instead of the actual version numbers and organizes them by application_id
     */
    function getCompatibleAppIds($version_id) {
        $apps = $this->query("
            SELECT
                `applications_versions`.`application_id`,
                `min`.`id`,
                `max`.`id`
            FROM
                `applications_versions`
            INNER JOIN
                `appversions` AS `min` ON `applications_versions`.`min`=`min`.`id`
            INNER JOIN
                `appversions` AS `max` ON `applications_versions`.`max`=`max`.`id`
            WHERE
                `applications_versions`.`version_id`='{$version_id}'
            ", true);
        
        $list = array();
        
        if (!empty($apps)) {
            foreach ($apps as $app) {
                $list[$app['applications_versions']['application_id']] = array(
                    'min' => $app['min']['id'],
                    'max' => $app['max']['id']
                );
            }
        }
        
        return $list;
    }
    
    /**
     * Adds a compatible application to the specified version
     * @param int $version_id version id
     * @param int $application_id application id
     * @param int $minVersion appversion id (not the actual version string)
     * @param int $maxVersion appversion id (not the actual version string)
     */
    function addCompatibleApp($version_id, $application_id, $minVersion, $maxVersion) {
        $this->execute("
                INSERT INTO
                    applications_versions (
                        application_id,
                        version_id,
                        min,
                        max
                    )
                    VALUES (
                        {$application_id},
                        {$version_id},
                        {$minVersion},
                        {$maxVersion}
                    )
            ");
    }
    
    /**
     * Removes a compatible application from the specified version
     * @param int $version_id version id
     * @param int $application_id application id
     */
    function removeCompatibleApp($version_id, $application_id) {
        $this->execute("
                DELETE FROM
                    applications_versions
                WHERE
                    version_id={$version_id} AND
                    application_id={$application_id}
            ");
    }
    
    /**
     * Updates compatiblity for the specified app and version
     * @param int $version_id version id
     * @param int $application_id application id
     * @param int $minVersion appversion id (not the actual version string)
     * @param int $maxVersion appversion id (not the actual version string)
     */
    function updateCompatibility($version_id, $application_id, $minVersion, $maxVersion) {
        $this->execute("
                UPDATE
                    applications_versions
                SET
                    min={$minVersion},
                    max={$maxVersion}
                WHERE
                    version_id={$version_id} AND
                    application_id={$application_id}
            ");
    }
    
    /**
     * Returns an array of file ids associated with the given version.
     * @param int $version_id version id
     * @return array
     */
    function getFileIDs($version_id) {
        $files = $this->query("SELECT id FROM files WHERE version_id={$version_id}");
        $file_ids = array();
        
        if (!empty($files)) {
            foreach ($files as $file) {
                $file_ids[] = $file['files']['id'];
            }
        }
        
        return $file_ids;
    }

    function save($data = null, $validate = true, $fieldList = array()) {
        $result = parent::save($data, $validate, $fieldList);

        if ($result) {
            $versions = $this->getCompatibleApps($this->id);

            if (empty($versions)) {
                $sql = "
                    SELECT
                        a.id
                    FROM
                        appversions AS a
                    WHERE
                        a.application_id = ".SITE_APP."
                    ORDER BY
                        a.version DESC
                    LIMIT 1";

                $rows = $this->query($sql);
                $version = $rows[0]['a']['id'];
                $this->addCompatibleApp($this->id, SITE_APP, $version, $version);
            }
        }

        return $result;
    }
}
?>