]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/display.php
* revert to the wikidb ref passing. there's no memory abuse there.
[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.58 2004-11-09 17:11:16 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     }
130
131     $request->appendValidators(array('pagerev' => $revision->getVersion(),
132                                      '%mtime' => $revision->get('mtime')));
133 /*
134     // FIXME: This is also in the template...
135     if ($request->getArg('action') != 'pdf' and !headers_sent()) {
136       // FIXME: enable MathML/SVG/... support
137       if (ENABLE_XHTML_XML
138              and (!isBrowserIE()
139                   and strstr($request->get('HTTP_ACCEPT'),'application/xhtml+xml')))
140             header("Content-Type: application/xhtml+xml; charset=" . $GLOBALS['charset']);
141         else
142             header("Content-Type: text/html; charset=" . $GLOBALS['charset']);
143     }
144 */
145     $page_content = $revision->getTransformedContent();
146
147     // if external searchengine (google) referrer, highlight the searchterm
148     // FIXME: move that to the transformer?
149     // OR: add the searchhightplugin line to the content?
150     if ($result = isExternalReferrer($request)) {
151         if (DEBUG and !empty($result['query'])) {
152             //$GLOBALS['SearchHighLightQuery'] = $result['query'];
153             /* simply add the SearchHighLight plugin to the top of the page. 
154                This just parses the wikitext, and doesn't highlight the markup */
155             include_once('lib/WikiPlugin.php');
156             $loader = new WikiPluginLoader;
157             $xml = $loader->expandPI('<'.'?plugin SearchHighLight s="'.$result['query'].'"?'.'>', $request, $markup);
158             if ($xml and is_array($xml)) {
159               foreach (array_reverse($xml) as $line) {
160                 array_unshift($page_content->_content, $line);
161               }
162               array_unshift($page_content->_content, 
163                             HTML::div(_("You searched for: "), HTML::strong($result['query'])));
164             }
165             
166             if (0) {
167             /* Parse the transformed (mixed HTML links + strings) lines?
168                This looks like overkill.
169              */
170             require_once("lib/TextSearchQuery.php");
171             $query = new TextSearchQuery($result['query']);
172             $hilight_re = $query->getHighlightRegexp();
173             //$matches = preg_grep("/$hilight_re/i", $revision->getContent());
174             // FIXME!
175             for ($i=0; $i < count($page_content->_content); $i++) {
176                 $found = false;
177                 $line = $page_content->_content[$i];
178                 if (is_string($line)) {
179                     while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
180                         $found = true;
181                         $line = substr($line, strlen($m[0]));
182                         $html[] = $m[1];    // prematch
183                         $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
184                     }
185                 }
186                 if ($found) {
187                     $html[] = $line;  // postmatch
188                     $page_content->_content[$i] = HTML::span(array('class' => 'search-context'),
189                                                              $html);
190                 }
191             }
192             }
193         }
194     }
195    
196     $toks['CONTENT'] = new Template('browse', $request, $page_content);
197     
198     $toks['TITLE'] = $pagetitle;
199     $toks['HEADER'] = $pageheader;
200     $toks['revision'] = $revision;
201     if (!empty($redirect_message))
202         $toks['redirected'] = $redirect_message;
203     $toks['ROBOTS_META'] = 'index,follow';
204     $toks['PAGE_DESCRIPTION'] = $page_content->getDescription();
205     $toks['PAGE_KEYWORDS'] = GleanKeywords($page);
206     if (!$template)
207         $template = new Template('html', $request);
208     
209     $template->printExpansion($toks);
210     $page->increaseHitCount();
211
212     if ($request->getArg('action') != 'pdf')
213         $request->checkValidators();
214     flush();
215 }
216
217 // $Log: not supported by cvs2svn $
218 // Revision 1.57  2004/11/01 10:43:57  rurban
219 // seperate PassUser methods into seperate dir (memory usage)
220 // fix WikiUser (old) overlarge data session
221 // remove wikidb arg from various page class methods, use global ->_dbi instead
222 // ...
223 //
224 // Revision 1.56  2004/10/14 13:44:14  rurban
225 // fix lib/display.php:159: Warning[2]: Argument to array_reverse() should be an array
226 //
227 // Revision 1.55  2004/09/26 14:58:35  rurban
228 // naive SearchHighLight implementation
229 //
230 // Revision 1.54  2004/09/17 14:19:41  rurban
231 // disable Content-Type header for now, until it is fixed
232 //
233 // Revision 1.53  2004/06/25 14:29:20  rurban
234 // WikiGroup refactoring:
235 //   global group attached to user, code for not_current user.
236 //   improved helpers for special groups (avoid double invocations)
237 // new experimental config option ENABLE_XHTML_XML (fails with IE, and document.write())
238 // fixed a XHTML validation error on userprefs.tmpl
239 //
240 // Revision 1.52  2004/06/14 11:31:37  rurban
241 // renamed global $Theme to $WikiTheme (gforge nameclash)
242 // inherit PageList default options from PageList
243 //   default sortby=pagename
244 // use options in PageList_Selectable (limit, sortby, ...)
245 // added action revert, with button at action=diff
246 // added option regex to WikiAdminSearchReplace
247 //
248 // Revision 1.51  2004/05/18 16:23:39  rurban
249 // rename split_pagename to SplitPagename
250 //
251 // Revision 1.50  2004/05/04 22:34:25  rurban
252 // more pdf support
253 //
254 // Revision 1.49  2004/04/18 01:11:52  rurban
255 // more numeric pagename fixes.
256 // fixed action=upload with merge conflict warnings.
257 // charset changed from constant to global (dynamic utf-8 switching)
258 //
259
260 // For emacs users
261 // Local Variables:
262 // mode: php
263 // tab-width: 8
264 // c-basic-offset: 4
265 // c-hanging-comment-ender-p: nil
266 // indent-tabs-mode: nil
267 // End:
268 ?>