Indefero

Indefero Git Source Tree


Root/src/IDF/Views/Wiki.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?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 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 ***** */
 
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
Pluf::loadFunction('Pluf_Shortcuts_RenderToResponse');
Pluf::loadFunction('Pluf_Shortcuts_GetObjectOr404');
Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel');
 
/**
 * Documentation pages views.
 */
class IDF_Views_Wiki
{
    /**
     * View list of issues for a given project.
     */
    public $index_precond = array('IDF_Precondition::accessWiki');
    public function index($request, $match, $api=false)
    {
        $prj = $request->project;
        $title = sprintf(__('%s Documentation'), (string) $prj);
        // Paginator to paginate the pages
        $pag = new Pluf_Paginator(new IDF_WikiPage());
        $pag->class = 'recent-issues';
        $pag->item_extra_props = array('project_m' => $prj,
                                       'shortname' => $prj->shortname,
                                       'current_user' => $request->user);
        $pag->summary = __('This table shows the documentation pages.');
        $pag->action = array('IDF_Views_Wiki::index', array($prj->shortname));
        $pag->edit_action = array('IDF_Views_Wiki::view', 'shortname', 'title');
        $sql = 'project=%s';
        $ptags = self::getWikiTags($prj);
        $dtag = array_pop($ptags); // The last tag is the deprecated tag.
        $ids = self::getDeprecatedPagesIds($prj, $dtag);
        if (count($ids)) {
            $sql .= ' AND id NOT IN ('.implode(',', $ids).')';
        }
        $pag->forced_where = new Pluf_SQL($sql, array($prj->id));
        $pag->extra_classes = array('right', '', 'a-c');
        $list_display = array(
             'title' => __('Page Title'),
             array('summary', 'IDF_Views_Wiki_SummaryAndLabels', __('Summary')),
             array('modif_dtime', 'Pluf_Paginator_DateYMD', __('Updated')),
                              );
        $pag->configure($list_display, array(), array('title', 'modif_dtime'));
        $pag->items_per_page = 25;
        $pag->no_results_text = __('No documentation pages were found.');
        $pag->sort_order = array('title', 'ASC');
        $pag->setFromRequest($request);
        $tags = $prj->getTagCloud('wiki');
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/index.html',
                                               array(
                                                     'page_title' => $title,
                                                     'pages' => $pag,
                                                     'tags' => $tags,
                                                     'deprecated' => count($ids),
                                                     'dlabel' => $dtag,
                                                     ),
                                               $request);
    }
 
    public $search_precond = array('IDF_Precondition::accessWiki',);
    public function search($request, $match)
    {
        $prj = $request->project;
        if (!isset($request->REQUEST['q']) or trim($request->REQUEST['q']) == '') {
            $url =  Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::index',
                                             array($prj->shortname));
            return new Pluf_HTTP_Response_Redirect($url);
        }
        $q = $request->REQUEST['q'];
        $title = sprintf(__('Documentation Search - %s'), $q);
        $pages = new Pluf_Search_ResultSet(IDF_Search::mySearch($q, $prj, 'IDF_WikiPage'));
        if (count($pages) > 100) {
            $pages->results = array_slice($pages->results, 0, 100);
        }
        $pag = new Pluf_Paginator();
        $pag->items = $pages;
        $pag->class = 'recent-issues';
        $pag->item_extra_props = array('project_m' => $prj,
                                       'shortname' => $prj->shortname,
                                       'current_user' => $request->user);
        $pag->summary = __('This table shows the pages found.');
        $pag->action = array('IDF_Views_Wiki::search', array($prj->shortname), array('q'=> $q));
        $pag->edit_action = array('IDF_Views_Wiki::view', 'shortname', 'title');
        $pag->extra_classes = array('right', '', 'a-c');
        $list_display = array(
             'title' => __('Page Title'),
             array('summary', 'IDF_Views_Wiki_SummaryAndLabels', __('Summary')),
             array('modif_dtime', 'Pluf_Paginator_DateYMD', __('Updated')),
                              );
        $pag->configure($list_display);
        $pag->items_per_page = 100;
        $pag->no_results_text = __('No pages were found.');
        $pag->setFromRequest($request);
        $params = array('page_title' => $title,
                        'pages' => $pag,
                        'q' => $q,
                        );
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/search.html', $params, $request);
 
    }
 
    /**
     * View list of pages with a given label.
     */
    public $listLabel_precond = array('IDF_Precondition::accessWiki');
    public function listLabel($request, $match)
    {
        $prj = $request->project;
        $tag = Pluf_Shortcuts_GetObjectOr404('IDF_Tag', $match[2]);
        $prj->inOr404($tag);
        $title = sprintf(__('%1$s Documentation Pages with Label %2$s'), (string) $prj,
                         (string) $tag);
        // Paginator to paginate the pages
        $ptags = self::getWikiTags($prj);
        $dtag = array_pop($ptags); // The last tag is the deprecated tag.
        $pag = new Pluf_Paginator(new IDF_WikiPage());
        $pag->model_view = 'join_tags';
        $pag->class = 'recent-issues';
        $pag->item_extra_props = array('project_m' => $prj,
                                       'shortname' => $prj->shortname);
        $pag->summary = sprintf(__('This table shows the documentation pages with label %s.'), (string) $tag);
        $pag->forced_where = new Pluf_SQL('project=%s AND idf_tag_id=%s', array($prj->id, $tag->id));
        $pag->action = array('IDF_Views_Wiki::listLabel', array($prj->shortname, $tag->id));
        $pag->edit_action = array('IDF_Views_Wiki::view', 'shortname', 'title');
        $pag->extra_classes = array('right', '', 'a-c');
        $list_display = array(
             'title' => __('Page Title'),
             array('summary', 'IDF_Views_Wiki_SummaryAndLabels', __('Summary')),
             array('modif_dtime', 'Pluf_Paginator_DateYMD', __('Updated')),
                              );
        $pag->configure($list_display, array(), array('title', 'modif_dtime'));
        $pag->items_per_page = 25;
        $pag->no_results_text = __('No documentation pages were found.');
        $pag->setFromRequest($request);
        $tags = $prj->getTagCloud('wiki');
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/index.html',
                                               array(
                                                     'page_title' => $title,
                                                     'label' => $tag,
                                                     'pages' => $pag,
                                                     'tags' => $tags,
                                                     'dlabel' => $dtag,
                                                     ),
                                               $request);
    }
 
    /**
     * Create a new documentation page.
     */
    public $create_precond = array('IDF_Precondition::accessWiki',
                                   'Pluf_Precondition::loginRequired');
    public function create($request, $match)
    {
        $prj = $request->project;
        $title = __('New Page');
        $preview = false;
        if ($request->method == 'POST') {
            $form = new IDF_Form_WikiCreate($request->POST,
                                            array('project' => $prj,
                                                  'user' => $request->user
                                                  ));
            if ($form->isValid() and !isset($request->POST['preview'])) {
                $page = $form->save();
                $urlpage = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view',
                                                    array($prj->shortname, $page->title));
                $request->user->setMessage(sprintf(__('The page <a href="%s">%s</a> has been created.'), $urlpage, Pluf_esc($page->title)));
                $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::index',
                                                array($prj->shortname));
                return new Pluf_HTTP_Response_Redirect($url);
            } elseif (isset($request->POST['preview'])) {
                $preview = $request->POST['content'];
            }
        } else {
            $pagename = (isset($request->GET['name'])) ?
                $request->GET['name'] : '';
            $form = new IDF_Form_WikiCreate(null,
                                            array('name' => $pagename,
                                                  'project' => $prj,
                                                  'user' => $request->user));
        }
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/create.html',
                                               array(
                                                     'auto_labels' => self::autoCompleteArrays($prj),
                                                     'page_title' => $title,
                                                     'form' => $form,
                                                     'preview' => $preview,
                                                     ),
                                               $request);
    }
 
    /**
     * View a documentation page.
     */
    public $view_precond = array('IDF_Precondition::accessWiki');
    public function view($request, $match)
    {
        $prj = $request->project;
        // Find the page
        $sql = new Pluf_SQL('project=%s AND title=%s',
                            array($prj->id, $match[2]));
        $pages = Pluf::factory('IDF_WikiPage')->getList(array('filter'=>$sql->gen()));
        if ($pages->count() != 1) {
            return new Pluf_HTTP_Response_NotFound($request);
        }
        $page = $pages[0];
        $oldrev = false;
        // We grab the old revision if requested.
        if (isset($request->GET['rev']) and preg_match('/^[0-9]+$/', $request->GET['rev'])) {
            $oldrev = Pluf_Shortcuts_GetObjectOr404('IDF_WikiRevision',
                                                    $request->GET['rev']);
            if ($oldrev->wikipage != $page->id or $oldrev->is_head == true) {
                return new Pluf_HTTP_Response_NotFound($request);
            }
        }
        $ptags = self::getWikiTags($prj);
        $dtag = array_pop($ptags); // The last tag is the deprecated tag.
        $tags = $page->get_tags_list();
        $dep = Pluf_Model_InArray($dtag, $tags);
        $title = $page->title;
        $revision = $page->get_current_revision();
        $false = Pluf_DB_BooleanToDb(false, $page->getDbConnection());
        $revs = $page->get_revisions_list(array('order' => 'creation_dtime DESC',
                                                'filter' => 'is_head='.$false));
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/view.html',
                                               array(
                                                     'page_title' => $title,
                                                     'page' => $page,
                                                     'oldrev' => $oldrev,
                                                     'rev' => $revision,
                                                     'revs' => $revs,
                                                     'tags' => $tags,
                                                     'deprecated' => $dep,
                                                     ),
                                               $request);
    }
 
    /**
     * Remove a revision of a page.
     */
    public $deleteRev_precond = array('IDF_Precondition::accessWiki',
                                      'IDF_Precondition::projectMemberOrOwner');
    public function deleteRev($request, $match)
    {
        $prj = $request->project;
        $oldrev = Pluf_Shortcuts_GetObjectOr404('IDF_WikiRevision', $match[2]);
        $page = $oldrev->get_wikipage();
        $prj->inOr404($page);
        if ($oldrev->is_head == true) {
            return new Pluf_HTTP_Response_NotFound($request);
        }
        if ($request->method == 'POST') {
            $oldrev->delete();
            $request->user->setMessage(__('The old revision has been deleted.'));
            $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view',
                                            array($prj->shortname, $page->title));
            return new Pluf_HTTP_Response_Redirect($url);
        }
 
        $title = sprintf(__('Delete Old Revision of %s'), $page->title);
        $revision = $page->get_current_revision();
        $false = Pluf_DB_BooleanToDb(false, $page->getDbConnection());
        $revs = $page->get_revisions_list(array('order' => 'creation_dtime DESC',
                                                'filter' => 'is_head='.$false));
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/delete.html',
                                               array(
                                                     'page_title' => $title,
                                                     'page' => $page,
                                                     'oldrev' => $oldrev,
                                                     'rev' => $revision,
                                                     'revs' => $revs,
                                                     'tags' => $page->get_tags_list(),
                                                     ),
                                               $request);
    }
 
    /**
     * View a documentation page.
     */
    public $update_precond = array('IDF_Precondition::accessWiki',
                                   'Pluf_Precondition::loginRequired');
    public function update($request, $match)
    {
        $prj = $request->project;
        // Find the page
        $sql = new Pluf_SQL('project=%s AND title=%s',
                            array($prj->id, $match[2]));
        $pages = Pluf::factory('IDF_WikiPage')->getList(array('filter'=>$sql->gen()));
        if ($pages->count() != 1) {
            return new Pluf_HTTP_Response_NotFound($request);
        }
        $page = $pages[0];
        $title = sprintf(__('Update %s'), $page->title);
        $revision = $page->get_current_revision();
        $preview = false;
        $params = array('project' => $prj,
                        'user' => $request->user,
                        'page' => $page);
        if ($request->method == 'POST') {
            $form = new IDF_Form_WikiUpdate($request->POST, $params);
            if ($form->isValid() and !isset($request->POST['preview'])) {
                $page = $form->save();
                $urlpage = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view',
                                                    array($prj->shortname, $page->title));
                $request->user->setMessage(sprintf(__('The page <a href="%s">%s</a> has been updated.'), $urlpage, Pluf_esc($page->title)));
                $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::index',
                                                array($prj->shortname));
                return new Pluf_HTTP_Response_Redirect($url);
            } elseif (isset($request->POST['preview'])) {
                $preview = $request->POST['content'];
            }
        } else {
                 
            $form = new IDF_Form_WikiUpdate(null, $params);
        }
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/update.html',
                                               array(
                                                     'auto_labels' => self::autoCompleteArrays($prj),
                                                     'page_title' => $title,
                                                     'page' => $page,
                                                     'rev' => $revision,
                                                     'form' => $form,
                                                     'preview' => $preview,
                                                     ),
                                               $request);
    }
 
    /**
     * Delete a Wiki page.
     */
    public $delete_precond = array('IDF_Precondition::accessWiki',
                                   'IDF_Precondition::projectMemberOrOwner');
    public function delete($request, $match)
    {
        $prj = $request->project;
        $page = Pluf_Shortcuts_GetObjectOr404('IDF_WikiPage', $match[2]);
        $prj->inOr404($page);
        $params = array('page' => $page);
        if ($request->method == 'POST') {
            $form = new IDF_Form_WikiDelete($request->POST, $params);
            if ($form->isValid()) {
                $form->save();
                $request->user->setMessage(__('The documentation page has been deleted.'));
                $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::index',
                                                array($prj->shortname));
                return new Pluf_HTTP_Response_Redirect($url);
            }
        } else {
            $form = new IDF_Form_WikiDelete(null, $params);
        }
        $title = sprintf(__('Delete Page %s'), $page->title);
        $revision = $page->get_current_revision();
        $false = Pluf_DB_BooleanToDb(false, $page->getDbConnection());
        $revs = $page->get_revisions_list(array('order' => 'creation_dtime DESC',
                                                'filter' => 'is_head='.$false));
        return Pluf_Shortcuts_RenderToResponse('idf/wiki/deletepage.html',
                                               array(
                                                     'page_title' => $title,
                                                     'page' => $page,
                                                     'form' => $form,
                                                     'rev' => $revision,
                                                     'revs' => $revs,
                                                     'tags' => $page->get_tags_list(),
                                                     ),
                                               $request);
    }
 
    /**
     * Get the wiki tags.
     *
     * @param IDF_Project
     * @return ArrayObject The tags
     */
    public static function getWikiTags($project)
    {
        return $project->getTagsFromConfig('labels_wiki_predefined',
                                           IDF_Form_WikiConf::init_predefined);
 
    }
 
    /**
     * Get deprecated page ids.
     *
     * @param IDF_Project
     * @param IDF_Tag Deprecated tag (null)
     * @return array Ids of the deprecated pages.
     */
    public static function getDeprecatedPagesIds($project, $dtag=null)
    {
        if (is_null($dtag)) {
            $ptags = self::getWikiTags($project);
            $dtag = array_pop($ptags); // The last tag is the deprecated tag
        }
        $sql = new Pluf_SQL('project=%s AND idf_tag_id=%s', array($project->id,
                                                                  $dtag->id));
        $ids = array();
        foreach (Pluf::factory('IDF_WikiPage')->getList(array('filter' => $sql->gen(), 'view' => 'join_tags'))
                 as $file) {
            $ids[] = (int) $file->id;
        }
        return $ids;
    }
 
    /**
     * Create the autocomplete arrays for the little AJAX stuff.
     */
    public static function autoCompleteArrays($project)
    {
        $conf = new IDF_Conf();
        $conf->setProject($project);
        $st = preg_split("/\015\012|\015|\012/",
                         $conf->getVal('labels_wiki_predefined', IDF_Form_WikiConf::init_predefined), -1, PREG_SPLIT_NO_EMPTY);
        $auto = '';
        foreach ($st as $s) {
            $v = '';
            $d = '';
            $_s = explode('=', $s, 2);
            if (count($_s) > 1) {
                $v = trim($_s[0]);
                $d = trim($_s[1]);
            } else {
                $v = trim($_s[0]);
            }
            $auto .= sprintf('{ name: "%s", to: "%s" }, ',
                             Pluf_esc($d), Pluf_esc($v));
        }
        return substr($auto, 0, -2);
    }
}
 
/**
 * Display the summary of a page, then on a new line, display the
 * list of labels.
 */
function IDF_Views_Wiki_SummaryAndLabels($field, $page, $extra='')
{
    $tags = array();
    foreach ($page->get_tags_list() as $tag) {
        $tags[] = Pluf_esc((string) $tag);
    }
    $out = '';
    if (count($tags)) {
        $out = '<br /><span class="note label">'.implode(', ', $tags).'</span>';
    }
    return Pluf_esc($page->summary).$out;
}

Archive Download this file

Page rendered in 0.11921s using 11 queries.