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