]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiBlog.php
Normalize header
[SourceForge/phpwiki.git] / lib / plugin / WikiBlog.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2002,2003,2007,2009 $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 (as AddComment)
24  * @author: Changed as baseclass to AddComment and WikiForum and EditToolbar integration by ReiniUrban.
25  */
26
27 require_once('lib/TextSearchQuery.php');
28
29 /**
30  * This plugin shows 'blogs' (comments/news) associated with a
31  * particular page and provides an input form for adding a new blog.
32  *
33  * USAGE:
34  * Add <?plugin WikiBlog ?> at your PersonalPage and BlogArchive and 
35  * BlogJournal will find the Blog entries automatically.
36  *
37  * Now it is also the base class for all attachable pagetypes: 
38  *    "wikiblog", "comment" and "wikiforum"
39  *
40  * HINTS/COMMENTS:
41  *
42  * To have the blog show up on a seperate page:
43  * On TopPage, use
44  *   <?plugin WikiBlog mode=add?>
45  * Create TopPage/Blog with this page as actionpage:
46  *   <?plugin WikiBlog pagename=TopPage mode=show?>
47  *
48  * To have the main ADMIN_USER Blog appear under Blog and not under WikiBlog/Blog
49  * or UserName/Blog as for other users blogs,
50  * define BLOG_DEFAULT_EMPTY_PREFIX=true 
51  * use the page Blog as basepage
52  * and user="" (as default for ADMIN or current user) and pagename="Blog" 
53  * in the various blog plugins (BlogArchives, BlogJournal)
54  *
55  * TODO:
56  *
57  * It also works as an action-page if you create a page called 'WikiBlog'
58  * containing this plugin.  This allows adding comments to any page
59  * by linking "PageName?action=WikiBlog".  Maybe a nice feature in
60  * lib/display.php would be to automatically check if there are
61  * blogs for the given page, then provide a link to them somewhere on
62  * the page.  Or maybe this just creates a huge mess...
63  *
64  * Maybe it would be a good idea to ENABLE blogging of only certain
65  * pages by setting metadata or something...?  If a page is non-bloggable
66  * the plugin is ignored (perhaps with a warning message).
67  *
68  * Should blogs be by default filtered out of RecentChanges et al???
69  *
70  * Think of better name for this module: Blog? WikiLog? WebLog? WikiDot?
71  *
72  * Have other 'styles' for the plugin?... e.g. 'quiet'.  Display only
73  * 'This page has 23 associated comments. Click here to view / add.'
74  *
75  * For admin user, put checkboxes beside comments to allow for bulk removal.
76  *
77  * Permissions for who can add blogs?  Display entry box only if
78  * user meets these requirements...?
79  *
80  * Code cleanup: break into functions, use templates (or at least remove CSS)
81  */
82
83 class WikiPlugin_WikiBlog
84 extends WikiPlugin
85 {
86     function getName () {
87         return _("WikiBlog");
88     }
89
90     function getDescription () {
91         return sprintf(_("Show and add blogs for %s"),'[pagename]');
92     }
93
94     function getVersion() {
95         return preg_replace("/[Revision: $]/", '',
96                             "\$Revision$");
97     }
98
99     // Arguments:
100     //  page - page which is blogged to (default current page)
101     //
102     //  order - 'normal' - place in chronological order
103     //        - 'reverse' - place in reverse chronological order
104     //
105     //  mode - 'show' - only show old blogs
106     //         'add' - only show entry box for new blog
107     //         'show,add' - show old blogs then entry box
108     //         'add,show' - show entry box followed by old blogs
109     //
110     // TODO:
111     //
112     // - arguments to allow selection of time range to display
113     // - arguments to display only XX blogs per page (can this 'paging'
114     //    co-exist with the wiki??  difficult)
115     // - arguments to allow comments outside this range to be
116     //    display as e.g. June 2002 archive, July 2002 archive, etc..
117     // - captions for 'show' and 'add' sections
118
119
120     function getDefaultArguments() {
121         return array('pagename'   => '[pagename]',
122                      'order'      => 'normal',
123                      'mode'       => 'show,add',
124                      'noheader'   => false
125                     );
126     }
127
128     function run($dbi, $argstr, &$request, $basepage) {
129         $args = $this->getArgs($argstr, $request);
130         // allow empty pagenames for ADMIN_USER style blogs: "Blog/day"
131         //if (!$args['pagename'])
132         //    return $this->error(_("No pagename specified"));
133
134         // Get our form args.
135         $blog = $request->getArg("edit");
136         $request->setArg("edit", false);
137             
138         if ($request->isPost() and !empty($blog['save'])) {
139             $this->add($request, $blog, 'wikiblog', $basepage); // noreturn
140         }
141         //TODO: preview
142
143         // Now we display previous comments and/or provide entry box
144         // for new comments
145         $html = HTML();
146         foreach (explode(',', $args['mode']) as $show) {
147             if (!empty($seen[$show]))
148                 continue;
149             $seen[$show] = 1;
150                 
151             switch ($show) {
152             case 'show':
153                 $html->pushContent($this->showAll($request, $args));
154                 break;
155             case 'add':
156                 $html->pushContent($this->showForm($request, $args));
157                 break;
158             default:
159                 return $this->error(sprintf("Bad mode ('%s')", $show));
160             }
161         }
162         return $html;
163     }
164
165     /**
166      * posted: required: pagename, content. optional: summary
167      */
168     function add (&$request, $posted, $type='wikiblog') {
169         // This is similar to editpage. Shouldn't we use just this for preview?
170         $parent = $posted['pagename'];
171         if (empty($parent)) {
172             $prefix = "";   // allow empty parent for default "Blog/day"
173             $parent = HOME_PAGE;
174         } elseif (($parent == 'Blog' or $parent == 'WikiBlog') and $type == 'wikiblog')
175         { // avoid Blog/Blog/2003-01-11/14:03:02+00:00
176             $prefix = "";
177             $parent = ''; // 'Blog';
178         } elseif ($parent == 'Comment' and $type == "comment")
179         {
180             $prefix = "";
181             $parent = ''; // 'Comment';
182         } elseif ($parent == 'Forum' and $type == "wikiforum")
183         {
184             $prefix = "";
185             $parent = ''; // 'Forum';
186         } else {
187             $prefix = $parent . SUBPAGE_SEPARATOR;
188         }
189         //$request->finish(fmt("No pagename specified for %s",$type));
190
191         $now = time();
192         $dbi = $request->getDbh();
193         $user = $request->getUser();
194         
195         /*
196          * Page^H^H^H^H Blog meta-data
197          * This method is reused for all attachable pagetypes: wikiblog, comment and wikiforum
198          *
199          * This is info that won't change for each revision.
200          * Nevertheless, it's now stored in the revision meta-data.
201          * Several reasons:
202          *  o It's more convenient to have all information required
203          *    to render a page revision in the revision meta-data.
204          *  o We can avoid a race condition, since version meta-data
205          *    updates are atomic with the version creation.
206          */
207
208         $blog_meta = array('ctime'      => $now,
209                            'creator'    => $user->getId(),
210                            'creator_id' => $user->getAuthenticatedId(),
211                            );
212         
213
214         // Version meta-data
215         $summary = trim($posted['summary']);
216         // edit: private only
217         $perm = new PagePermission();
218         $perm->perm['edit'] = $perm->perm['remove'];
219         $version_meta = array('author'    => $blog_meta['creator'],
220                               'author_id' => $blog_meta['creator_id'],
221                               'markup'    => 2.0,   // assume new markup
222                               'summary'   => $summary ? $summary : _("New comment."),
223                               'mtime'     => $now,
224                               'pagetype'  => $type,
225                               $type       => $blog_meta,
226                               'perm'      => $perm->perm,
227                               );
228         if ($type == 'comment')
229             unset($version_meta['summary']);
230
231         // Comment body.
232         $body = trim($posted['content']);
233
234         $saved = false;
235         if ($type != 'wikiforum')
236             $pagename = $this->_blogPrefix($type);
237         else {
238             $pagename = substr($summary,0,12);
239             if (empty($pagename)) {
240                 $saved = true;
241                 trigger_error("Empty title", E_USER_WARNING);
242             }
243         }
244         while (!$saved) {
245             // Generate the page name.  For now, we use the format:
246             //   Rootname/Blog/2003-01-11/14:03:02+00:00
247             // Rootname = $prefix, Blog = $pagename, 
248             // This gives us natural chronological order when sorted
249             // alphabetically. "Rootname/" is optional.
250             // Esp. if Rootname is named Blog, it is omitted.
251
252             $time = Iso8601DateTime();
253             // Check intermediate pages. If not existing they should RedirectTo the parent page.
254             // Maybe add the BlogArchives plugin instead for the new interim subpage.
255             $redirected = $prefix . $pagename;
256             if (!$dbi->isWikiPage($redirected)) {
257                 if (!$parent) $parent = HOME_PAGE;
258                 require_once('lib/loadsave.php');
259                 $pageinfo = array('pagename' => $redirected,
260                                   'content'  => '<?plugin RedirectTo page="'.$parent.'" ?>',
261                                   'pagedata' => array(),
262                                   'versiondata' => array('author' => $blog_meta['creator'], 'is_minor_edit' => 1),
263                                   );
264                 SavePage($request, $pageinfo, '', '');
265             }
266             $redirected = $prefix . $pagename . SUBPAGE_SEPARATOR . preg_replace("/T.*/", "", "$time");
267             if (!$dbi->isWikiPage($redirected)) {
268                 if (!$parent) $parent = HOME_PAGE;
269                 require_once('lib/loadsave.php');
270                 $pageinfo = array('pagename' => $redirected,
271                                   'content'  => '<?plugin RedirectTo page="'.$parent.'" ?>',
272                                   'pagedata' => array(),
273                                   'versiondata' => array('author' => $blog_meta['creator'], 'is_minor_edit' => 1),
274                                   );
275                 SavePage($request, $pageinfo, '', '');
276             }
277
278             $p = $dbi->getPage($prefix . $pagename . SUBPAGE_SEPARATOR 
279                                . str_replace("T", SUBPAGE_SEPARATOR, "$time"));
280             $pr = $p->getCurrentRevision();
281
282             // Version should be zero.  If not, page already exists
283             // so increment timestamp and try again.
284             if ($pr->getVersion() > 0) {
285                 $now++;
286                 continue;
287             }
288
289             // FIXME: there's a slight, but currently unimportant
290             // race condition here.  If someone else happens to
291             // have just created a blog with the same name,
292             // we'll have locked it before we discover that the name
293             // is taken.
294             $saved = $p->save($body, 1, $version_meta);
295
296             $now++;
297         }
298         
299         $dbi->touch();
300         $request->setArg("mode", "show");
301         $request->redirect($request->getURLtoSelf()); // noreturn
302
303         // FIXME: when submit a comment from preview mode,
304         // adds the comment properly but jumps to browse mode.
305         // Any way to jump back to preview mode???
306     }
307
308     function showAll (&$request, $args, $type="wikiblog") {
309         // FIXME: currently blogSearch uses WikiDB->titleSearch to
310         // get results, so results are in alphabetical order.
311         // When PageTypes fully implemented, could have smarter
312         // blogSearch implementation / naming scheme.
313         
314         $dbi = $request->getDbh();
315         $basepage = $args['pagename'];
316         $blogs = $this->findBlogs($dbi, $basepage, $type);
317         $html = HTML();
318         if ($blogs) {
319             // First reorder
320             usort($blogs, array("WikiPlugin_WikiBlog",
321                                 "cmp"));
322             if ($args['order'] == 'reverse')
323                 $blogs = array_reverse($blogs);
324
325             $name = $this->_blogPrefix($type);
326             if (!$args['noheader'])
327                 $html->pushContent(HTML::h4(array('class' => "$type-heading"),
328                                             fmt("%s on %s:", $name, WikiLink($basepage))));
329             foreach ($blogs as $rev) {
330                 if (!$rev->get($type)) {
331                     // Ack! this is an old-style blog with data ctime in page meta-data.
332                     $content = $this->_transformOldFormatBlog($rev, $type);
333                 }
334                 else {
335                     $content = $rev->getTransformedContent($type);
336                 }
337                 $html->pushContent($content);
338             }
339             
340         }
341         return $html;
342     }
343
344     // Subpage for the basepage. All Blogs/Forum/Comment entries are 
345     // Subpages under this pagename, to find them faster.
346     function _blogPrefix($type='wikiblog') {
347         if ($type == 'wikiblog')
348             $basepage = "Blog";
349         elseif ($type == 'comment')
350             $basepage = "Comment";
351         elseif ($type == 'wikiforum')
352             $basepage = substr($summary,0,12);
353             //$basepage = _("Message"); // FIXME: we use now the first 12 chars of the summary
354         return $basepage;
355     }
356
357     function _transformOldFormatBlog($rev, $type='wikiblog') {
358         $page = $rev->getPage();
359         $metadata = array();
360         foreach (array('ctime', 'creator', 'creator_id') as $key)
361             $metadata[$key] = $page->get($key);
362         if (empty($metadata) and $type != 'wikiblog')
363             $metadata[$key] = $page->get('wikiblog');
364         $meta = $rev->getMetaData();
365         $meta[$type] = $metadata;
366         return new TransformedText($page, $rev->getPackedContent(), $meta, $type);
367     }
368
369     function findBlogs (&$dbi, $basepage='', $type='wikiblog') {
370         $prefix = (empty($basepage) 
371                    ? "" 
372                    :  $basepage . SUBPAGE_SEPARATOR) . $this->_blogPrefix($type);
373         $pages = $dbi->titleSearch(new TextSearchQuery('"'.$prefix.'"', true, 'none'));
374
375         $blogs = array();
376         while ($page = $pages->next()) {
377             if (!string_starts_with($page->getName(), $prefix))
378                 continue;
379             $current = $page->getCurrentRevision();
380             if ($current->get('pagetype') == $type) {
381                 $blogs[] = $current;
382             }
383         }
384         return $blogs;
385     }
386
387     function cmp($a, $b) {
388         return(strcmp($a->get('mtime'),
389                       $b->get('mtime')));
390     }
391     
392     function showForm (&$request, $args, $template='blogform') {
393         // Show blog-entry form.
394         $args = array('PAGENAME' => $args['pagename'],
395                       'HIDDEN_INPUTS' => 
396                       HiddenInputs($request->getArgs()));
397         if (ENABLE_EDIT_TOOLBAR and !ENABLE_WYSIWYG and ($template != 'addcomment')) {
398             include_once("lib/EditToolbar.php");
399             $toolbar = new EditToolbar();
400             $args = array_merge($args, $toolbar->getTokens());
401         }
402         return new Template($template, $request, $args);
403     }
404
405     // "2004-12" => "December 2004"
406     function _monthTitle($month){
407         if (!$month) $month = strftime("%Y-%m");
408         //list($year,$mon) = explode("-",$month);
409         return strftime("%B %Y", strtotime($month."-01"));
410     }
411
412     // "UserName/Blog/2004-12-13/12:28:50+01:00" => array('month' => "2004-12", ...)
413     function _blog($rev_or_page) {
414         $pagename = $rev_or_page->getName();
415         if (preg_match("/^(.*Blog)\/(\d\d\d\d-\d\d)-(\d\d)\/(.*)/", $pagename, $m))
416             list(,$prefix,$month,$day,$time) = $m;
417         return array('pagename' => $pagename,
418                      // page (list pages per month) or revision (list months)?
419                      //'title' => isa($rev_or_page,'WikiDB_PageRevision') ? $rev_or_page->get('summary') : '',
420                      //'monthtitle' => $this->_monthTitle($month),
421                      'month'   => $month,
422                      'day'     => $day,
423                      'time'    => $time,
424                      'prefix'  => $prefix);
425     }
426
427     function _nonDefaultArgs($args) {
428         return array_diff_assoc($args, $this->getDefaultArguments());
429     }
430
431 };
432
433 // For emacs users
434 // Local Variables:
435 // mode: php
436 // tab-width: 8
437 // c-basic-offset: 4
438 // c-hanging-comment-ender-p: nil
439 // indent-tabs-mode: nil
440 // End:
441 ?>