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