4 * Copyright 2002,2003,2007,2009 $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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * @author: MichaelVanDam, major refactor by JeffDairiki (as AddComment)
24 * @author: Changed as baseclass to AddComment and WikiForum and EditToolbar integration by ReiniUrban.
27 require_once 'lib/TextSearchQuery.php';
30 * This plugin shows 'blogs' (comments/news) associated with a
31 * particular page and provides an input form for adding a new blog.
35 * It also works as an action-page if you create a page called 'WikiBlog'
36 * containing this plugin. This allows adding comments to any page
37 * by linking "PageName?action=WikiBlog". Maybe a nice feature in
38 * lib/display.php would be to automatically check if there are
39 * blogs for the given page, then provide a link to them somewhere on
40 * the page. Or maybe this just creates a huge mess...
42 * Maybe it would be a good idea to ENABLE blogging of only certain
43 * pages by setting metadata or something...? If a page is non-bloggable
44 * the plugin is ignored (perhaps with a warning message).
46 * Should blogs be by default filtered out of RecentChanges et al???
48 * Think of better name for this module: Blog? WikiLog? WebLog? WikiDot?
50 * Have other 'styles' for the plugin?... e.g. 'quiet'. Display only
51 * 'This page has 23 associated comments. Click here to view / add.'
53 * For admin user, put checkboxes beside comments to allow for bulk removal.
55 * Permissions for who can add blogs? Display entry box only if
56 * user meets these requirements...?
58 * Code cleanup: break into functions, use templates (or at least remove CSS)
61 class WikiPlugin_WikiBlog
64 function getDescription()
66 return sprintf(_("Show and add blogs for %s."), '[pagename]');
72 // - arguments to allow selection of time range to display
73 // - arguments to display only XX blogs per page (can this 'paging'
74 // co-exist with the wiki?? difficult)
75 // - arguments to allow comments outside this range to be
76 // display as e.g. June 2002 archive, July 2002 archive, etc..
77 // - captions for 'show' and 'add' sections
79 function getDefaultArguments()
81 return array('pagename' => '[pagename]',
90 * @param string $argstr
91 * @param WikiRequest $request
92 * @param string $basepage
95 function run($dbi, $argstr, &$request, $basepage)
97 $args = $this->getArgs($argstr, $request);
98 // allow empty pagenames for ADMIN_USER style blogs: "Blog/day"
99 //if (!$args['pagename'])
100 // return $this->error(_("No pagename specified"));
102 // Get our form args.
103 $blog = $request->getArg("edit");
104 $request->setArg("edit", false);
106 if ($request->isPost() and !empty($blog['save'])) {
107 $this->add($request, $blog, 'wikiblog', $basepage); // noreturn
111 // Now we display previous comments and/or provide entry box
114 foreach (explode(',', $args['mode']) as $show) {
115 if (!empty($seen[$show]))
121 $html->pushContent($this->showAll($request, $args));
124 $html->pushContent($this->showForm($request, $args));
127 return $this->error(sprintf("Bad mode (ā%sā)", $show));
134 * posted: required: pagename, content. optional: summary
136 * @param WikiRequest $request
137 * @param array $posted
138 * @param string $type
140 function add(&$request, $posted, $type = 'wikiblog')
142 // This is similar to editpage. Shouldn't we use just this for preview?
143 $parent = $posted['pagename'];
144 if (empty($parent)) {
145 $prefix = ""; // allow empty parent for default "Blog/day"
147 } elseif (($parent == 'Blog' or $parent == 'WikiBlog') and $type == 'wikiblog') { // avoid Blog/Blog/2003-01-11/14:03:02+00:00
149 $parent = ''; // 'Blog';
150 } elseif ($parent == 'Comment' and $type == "comment") {
152 $parent = ''; // 'Comment';
153 } elseif ($parent == 'Forum' and $type == "wikiforum") {
155 $parent = ''; // 'Forum';
157 $prefix = $parent . '/';
159 //$request->finish(fmt("No pagename specified for %s",$type));
162 $dbi = $request->getDbh();
163 $user = $request->getUser();
166 * Page^H^H^H^H Blog meta-data
167 * This method is reused for all attachable pagetypes: wikiblog, comment and wikiforum
169 * This is info that won't change for each revision.
170 * Nevertheless, it's now stored in the revision meta-data.
172 * o It's more convenient to have all information required
173 * to render a page revision in the revision meta-data.
174 * o We can avoid a race condition, since version meta-data
175 * updates are atomic with the version creation.
178 $blog_meta = array('ctime' => $now,
179 'creator' => $user->getId(),
180 'creator_id' => $user->getAuthenticatedId(),
184 $summary = trim($posted['summary']);
185 // edit: private only
186 $perm = new PagePermission();
187 $perm->perm['edit'] = $perm->perm['remove'];
188 $version_meta = array('author' => $blog_meta['creator'],
189 'author_id' => $blog_meta['creator_id'],
190 'summary' => $summary ? $summary : _("New comment."),
194 'perm' => $perm->perm,
196 if ($type == 'comment')
197 unset($version_meta['summary']);
200 $body = trim($posted['content']);
203 if ($type != 'wikiforum')
204 $pagename = $this->blogPrefix($type);
206 $pagename = substr($summary, 0, 12);
207 if (empty($pagename)) {
209 trigger_error("Empty title", E_USER_WARNING);
213 // Generate the page name. For now, we use the format:
214 // Rootname/Blog/2003-01-11/14:03:02+00:00
215 // Rootname = $prefix, Blog = $pagename,
216 // This gives us natural chronological order when sorted
217 // alphabetically. "Rootname/" is optional.
218 // Esp. if Rootname is named Blog, it is omitted.
220 $time = Iso8601DateTime();
221 // Check intermediate pages. If not existing they should RedirectTo the parent page.
222 // Maybe add the BlogArchives plugin instead for the new interim subpage.
223 $redirected = $prefix . $pagename;
224 if (!$dbi->isWikiPage($redirected)) {
225 if (!$parent) $parent = HOME_PAGE;
226 require_once 'lib/loadsave.php';
227 $pageinfo = array('pagename' => $redirected,
228 'content' => '<<RedirectTo page="' . $parent . '">>',
229 'pagedata' => array(),
230 'versiondata' => array('author' => $blog_meta['creator'], 'is_minor_edit' => 1),
232 SavePage($request, $pageinfo, '', '');
234 $redirected = $prefix . $pagename . '/' . preg_replace("/T.*/", "", "$time");
235 if (!$dbi->isWikiPage($redirected)) {
236 if (!$parent) $parent = HOME_PAGE;
237 require_once 'lib/loadsave.php';
238 $pageinfo = array('pagename' => $redirected,
239 'content' => '<<RedirectTo page="' . $parent . '">>',
240 'pagedata' => array(),
241 'versiondata' => array('author' => $blog_meta['creator'], 'is_minor_edit' => 1),
243 SavePage($request, $pageinfo, '', '');
246 $p = $dbi->getPage($prefix . $pagename . '/'
247 . str_replace("T", '/', "$time"));
248 $pr = $p->getCurrentRevision();
250 // Version should be zero. If not, page already exists
251 // so increment timestamp and try again.
252 if ($pr->getVersion() > 0) {
257 // FIXME: there's a slight, but currently unimportant
258 // race condition here. If someone else happens to
259 // have just created a blog with the same name,
260 // we'll have locked it before we discover that the name
262 $saved = $p->save($body, 1, $version_meta);
268 $request->setArg("mode", "show");
269 $request->redirect($request->getURLtoSelf()); // noreturn
271 // FIXME: when submit a comment from preview mode,
272 // adds the comment properly but jumps to browse mode.
273 // Any way to jump back to preview mode???
276 function showAll(&$request, $args, $type = "wikiblog")
278 // FIXME: currently blogSearch uses WikiDB->titleSearch to
279 // get results, so results are in alphabetical order.
280 // When PageTypes fully implemented, could have smarter
281 // blogSearch implementation / naming scheme.
283 $dbi = $request->getDbh();
284 $basepage = $args['pagename'];
285 $blogs = $this->findBlogs($dbi, $basepage, $type);
289 usort($blogs, array("WikiPlugin_WikiBlog",
291 if ($args['order'] == 'reverse')
292 $blogs = array_reverse($blogs);
294 $name = $this->blogPrefix($type);
295 if (!$args['noheader'])
296 $html->pushContent(HTML::h4(array('class' => "$type-heading"),
297 fmt("%s on %s:", $name, WikiLink($basepage))));
298 foreach ($blogs as $rev) {
299 if (!$rev->get($type)) {
300 // Ack! this is an old-style blog with data ctime in page meta-data.
301 $content = $this->transformOldFormatBlog($rev, $type);
303 $content = $rev->getTransformedContent($type);
305 $html->pushContent($content);
312 // Subpage for the basepage. All Blogs/Forum/Comment entries are
313 // Subpages under this pagename, to find them faster.
314 protected function blogPrefix($type = 'wikiblog')
316 if ($type == 'wikiblog')
318 elseif ($type == 'comment')
319 $basepage = "Comment";
320 elseif ($type == 'wikiforum')
321 // $basepage = substr($summary, 0, 12);
322 $basepage = _("Message"); // FIXME: we use now the first 12 chars of the summary
326 private function transformOldFormatBlog($rev, $type = 'wikiblog')
328 $page = $rev->getPage();
330 foreach (array('ctime', 'creator', 'creator_id') as $key)
331 $metadata[$key] = $page->get($key);
332 if (empty($metadata) and $type != 'wikiblog')
333 $metadata[$key] = $page->get('wikiblog');
334 $meta = $rev->getMetaData();
335 $meta[$type] = $metadata;
336 return new TransformedText($page, $rev->getPackedContent(), $meta, $type);
339 function findBlogs(&$dbi, $basepage = '', $type = 'wikiblog')
341 $prefix = (empty($basepage)
343 : $basepage . '/') . $this->blogPrefix($type);
344 $pages = $dbi->titleSearch(new TextSearchQuery('"' . $prefix . '"', true, 'none'));
347 while ($page = $pages->next()) {
348 if (!string_starts_with($page->getName(), $prefix))
350 $current = $page->getCurrentRevision();
351 if ($current->get('pagetype') == $type) {
360 return (strcmp($a->get('mtime'),
364 function showForm(&$request, $args, $template = 'blogform')
366 // Show blog-entry form.
367 $args = array('PAGENAME' => $args['pagename'],
369 HiddenInputs($request->getArgs()));
370 if (ENABLE_EDIT_TOOLBAR and !ENABLE_WYSIWYG and ($template != 'addcomment')) {
371 include_once 'lib/EditToolbar.php';
372 $toolbar = new EditToolbar();
373 $args = array_merge($args, $toolbar->getTokens());
375 return new Template($template, $request, $args);
378 // "2004-12" => "December 2004"
379 protected function monthTitle($month)
381 if (!$month) $month = strftime("%Y-%m");
382 //list($year,$mon) = explode("-",$month);
383 return strftime("%B %Y", strtotime($month . "-01"));
386 // "UserName/Blog/2004-12-13/12:28:50+01:00" => array('month' => "2004-12", ...)
387 protected function _blog($rev_or_page)
389 $pagename = $rev_or_page->getName();
390 if (preg_match("/^(.*Blog)\/(\d\d\d\d-\d\d)-(\d\d)\/(.*)/", $pagename, $m))
391 list(, $prefix, $month, $day, $time) = $m;
392 return array('pagename' => $pagename,
393 // page (list pages per month) or revision (list months)?
394 //'title' => is_a($rev_or_page,'WikiDB_PageRevision') ? $rev_or_page->get('summary') : '',
395 //'monthtitle' => $this->monthTitle($month),
399 'prefix' => $prefix);
402 protected function nonDefaultArgs($args)
404 return array_diff_assoc($args, $this->getDefaultArguments());
413 // c-hanging-comment-ender-p: nil
414 // indent-tabs-mode: nil