]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiBlog.php
new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
[SourceForge/phpwiki.git] / lib / plugin / WikiBlog.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiBlog.php,v 1.19 2004-11-26 18:39:02 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.19 $");
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 = substr($summary,0,12);
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 ($type == 'wikiblog')
286                 $name = _("Blogs");
287             elseif ($type == 'comment')
288                 $name = _("Comments");
289             elseif ($type == 'wikiforum')
290                 $name = _("Messages");
291             if (!$args['noheader'])
292                 $html->pushContent(HTML::h4(array('class' => "$type-heading"),
293                                             fmt("%s on %s:", $name, WikiLink($parent))));
294             foreach ($blogs as $rev) {
295                 if (!$rev->get($type)) {
296                     // Ack! this is an old-style blog with data ctime in page meta-data.
297                     $content = $this->_transformOldFormatBlog($rev,$type);
298                 }
299                 else {
300                     $content = $rev->getTransformedContent($type);
301                 }
302                 $html->pushContent($content);
303             }
304             
305         }
306         return $html;
307     }
308
309     function _transformOldFormatBlog($rev, $type='wikiblog') {
310         $page = $rev->getPage();
311         $metadata = array();
312         foreach (array('ctime', 'creator', 'creator_id') as $key)
313             $metadata[$key] = $page->get($key);
314         if (empty($metadata) and $type != 'wikiblog')
315             $metadata[$key] = $page->get('wikiblog');
316         $meta = $rev->getMetaData();
317         $meta[$type] = $metadata;
318         return new TransformedText($page, $rev->getPackedContent(), $meta, $type);
319     }
320
321     function findBlogs (&$dbi, $parent, $type='wikiblog') {
322         $prefix = $parent . SUBPAGE_SEPARATOR;
323         $pfxlen = strlen($prefix);
324         require_once('lib/TextSearchQuery.php');
325         $pages = $dbi->titleSearch(new TextSearchQuery($prefix, true));
326
327         $blogs = array();
328         while ($page = $pages->next()) {
329             $name = $page->getName();
330             if (substr($name, 0, $pfxlen) != $prefix)
331                 continue;
332             $current = $page->getCurrentRevision();
333             if ($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.18  2004/05/14 20:55:04  rurban
354 // simplified RecentComments
355 //
356 // Revision 1.17  2004/05/14 17:33:12  rurban
357 // new plugin RecentChanges
358 //
359 // Revision 1.16  2004/04/19 18:27:46  rurban
360 // Prevent from some PHP5 warnings (ref args, no :: object init)
361 //   php5 runs now through, just one wrong XmlElement object init missing
362 // Removed unneccesary UpgradeUser lines
363 // Changed WikiLink to omit version if current (RecentChanges)
364 //
365 // Revision 1.15  2004/04/18 05:42:17  rurban
366 // more fixes for page="0"
367 // better WikiForum support
368 //
369 // Revision 1.14  2004/03/29 21:33:32  rurban
370 // possible fix for problem reported by Whit Blauvelt
371 //   Message-ID: <20040327211707.GA22374@free.transpect.com>
372 // create intermediate redirect subpages for blog/comment/forum
373 //
374 // Revision 1.13  2004/03/15 10:59:40  rurban
375 // fix also early attach type bug, attached as wikiblog pagetype
376 //
377 // Revision 1.11  2004/03/12 20:59:31  rurban
378 // important cookie fix by Konstantin Zadorozhny
379 // new editpage feature: JS_SEARCHREPLACE
380 //
381 // Revision 1.10  2004/03/12 17:32:41  rurban
382 // new base class PageType_attach as base class for WikiBlog, Comment, and WikiForum.
383 // new plugin AddComment, which is a WikiBlog with different pagetype and template,
384 //   based on WikiBlog. WikiForum comes later.
385 //
386 // Revision 1.9  2004/02/27 02:10:50  rurban
387 // Patch #891133 by pablom517
388 //   "WikiBlog Plugin now sorts logs correctly"
389 //
390 // Revision 1.8  2004/02/17 12:11:36  rurban
391 // 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, ...)
392 //
393 // Revision 1.7  2003/11/17 16:23:55  carstenklapp
394 // Switched to Iso8601DateTime and more use of SUBPAGE_SEPARATOR. This
395 // allows plugin UnfoldSubpages (for example) to be added to page
396 // XxYy/Blog/ where desired, for a view of all Blogs in one day. This
397 // change should not break existing BLOGs, we are only checking for
398 // pagetype == 'wikiblog' now instead of relying on the subpage name to
399 // collect blog subpages. (** WARNING: Do not add UnfoldSubpages to both
400 // XxYy/Blog/ and XxYy/Blog/2003-11/16/ pages, due to recursion bug in
401 // UnfoldSubpages plugin.)
402 //
403 // Revision 1.6  2003/02/21 04:20:09  dairiki
404 // Big refactor. Formatting now done by the stuff in PageType.php.
405 // Split the template into two separate ones: one for the add comment form,
406 // one for comment display.
407 //
408 // Revision 1.5  2003/02/16 19:47:17  dairiki
409 // Update WikiDB timestamp when editing or deleting pages.
410 //
411 // Revision 1.4  2003/01/11 22:23:00  carstenklapp
412 // More refactoring to use templated output. Use page meta "summary" field.
413 //
414 // Revision 1.3  2003/01/06 02:29:02  carstenklapp
415 // New: use blog.tmpl template to format output. Some cosmetic
416 // issues, it mostly works but code still needs cleanup. Added
417 // getVersion() for PluginManager.
418 //
419
420 // For emacs users
421 // Local Variables:
422 // mode: php
423 // tab-width: 8
424 // c-basic-offset: 4
425 // c-hanging-comment-ender-p: nil
426 // indent-tabs-mode: nil
427 // End:
428 ?>