2 // display.php: fetch page or get default content
3 rcs_id('$Id: display.php,v 1.45 2003-03-07 20:51:59 dairiki Exp $');
5 require_once('lib/Template.php');
8 * Guess a short description of the page.
12 * This algorithm was suggested on MeatballWiki by
13 * Alex Schroeder <kensanata@yahoo.com>.
15 * Use the first paragraph in the page which contains at least two
18 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
20 function GleanDescription ($rev) {
22 = pcre_fix_posix_classes("/[.?!]\s+[[:upper:])]"
24 . "[.?!]\s*([[:upper:])]|$)/sx");
26 $content = preg_replace("/(['\"])/", "\$1", $rev->getPackedContent());
28 // Iterate through paragraphs.
29 while (preg_match('/(?: ^ \w .* $ \n? )+/mx', $content, $m)) {
32 // Return paragraph if it contains at least two sentences.
33 if (preg_match($two_sentences, $paragraph)) {
34 return preg_replace("/\s*\n\s*/", " ", trim($paragraph));
37 $content = substr(strstr($content, $paragraph), strlen($paragraph));
43 * Extract keywords from Category* links on page.
45 function GleanKeywords ($page) {
46 global $KeywordLinkRegexp;
48 $links = $page->getLinks(false);
50 $keywords[] = split_pagename($page->getName());
52 while ($link = $links->next())
53 if (preg_match("/${KeywordLinkRegexp}/x", $link->getName(), $m))
54 $keywords[] = split_pagename($m[0]);
56 $keywords[] = WIKI_NAME;
58 return join(', ', $keywords);
61 /** Make a link back to redirecting page.
63 * @param $pagename string Name of redirecting page.
64 * @return XmlContent Link to the redirecting page.
66 function RedirectorLink($pagename) {
67 $url = WikiURL($pagename, array('redirectfrom' => ''));
68 return HTML::a(array('class' => 'redirectfrom wiki',
74 function actionPage(&$request, $action) {
77 $pagename = $request->getArg('pagename');
78 $version = $request->getArg('version');
80 $page = $request->getPage();
81 $revision = $page->getCurrentRevision();
83 $dbi = $request->getDbh();
84 $actionpage = $dbi->getPage($action);
85 $actionrev = $actionpage->getCurrentRevision();
87 // $splitname = split_pagename($pagename);
89 $pagetitle = HTML(fmt("%s: %s", $actionpage->getName(),
90 $Theme->linkExistingWikiWord($pagename, false, $version)));
92 $validators = new HTTP_ValidatorSet(array('pageversion' => $revision->getVersion(),
93 '%mtime' => $revision->get('mtime')));
95 $request->appendValidators(array('pagerev' => $revision->getVersion(),
96 '%mtime' => $revision->get('mtime')));
97 $request->appendValidators(array('actionpagerev' => $actionrev->getVersion(),
98 '%mtime' => $actionrev->get('mtime')));
100 $transformedContent = $actionrev->getTransformedContent();
101 $template = Template('browse', array('CONTENT' => $transformedContent));
103 header("Content-Type: text/html; charset=" . CHARSET);
104 GeneratePage($template, $pagetitle, $revision);
105 $request->checkValidators();
109 function displayPage(&$request, $template=false) {
110 $pagename = $request->getArg('pagename');
111 $version = $request->getArg('version');
112 $page = $request->getPage();
114 $revision = $page->getRevision($version);
116 NoSuchRevision($request, $page, $version);
119 $revision = $page->getCurrentRevision();
122 $splitname = split_pagename($pagename);
123 if (isSubPage($pagename)) {
124 $pages = explode(SUBPAGE_SEPARATOR,$pagename);
125 $last_page = array_pop($pages); // deletes last element from array as side-effect
126 $pagetitle = HTML::span(HTML::a(array('href' => WikiURL($pages[0]),
127 'class' => 'pagetitle'
129 split_pagename($pages[0] . SUBPAGE_SEPARATOR)));
130 $first_pages = $pages[0] . SUBPAGE_SEPARATOR;
132 foreach ($pages as $p) {
133 $pagetitle->pushContent(HTML::a(array('href' => WikiURL($first_pages . $p),
134 'class' => 'backlinks'),
135 split_pagename($p . SUBPAGE_SEPARATOR)));
136 $first_pages .= $p . SUBPAGE_SEPARATOR;
138 $backlink = HTML::a(array('href' => WikiURL($pagename,
139 array('action' => _("BackLinks"))),
140 'class' => 'backlinks'),
141 split_pagename($last_page));
142 $backlink->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
143 $pagetitle->pushContent($backlink);
145 $pagetitle = HTML::a(array('href' => WikiURL($pagename,
146 array('action' => _("BackLinks"))),
147 'class' => 'backlinks'),
149 $pagetitle->addTooltip(sprintf(_("BackLinks for %s"), $pagename));
150 if ($request->getArg('frame'))
151 $pagetitle->setAttr('target', '_top');
154 $pageheader = $pagetitle;
155 if (($redirect_from = $request->getArg('redirectfrom'))) {
156 $redirect_message = HTML::span(array('class' => 'redirectfrom'),
157 fmt("(Redirected from %s)",
158 RedirectorLink($redirect_from)));
159 $pageheader = HTML($pagetitle, $redirect_message);
162 $request->appendValidators(array('pagerev' => $revision->getVersion(),
163 '%mtime' => $revision->get('mtime')));
165 // FIXME: should probably be in a template...
166 header("Content-Type: text/html; charset=" . CHARSET); // FIXME: this gets done twice?
168 $toks['CONTENT'] = new Template('browse', $request,
169 $revision->getTransformedContent());
172 $toks['TITLE'] = $pagetitle;
173 $toks['HEADER'] = $pageheader;
174 $toks['revision'] = $revision;
175 $toks['ROBOTS_META'] = 'index,follow';
176 $toks['PAGE_DESCRIPTION'] = GleanDescription($revision);
177 $toks['PAGE_KEYWORDS'] = GleanKeywords($page);
180 $template = new Template('html', $request);
182 $template->printExpansion($toks);
183 $page->increaseHitCount();
185 $request->checkValidators();
194 // c-hanging-comment-ender-p: nil
195 // indent-tabs-mode: nil