Indefero

Indefero Git Source Tree


Root/src/IDF/Wiki/ResourceRevision.php

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
332
333
334
335
336
337
338
339
340
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * A single resource revision.
 */
class IDF_Wiki_ResourceRevision extends Pluf_Model
{
    public $_model = __CLASS__;
 
    function init()
    {
        $this->_a['table'] = 'idf_wikiresourcerevs';
        $this->_a['model'] = __CLASS__;
        $this->_a['cols'] = array(
                             // It is mandatory to have an "id" column.
                            'id' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Sequence',
                                  'blank' => true,
                                  ),
                            'wikiresource' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'IDF_Wiki_Resource',
                                  'blank' => false,
                                  'verbose' => __('resource'),
                                  'relate_name' => 'revisions',
                                  ),
                            'is_head' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Boolean',
                                  'blank' => false,
                                  'default' => false,
                                  'help_text' => 'If this revision is the latest, we mark it as being the head revision.',
                                  'index' => true,
                                  ),
                            'summary' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 250,
                                  'verbose' => __('summary'),
                                  'help_text' => __('A one line description of the changes.'),
                                  ),
                            'filesize' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Integer',
                                  'blank' => false,
                                  'default' => 0,
                                  'verbose' => __('file size in bytes'),
                                  ),
                            'fileext' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Varchar',
                                  'blank' => false,
                                  'size' => 10,
                                  'verbose' => __('File extension'),
                                  'help_text' => __('The file extension of the uploaded resource.'),
                                  ),
                            'submitter' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Foreignkey',
                                  'model' => 'Pluf_User',
                                  'blank' => false,
                                  'verbose' => __('submitter'),
                                  'relate_name' => 'submitted_downloads',
                                  ),
                            'pageusage' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Manytomany',
                                  'model' => 'IDF_Wiki_PageRevision',
                                  'blank' => true,
                                  'verbose' => __('page usage'),
                                  'help_text' => 'Records on which pages this resource revision is used.',
                                  ),
                            'creation_dtime' =>
                            array(
                                  'type' => 'Pluf_DB_Field_Datetime',
                                  'blank' => true,
                                  'verbose' => __('creation date'),
                                  ),
                            );
        $table = $this->_con->pfx.'idf_wiki_pagerevision_idf_wiki_resourcerevision_assoc';
        $this->_a['views'] = array(
            'join_pagerevision' =>
                array(
                    'join' => 'LEFT JOIN '.$table
                             .' ON idf_wiki_resourcerevision_id=id',
                ),
        );
    }
 
    function __toString()
    {
        return sprintf(__('id %d: %s'), $this->id, $this->summary);
    }
 
    function _toIndex()
    {
        return '';
    }
 
    function preDelete()
    {
        // if we kill off a head revision, ensure that we either mark a previous revision as head
        if ($this->is_head) {
            $sql = new Pluf_SQL('wikiresource=%s and id!=%s', array($this->wikiresource, $this->id));
            $revs = Pluf::factory('IDF_Wiki_ResourceRevision')->getList(array('filter'=>$sql->gen(), 'order'=>'id DESC'));
            if ($revs->count() > 0) {
                $previous = $revs[0];
                $previous->is_head = true;
                $previous->update();
            }
        }
 
        @unlink($this->getFilePath());
        IDF_Timeline::remove($this);
    }
 
    function preSave($create=false)
    {
        if ($this->id == '') {
            $this->creation_dtime = gmdate('Y-m-d H:i:s');
            $this->is_head = true;
        }
    }
 
    function postSave($create=false)
    {
        $resource = $this->get_wikiresource();
 
        if ($create) {
            $sql = new Pluf_SQL('wikiresource=%s', array($this->wikiresource));
            $rev = Pluf::factory('IDF_Wiki_ResourceRevision')->getList(array('filter'=>$sql->gen()));
            if ($rev->count() > 1) {
                IDF_Timeline::insert($this, $resource->get_project(), $this->get_submitter());
                foreach ($rev as $r) {
                    if ($r->id != $this->id and $r->is_head) {
                        $r->is_head = false;
                        $r->update();
                    }
                }
            }
        }
 
        // update the modification timestamp
        $resource->update();
    }
 
    function getFilePath()
    {
        return sprintf(Pluf::f('upload_path').'/'.$this->get_wikiresource()->get_project()->shortname.'/wiki/res/%d/%d.%s',
            $this->get_wikiresource()->id, $this->id, $this->fileext);
    }
 
    function getViewURL()
    {
        $prj = $this->get_wikiresource()->get_project();
        $resource = $this->get_wikiresource();
        return Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::viewResource',
                                        array($prj->shortname, $resource->title),
                                        array('rev' => $this->id));
    }
 
    function getRawURL($attachment = false)
    {
        $query = $attachment ? array('attachment' => 1) : array();
        $prj = $this->get_wikiresource()->get_project();
        return Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::rawResource',
                                        array($prj->shortname, $this->id),
                                        $query);
    }
 
    /**
     * Returns the page revisions which contain references to this resource revision
     */
    function getPageRevisions()
    {
        $db =& Pluf::db();
        $sql_results = $db->select(
            'SELECT idf_wiki_pagerevision_id as id '.
            'FROM '.Pluf::f('db_table_prefix', '').'idf_wiki_pagerevision_idf_wiki_resourcerevision_assoc '.
            'WHERE idf_wiki_resourcerevision_id='.$this->id
        );
        $ids = array(0);
        foreach ($sql_results as $id) {
            $ids[] = $id['id'];
        }
        $ids = implode (',', $ids);
 
        $sql = new Pluf_SQL('id IN ('.$ids.')');
        return Pluf::factory('IDF_Wiki_PageRevision')
            ->getList(array('filter' => $sql->gen()));
    }
 
    /**
     * Renders the resource with the given view options, including a link to the resource' detail page
     */
    function render($opts = array())
    {
        // give some reasonable defaults
        $opts = array_merge(array(
            'align' => 'left',
            'width' => '',
            'height' => '',
            'preview' => 'yes', // if possible
            'title' => '',
        ), $opts);
 
        $attrs  = array('class="resource-container"');
        $styles = array();
        if (!empty($opts['align'])) {
            switch ($opts['align']) {
                case 'left':
                    $styles[] = 'float: left';
                    $styles[] = 'margin-right: 10px';
                    break;
                case 'center':
                    $styles[] = 'margin: 0 auto 0 auto';
                    break;
                case 'right':
                    $styles[] = 'float: right';
                    $styles[] = 'margin-left: 10px';
                    break;
            }
        }
        if (!empty($opts['width'])) {
            $styles[] = 'width:'.$opts['width'];
        }
        if (!empty($opts['height'])) {
            $styles[] = 'height:'.$opts['height'];
        }
 
        $raw = $this->renderRaw();
        $viewUrl = $this->getViewURL();
        $download = '';
        $html = '<div class="resource-container" style="'.implode(';', $styles).'">';
        if ($opts['preview'] == 'yes' && !empty($raw)) {
            $html .= '<div class="preview">'.$raw.'</div>'."\n";
        } else {
            $rawUrl = $this->getRawURL(true);
            $download = '<a href="'.$rawUrl.'" class="download" title="'.sprintf(__('Download (%s)'), Pluf_Utils::prettySize($this->filesize)).'"></a>';
        }
        $resource = $this->get_wikiresource();
        $title = $opts['title'];
        if (empty($title)) {
            $title = $resource->title.' - '.$resource->mime_type.' - '.Pluf_Utils::prettySize($this->filesize);
        }
        $html .= '<div class="title">'.$download.'<a href="'.$viewUrl.'" title="'.__('View resource details').'">'.$title.'</a></div>'."\n";
        $html .= '</div>';
        return $html;
    }
 
    /**
     * Renders a raw version of the resource, without any possibilities of formatting or the like
     */
    function renderRaw()
    {
        $resource = $this->get_wikiresource();
        $url = $this->getRawURL();
        if (preg_match('#^image/(gif|jpeg|png|tiff)$#', $resource->mime_type)) {
            return sprintf('<img src="%s" alt="%s" />', $url, $resource->title);
        }
 
        if (preg_match('#^text/(plain|xml|html|sgml|javascript|ecmascript|css)$#', $resource->mime_type)) {
            return sprintf('<iframe src="%s" alt="%s"></iframe>', $url, $resource->title);
        }
 
        return '';
    }
 
 
    public function timelineFragment($request)
    {
        $resource = $this->get_wikiresource();
        $url = Pluf::f('url_base')
            .Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::viewResource',
                                      array($request->project->shortname,
                                            $resource->title),
                                      array('rev' => $this->id));
 
        $out = "\n".'<tr class="log"><td><a href="'.$url.'">'.
            Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')).
            '</a></td><td>';
        $stag = new IDF_Template_ShowUser();
        $user = $stag->start($this->get_submitter(), $request, '', false);
        $out .= sprintf(__('<a href="%1$s" title="View resource">%2$s</a>, %3$s'), $url, Pluf_esc($resource->title), Pluf_esc($this->summary));
        $out .= '</td></tr>';
        $out .= "\n".'<tr class="extra"><td colspan="2">
<div class="helptext right">'.sprintf(__('Change of <a href="%1$s">%2$s</a>, by %3$s'), $url, Pluf_esc($resource->title), $user).'</div></td></tr>';
        return Pluf_Template::markSafe($out);
    }
 
    public function feedFragment($request)
    {
        $resource = $this->get_wikiresource();
        $url = Pluf::f('url_base')
            .Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::viewResource',
                                      array($request->project->shortname,
                                            $resource->title),
                                      array('rev' => $this->id));
 
        $title = sprintf(__('%1$s: Documentation resource %2$s updated - %3$s'),
                         $request->project->name,
                         $resource->title, $resource->summary);
        $date = Pluf_Date::gmDateToGmString($this->creation_dtime);
        $context = new Pluf_Template_Context_Request(
            $request,
            array('url' => $url,
                  'title' => $title,
                  'resource' => $resource,
                  'rev' => $this,
                  'create' => false,
                  'date' => $date)
        );
        $tmpl = new Pluf_Template('idf/wiki/feedfragment-resource.xml');
        return $tmpl->render($context);
    }
}

Archive Download this file

Page rendered in 0.12836s using 11 queries.