]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiBlog.php
possible fix for problem reported by Whit Blauvelt
[SourceForge/phpwiki.git] / lib / plugin / WikiBlog.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiBlog.php,v 1.14 2004-03-29 21:33:32 rurban Exp $');
3 /*
4  Copyright 2002, 2003 $ThePhpWikiProgrammingTeam
5  
6  This file is part of PhpWiki.
7
8  PhpWiki 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  PhpWiki 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 PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 /**
23  * @author: MichaelVanDam, major refactor by JeffDairiki
24  */
25
26 require_once('lib/TextSearchQuery.php');
27
28 /**
29  * This plugin shows 'blogs' (comments/news) associated with a
30  * particular page and provides an input form for adding a new blog.
31  *
32  * Now it is also the base class for all attachable pagetypes: 
33  *    wikiblog, comment and wikiforum
34  *
35  * HINTS/COMMENTS:
36  *
37  * To have comments show up on a separate page:
38  * On TopPage, use
39  *   <?plugin WikiBlog mode=add?>
40  * Create TopPage/Comments with:
41  *   <?plugin WikiBlog page=TopPage mode=show?>
42  *
43  * TODO:
44  *
45  * It also works as an action-page if you create a page called 'WikiBlog'
46  * containing this plugin.  This allows adding comments to any page
47  * by linking "PageName?action=WikiBlog".  Maybe a nice feature in
48  * lib/displaypage.php would be to automatically check if there are
49  * blogs for the given page, then provide a link to them somewhere on
50  * the page.  Or maybe this just creates a huge mess...
51  *
52  * Maybe it would be a good idea to ENABLE blogging of only certain
53  * pages by setting metadata or something...?  If a page is non-bloggable
54  * the plugin is ignored (perhaps with a warning message).
55  *
56  * Should blogs be by default filtered out of RecentChanges et al???
57  *
58  * Think of better name for this module: Blog? WikiLog? WebLog? WikiDot?
59  *
60  * Have other 'styles' for the plugin?... e.g. 'quiet'.  Display only
61  * 'This page has 23 associated comments. Click here to view / add.'
62  *
63  * For admin user, put checkboxes beside comments to allow for bulk removal.
64  *
65  * Permissions for who can add blogs?  Display entry box only if
66  * user meets these requirements...?
67  *
68  * Code cleanup: break into functions, use templates (or at least remove CSS)
69  *
70  */
71
72
73 class WikiPlugin_WikiBlog
74 extends WikiPlugin
75 {
76     function getName () {
77         return _("WikiBlog");
78     }
79
80     function getDescription () {
81         return sprintf(_("Show and add blogs for %s"),'[pagename]');
82     }
83
84     function getVersion() {
85         return preg_replace("/[Revision: $]/", '',
86                             "\$Revision: 1.14 $");
87     }
88
89     // Arguments:
90     //
91     //  page - page which is blogged to (default current page)
92     //
93     //  order - 'normal' - place in chronological order
94     //        - 'reverse' - place in reverse chronological order
95     //
96     //  mode - 'show' - only show old blogs
97     //         'add' - only show entry box for new blog
98     //         'show,add' - show old blogs then entry box
99     //         'add,show' - show entry box followed by old blogs
100     //
101     // TODO:
102     //
103     // - arguments to allow selection of time range to display
104     // - arguments to display only XX blogs per page (can this 'paging'
105     //    co-exist with the wiki??  difficult)
106     // - arguments to allow comments outside this range to be
107     //    display as e.g. June 2002 archive, July 2002 archive, etc..
108     // - captions for 'show' and 'add' sections
109
110
111     function getDefaultArguments() {
112         return array('pagename'   => '[pagename]',
113                      'order'      => 'normal',
114                      'mode'       => 'show,add',
115                      'noheader'   => false
116                     );
117     }
118
119     function run($dbi, $argstr, &$request, $basepage) {
120         $args = $this->getArgs($argstr, $request);
121         if (!$args['pagename'])
122             return $this->error(_("No pagename specified"));
123
124         // Get our form args.
125         $blog = $request->getArg("blog");
126         $request->setArg('blog', false);
127             
128         if ($request->isPost() and !empty($blog['addblog'])) {
129             $this->add($request, $blog); // noreturn
130         }
131
132         // Now we display previous comments and/or provide entry box
133         // for new comments
134         $html = HTML();
135         foreach (explode(',', $args['mode']) as $show) {
136             if (!empty($seen[$show]))
137                 continue;
138             $seen[$show] = 1;
139                 
140             switch ($show) {
141             case 'show':
142                 $html->pushContent($this->showAll($request, $args));
143                 break;
144             case 'add':
145                 $html->pushContent($this->showForm($request, $args));
146                 break;
147             default:
148                 return $this->error(sprintf("Bad mode ('%s')", $show));
149             }
150         }
151         return $html;
152     }
153
154     function add (&$request, $blog, $type='wikiblog') {
155         if (!($parent = $blog['pagename']))
156             $request->finish(fmt("No pagename specified for %s",$type));
157
158         $user = $request->getUser();
159         $now = time();
160         $dbi = $request->getDbh();
161         
162         /*
163          * Page^H^H^H^H Blog meta-data
164          * This method is reused for all attachable pagetypes: wikiblog, comment and wikiforum
165          *
166          * This is info that won't change for each revision.
167          * Nevertheless, it's now stored in the revision meta-data.
168          * Several reasons:
169          *  o It's more convenient to have all information required
170          *    to render a page revision in the revision meta-data.
171          *  o We can avoid a race condition, since version meta-data
172          *    updates are atomic with the version creation.
173          */
174
175         $blog_meta = array('ctime'      => $now,
176                            'creator'    => $user->getId(),
177                            'creator_id' => $user->getAuthenticatedId(),
178                            );
179         
180
181         // Version meta-data
182         $summary = trim($blog['summary']);
183         $version_meta = array('author'    => $blog_meta['creator'],
184                               'author_id' => $blog_meta['creator_id'],
185                               'markup'    => 2.0,   // assume new markup
186                               'summary'   => $summary ? $summary : _("New comment."),
187                               'mtime'     => $now,
188                               'pagetype'  => $type,
189                               $type       => $blog_meta,
190                               );
191         if ($type == 'comment')
192             unset($version_meta['summary']);
193
194         // Comment body.
195         $body = trim($blog['body']);
196
197         $saved = false;
198         while (!$saved) {
199             // Generate the page name.  For now, we use the format:
200             //   Rootname/Blog/2003-01-11/14:03:02+00:00
201             // This gives us natural chronological order when sorted
202             // alphabetically.
203
204             $time = Iso8601DateTime();
205             if ($type == 'wikiblog')
206                 $pagename = "Blog";
207             elseif ($type == 'comment')
208                 $pagename = "Comment";
209             elseif ($type == 'wikiforum')
210                 $pagename = "Topic";
211
212             // Check intermediate pages. If not existing they should RedirectTo the parent page.
213             $redirected = $parent . SUBPAGE_SEPARATOR . $pagename;
214             if (!$dbi->isWikiPage($redirected)) {
215                 require_once('lib/loadsave.php');
216                 $pageinfo = array('pagename' => $redirected,
217                                   'content'  => '<?plugin RedirectTo page='.$parent.' ?>',
218                                   'pagedata' => array(),
219                                   'versiondata' => array('author' => $blog_meta['creator']),
220                                   );
221                 SavePage($request, $pageinfo, '', '');
222             }
223             $redirected = $parent . SUBPAGE_SEPARATOR . $pagename . SUBPAGE_SEPARATOR . preg_replace("/T.*/", "", "$time");
224             if (!$dbi->isWikiPage($redirected)) {
225                 require_once('lib/loadsave.php');
226                 $pageinfo = array('pagename' => $redirected,
227                                   'content'  => '<?plugin RedirectTo page='.$parent.' ?>',
228                                   'pagedata' => array(),
229                                   'versiondata' => array('author' => $blog_meta['creator']),
230                                   );
231                 SavePage($request, $pageinfo, '', '');
232             }
233
234             $p = $dbi->getPage($parent . SUBPAGE_SEPARATOR . $pagename . SUBPAGE_SEPARATOR . str_replace("T", SUBPAGE_SEPARATOR, "$time"));
235             $pr = $p->getCurrentRevision();
236
237             // Version should be zero.  If not, page already exists
238             // so increment timestamp and try again.
239             if ($pr->getVersion() > 0) {
240                 $now++;
241                 continue;
242             }
243
244             // FIXME: there's a slight, but currently unimportant
245             // race condition here.  If someone else happens to
246             // have just created a blog with the same name,
247             // we'll have locked it before we discover that the name
248             // is taken.
249             /*
250              * FIXME:  For now all blogs are locked.  It would be
251              * nice to allow only the 'creator' to edit by default.
252              */
253             $p->set('locked', true); //lock by default
254             $saved = $p->save($body, 1, $version_meta);
255
256             $now++;
257         }
258         
259         $dbi->touch();
260         $request->redirect($request->getURLtoSelf()); // noreturn
261
262         // FIXME: when submit a comment from preview mode,
263         // adds the comment properly but jumps to browse mode.
264         // Any way to jump back to preview mode???
265     }
266
267     function showAll (&$request, $args, $type="wikiblog") {
268         // FIXME: currently blogSearch uses WikiDB->titleSearch to
269         // get results, so results are in alphabetical order.
270         // When PageTypes fully implemented, could have smarter
271         // blogSearch implementation / naming scheme.
272         
273         $dbi = $request->getDbh();
274
275         $parent = $args['pagename'];
276         $blogs = $this->findBlogs($dbi, $parent, $type);
277         $html = HTML();
278         if ($blogs) {
279             // First reorder
280             usort($blogs, array("WikiPlugin_WikiBlog",
281                                 "cmp"));
282             if ($args['order'] == 'reverse')
283                 $blogs = array_reverse($blogs);
284            
285             if (!$args['noheader'])
286                 $html->pushContent(HTML::h4(array('class' => "$type-heading"),
287                                             fmt("Comments on %s:", WikiLink($parent))));
288             foreach ($blogs as $rev) {
289                 if (!$rev->get($type)) {
290                     // Ack! this is an old-style blog with data ctime in page meta-data.
291                     $content = $this->_transformOldFormatBlog($rev,$type);
292                 }
293                 else {
294                     $content = $rev->getTransformedContent($type);
295                 }
296                 $html->pushContent($content);
297             }
298             
299         }
300         return $html;
301     }
302
303     function _transformOldFormatBlog($rev, $type='wikiblog') {
304         $page = $rev->getPage();
305         $metadata = array();
306         foreach (array('ctime', 'creator', 'creator_id') as $key)
307             $metadata[$key] = $page->get($key);
308         if (empty($metadata) and $type != 'wikiblog')
309             $metadata[$key] = $page->get('wikiblog');
310         $meta = $rev->getMetaData();
311         $meta[$type] = $metadata;
312         return new TransformedText($page, $rev->getPackedContent(), $meta, $type);
313     }
314
315     function findBlogs (&$dbi, $parent, $type='wikiblog') {
316         $prefix = $parent . SUBPAGE_SEPARATOR;
317         $pfxlen = strlen($prefix);
318             require_once('lib/TextSearchQuery.php');
319         $pages = $dbi->titleSearch(new TextSearchQuery ($prefix));
320
321         $blogs = array();
322         while ($page = $pages->next()) {
323             // FIXME:
324             // Verify that it is a blog page.  If not, go to next page.
325             // When we proper blogSearch implementation this will not
326             // be necessary. 
327             $name = $page->getName();
328             if (substr($name, 0, $pfxlen) != $prefix)
329                 continue;
330             $current = $page->getCurrentRevision();
331 //use only pagetype
332             if (/*preg_match("/^Blog-([[:digit:]]{14})$/", substr($name, $pfxlen))
333                 or */$current->get('pagetype') == $type) {
334                 $blogs[] = $current;
335             }
336         }
337         return $blogs;
338     }
339
340     function cmp($a, $b) {
341         return(strcmp($a->get('mtime'),
342                       $b->get('mtime')));
343     }
344     
345     function showForm (&$request, $args, $template='blogform') {
346         // Show blog-entry form.
347         return new Template($template, $request,
348                             array('PAGENAME' => $args['pagename']));
349     }
350 };
351
352 // $Log: not supported by cvs2svn $
353 // Revision 1.13  2004/03/15 10:59:40  rurban
354 // fix also early attach type bug, attached as wikiblog pagetype
355 //
356 // Revision 1.11  2004/03/12 20:59:31  rurban
357 // important cookie fix by Konstantin Zadorozhny
358 // new editpage feature: JS_SEARCHREPLACE
359 //
360 // Revision 1.10  2004/03/12 17:32:41  rurban
361 // new base class PageType_attach as base class for WikiBlog, Comment, and WikiForum.
362 // new plugin AddComment, which is a WikiBlog with different pagetype and template,
363 //   based on WikiBlog. WikiForum comes later.
364 //
365 // Revision 1.9  2004/02/27 02:10:50  rurban
366 // Patch #891133 by pablom517
367 //   "WikiBlog Plugin now sorts logs correctly"
368 //
369 // Revision 1.8  2004/02/17 12:11:36  rurban
370 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
371 //
372 // Revision 1.7  2003/11/17 16:23:55  carstenklapp
373 // Switched to Iso8601DateTime and more use of SUBPAGE_SEPARATOR. This
374 // allows plugin UnfoldSubpages (for example) to be added to page
375 // XxYy/Blog/ where desired, for a view of all Blogs in one day. This
376 // change should not break existing BLOGs, we are only checking for
377 // pagetype == 'wikiblog' now instead of relying on the subpage name to
378 // collect blog subpages. (** WARNING: Do not add UnfoldSubpages to both
379 // XxYy/Blog/ and XxYy/Blog/2003-11/16/ pages, due to recursion bug in
380 // UnfoldSubpages plugin.)
381 //
382 // Revision 1.6  2003/02/21 04:20:09  dairiki
383 // Big refactor. Formatting now done by the stuff in PageType.php.
384 // Split the template into two separate ones: one for the add comment form,
385 // one for comment display.
386 //
387 // Revision 1.5  2003/02/16 19:47:17  dairiki
388 // Update WikiDB timestamp when editing or deleting pages.
389 //
390 // Revision 1.4  2003/01/11 22:23:00  carstenklapp
391 // More refactoring to use templated output. Use page meta "summary" field.
392 //
393 // Revision 1.3  2003/01/06 02:29:02  carstenklapp
394 // New: use blog.tmpl template to format output. Some cosmetic
395 // issues, it mostly works but code still needs cleanup. Added
396 // getVersion() for PluginManager.
397 //
398
399 // For emacs users
400 // Local Variables:
401 // mode: php
402 // tab-width: 8
403 // c-basic-offset: 4
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil
406 // End:
407 ?>