]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/doc.php
Merge branch 'master' into 0.9.x
[quix0rs-gnu-social.git] / actions / doc.php
1 <?php
2
3 /**
4  * Documentation action.
5  *
6  * PHP version 5
7  *
8  * @category Action
9  * @package  StatusNet
10  * @author   Evan Prodromou <evan@status.net>
11  * @author   Robin Millette <millette@status.net>
12  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13  * @link     http://status.net/
14  *
15  * StatusNet - the distributed open-source microblogging tool
16  * Copyright (C) 2008, 2009, StatusNet, Inc.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Affero General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU Affero General Public License for more details.
27  *
28  * You should have received a copy of the GNU Affero General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 /**
37  * Documentation class.
38  *
39  * @category Action
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Robin Millette <millette@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
44  * @link     http://status.net/
45  */
46 class DocAction extends Action
47 {
48     var $output   = null;
49     var $filename = null;
50     var $title    = null;
51
52     function prepare($args)
53     {
54         parent::prepare($args);
55
56         $this->title  = $this->trimmed('title');
57         $this->output = null;
58
59         $this->loadDoc();
60         return true;
61     }
62
63     /**
64      * Handle a request
65      *
66      * @param array $args array of arguments
67      *
68      * @return nothing
69      */
70     function handle($args)
71     {
72         parent::handle($args);
73         $this->showPage();
74     }
75
76     /**
77      * Page title
78      *
79      * Gives the page title of the document. Override default for hAtom entry.
80      *
81      * @return void
82      */
83
84     function showPageTitle()
85     {
86         $this->element('h1', array('class' => 'entry-title'), $this->title());
87     }
88
89     /**
90      * Block for content.
91      *
92      * Overrides default from Action to wrap everything in an hAtom entry.
93      *
94      * @return void.
95      */
96
97     function showContentBlock()
98     {
99         $this->elementStart('div', array('id' => 'content', 'class' => 'hentry'));
100         $this->showPageTitle();
101         $this->showPageNoticeBlock();
102         $this->elementStart('div', array('id' => 'content_inner',
103                                          'class' => 'entry-content'));
104         // show the actual content (forms, lists, whatever)
105         $this->showContent();
106         $this->elementEnd('div');
107         $this->elementEnd('div');
108     }
109
110     /**
111      * Display content.
112      *
113      * Shows the content of the document.
114      *
115      * @return void
116      */
117
118     function showContent()
119     {
120         $this->raw($this->output);
121     }
122
123     /**
124      * Page title.
125      *
126      * Uses the title of the document.
127      *
128      * @return page title
129      */
130     function title()
131     {
132         return ucfirst($this->title);
133     }
134
135     /**
136      * These pages are read-only.
137      *
138      * @param array $args unused.
139      *
140      * @return boolean read-only flag (false)
141      */
142
143     function isReadOnly($args)
144     {
145         return true;
146     }
147
148     function loadDoc()
149     {
150         if (Event::handle('StartLoadDoc', array(&$this->title, &$this->output))) {
151
152             $this->filename = $this->getFilename();
153
154             if (empty($this->filename)) {
155                 throw new ClientException(sprintf(_('No such document "%s"'), $this->title), 404);
156             }
157
158             $c = file_get_contents($this->filename);
159
160             $this->output = common_markup_to_html($c);
161
162             Event::handle('EndLoadDoc', array($this->title, &$this->output));
163         }
164     }
165
166     function getFilename()
167     {
168         if (file_exists(INSTALLDIR.'/local/doc-src/'.$this->title)) {
169             $localDef = INSTALLDIR.'/local/doc-src/'.$this->title;
170         }
171
172         $local = glob(INSTALLDIR.'/local/doc-src/'.$this->title.'.*');
173
174         if (count($local) || isset($localDef)) {
175             return $this->negotiateLanguage($local, $localDef);
176         }
177
178         if (file_exists(INSTALLDIR.'/doc-src/'.$this->title)) {
179             $distDef = INSTALLDIR.'/doc-src/'.$this->title;
180         }
181
182         $dist = glob(INSTALLDIR.'/doc-src/'.$this->title.'.*');
183
184         if (count($dist) || isset($distDef)) {
185             return $this->negotiateLanguage($dist, $distDef);
186         }
187
188         return null;
189     }
190
191     function negotiateLanguage($filenames, $defaultFilename=null)
192     {
193         // XXX: do this better
194
195         $langcode = common_language();
196
197         foreach ($filenames as $filename) {
198             if (preg_match('/\.'.$langcode.'$/', $filename)) {
199                 return $filename;
200             }
201         }
202
203         return $defaultFilename;
204     }
205 }