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