| 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 | */ |
| 28 | class 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 | /** |
| 115 | * We drop the information from the timeline. |
| 116 | */ |
| 117 | function preDelete() |
| 118 | { |
| 119 | IDF_Timeline::remove($this); |
| 120 | } |
| 121 | |
| 122 | function preSave($create=false) |
| 123 | { |
| 124 | if ($this->id == '') { |
| 125 | $this->creation_dtime = gmdate('Y-m-d H:i:s'); |
| 126 | $this->is_head = true; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | function postSave($create=false) |
| 131 | { |
| 132 | if ($create) { |
| 133 | // Check if more than one revision for this page. We do |
| 134 | // not want to insert the first revision in the timeline |
| 135 | // as the page itself is inserted. We do not insert on |
| 136 | // update as update is performed to change the is_head |
| 137 | // flag. |
| 138 | $sql = new Pluf_SQL('wikipage=%s', array($this->wikipage)); |
| 139 | $rev = Pluf::factory('IDF_WikiRevision')->getList(array('filter'=>$sql->gen())); |
| 140 | if ($rev->count() > 1) { |
| 141 | IDF_Timeline::insert($this, $this->get_wikipage()->get_project(), |
| 142 | $this->get_submitter()); |
| 143 | foreach ($rev as $r) { |
| 144 | if ($r->id != $this->id and $r->is_head) { |
| 145 | $r->is_head = false; |
| 146 | $r->update(); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | $page = $this->get_wikipage(); |
| 151 | $page->update(); // Will update the modification timestamp. |
| 152 | IDF_Search::index($page); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public function timelineFragment($request) |
| 157 | { |
| 158 | $page = $this->get_wikipage(); |
| 159 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', |
| 160 | array($request->project->shortname, |
| 161 | $page->title)); |
| 162 | $out = "\n".'<tr class="log"><td><a href="'.$url.'">'. |
| 163 | Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')). |
| 164 | '</a></td><td>'; |
| 165 | $stag = new IDF_Template_ShowUser(); |
| 166 | $user = $stag->start($this->get_submitter(), $request, '', false); |
| 167 | $out .= sprintf(__('<a href="%1$s" title="View page">%2$s</a>, %3$s'), $url, Pluf_esc($page->title), Pluf_esc($this->summary)); |
| 168 | if ($this->changedRevision()) { |
| 169 | $out .= '<div class="issue-changes-timeline">'; |
| 170 | $changes = $this->changes; |
| 171 | foreach ($changes as $w => $v) { |
| 172 | $out .= '<strong>'; |
| 173 | switch ($w) { |
| 174 | case 'lb': |
| 175 | $out .= __('Labels:'); break; |
| 176 | } |
| 177 | $out .= '</strong> '; |
| 178 | if ($w == 'lb') { |
| 179 | $out .= Pluf_esc(implode(', ', $v)); |
| 180 | } else { |
| 181 | $out .= Pluf_esc($v); |
| 182 | } |
| 183 | $out .= ' '; |
| 184 | } |
| 185 | $out .= '</div>'; |
| 186 | } |
| 187 | $out .= '</td></tr>'; |
| 188 | $out .= "\n".'<tr class="extra"><td colspan="2"> |
| 189 | <div class="helptext right">'.sprintf(__('Change of <a href="%s">%s</a>, by %s'), $url, Pluf_esc($page->title), $user).'</div></td></tr>'; |
| 190 | return Pluf_Template::markSafe($out); |
| 191 | } |
| 192 | |
| 193 | public function feedFragment($request) |
| 194 | { |
| 195 | $page = $this->get_wikipage(); |
| 196 | if (!$this->is_head) { |
| 197 | $url = Pluf::f('url_base') |
| 198 | .Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', |
| 199 | array($request->project->shortname, |
| 200 | $page->title), |
| 201 | array('rev' => $this->id)); |
| 202 | } else { |
| 203 | $url = Pluf::f('url_base') |
| 204 | .Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', |
| 205 | array($request->project->shortname, |
| 206 | $page->title)); |
| 207 | } |
| 208 | $title = sprintf(__('%s: Documentation page %s updated - %s'), |
| 209 | $request->project->name, |
| 210 | $page->title, $page->summary); |
| 211 | $date = Pluf_Date::gmDateToGmString($this->creation_dtime); |
| 212 | $context = new Pluf_Template_Context_Request( |
| 213 | $request, |
| 214 | array('url' => $url, |
| 215 | 'title' => $title, |
| 216 | 'page' => $page, |
| 217 | 'rev' => $this, |
| 218 | 'create' => false, |
| 219 | 'date' => $date) |
| 220 | ); |
| 221 | $tmpl = new Pluf_Template('idf/wiki/feedfragment.xml'); |
| 222 | return $tmpl->render($context); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Notification of change of a WikiPage. |
| 229 | * |
| 230 | * The content of a WikiPage is in the IDF_WikiRevision object, |
| 231 | * this is why we send the notificatin from there. This means that |
| 232 | * when the create flag is set, this is for the creation of a |
| 233 | * wikipage and not, for the addition of a new revision. |
| 234 | * |
| 235 | * Usage: |
| 236 | * <pre> |
| 237 | * $this->notify($conf); // Notify the creation of a wiki page |
| 238 | * $this->notify($conf, false); // Notify the update of the page |
| 239 | * </pre> |
| 240 | * |
| 241 | * @param IDF_Conf Current configuration |
| 242 | * @param bool Creation (true) |
| 243 | */ |
| 244 | public function notify($conf, $create=true) |
| 245 | { |
| 246 | if ('' == $conf->getVal('wiki_notification_email', '')) { |
| 247 | return; |
| 248 | } |
| 249 | $current_locale = Pluf_Translation::getLocale(); |
| 250 | $langs = Pluf::f('languages', array('en')); |
| 251 | Pluf_Translation::loadSetLocale($langs[0]); |
| 252 | $context = new Pluf_Template_Context( |
| 253 | array( |
| 254 | 'page' => $this->get_wikipage(), |
| 255 | 'rev' => $this, |
| 256 | 'project' => $this->get_wikipage()->get_project(), |
| 257 | 'url_base' => Pluf::f('url_base'), |
| 258 | ) |
| 259 | ); |
| 260 | if ($create) { |
| 261 | $template = 'idf/wiki/wiki-created-email.txt'; |
| 262 | $title = sprintf(__('New Documentation Page %s - %s (%s)'), |
| 263 | $this->get_wikipage()->title, |
| 264 | $this->get_wikipage()->summary, |
| 265 | $this->get_wikipage()->get_project()->shortname); |
| 266 | } else { |
| 267 | $template = 'idf/wiki/wiki-updated-email.txt'; |
| 268 | $title = sprintf(__('Documentation Page Changed %s - %s (%s)'), |
| 269 | $this->get_wikipage()->title, |
| 270 | $this->get_wikipage()->summary, |
| 271 | $this->get_wikipage()->get_project()->shortname); |
| 272 | } |
| 273 | $tmpl = new Pluf_Template($template); |
| 274 | $text_email = $tmpl->render($context); |
| 275 | $email = new Pluf_Mail(Pluf::f('from_email'), |
| 276 | $conf->getVal('wiki_notification_email'), |
| 277 | $title); |
| 278 | $email->addTextMessage($text_email); |
| 279 | $email->sendMail(); |
| 280 | Pluf_Translation::loadSetLocale($current_locale); |
| 281 | } |
| 282 | } |
| 283 | |