]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/display.php
actionPage has no toks: fix format=rss* on actionpages
[SourceForge/phpwiki.git] / lib / display.php
1 <?php
2 // display.php: fetch page or get default content
3 rcs_id('$Id: display.php,v 1.74 2007-06-07 17:01:27 rurban Exp $');
4
5 require_once('lib/Template.php');
6
7 /**
8  * Extract keywords from Category* links on page. 
9  */
10 function GleanKeywords ($page) {
11     if (!defined('KEYWORDS')) return '';
12     require_once("lib/TextSearchQuery.php");
13     $search = new TextSearchQuery(KEYWORDS, true);
14     $KeywordLinkRegexp = $search->asRegexp();
15     // iterate over the pagelinks (could be a large number) [15ms on PluginManager]
16     // or do a titleSearch and check the categories if they are linked?
17     $links = $page->getPageLinks();
18     $keywords[] = SplitPagename($page->getName());
19     while ($link = $links->next()) {
20         $m = array();
21         if (preg_match($KeywordLinkRegexp, $link->getName(), $m))
22             $keywords[] = SplitPagename($m[0]);
23     }
24     $keywords[] = WIKI_NAME;
25     return join(', ', $keywords);
26 }
27
28 /** Make a link back to redirecting page.
29  *
30  * @param $pagename string  Name of redirecting page.
31  * @return XmlContent Link to the redirecting page.
32  */
33 function RedirectorLink($pagename) {
34     $url = WikiURL($pagename, array('redirectfrom' => ''));
35     return HTML::a(array('class' => 'redirectfrom wiki',
36                          'href' => $url),
37                    $pagename);
38 }
39
40     
41 function actionPage(&$request, $action) {
42     global $WikiTheme;
43
44     $pagename = $request->getArg('pagename');
45     $version = $request->getArg('version');
46
47     $page = $request->getPage();
48     $revision = $page->getCurrentRevision();
49
50     $dbi = $request->getDbh();
51     $actionpage = $dbi->getPage($action);
52     $actionrev = $actionpage->getCurrentRevision();
53
54     $pagetitle = HTML(fmt("%s: %s", 
55                           $actionpage->getName(),
56                           $WikiTheme->linkExistingWikiWord($pagename, false, $version)));
57
58     $request->setValidators(array('pageversion' => $revision->getVersion(),
59                                   '%mtime' => $revision->get('mtime')));
60     $request->appendValidators(array('pagerev' => $revision->getVersion(),
61                                      '%mtime' => $revision->get('mtime')));
62     $request->appendValidators(array('actionpagerev' => $actionrev->getVersion(),
63                                      '%mtime' => $actionrev->get('mtime')));
64
65     $transformedContent = $actionrev->getTransformedContent();
66  
67    /* Optionally tell google (and others) not to take notice of action pages.
68       RecentChanges or AllPages might be an exception.
69    */
70     $args = array();
71     if (GOOGLE_LINKS_NOFOLLOW)
72         $args = array('ROBOTS_META' => "noindex,nofollow");
73
74     /* Handle other formats: So far we had html only.
75        xml is requested by loaddump, rss is handled by recentchanges, 
76        pdf is a special action, but should be a format to dump multiple pages
77        if the actionpage plugin returns a pagelist.
78        rdf and owl are handled by SemanticWeb.
79     */
80     $format = $request->getArg('format');
81     /* At first the single page formats: html, xml */
82     if ($pagename == _("LinkDatabase")) {
83         $template = Template('browse', array('CONTENT' => $transformedContent));
84         GeneratePage($template, $pagetitle, $revision, $args);
85     } elseif (!$format or $format == 'html' or $format == 'sidebar' or $format == 'contribs') {
86         $template = Template('browse', array('CONTENT' => $transformedContent));
87         GeneratePage($template, $pagetitle, $revision, $args);
88     } elseif ($format == 'xml') {
89         $template = Template('browse', array('CONTENT' => $transformedContent));
90         GeneratePageAsXML($template, $pagetitle, $revision, $args);
91     } else {
92         $pagelist = null;
93         require_once('lib/WikiPlugin.php');
94         // Then the multi-page formats
95         // rss (if not already handled by RecentChanges)
96         // Need the pagelist from the first plugin
97         foreach($transformedContent->_content as $cached_element) {
98             if (is_a($cached_element, "Cached_PluginInvocation")) {
99                 $loader = new WikiPluginLoader;
100                 $markup = null;
101                 // return the first found pagelist
102                 $pagelist = $loader->expandPI($cached_element->_pi, $request, $markup, $pagename);
103                 if (is_a($pagelist, 'PageList'))
104                     break;
105             }
106         }
107         if (!$pagelist or !is_a($pagelist, 'PageList')) {
108             if (!in_array($format, array("rss91","rss2","rss","atom","rdf")))
109                 trigger_error(sprintf("Format %s requires an actionpage returning a pagelist.", $format)
110                               ."\n".("Fall back to single page mode"), E_USER_WARNING);
111             $pagelist = new PageList();
112             $pagelist->addPage($page);
113         }
114         if ($format == 'pdf') {
115             require_once("lib/pdf.php");
116             ConvertAndDisplayPdfPageList($request, $pagelist);
117         } elseif (in_array($format, array("rss91","rss2","rss"))) {
118             $args = $request->getArgs();
119             if ($pagename == _("RecentChanges")) {
120                 $template->printExpansion($args);
121             } else {
122                 require_once("lib/plugin/RecentChanges.php");
123                 $plugin = new WikiPlugin_RecentChanges();
124                 return $plugin->format($plugin->getChanges($request->_dbi, $args), $args);
125             }
126         } elseif ($format == 'atom') {
127             require_once("lib/RssWriter.php");
128             $rdf = new AtomFeed($request, $pagelist);
129             $rdf->__spew();
130         } elseif ($format == 'rdf') { // all semantic relations and attributes
131             require_once("lib/SemanticWeb.php");
132             $rdf = new RdfWriter($request, $pagelist);
133             $rdf->format();
134         } elseif ($format == 'owl') {
135             require_once("lib/SemanticWeb.php");
136             $rdf = new OwlWriter($request, $pagelist);
137             $rdf->format();
138         } elseif ($format == 'kbmodel') {
139             require_once("lib/SemanticWeb.php");
140             $model = new ModelWriter($request, $pagelist);
141             $model->format();
142         } else {
143             $template = Template('browse', array('CONTENT' => $transformedContent));
144             GeneratePage($template, $pagetitle, $revision, $args);
145         }
146     }
147     $request->checkValidators();
148     flush();
149     return '';
150 }
151
152 function displayPage(&$request, $template=false) {
153     global $WikiTheme;
154     $pagename = $request->getArg('pagename');
155     $version = $request->getArg('version');
156     $page = $request->getPage();
157     if ($version) {
158         $revision = $page->getRevision($version);
159         if (!$revision)
160             NoSuchRevision($request, $page, $version);
161         /* Tell Google (and others) to ignore old versions of pages */
162         $toks['ROBOTS_META'] = "noindex,nofollow";
163     }
164     else {
165         $revision = $page->getCurrentRevision();
166     }
167
168     if (isSubPage($pagename)) {
169         $pages = explode(SUBPAGE_SEPARATOR, $pagename);
170         $last_page = array_pop($pages); // deletes last element from array as side-effect
171         $pageheader = HTML::span(HTML::a(array('href' => WikiURL($pages[0]),
172                                                'class' => 'pagetitle'
173                                               ),
174                                         $WikiTheme->maybeSplitWikiWord($pages[0] . SUBPAGE_SEPARATOR)));
175         $first_pages = $pages[0] . SUBPAGE_SEPARATOR;
176         array_shift($pages);
177         foreach ($pages as $p)  {
178             $pageheader->pushContent(HTML::a(array('href' => WikiURL($first_pages . $p),
179                                                   'class' => 'backlinks'),
180                                             $WikiTheme->maybeSplitWikiWord($p . SUBPAGE_SEPARATOR)));
181             $first_pages .= $p . SUBPAGE_SEPARATOR;
182         }
183         $backlink = HTML::a(array('href' => WikiURL($pagename,
184                                                     array('action' => _("BackLinks"))),
185                                   'class' => 'backlinks'),
186                             $WikiTheme->maybeSplitWikiWord($last_page));
187         $backlink->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
188         $pageheader->pushContent($backlink);
189     } else {
190         $pageheader = HTML::a(array('href' => WikiURL($pagename,
191                                                      array('action' => _("BackLinks"))),
192                                    'class' => 'backlinks'),
193                              $WikiTheme->maybeSplitWikiWord($pagename));
194         $pageheader->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
195         if ($request->getArg('frame'))
196             $pageheader->setAttr('target', '_top');
197     }
198
199     $pagetitle = SplitPagename($pagename);
200     if (($redirect_from = $request->getArg('redirectfrom'))) {
201         $redirect_message = HTML::span(array('class' => 'redirectfrom'),
202                                        fmt("(Redirected from %s)",
203                                            RedirectorLink($redirect_from)));
204     // abuse the $redirected template var for some status update notice                                       
205     } elseif ($request->getArg('errormsg')) { 
206         $redirect_message = $request->getArg('errormsg');
207         $request->setArg('errormsg', false);
208     }
209
210     $request->appendValidators(array('pagerev' => $revision->getVersion(),
211                                      '%mtime' => $revision->get('mtime')));
212 /*
213     // FIXME: This is also in the template...
214     if ($request->getArg('action') != 'pdf' and !headers_sent()) {
215       // FIXME: enable MathML/SVG/... support
216       if (ENABLE_XHTML_XML
217              and (!isBrowserIE()
218                   and strstr($request->get('HTTP_ACCEPT'),'application/xhtml+xml')))
219             header("Content-Type: application/xhtml+xml; charset=" . $GLOBALS['charset']);
220         else
221             header("Content-Type: text/html; charset=" . $GLOBALS['charset']);
222     }
223 */
224
225     $toks['TITLE'] = $pagetitle;   // <title> tag
226     $toks['HEADER'] = $pageheader; // h1 with backlink
227     $toks['revision'] = $revision;
228
229     // On external searchengine (google) referrer, highlight the searchterm and 
230     // pass through the Searchhighlight actionpage.
231     if ($result = isExternalReferrer($request)) {
232         if (!empty($result['query'])) {
233             if (ENABLE_SEARCHHIGHLIGHT) {
234                 $request->_searchhighlight = $result;
235                 $request->appendValidators(array('%mtime' => time())); // force no cache(?)
236                 // Should be changed to check the engine and search term only
237                 // $request->setArg('nocache', 1); 
238                 $page_content = new TransformedText($revision->getPage(),
239                                                     $revision->getPackedContent(),
240                                                     $revision->getMetaData());
241                 /* Now add the SearchHighlight plugin to the top of the page, in memory only.
242                    You can parametrize this by changing the SearchHighlight action page.
243                 */
244                 if ($actionpage = $request->findActionPage('SearchHighlight')) {
245                     $actionpage = $request->getPage($actionpage);
246                     $actionrev = $actionpage->getCurrentRevision();
247                     $pagetitle = HTML(fmt("%s: %s", 
248                                           $actionpage->getName(),
249                                           $WikiTheme->linkExistingWikiWord($pagename, false, $version)));
250                     $request->appendValidators(array('actionpagerev' => $actionrev->getVersion(),
251                                                      '%mtime' => $actionrev->get('mtime')));
252                     $toks['SEARCH_ENGINE'] = $result['engine'];
253                     $toks['SEARCH_ENGINE_URL'] = $result['engine_url'];
254                     $toks['SEARCH_TERM'] = $result['query'];
255                     //$toks['HEADER'] = HTML($actionpage->getName(),": ",$pageheader); // h1 with backlink
256                     $actioncontent = new TransformedText($actionrev->getPage(),
257                                                          $actionrev->getPackedContent(),
258                                                          $actionrev->getMetaData());
259                     // prepend the actionpage in front of the hightlighted content
260                     $toks['CONTENT'] = HTML($actioncontent, $page_content);
261                 }
262             }
263         } else {
264             $page_content = $revision->getTransformedContent();
265         }
266     } else {
267         $page_content = $revision->getTransformedContent();
268     }
269    
270     /* Check for special pagenames, which are no actionpages. */
271     /*
272     if ( $pagename == _("RecentVisitors")) {
273         $toks['ROBOTS_META']="noindex,follow";
274     } else
275     */
276     if ($pagename == _("SandBox")) {
277         $toks['ROBOTS_META']="noindex,nofollow";
278     } else if (!isset($toks['ROBOTS_META'])) {
279         $toks['ROBOTS_META'] = "index,follow";
280     }
281     if (!isset($toks['CONTENT']))
282         $toks['CONTENT'] = new Template('browse', $request, $page_content);
283     if (!empty($redirect_message))
284         $toks['redirected'] = $redirect_message;
285     
286     $toks['PAGE_DESCRIPTION'] = $page_content->getDescription();
287     $toks['PAGE_KEYWORDS'] = GleanKeywords($page);
288     if (!$template)
289         $template = new Template('html', $request);
290
291     // Handle other formats: So far we had html only.
292     // xml is requested by loaddump, rss is handled by RecentChanges, 
293     // pdf is a special action, but should be a format to dump multiple pages
294     // if the actionpage plugin returns a pagelist.
295     // rdf, owl, kbmodel, daml, ... are handled by SemanticWeb.
296     $format = $request->getArg('format');
297     /* Only single page versions. rss only if not already handled by RecentChanges.
298      */
299     if (!$format or $format == 'html' or $format == 'sidebar' or $format == 'contribs') {
300         $template->printExpansion($toks);
301     } elseif ($format == 'xml') {
302         $template = new Template('htmldump', $request);
303         $template->printExpansion($toks);
304     } else {
305         // No pagelist here. Single page version only
306         require_once("lib/PageList.php");
307         $pagelist = new PageList();
308         $pagelist->addPage($page);
309         if ($format == 'pdf') {
310             require_once("lib/pdf.php");
311             ConvertAndDisplayPdfPageList($request, $pagelist);
312         } elseif (in_array($format, array("rss91","rss2","rss"))) {
313             if ($pagename == _("RecentChanges"))
314                 $template->printExpansion($toks);
315             else {    
316                 require_once("lib/plugin/RecentChanges.php");
317                 $plugin = new WikiPlugin_RecentChanges();
318                 $args = $request->getArgs();
319                 return $plugin->format($plugin->getChanges($request->_dbi, $args), $args);
320             }
321         /*} elseif ($format == 'atom') {
322             require_once("lib/RssWriter.php");
323             $rdf = new AtomWriter($request, $pagelist);
324             $rdf->format();*/
325         } elseif ($format == 'rdf') { // all semantic relations and attributes
326             require_once("lib/SemanticWeb.php");
327             $rdf = new RdfWriter($request, $pagelist);
328             $rdf->format();
329         } elseif ($format == 'owl') {
330             require_once("lib/SemanticWeb.php");
331             $rdf = new OwlWriter($request, $pagelist);
332             $rdf->format();
333         } elseif ($format == 'kbmodel') {
334             require_once("lib/SemanticWeb.php");
335             $model = new ModelWriter($request, $pagelist);
336             $model->format();
337         } else {
338             if (!in_array($pagename, array(_("LinkDatabase"))))
339                 trigger_error(sprintf("Unhandled format %s. Reverting to html", $format), E_USER_WARNING);
340             $template->printExpansion($toks);
341         }
342     }
343     
344     $page->increaseHitCount();
345
346     if ($request->getArg('action') != 'pdf')
347         $request->checkValidators();
348     flush();
349     return '';
350 }
351
352 // $Log: not supported by cvs2svn $
353 // Revision 1.73  2007/05/30 20:43:31  rurban
354 // added MonoBook UserContribs
355 //
356 // Revision 1.72  2007/05/13 18:13:12  rurban
357 // LinkDatabase format exceptions
358 //
359 // Revision 1.71  2007/02/17 22:39:05  rurban
360 // format=rss overhaul
361 //
362 // Revision 1.70  2007/01/22 23:43:06  rurban
363 // Add RecentChanges format=sidebar
364 //
365 // Revision 1.69  2007/01/20 15:53:51  rurban
366 // Rewrite of SearchHighlight: through ActionPage and InlineParser
367 //
368 // Revision 1.68  2007/01/20 11:25:19  rurban
369 // actionPage: request is already global
370 //
371 // Revision 1.67  2007/01/07 18:44:20  rurban
372 // Support format handlers for single- and multi-page: pagelists from actionpage plugins. Use USE_SEARCHHIGHLIGHT. Fix InlineHighlight (still experimental).
373 //
374 // Revision 1.66  2006/03/19 14:26:29  rurban
375 // sf.net patch by Matt Brown: Add rel=nofollow to more actions
376 //
377 // Revision 1.65  2005/05/05 08:54:40  rurban
378 // fix pagename split for title and header
379 //
380 // Revision 1.64  2005/04/23 11:21:55  rurban
381 // honor theme-specific SplitWikiWord in the HEADER
382 //
383 // Revision 1.63  2004/11/30 17:48:38  rurban
384 // just comments
385 //
386 // Revision 1.62  2004/11/30 09:51:35  rurban
387 // changed KEYWORDS from pageprefix to search term. added installer detection.
388 //
389 // Revision 1.61  2004/11/21 11:59:19  rurban
390 // remove final \n to be ob_cache independent
391 //
392 // Revision 1.60  2004/11/19 19:22:03  rurban
393 // ModeratePage part1: change status
394 //
395 // Revision 1.59  2004/11/17 20:03:58  rurban
396 // Typo: call SearchHighlight not SearchHighLight
397 //
398 // Revision 1.58  2004/11/09 17:11:16  rurban
399 // * revert to the wikidb ref passing. there's no memory abuse there.
400 // * use new wikidb->_cache->_id_cache[] instead of wikidb->_iwpcache, to effectively
401 //   store page ids with getPageLinks (GleanDescription) of all existing pages, which
402 //   are also needed at the rendering for linkExistingWikiWord().
403 //   pass options to pageiterator.
404 //   use this cache also for _get_pageid()
405 //   This saves about 8 SELECT count per page (num all pagelinks).
406 // * fix passing of all page fields to the pageiterator.
407 // * fix overlarge session data which got broken with the latest ACCESS_LOG_SQL changes
408 //
409 // Revision 1.57  2004/11/01 10:43:57  rurban
410 // seperate PassUser methods into seperate dir (memory usage)
411 // fix WikiUser (old) overlarge data session
412 // remove wikidb arg from various page class methods, use global ->_dbi instead
413 // ...
414 //
415 // Revision 1.56  2004/10/14 13:44:14  rurban
416 // fix lib/display.php:159: Warning[2]: Argument to array_reverse() should be an array
417 //
418 // Revision 1.55  2004/09/26 14:58:35  rurban
419 // naive SearchHighLight implementation
420 //
421 // Revision 1.54  2004/09/17 14:19:41  rurban
422 // disable Content-Type header for now, until it is fixed
423 //
424 // Revision 1.53  2004/06/25 14:29:20  rurban
425 // WikiGroup refactoring:
426 //   global group attached to user, code for not_current user.
427 //   improved helpers for special groups (avoid double invocations)
428 // new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
429 // fixed a XHTML validation error on userprefs.tmpl
430 //
431 // Revision 1.52  2004/06/14 11:31:37  rurban
432 // renamed global $Theme to $WikiTheme (gforge nameclash)
433 // inherit PageList default options from PageList
434 //   default sortby=pagename
435 // use options in PageList_Selectable (limit, sortby, ...)
436 // added action revert, with button at action=diff
437 // added option regex to WikiAdminSearchReplace
438 //
439 // Revision 1.51  2004/05/18 16:23:39  rurban
440 // rename split_pagename to SplitPagename
441 //
442 // Revision 1.50  2004/05/04 22:34:25  rurban
443 // more pdf support
444 //
445 // Revision 1.49  2004/04/18 01:11:52  rurban
446 // more numeric pagename fixes.
447 // fixed action=upload with merge conflict warnings.
448 // charset changed from constant to global (dynamic utf-8 switching)
449 //
450
451 // For emacs users
452 // Local Variables:
453 // mode: php
454 // tab-width: 8
455 // c-basic-offset: 4
456 // c-hanging-comment-ender-p: nil
457 // indent-tabs-mode: nil
458 // End:
459 ?>