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