]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/display.php
remove final \n to be ob_cache independent
[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.61 2004-11-21 11:59:19 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     global $KeywordLinkRegexp;
12
13     $links = $page->getPageLinks();
14     $keywords[] = SplitPagename($page->getName());
15     while ($link = $links->next()) {
16         if (preg_match("/${KeywordLinkRegexp}/x", $link->getName(), $m))
17             $keywords[] = SplitPagename($m[0]);
18     }
19     $keywords[] = WIKI_NAME;
20     return join(', ', $keywords);
21 }
22
23 /** Make a link back to redirecting page.
24  *
25  * @param $pagename string  Name of redirecting page.
26  * @return XmlContent Link to the redirecting page.
27  */
28 function RedirectorLink($pagename) {
29     $url = WikiURL($pagename, array('redirectfrom' => ''));
30     return HTML::a(array('class' => 'redirectfrom wiki',
31                          'href' => $url),
32                    $pagename);
33 }
34
35     
36 function actionPage(&$request, $action) {
37     global $WikiTheme;
38
39     $pagename = $request->getArg('pagename');
40     $version = $request->getArg('version');
41
42     $page = $request->getPage();
43     $revision = $page->getCurrentRevision();
44
45     $dbi = $request->getDbh();
46     $actionpage = $dbi->getPage($action);
47     $actionrev = $actionpage->getCurrentRevision();
48
49     $pagetitle = HTML(fmt("%s: %s", 
50                           $actionpage->getName(),
51                           $WikiTheme->linkExistingWikiWord($pagename, false, $version)));
52
53     $validators = new HTTP_ValidatorSet(array('pageversion' => $revision->getVersion(),
54                                               '%mtime' => $revision->get('mtime')));
55                                         
56     $request->appendValidators(array('pagerev' => $revision->getVersion(),
57                                      '%mtime' => $revision->get('mtime')));
58     $request->appendValidators(array('actionpagerev' => $actionrev->getVersion(),
59                                      '%mtime' => $actionrev->get('mtime')));
60
61     $transformedContent = $actionrev->getTransformedContent();
62     $template = Template('browse', array('CONTENT' => $transformedContent));
63 /*
64     if (!headers_sent()) {
65         //FIXME: does not work yet. document.write not supported (signout button)
66         // http://www.w3.org/People/mimasa/test/xhtml/media-types/results
67         if (ENABLE_XHTML_XML 
68             and (!isBrowserIE() and
69                  strstr($request->get('HTTP_ACCEPT'),'application/xhtml+xml')))
70             header("Content-Type: application/xhtml+xml; charset=" . $GLOBALS['charset']);
71         else
72             header("Content-Type: text/html; charset=" . $GLOBALS['charset']);
73     }
74 */    
75     GeneratePage($template, $pagetitle, $revision);
76     $request->checkValidators();
77     flush();
78 }
79
80 function displayPage(&$request, $template=false) {
81     $pagename = $request->getArg('pagename');
82     $version = $request->getArg('version');
83     $page = $request->getPage();
84     if ($version) {
85         $revision = $page->getRevision($version);
86         if (!$revision)
87             NoSuchRevision($request, $page, $version);
88     }
89     else {
90         $revision = $page->getCurrentRevision();
91     }
92
93     if (isSubPage($pagename)) {
94         $pages = explode(SUBPAGE_SEPARATOR, $pagename);
95         $last_page = array_pop($pages); // deletes last element from array as side-effect
96         $pagetitle = HTML::span(HTML::a(array('href' => WikiURL($pages[0]),
97                                               'class' => 'pagetitle'
98                                               ),
99                                         SplitPagename($pages[0] . SUBPAGE_SEPARATOR)));
100         $first_pages = $pages[0] . SUBPAGE_SEPARATOR;
101         array_shift($pages);
102         foreach ($pages as $p)  {
103             $pagetitle->pushContent(HTML::a(array('href' => WikiURL($first_pages . $p),
104                                                   'class' => 'backlinks'),
105                                        SplitPagename($p . SUBPAGE_SEPARATOR)));
106             $first_pages .= $p . SUBPAGE_SEPARATOR;
107         }
108         $backlink = HTML::a(array('href' => WikiURL($pagename,
109                                                     array('action' => _("BackLinks"))),
110                                   'class' => 'backlinks'),
111                             SplitPagename($last_page));
112         $backlink->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
113         $pagetitle->pushContent($backlink);
114     } else {
115         $pagetitle = HTML::a(array('href' => WikiURL($pagename,
116                                                      array('action' => _("BackLinks"))),
117                                    'class' => 'backlinks'),
118                              SplitPagename($pagename));
119         $pagetitle->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
120         if ($request->getArg('frame'))
121             $pagetitle->setAttr('target', '_top');
122     }
123
124     $pageheader = $pagetitle;
125     if (($redirect_from = $request->getArg('redirectfrom'))) {
126         $redirect_message = HTML::span(array('class' => 'redirectfrom'),
127                                        fmt("(Redirected from %s)",
128                                            RedirectorLink($redirect_from)));
129     // abuse the $redirected template var for some status update notice                                       
130     } elseif ($request->getArg('errormsg')) { 
131         $redirect_message = $request->getArg('errormsg');
132         $request->setArg('errormsg', false);
133     }
134
135     $request->appendValidators(array('pagerev' => $revision->getVersion(),
136                                      '%mtime' => $revision->get('mtime')));
137 /*
138     // FIXME: This is also in the template...
139     if ($request->getArg('action') != 'pdf' and !headers_sent()) {
140       // FIXME: enable MathML/SVG/... support
141       if (ENABLE_XHTML_XML
142              and (!isBrowserIE()
143                   and strstr($request->get('HTTP_ACCEPT'),'application/xhtml+xml')))
144             header("Content-Type: application/xhtml+xml; charset=" . $GLOBALS['charset']);
145         else
146             header("Content-Type: text/html; charset=" . $GLOBALS['charset']);
147     }
148 */
149     $page_content = $revision->getTransformedContent();
150
151     // if external searchengine (google) referrer, highlight the searchterm
152     // FIXME: move that to the transformer?
153     // OR: add the searchhightplugin line to the content?
154     if ($result = isExternalReferrer($request)) {
155         if (DEBUG and !empty($result['query'])) {
156             //$GLOBALS['SearchHighlightQuery'] = $result['query'];
157             /* simply add the SearchHighlight plugin to the top of the page. 
158                This just parses the wikitext, and doesn't highlight the markup */
159             include_once('lib/WikiPlugin.php');
160             $loader = new WikiPluginLoader;
161             $xml = $loader->expandPI('<'.'?plugin SearchHighlight s="'.$result['query'].'"?'.'>', $request, $markup);
162             if ($xml and is_array($xml)) {
163               foreach (array_reverse($xml) as $line) {
164                 array_unshift($page_content->_content, $line);
165               }
166               array_unshift($page_content->_content, 
167                             HTML::div(_("You searched for: "), HTML::strong($result['query'])));
168             }
169             
170             if (0) {
171             /* Parse the transformed (mixed HTML links + strings) lines?
172                This looks like overkill.
173              */
174             require_once("lib/TextSearchQuery.php");
175             $query = new TextSearchQuery($result['query']);
176             $hilight_re = $query->getHighlightRegexp();
177             //$matches = preg_grep("/$hilight_re/i", $revision->getContent());
178             // FIXME!
179             for ($i=0; $i < count($page_content->_content); $i++) {
180                 $found = false;
181                 $line = $page_content->_content[$i];
182                 if (is_string($line)) {
183                     while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
184                         $found = true;
185                         $line = substr($line, strlen($m[0]));
186                         $html[] = $m[1];    // prematch
187                         $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
188                     }
189                 }
190                 if ($found) {
191                     $html[] = $line;  // postmatch
192                     $page_content->_content[$i] = HTML::span(array('class' => 'search-context'),
193                                                              $html);
194                 }
195             }
196             }
197         }
198     }
199    
200     $toks['CONTENT'] = new Template('browse', $request, $page_content);
201     
202     $toks['TITLE'] = $pagetitle;
203     $toks['HEADER'] = $pageheader;
204     $toks['revision'] = $revision;
205     if (!empty($redirect_message))
206         $toks['redirected'] = $redirect_message;
207     $toks['ROBOTS_META'] = 'index,follow';
208     $toks['PAGE_DESCRIPTION'] = $page_content->getDescription();
209     $toks['PAGE_KEYWORDS'] = GleanKeywords($page);
210     if (!$template)
211         $template = new Template('html', $request);
212     
213     $template->printExpansion($toks);
214     $page->increaseHitCount();
215
216     if ($request->getArg('action') != 'pdf')
217         $request->checkValidators();
218     flush();
219 }
220
221 // $Log: not supported by cvs2svn $
222 // Revision 1.60  2004/11/19 19:22:03  rurban
223 // ModeratePage part1: change status
224 //
225 // Revision 1.59  2004/11/17 20:03:58  rurban
226 // Typo: call SearchHighlight not SearchHighLight
227 //
228 // Revision 1.58  2004/11/09 17:11:16  rurban
229 // * revert to the wikidb ref passing. there's no memory abuse there.
230 // * use new wikidb->_cache->_id_cache[] instead of wikidb->_iwpcache, to effectively
231 //   store page ids with getPageLinks (GleanDescription) of all existing pages, which
232 //   are also needed at the rendering for linkExistingWikiWord().
233 //   pass options to pageiterator.
234 //   use this cache also for _get_pageid()
235 //   This saves about 8 SELECT count per page (num all pagelinks).
236 // * fix passing of all page fields to the pageiterator.
237 // * fix overlarge session data which got broken with the latest ACCESS_LOG_SQL changes
238 //
239 // Revision 1.57  2004/11/01 10:43:57  rurban
240 // seperate PassUser methods into seperate dir (memory usage)
241 // fix WikiUser (old) overlarge data session
242 // remove wikidb arg from various page class methods, use global ->_dbi instead
243 // ...
244 //
245 // Revision 1.56  2004/10/14 13:44:14  rurban
246 // fix lib/display.php:159: Warning[2]: Argument to array_reverse() should be an array
247 //
248 // Revision 1.55  2004/09/26 14:58:35  rurban
249 // naive SearchHighLight implementation
250 //
251 // Revision 1.54  2004/09/17 14:19:41  rurban
252 // disable Content-Type header for now, until it is fixed
253 //
254 // Revision 1.53  2004/06/25 14:29:20  rurban
255 // WikiGroup refactoring:
256 //   global group attached to user, code for not_current user.
257 //   improved helpers for special groups (avoid double invocations)
258 // new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
259 // fixed a XHTML validation error on userprefs.tmpl
260 //
261 // Revision 1.52  2004/06/14 11:31:37  rurban
262 // renamed global $Theme to $WikiTheme (gforge nameclash)
263 // inherit PageList default options from PageList
264 //   default sortby=pagename
265 // use options in PageList_Selectable (limit, sortby, ...)
266 // added action revert, with button at action=diff
267 // added option regex to WikiAdminSearchReplace
268 //
269 // Revision 1.51  2004/05/18 16:23:39  rurban
270 // rename split_pagename to SplitPagename
271 //
272 // Revision 1.50  2004/05/04 22:34:25  rurban
273 // more pdf support
274 //
275 // Revision 1.49  2004/04/18 01:11:52  rurban
276 // more numeric pagename fixes.
277 // fixed action=upload with merge conflict warnings.
278 // charset changed from constant to global (dynamic utf-8 switching)
279 //
280
281 // For emacs users
282 // Local Variables:
283 // mode: php
284 // tab-width: 8
285 // c-basic-offset: 4
286 // c-hanging-comment-ender-p: nil
287 // indent-tabs-mode: nil
288 // End:
289 ?>