2 rcs_id('$Id: WikiBlog.php,v 1.14 2004-03-29 21:33:32 rurban Exp $');
4 Copyright 2002, 2003 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
23 * @author: MichaelVanDam, major refactor by JeffDairiki
26 require_once('lib/TextSearchQuery.php');
29 * This plugin shows 'blogs' (comments/news) associated with a
30 * particular page and provides an input form for adding a new blog.
32 * Now it is also the base class for all attachable pagetypes:
33 * wikiblog, comment and wikiforum
37 * To have comments show up on a separate page:
39 * <?plugin WikiBlog mode=add?>
40 * Create TopPage/Comments with:
41 * <?plugin WikiBlog page=TopPage mode=show?>
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...
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).
56 * Should blogs be by default filtered out of RecentChanges et al???
58 * Think of better name for this module: Blog? WikiLog? WebLog? WikiDot?
60 * Have other 'styles' for the plugin?... e.g. 'quiet'. Display only
61 * 'This page has 23 associated comments. Click here to view / add.'
63 * For admin user, put checkboxes beside comments to allow for bulk removal.
65 * Permissions for who can add blogs? Display entry box only if
66 * user meets these requirements...?
68 * Code cleanup: break into functions, use templates (or at least remove CSS)
73 class WikiPlugin_WikiBlog
80 function getDescription () {
81 return sprintf(_("Show and add blogs for %s"),'[pagename]');
84 function getVersion() {
85 return preg_replace("/[Revision: $]/", '',
86 "\$Revision: 1.14 $");
91 // page - page which is blogged to (default current page)
93 // order - 'normal' - place in chronological order
94 // - 'reverse' - place in reverse chronological order
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
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
111 function getDefaultArguments() {
112 return array('pagename' => '[pagename]',
114 'mode' => 'show,add',
119 function run($dbi, $argstr, &$request, $basepage) {
120 $args = $this->getArgs($argstr, $request);
121 if (!$args['pagename'])
122 return $this->error(_("No pagename specified"));
124 // Get our form args.
125 $blog = $request->getArg("blog");
126 $request->setArg('blog', false);
128 if ($request->isPost() and !empty($blog['addblog'])) {
129 $this->add($request, $blog); // noreturn
132 // Now we display previous comments and/or provide entry box
135 foreach (explode(',', $args['mode']) as $show) {
136 if (!empty($seen[$show]))
142 $html->pushContent($this->showAll($request, $args));
145 $html->pushContent($this->showForm($request, $args));
148 return $this->error(sprintf("Bad mode ('%s')", $show));
154 function add (&$request, $blog, $type='wikiblog') {
155 if (!($parent = $blog['pagename']))
156 $request->finish(fmt("No pagename specified for %s",$type));
158 $user = $request->getUser();
160 $dbi = $request->getDbh();
163 * Page^H^H^H^H Blog meta-data
164 * This method is reused for all attachable pagetypes: wikiblog, comment and wikiforum
166 * This is info that won't change for each revision.
167 * Nevertheless, it's now stored in the revision meta-data.
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.
175 $blog_meta = array('ctime' => $now,
176 'creator' => $user->getId(),
177 'creator_id' => $user->getAuthenticatedId(),
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."),
191 if ($type == 'comment')
192 unset($version_meta['summary']);
195 $body = trim($blog['body']);
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
204 $time = Iso8601DateTime();
205 if ($type == 'wikiblog')
207 elseif ($type == 'comment')
208 $pagename = "Comment";
209 elseif ($type == 'wikiforum')
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']),
221 SavePage($request, $pageinfo, '', '');
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']),
231 SavePage($request, $pageinfo, '', '');
234 $p = $dbi->getPage($parent . SUBPAGE_SEPARATOR . $pagename . SUBPAGE_SEPARATOR . str_replace("T", SUBPAGE_SEPARATOR, "$time"));
235 $pr = $p->getCurrentRevision();
237 // Version should be zero. If not, page already exists
238 // so increment timestamp and try again.
239 if ($pr->getVersion() > 0) {
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
250 * FIXME: For now all blogs are locked. It would be
251 * nice to allow only the 'creator' to edit by default.
253 $p->set('locked', true); //lock by default
254 $saved = $p->save($body, 1, $version_meta);
260 $request->redirect($request->getURLtoSelf()); // noreturn
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???
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.
273 $dbi = $request->getDbh();
275 $parent = $args['pagename'];
276 $blogs = $this->findBlogs($dbi, $parent, $type);
280 usort($blogs, array("WikiPlugin_WikiBlog",
282 if ($args['order'] == 'reverse')
283 $blogs = array_reverse($blogs);
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);
294 $content = $rev->getTransformedContent($type);
296 $html->pushContent($content);
303 function _transformOldFormatBlog($rev, $type='wikiblog') {
304 $page = $rev->getPage();
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);
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));
322 while ($page = $pages->next()) {
324 // Verify that it is a blog page. If not, go to next page.
325 // When we proper blogSearch implementation this will not
327 $name = $page->getName();
328 if (substr($name, 0, $pfxlen) != $prefix)
330 $current = $page->getCurrentRevision();
332 if (/*preg_match("/^Blog-([[:digit:]]{14})$/", substr($name, $pfxlen))
333 or */$current->get('pagetype') == $type) {
340 function cmp($a, $b) {
341 return(strcmp($a->get('mtime'),
345 function showForm (&$request, $args, $template='blogform') {
346 // Show blog-entry form.
347 return new Template($template, $request,
348 array('PAGENAME' => $args['pagename']));
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
356 // Revision 1.11 2004/03/12 20:59:31 rurban
357 // important cookie fix by Konstantin Zadorozhny
358 // new editpage feature: JS_SEARCHREPLACE
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.
365 // Revision 1.9 2004/02/27 02:10:50 rurban
366 // Patch #891133 by pablom517
367 // "WikiBlog Plugin now sorts logs correctly"
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, ...)
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.)
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.
387 // Revision 1.5 2003/02/16 19:47:17 dairiki
388 // Update WikiDB timestamp when editing or deleting pages.
390 // Revision 1.4 2003/01/11 22:23:00 carstenklapp
391 // More refactoring to use templated output. Use page meta "summary" field.
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.
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil