]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiBlog.php
More refactoring to use templated output. Use page meta "summary" field.
[SourceForge/phpwiki.git] / lib / plugin / WikiBlog.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiBlog.php,v 1.4 2003-01-11 22:23:00 carstenklapp Exp $');
3 /**
4  * Author: MichaelVanDam
5  */
6
7 require_once('lib/TextSearchQuery.php');
8 //require_once('lib/plugin/IncludePage.php');
9
10 /**
11  * This plugin shows 'blogs' (comments/news) associated with a
12  * particular page and provides an input form for adding a new blog.
13  *
14  * TODO:
15  *
16  * It also works as an action-page if you create a page called 'WikiBlog'
17  * containing this plugin.  This allows adding comments to any page
18  * by linking "PageName?action=WikiBlog".  Maybe a nice feature in
19  * lib/displaypage.php would be to automatically check if there are
20  * blogs for the given page, then provide a link to them somewhere on
21  * the page.  Or maybe this just creates a huge mess...
22  *
23  * Maybe it would be a good idea to ENABLE blogging of only certain
24  * pages by setting metadata or something...?  If a page is non-bloggable
25  * the plugin is ignored (perhaps with a warning message).
26  *
27  * Should blogs be by default filtered out of RecentChanges et al???
28  *
29  * Think of better name for this module: Blog? WikiLog? WebLog? WikiDot?
30  *
31  * Have other 'styles' for the plugin?... e.g. 'quiet'.  Display only
32  * 'This page has 23 associated comments. Click here to view / add.'
33  *
34  * For admin user, put checkboxes beside comments to allow for bulk removal.
35  *
36  * Permissions for who can add blogs?  Display entry box only if
37  * user meets these requirements...?
38  *
39  * Code cleanup: break into functions, use templates (or at least remove CSS)
40  *
41  */
42
43
44 class WikiPlugin_WikiBlog
45 extends WikiPlugin
46 {
47     function getName () {
48         return _("WikiBlog");
49     }
50
51     function getDescription () {
52         return sprintf(_("Show and add blogs for %s"),'[pagename]');
53     }
54
55     function getVersion() {
56         return preg_replace("/[Revision: $]/", '',
57                             "\$Revision: 1.4 $");
58     }
59
60     // Arguments:
61     //
62     //  page - page which is blogged to (default current page)
63     //
64     //  order - 'normal' - place in chronological order
65     //        - 'reverse' - place in reverse chronological order
66     //
67     //  mode - 'show' - only show old blogs
68     //         'add' - only show entry box for new blog
69     //         'show,add' - show old blogs then entry box
70     //         'add,show' - show entry box followed by old blogs
71     //
72     // TODO:
73     //
74     // - arguments to allow selection of time range to display
75     // - arguments to display only XX blogs per page (can this 'paging'
76     //    co-exist with the wiki??  difficult)
77     // - arguments to allow comments outside this range to be
78     //    display as e.g. June 2002 archive, July 2002 archive, etc..
79     // - captions for 'show' and 'add' sections
80
81
82     function getDefaultArguments() {
83         return array('page'       => '[pagename]',
84                      'order'      => 'normal',
85                      'mode'       => 'show,add',
86                      'noheader'   => false
87                     );
88     }
89
90
91     function run($dbi, $argstr, $request) {
92         $this->_args = $this->getArgs($argstr, $request);
93         extract($this->_args);
94         if (!$page)
95             return '';
96
97
98         // Look at arguments to see if blog was submitted.  If so,
99         // process this before displaying anything.
100
101         if ($request->getArg('addblog')) {
102
103             // TODO: change args to blog[page], blog[summary], blog[body] etc
104
105             $blog_page = $request->getArg('blog[page]');
106
107
108             // Generate the page name.  For now, we use the format:
109             //   Rootname/Blog-YYYYMMDDHHMMSS
110             // This gives us natural chronological order when sorted
111             // alphabetically.
112             // This is inside a loop because there is a small
113             // chance that another user could add a blog with
114             // the same timestamp.  If such a conflict is
115             // detected, increment timestamp by 1 second until
116             // a unique name is found.
117
118             $now = time();
119
120             $p = false;  // will store our page
121             $time = false; // will store our timestamp
122
123             $saved = false;
124             while (!$saved) {
125
126 //change to ISO formatted time? 2003-01-11T24:03:02
127                 $time = strftime ('%Y%m%d%H%M%S', $now);
128
129 //add a subpage between, to allow one to create a page containing only all the blogs of the parent page
130 //                $p = $dbi->getPage($blog_page . SUBPAGE_SEPARATOR . _("Comments") . SUBPAGE_SEPARATOR . "Blog-$time");
131                 $p = $dbi->getPage($blog_page . SUBPAGE_SEPARATOR . "Blog-$time");
132
133                 $pr = $p->getCurrentRevision();
134
135                 // Version should be zero.  If not, page already exists
136                 // so increment timestamp and try again.
137
138                 if ($pr->getVersion() > 0) $now ++;
139                 else $saved = true;
140
141             }
142
143
144             // Generate meta-data for page
145             // This is info that won't change for each revision:
146             //   ctime (time of creation), creator, and creator_id.
147             // Create-date is really the relevant date for a blog,
148             // not the last-modified date.
149
150             // FIXME:  For now all blogs are locked.  It would be
151             // nice to allow only the 'creator' to edit by default.
152
153             $user = $request->getUser();
154             $user_id = $user->getId();
155             $user_auth_id = $user->getAuthenticatedId();
156
157             $p->set ('ctime', $now);
158             $p->set ('creator', $user_id);
159             $p->set ('creator_id', $user_auth_id);
160             $p->set ('locked', true);  // lock by default
161             $p->set ('pagetype', 'wikiblog');
162
163             // Generate meta-data for page revision
164
165             $meta = array('author' => $user_id,
166                           'author_id' => $user_auth_id,
167                           'markup' => 2.0,   // assume new markup
168                           'summary' => _("New comment."),
169                           );
170
171             // FIXME: For now the page format is:
172             //
173             //   __Summary__
174             //   Body
175             //
176             // This helps during editing (using standard editor)
177             // when the page/revision metadata is not available
178             // to the user.  If we had a pagetype-specific editor
179             // then we could put the Summary into meta-data and
180             // still make it available for editing.
181             // Also, it helps for now during rendering because we
182             // don't need to create a new PageType class just yet...
183
184             // FIXME: move summary into the page's summary field
185             // PageType now displays the summary field when viewing
186             // an individual blog page
187             $summary = trim($request->getArg('blog[summary]'));
188             $body = trim($request->getArg('blog[body]'));
189
190             if ($summary)
191                 $meta['summary'] = $summary;
192
193             $pr = $p->createRevision($pr->getVersion()+1,
194                                      $body,
195                                      $meta,
196                                      ExtractWikiPageLinks($body));
197
198             // FIXME: Detect if !$pr ??  How to handle errors?
199
200             // Save was successful.  Unset all the arguments and
201             // redirect to page that user was viewing before.
202             // Unsetting arguments will prevent double-submitting
203             // problem and will clear out the text-boxes.
204
205             $request->setArg('addblog', false);
206             $request->setArg('blog[page]', false);
207             $request->setArg('blog[body]', false);
208             $request->setArg('blog[summary]', false);
209
210             $url = $request->getURLtoSelf();
211             $request->redirect($url);
212
213             // FIXME: when submit a comment from preview mode,
214             // adds the comment properly but jumps to browse mode.
215             // Any way to jump back to preview mode???
216
217
218             // The rest of the output will not be seen due to
219             // the redirect.
220         }
221
222         // Now we display previous comments and/or provide entry box
223         // for new comments
224
225         $showblogs = true;
226         $showblogform = true;
227
228         switch ($mode) {
229
230             case 'show':
231                 $showblogform = false;
232
233             case 'add':
234                 $showblogs = false;
235
236             case 'show,add':
237             case 'add,show':
238             default:
239
240             // TODO: implement ordering show,add vs add,show !
241
242         }
243
244
245         $html = HTML();
246
247         if ($showblogs) {
248             $html->pushContent($this->showBlogs ($dbi, $request, $page, $order));
249         }
250
251         if ($showblogform) {
252             $html->pushContent($this->showBlogForm ($dbi, $request, $page));
253         }
254
255         return $html;
256
257     }
258
259
260     function showBlogs ($dbi, $request, $page, $order) {
261
262         // Display comments:
263
264         // FIXME: currently blogSearch uses WikiDB->titleSearch to
265         // get results, so results are in alphabetical order.
266         // When PageTypes fully implemented, could have smarter
267         // blogSearch implementation / naming scheme.
268
269         $pages = $dbi->blogSearch($page, $order);
270
271         $all_comments = HTML();
272
273         while ($p = $pages->next()) {
274
275             // FIXME:
276             // Verify that it is a blog page.  If not, go to next page.
277             // When we proper blogSearch implementation this will not
278             // be necessary.  Non-blog pages will not be returned by
279             // blogSearch.
280
281             $name = $p->getName();
282             // If page contains '/', we must escape them
283             // FIXME: only works for '/' SUBPAGE_SEPARATOR
284             $escpage = preg_replace ("/\//", '\/', $page);
285             if (!preg_match("/^$escpage\/Blog-([[:digit:]]{14})$/", $name, $matches))
286                 continue;
287
288             // Display contents:
289
290             // TODO: ultimately have a function in PageType to handle
291             // display of blog entries...
292
293             // If we want to use IncludePage plugin to display blog:
294             // $i = new WikiPlugin_IncludePage;
295             // $html->pushContent($i->run($dbi, "page=$name", $request));
296
297             global $WikiNameRegexp;
298             global $Theme;
299
300             $ctime = $p->get('ctime');
301             $ctime = $Theme->formatDateTime($ctime);
302
303             $creator = $p->get('creator');
304             $creator_id = $p->get('creator_id');
305             $creator_orig = $creator;
306
307             if (preg_match("/^$WikiNameRegexp\$/", $creator)
308                                && $dbi->isWikiPage($creator))
309                 $creator = WikiLink($creator);
310
311             $pr = $p->getCurrentRevision();
312
313             $modified = ($pr->getVersion() > 1);
314
315             $mtime = $pr->get('mtime');
316             $mtime = $Theme->formatDateTime($mtime);
317
318             $author    = $pr->get('author');
319             $author_id = $pr->get('author_id');
320             $author_orig = $author;
321
322             $summary = $pr->get('summary');
323
324             if (preg_match("/^$WikiNameRegexp\$/", $author)
325                                && $dbi->isWikiPage($author))
326                 $authorlink = WikiLink($author);
327
328             $browseaction = WikiURL($name);
329             $editaction = WikiURL($name, array('action' => 'edit'));
330             $removeaction = WikiURL($name, array('action' => 'remove'));
331
332             $browselink = HTML::a(array('href' => $browseaction,
333                                         'class'=>'wiki'), $ctime);
334             $editlink = HTML::a(array('href' => $editaction,
335                                       'class' => 'wikiaction'), 'Edit');
336             $removelink = HTML::a(array('href' => $removeaction,
337                                         'class'=>'wikiadmin'), 'Delete');
338
339             // FIXME if necessary:
340             $creator_id_string = (strcmp($creator_orig, $creator_id) == 0) ? '' : ' (' . $creator_id . ')';
341
342             // FIXME if necessary:
343             $author_id_string = (strcmp($author_orig, $author_id) == 0) ? '' : ' (' . $author_id . ')';
344
345             // FIXME: for now we just show all links on all entries.
346             // This should be customizable.
347
348             @$blognumber++; //hackish fixme                                              
349             $args['SHOWHEADER'] = (! $this->_args['noheader'])
350                                   && ($blognumber == 1);
351             $args['SHOWBLOGFORM'] = false;
352             $args['SHOWBLOG'] = true;
353
354             $args['PAGELINK'] = WikiLink($page);
355             $args['PAGENAME'] = $page;
356             $args['AUTHOR'] = $author;
357
358              // FIXME: use $request's api to get user
359             $isadmin = $request->_user->isadmin();
360             $isauthor = $request->_user->getId() == $author;
361
362             $args['EDITLINK'] = ($isadmin) || ($isauthor) ? $editlink : "";
363             $args['REMOVELINK'] = ($isadmin) ? $removelink : "";
364             $args['AUTHORLINK'] = $authorlink;
365             $args['SUMMARY'] = $summary;
366             $args['CONTENT'] = TransformInline($pr->getPackedContent());
367             $args['DATELINK'] = $browselink;
368
369             $args['datemodified'] = $modified ? $mtime : '';
370             $args['authormodified'] = $pr->get('author');
371             $blogformtemplate = new Template('blog', $request, $args);
372             $all_comments->pushContent($blogformtemplate);
373         }
374         return $all_comments;
375     }
376
377     function showBlogForm ($dbi, $request, $page) {
378         // Show blog-entry form.
379             $args['PAGENAME'] = $page;
380             $args['SHOWHEADER'] = false;
381             $args['SHOWBLOGFORM'] = true;
382             $args['SHOWBLOG'] = false;
383             $blogformtemplate = new Template('blog', $request, $args);
384             return $blogformtemplate;
385     }
386 };
387
388 // $Log: not supported by cvs2svn $
389 // Revision 1.3  2003/01/06 02:29:02  carstenklapp
390 // New: use blog.tmpl template to format output. Some cosmetic
391 // issues, it mostly works but code still needs cleanup. Added
392 // getVersion() for PluginManager.
393 //
394
395 // For emacs users
396 // Local Variables:
397 // mode: php
398 // tab-width: 8
399 // c-basic-offset: 4
400 // c-hanging-comment-ender-p: nil
401 // indent-tabs-mode: nil
402 // End:
403 ?>