Indefero

Indefero Git Source Tree

Root/src/IDF/WikiRevision.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of InDefero, an open source project management application.
6# Copyright (C) 2008 Céondo Ltd and contributors.
7#
8# InDefero is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# InDefero is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24/**
25 * A revision of a wiki page.
26 *
27 */
28class IDF_WikiRevision extends Pluf_Model
29{
30 public $_model = __CLASS__;
31
32 function init()
33 {
34 $this->_a['table'] = 'idf_wikirevisions';
35 $this->_a['model'] = __CLASS__;
36 $this->_a['cols'] = array(
37 // It is mandatory to have an "id" column.
38 'id' =>
39 array(
40 'type' => 'Pluf_DB_Field_Sequence',
41 'blank' => true,
42 ),
43 'wikipage' =>
44 array(
45 'type' => 'Pluf_DB_Field_Foreignkey',
46 'model' => 'IDF_WikiPage',
47 'blank' => false,
48 'verbose' => __('page'),
49 'relate_name' => 'revisions',
50 ),
51 'is_head' =>
52 array(
53 'type' => 'Pluf_DB_Field_Boolean',
54 'blank' => false,
55 'default' => false,
56 'help_text' => 'If this revision is the latest, we mark it as being the head revision.',
57 'index' => true,
58
59 ),
60 'summary' =>
61 array(
62 'type' => 'Pluf_DB_Field_Varchar',
63 'blank' => false,
64 'size' => 250,
65 'verbose' => __('summary'),
66 'help_text' => __('A one line description of the changes.'),
67 ),
68 'content' =>
69 array(
70 'type' => 'Pluf_DB_Field_Compressed',
71 'blank' => false,
72 'verbose' => __('content'),
73 ),
74 'submitter' =>
75 array(
76 'type' => 'Pluf_DB_Field_Foreignkey',
77 'model' => 'Pluf_User',
78 'blank' => false,
79 'verbose' => __('submitter'),
80 ),
81 'changes' =>
82 array(
83 'type' => 'Pluf_DB_Field_Serialized',
84 'blank' => true,
85 'verbose' => __('changes'),
86 'help_text' => 'Serialized array of the changes in the issue.',
87 ),
88 'creation_dtime' =>
89 array(
90 'type' => 'Pluf_DB_Field_Datetime',
91 'blank' => true,
92 'verbose' => __('creation date'),
93 ),
94 );
95 $this->_a['idx'] = array(
96 'creation_dtime_idx' =>
97 array(
98 'col' => 'creation_dtime',
99 'type' => 'normal',
100 ),
101 );
102 }
103
104 function changedRevision()
105 {
106 return (is_array($this->changes) and count($this->changes) > 0);
107 }
108
109 function _toIndex()
110 {
111 return $this->content;
112 }
113
114 function preSave($create=false)
115 {
116 if ($this->id == '') {
117 $this->creation_dtime = gmdate('Y-m-d H:i:s');
118 $this->is_head = true;
119 }
120 }
121
122 function postSave($create=false)
123 {
124 if ($create) {
125 // Check if more than one revision for this page. We do
126 // not want to insert the first revision in the timeline
127 // as the page itself is inserted. We do not insert on
128 // update as update is performed to change the is_head
129 // flag.
130 $sql = new Pluf_SQL('wikipage=%s', array($this->wikipage));
131 $rev = Pluf::factory('IDF_WikiRevision')->getList(array('filter'=>$sql->gen()));
132 if ($rev->count() > 1) {
133 IDF_Timeline::insert($this, $this->get_wikipage()->get_project(),
134 $this->get_submitter());
135 foreach ($rev as $r) {
136 if ($r->id != $this->id and $r->is_head) {
137 $r->is_head = false;
138 $r->update();
139 }
140 }
141 }
142 $page = $this->get_wikipage();
143 $page->update(); // Will update the modification timestamp.
144 IDF_Search::index($page);
145 }
146 }
147
148 public function timelineFragment($request)
149 {
150 $page = $this->get_wikipage();
151 $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view',
152 array($request->project->shortname,
153 $page->title));
154 $out = "\n".'<tr class="log"><td><a href="'.$url.'">'.
155 Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')).
156 '</a></td><td>';
157 $submitter = $this->get_submitter();
158 $out .= sprintf(__('<a href="%1$s" title="View page">%2$s</a>, %3$s'), $url, Pluf_esc($page->title), Pluf_esc($this->summary));
159 //$out .= '<strong>'.__('Summary:').'</strong>&nbsp;'.Pluf_esc($this->summary).' ';
160 if ($this->changedRevision()) {
161 $out .= '<div class="issue-changes-timeline">';
162 foreach ($this->changes as $w => $v) {
163 $out .= '<strong>';
164 switch ($w) {
165 case 'lb':
166 $out .= __('Labels:'); break;
167 }
168 $out .= '</strong>&nbsp;';
169 if ($w == 'lb') {
170 $out .= Pluf_esc(implode(', ', $v));
171 } else {
172 $out .= Pluf_esc($v);
173 }
174 $out .= ' ';
175 }
176 $out .= '</div>';
177 }
178 $out .= '</td></tr>';
179 $out .= "\n".'<tr class="extra"><td colspan="2">
180<div class="helptext right">'.sprintf(__('Change of <a href="%s">%s</a>'), $url, Pluf_esc($page->title)).', '.__('by').' '.Pluf_esc($submitter).'</div></td></tr>';
181 return Pluf_Template::markSafe($out);
182 }
183}
184

Archive Download this file