| 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 | Pluf::loadFunction('Pluf_HTTP_URL_urlForView'); |
| 25 | Pluf::loadFunction('Pluf_Template_dateAgo'); |
| 26 | |
| 27 | /** |
| 28 | * Base definition of a wiki page. |
| 29 | * |
| 30 | * A wiki page can have tags and be starred by the users. The real |
| 31 | * content of the page is stored in the IDF_WikiRevision |
| 32 | * object. Several revisions are associated to a given page. |
| 33 | */ |
| 34 | class IDF_WikiPage extends Pluf_Model |
| 35 | { |
| 36 | public $_model = __CLASS__; |
| 37 | |
| 38 | function init() |
| 39 | { |
| 40 | $this->_a['table'] = 'idf_wikipages'; |
| 41 | $this->_a['model'] = __CLASS__; |
| 42 | $this->_a['cols'] = array( |
| 43 | // It is mandatory to have an "id" column. |
| 44 | 'id' => |
| 45 | array( |
| 46 | 'type' => 'Pluf_DB_Field_Sequence', |
| 47 | 'blank' => true, |
| 48 | ), |
| 49 | 'project' => |
| 50 | array( |
| 51 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 52 | 'model' => 'IDF_Project', |
| 53 | 'blank' => false, |
| 54 | 'verbose' => __('project'), |
| 55 | 'relate_name' => 'wikipages', |
| 56 | ), |
| 57 | 'title' => |
| 58 | array( |
| 59 | 'type' => 'Pluf_DB_Field_Varchar', |
| 60 | 'blank' => false, |
| 61 | 'size' => 250, |
| 62 | 'verbose' => __('title'), |
| 63 | 'help_text' => __('The title of the page must only contain letters, digits or the dash character. For example: My-new-Wiki-Page.'), |
| 64 | ), |
| 65 | 'summary' => |
| 66 | array( |
| 67 | 'type' => 'Pluf_DB_Field_Varchar', |
| 68 | 'blank' => false, |
| 69 | 'size' => 250, |
| 70 | 'verbose' => __('summary'), |
| 71 | 'help_text' => __('A one line description of the page content.'), |
| 72 | ), |
| 73 | 'submitter' => |
| 74 | array( |
| 75 | 'type' => 'Pluf_DB_Field_Foreignkey', |
| 76 | 'model' => 'Pluf_User', |
| 77 | 'blank' => false, |
| 78 | 'verbose' => __('submitter'), |
| 79 | 'relate_name' => 'submitted_wikipages', |
| 80 | ), |
| 81 | 'interested' => |
| 82 | array( |
| 83 | 'type' => 'Pluf_DB_Field_Manytomany', |
| 84 | 'model' => 'Pluf_User', |
| 85 | 'blank' => true, |
| 86 | 'verbose' => __('interested users'), |
| 87 | 'help_text' => 'Interested users will get an email notification when the wiki page is changed.', |
| 88 | ), |
| 89 | 'tags' => |
| 90 | array( |
| 91 | 'type' => 'Pluf_DB_Field_Manytomany', |
| 92 | 'blank' => true, |
| 93 | 'model' => 'IDF_Tag', |
| 94 | 'verbose' => __('labels'), |
| 95 | ), |
| 96 | 'creation_dtime' => |
| 97 | array( |
| 98 | 'type' => 'Pluf_DB_Field_Datetime', |
| 99 | 'blank' => true, |
| 100 | 'verbose' => __('creation date'), |
| 101 | ), |
| 102 | 'modif_dtime' => |
| 103 | array( |
| 104 | 'type' => 'Pluf_DB_Field_Datetime', |
| 105 | 'blank' => true, |
| 106 | 'verbose' => __('modification date'), |
| 107 | ), |
| 108 | ); |
| 109 | $this->_a['idx'] = array( |
| 110 | 'modif_dtime_idx' => |
| 111 | array( |
| 112 | 'col' => 'modif_dtime', |
| 113 | 'type' => 'normal', |
| 114 | ), |
| 115 | ); |
| 116 | $table = $this->_con->pfx.'idf_tag_idf_wikipage_assoc'; |
| 117 | $this->_a['views'] = array( |
| 118 | 'join_tags' => |
| 119 | array( |
| 120 | 'join' => 'LEFT JOIN '.$table |
| 121 | .' ON idf_wikipage_id=id', |
| 122 | ), |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | function __toString() |
| 127 | { |
| 128 | return $this->title.' - '.$this->summary; |
| 129 | } |
| 130 | |
| 131 | function _toIndex() |
| 132 | { |
| 133 | $rev = $this->get_current_revision()->_toIndex(); |
| 134 | $str = str_repeat($this->title.' '.$this->summary.' ', 4).' '.$rev; |
| 135 | return Pluf_Text::cleanString(html_entity_decode($str, ENT_QUOTES, 'UTF-8')); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * We drop the information from the timeline. |
| 140 | */ |
| 141 | function preDelete() |
| 142 | { |
| 143 | IDF_Timeline::remove($this); |
| 144 | IDF_Search::remove($this); |
| 145 | } |
| 146 | |
| 147 | function get_current_revision() |
| 148 | { |
| 149 | $true = Pluf_DB_BooleanToDb(true, $this->getDbConnection()); |
| 150 | $rev = $this->get_revisions_list(array('filter' => 'is_head='.$true, |
| 151 | 'nb' => 1)); |
| 152 | return ($rev->count() == 1) ? $rev[0] : null; |
| 153 | } |
| 154 | |
| 155 | function preSave($create=false) |
| 156 | { |
| 157 | if ($this->id == '') { |
| 158 | $this->creation_dtime = gmdate('Y-m-d H:i:s'); |
| 159 | } |
| 160 | $this->modif_dtime = gmdate('Y-m-d H:i:s'); |
| 161 | } |
| 162 | |
| 163 | function postSave($create=false) |
| 164 | { |
| 165 | // Note: No indexing is performed here. The indexing is |
| 166 | // triggered in the postSave step of the revision to ensure |
| 167 | // that the page as a given revision in the database when |
| 168 | // doing the indexing. |
| 169 | if ($create) { |
| 170 | IDF_Timeline::insert($this, $this->get_project(), |
| 171 | $this->get_submitter()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns an HTML fragment used to display this wikipage in the |
| 177 | * timeline. |
| 178 | * |
| 179 | * The request object is given to be able to check the rights and |
| 180 | * as such create links to other items etc. You can consider that |
| 181 | * if displayed, you can create a link to it. |
| 182 | * |
| 183 | * @param Pluf_HTTP_Request |
| 184 | * @return Pluf_Template_SafeString |
| 185 | */ |
| 186 | public function timelineFragment($request) |
| 187 | { |
| 188 | $url = Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', |
| 189 | array($request->project->shortname, |
| 190 | $this->title)); |
| 191 | $out = '<tr class="log"><td><a href="'.$url.'">'. |
| 192 | Pluf_esc(Pluf_Template_dateAgo($this->creation_dtime, 'without')). |
| 193 | '</a></td><td>'; |
| 194 | $stag = new IDF_Template_ShowUser(); |
| 195 | $user = $stag->start($this->get_submitter(), $request, '', false); |
| 196 | $out .= sprintf(__('<a href="%1$s" title="View page">%2$s</a>, %3$s'), $url, Pluf_esc($this->title), Pluf_esc($this->summary)).'</td>'; |
| 197 | $out .= "\n".'<tr class="extra"><td colspan="2"> |
| 198 | <div class="helptext right">'.sprintf(__('Creation of <a href="%s">page %s</a>, by %s'), $url, Pluf_esc($this->title), $user).'</div></td></tr>'; |
| 199 | return Pluf_Template::markSafe($out); |
| 200 | } |
| 201 | |
| 202 | public function feedFragment($request) |
| 203 | { |
| 204 | $url = Pluf::f('url_base') |
| 205 | .Pluf_HTTP_URL_urlForView('IDF_Views_Wiki::view', |
| 206 | array($request->project->shortname, |
| 207 | $this->title)); |
| 208 | $title = sprintf(__('%s: Documentation page %s added - %s'), |
| 209 | $request->project->name, |
| 210 | $this->title, $this->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' => $this, |
| 217 | 'rev' => $this->get_current_revision(), |
| 218 | 'create' => true, |
| 219 | 'date' => $date) |
| 220 | ); |
| 221 | $tmpl = new Pluf_Template('idf/wiki/feedfragment.xml'); |
| 222 | return $tmpl->render($context); |
| 223 | } |
| 224 | } |