]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateToc.php
message --> err_header
[SourceForge/phpwiki.git] / lib / plugin / CreateToc.php
1 <?php
2
3 /*
4  * Copyright 2004,2005 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2010 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * CreateToc:  Create a Table of Contents and automatically link to headers
26  *
27  * Usage:
28  *  <<CreateToc arguments>>
29  * @author:  Reini Urban, Marc-Etienne Vargenau
30  *
31  * Known problems:
32  * - MacIE will not work with jshide.
33  * - it will crash with old markup and Apache2 (?)
34  * - Certain corner-edges will not work with TOC_FULL_SYNTAX.
35  *   I believe I fixed all of them now, but who knows?
36  * - bug #969495 "existing labels not honored" seems to be fixed.
37  */
38
39 if (!defined('TOC_FULL_SYNTAX'))
40     define('TOC_FULL_SYNTAX', 1);
41
42 class WikiPlugin_CreateToc
43     extends WikiPlugin
44 {
45     function getName()
46     {
47         return _("CreateToc");
48     }
49
50     function getDescription()
51     {
52         return _("Create a Table of Contents and automatically link to headers");
53     }
54
55     function getDefaultArguments()
56     {
57         return array('extracollapse' => 1, // provide an entry +/- link to collapse
58             'firstlevelstyle' => 'number', // 'number', 'letter' or 'roman'
59             'headers' => "1,2,3,4,5", // "!!!"=>h2, "!!"=>h3, "!"=>h4
60             // "1"=>h2, "2"=>h3, "3"=>h4, "4"=>h5, "5"=>h6
61             'indentstr' => '&nbsp;&nbsp;',
62             'jshide' => 0, // collapsed TOC as DHTML button
63             'liststyle' => 'dl', // 'dl' or 'ul' or 'ol'
64             'noheader' => 0, // omit "Table of Contents" header
65             'notoc' => 0, // do not display TOC, only number headers
66             'pagename' => '[pagename]', // TOC of another page here?
67             'position' => 'full', // full, right or left
68             'width' => '200px',
69             'with_counter' => 0,
70             'with_toclink' => 0, // link back to TOC
71             'version' => false,
72         );
73     }
74
75     // Initialisation of toc counter
76     function _initTocCounter()
77     {
78         $counter = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
79         return $counter;
80     }
81
82     // Update toc counter with a new title
83     function _tocCounter(&$counter, $level)
84     {
85         $counter[$level]++;
86         for ($i = $level + 1; $i <= 5; $i++) {
87             $counter[$i] = 0;
88         }
89     }
90
91     function _roman_counter($number)
92     {
93
94         $n = intval($number);
95         $result = '';
96
97         $lookup = array('C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
98             'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
99
100         foreach ($lookup as $roman => $value) {
101             $matches = intval($n / $value);
102             $result .= str_repeat($roman, $matches);
103             $n = $n % $value;
104         }
105         return $result;
106     }
107
108     function _letter_counter($number)
109     {
110         if ($number <= 26) {
111             return chr(ord("A") + $number - 1);
112         } else {
113             return chr(ord("A") + ($number / 26) - 1) . chr(ord("A") + ($number % 26));
114         }
115     }
116
117     // Get string corresponding to the current title
118     function _getCounter(&$counter, $level, $firstlevelstyle)
119     {
120         if ($firstlevelstyle == 'roman') {
121             $str = $this->_roman_counter($counter[1]);
122         } elseif ($firstlevelstyle == 'letter') {
123             $str = $this->_letter_counter($counter[1]);
124         } else {
125             $str = $counter[1];
126         }
127         for ($i = 2; $i <= 5; $i++) {
128             if ($counter[$i] != 0)
129                 $str .= '.' . $counter[$i];
130         }
131         return $str;
132     }
133
134     // Get HTML header corresponding to current level (level is set of ! or =)
135     function _getHeader($level)
136     {
137
138         $count = substr_count($level, '!');
139         switch ($count) {
140             case 3:
141                 return "h2";
142             case 2:
143                 return "h3";
144             case 1:
145                 return "h4";
146         }
147         $count = substr_count($level, '=');
148         switch ($count) {
149             case 2:
150                 return "h2";
151             case 3:
152                 return "h3";
153             case 4:
154                 return "h4";
155             case 5:
156                 return "h5";
157             case 6:
158                 return "h6";
159         }
160         return "";
161     }
162
163     function _quote($heading)
164     {
165         if (TOC_FULL_SYNTAX) {
166             $theading = TransformInline($heading);
167             if ($theading)
168                 return preg_quote($theading->asXML(), "/");
169             else
170                 return XmlContent::_quote(preg_quote($heading, "/"));
171         } else {
172             return XmlContent::_quote(preg_quote($heading, "/"));
173         }
174     }
175
176     /*
177      * @param $hstart id (in $content) of heading start
178      * @param $hend   id (in $content) of heading end
179      */
180     function searchHeader($content, $start_index, $heading,
181                           $level, &$hstart, &$hend, $basepage = false)
182     {
183         $hstart = 0;
184         $hend = 0;
185         $h = $this->_getHeader($level);
186         $qheading = $this->_quote($heading);
187         for ($j = $start_index; $j < count($content); $j++) {
188             if (is_string($content[$j])) {
189                 if (preg_match("/<$h>$qheading<\/$h>/",
190                     $content[$j])
191                 )
192                     return $j;
193             } elseif (isa($content[$j], 'cached_link')) {
194                 if (method_exists($content[$j], 'asXML')) {
195                     $content[$j]->_basepage = $basepage;
196                     $content[$j] = $content[$j]->asXML();
197                 } else
198                     $content[$j] = $content[$j]->asString();
199                 // shortcut for single wikiword or link headers
200                 if ($content[$j] == $heading
201                     and substr($content[$j - 1], -4, 4) == "<$h>"
202                         and substr($content[$j + 1], 0, 5) == "</$h>"
203                 ) {
204                     $hstart = $j - 1;
205                     $hend = $j + 1;
206                     return $j; // single wikiword
207                 } elseif (TOC_FULL_SYNTAX) {
208                     //DONE: To allow "!! WikiWord link" or !! http://anylink/
209                     // Check against joined content (after cached_plugininvocation).
210                     // The first link is the anchor then.
211                     if (preg_match("/<$h>(?!.*<\/$h>)/", $content[$j - 1])) {
212                         $hstart = $j - 1;
213                         $joined = '';
214                         for ($k = max($j - 1, $start_index); $k < count($content); $k++) {
215                             if (is_string($content[$k]))
216                                 $joined .= $content[$k];
217                             elseif (method_exists($content[$k], 'asXML'))
218                                 $joined .= $content[$k]->asXML(); else
219                                 $joined .= $content[$k]->asString();
220                             if (preg_match("/<$h>$qheading<\/$h>/", $joined)) {
221                                 $hend = $k;
222                                 return $k;
223                             }
224                         }
225                     }
226                 }
227             }
228         }
229         // Do not trigger error, it happens e.g. for "<<CreateToc pagename=AnotherPage>>"
230         // trigger_error("Heading <$h> $heading </$h> not found\n", E_USER_NOTICE);
231
232         return 0;
233     }
234
235     /** prevent from duplicate anchors,
236      *  beautify spaces: " " => "_" and not "x20."
237      */
238     function _nextAnchor($s)
239     {
240         static $anchors = array();
241
242         $s = str_replace(' ', '_', $s);
243         $i = 1;
244         $anchor = $s;
245         while (!empty($anchors[$anchor])) {
246             $anchor = sprintf("%s_%d", $s, $i++);
247         }
248         $anchors[$anchor] = $i;
249         return $anchor;
250     }
251
252     // We have to find headers in both:
253     // - classic Phpwiki syntax (lines starting with "!", "!!" or "!!!")
254     // - Wikicreole syntax (lines starting with "==", "===", etc.)
255     // We must omit lines starting with "!" if inside a Mediawiki table
256     // (they represent a table header)
257     // Feature request: proper nesting; multiple levels (e.g. 1,3)
258     function extractHeaders(&$content, &$markup, $backlink = 0,
259                             $counter = 0, $levels = false, $firstlevelstyle = 'number', $basepage = '')
260     {
261         if (!$levels) $levels = array(1, 2);
262         $tocCounter = $this->_initTocCounter();
263         reset($levels);
264         sort($levels);
265         $headers = array();
266         $j = 0;
267         $insidetable = false;
268         $insideverbatim = false;
269         for ($i = 0; $i < count($content); $i++) {
270             if (preg_match('/^\s*{\|/', $content[$i])) {
271                 $insidetable = true;
272                 continue;
273             } elseif (preg_match('/^\s*{{{/', $content[$i])
274                 || preg_match('/^\s*<pre>/', $content[$i])
275                 || preg_match('/^\s*<verbatim>/', $content[$i])
276             ) {
277                 $insideverbatim = true;
278                 continue;
279             } elseif (preg_match('/^\s*\|}/', $content[$i])) {
280                 $insidetable = false;
281                 continue;
282             } elseif (preg_match('/^\s*}}}/', $content[$i])
283                 || preg_match('/^\s*<\/pre>/', $content[$i])
284                 || preg_match('/^\s*<\/verbatim>/', $content[$i])
285             ) {
286                 $insideverbatim = false;
287                 continue;
288             }
289             if (($insidetable) || ($insideverbatim)) {
290                 continue;
291             }
292             foreach ($levels as $level) {
293                 if ($level < 1 or $level > 5) continue;
294                 $phpwikiclassiclevel = 4 - $level;
295                 $wikicreolelevel = $level + 1;
296                 $trim = trim($content[$i]);
297
298                 if ((((strpos($trim, '=') === 0))
299                     && (preg_match('/^\s*(={' . $wikicreolelevel . ',' . $wikicreolelevel . '})([^=].*)$/', $content[$i], $match)))
300                     or (((strpos($trim, '!') === 0))
301                         && ((preg_match('/^\s*(!{' . $phpwikiclassiclevel . ',' . $phpwikiclassiclevel . '})([^!].*)$/', $content[$i], $match))))
302                 ) {
303                     $this->_tocCounter($tocCounter, $level);
304                     if (!strstr($content[$i], '#[')) {
305                         $s = trim($match[2]);
306                         // If it is Wikicreole syntax, remove '='s at the end
307                         if (string_starts_with($match[1], "=")) {
308                             $s = trim($s, "=");
309                             $s = trim($s);
310                         }
311                         $anchor = $this->_nextAnchor($s);
312                         $manchor = MangleXmlIdentifier($anchor);
313                         $texts = $s;
314                         if ($counter) {
315                             $texts = $this->_getCounter($tocCounter, $level, $firstlevelstyle) . ' ' . $s;
316                         }
317                         $headers[] = array('text' => $texts,
318                             'anchor' => $anchor,
319                             'level' => $level);
320                         // Change original wikitext, but that is useless art...
321                         $content[$i] = $match[1] . " #[|$manchor][$s|#TOC]";
322                         // And now change the to be printed markup (XmlTree):
323                         // Search <hn>$s</hn> line in markup
324                         /* Url for backlink */
325                         $url = WikiURL(new WikiPageName($basepage, false, "TOC"));
326
327                         $j = $this->searchHeader($markup->_content, $j, $s,
328                             $match[1], $hstart, $hend,
329                             $markup->_basepage);
330                         if ($j and isset($markup->_content[$j])) {
331                             $x = $markup->_content[$j];
332                             $qheading = $this->_quote($s);
333                             if ($counter)
334                                 $counterString = $this->_getCounter($tocCounter, $level, $firstlevelstyle);
335                             if (($hstart === 0) && is_string($markup->_content[$j])) {
336                                 if ($backlink) {
337                                     if ($counter)
338                                         $anchorString = "<a href=\"$url\" id=\"$manchor\">$counterString</a> - \$2";
339                                     else
340                                         $anchorString = "<a href=\"$url\" id=\"$manchor\">\$2</a>";
341                                 } else {
342                                     $anchorString = "<a id=\"$manchor\"></a>";
343                                     if ($counter)
344                                         $anchorString .= "$counterString - ";
345                                 }
346                                 if ($x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
347                                     "\$1$anchorString\$2\$3", $x, 1)
348                                 ) {
349                                     if ($backlink) {
350                                         $x = preg_replace('/(<h\d>)(' . $qheading . ')(<\/h\d>)/',
351                                             "\$1$anchorString\$3",
352                                             $markup->_content[$j], 1);
353                                     }
354                                     $markup->_content[$j] = $x;
355                                 }
356                             } else {
357                                 $x = $markup->_content[$hstart];
358                                 $h = $this->_getHeader($match[1]);
359
360                                 if ($backlink) {
361                                     if ($counter) {
362                                         $anchorString = "\$1<a href=\"$url\" id=\"$manchor\">$counterString</a> - ";
363                                     } else {
364                                         /* Not possible to make a backlink on a
365                                          * title with a WikiWord */
366                                         $anchorString = "\$1<a id=\"$manchor\"></a>";
367                                     }
368                                 } else {
369                                     $anchorString = "\$1<a id=\"$manchor\"></a>";
370                                     if ($counter)
371                                         $anchorString .= "$counterString - ";
372                                 }
373                                 $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
374                                     $anchorString, $x, 1);
375                                 if ($backlink) {
376                                     $x = preg_replace("/(<$h>)(?!.*<\/$h>)/",
377                                         $anchorString,
378                                         $markup->_content[$hstart], 1);
379                                 }
380                                 $markup->_content[$hstart] = $x;
381                             }
382                         }
383                     }
384                 }
385             }
386         }
387         return $headers;
388     }
389
390     function run($dbi, $argstr, &$request, $basepage)
391     {
392         global $WikiTheme;
393         extract($this->getArgs($argstr, $request));
394         if ($pagename) {
395             // Expand relative page names.
396             $page = new WikiPageName($pagename, $basepage);
397             $pagename = $page->name;
398         }
399         if (!$pagename) {
400             return $this->error(sprintf(_("A required argument '%s' is missing."), 'pagename'));
401         }
402         if (isBrowserIE() and browserDetect("Mac")) {
403             $jshide = 0;
404         }
405         if (($notoc) or ($liststyle == 'ol')) {
406             $with_counter = 1;
407         }
408         if ($firstlevelstyle and ($firstlevelstyle != 'number')
409             and ($firstlevelstyle != 'letter')
410                 and ($firstlevelstyle != 'roman')
411         ) {
412             return $this->error(_("Error: firstlevelstyle must be 'number', 'letter' or 'roman'"));
413         }
414
415         // Check if page exists.
416         if (!($dbi->isWikiPage($pagename))) {
417             return $this->error(sprintf(_("Page '%s' does not exist."), $pagename));
418         }
419
420         // Check if user is allowed to get the page.
421         if (!mayAccessPage('view', $pagename)) {
422             return $this->error(sprintf(_("Illegal access to page %s: no read access"),
423                 $pagename));
424         }
425
426         $page = $dbi->getPage($pagename);
427
428         if ($version) {
429             if (!is_whole_number($version) or !($version > 0)) {
430                 return $this->error(_("Error: version must be a positive integer."));
431             }
432             $r = $page->getRevision($version);
433             if ((!$r) || ($r->hasDefaultContents())) {
434                 return $this->error(sprintf(_("%s: no such revision %d."),
435                     $pagename, $version));
436             }
437         } else {
438             $r = $page->getCurrentRevision();
439         }
440
441         $current = $page->getCurrentRevision();
442         //FIXME: I suspect this only to crash with Apache2
443         if (!$current->get('markup') or $current->get('markup') < 2) {
444             if (in_array(php_sapi_name(), array('apache2handler', 'apache2filter'))) {
445                 return $this->error(_("CreateToc disabled for old markup."));
446             }
447         }
448
449         $content = $r->getContent();
450
451         $html = HTML::div(array('class' => 'toc', 'id' => GenerateId("toc")));
452         if ($notoc) {
453             $html->setAttr('style', 'display:none;');
454         }
455         if (($position == "left") or ($position == "right")) {
456             $html->setAttr('style', 'float:' . $position . '; width:' . $width . ';');
457         }
458         $toclistid = GenerateId("toclist");
459         $list = HTML::div(array('id' => $toclistid, 'class' => 'toclist'));
460         if (!strstr($headers, ",")) {
461             $headers = array($headers);
462         } else {
463             $headers = explode(",", $headers);
464         }
465         $levels = array();
466         foreach ($headers as $h) {
467             //replace !!! with level 1, ...
468             if (strstr($h, "!")) {
469                 $hcount = substr_count($h, '!');
470                 $level = min(max(1, $hcount), 3);
471                 $levels[] = $level;
472             } else {
473                 $level = min(max(1, (int)$h), 5);
474                 $levels[] = $level;
475             }
476         }
477         if (TOC_FULL_SYNTAX)
478             require_once 'lib/InlineParser.php';
479         if ($headers = $this->extractHeaders($content, $dbi->_markup,
480             $with_toclink, $with_counter,
481             $levels, $firstlevelstyle, $basepage)
482         ) {
483             foreach ($headers as $h) {
484                 // proper heading indent
485                 $level = $h['level'];
486                 $indent = $level - 1;
487                 $link = new WikiPageName($pagename, $page, $h['anchor']);
488                 $li = WikiLink($link, 'known', $h['text']);
489                 // Hack to suppress pagename before #
490                 // $li->_attr["href"] = strstr($li->_attr["href"], '#');
491                 $list->pushContent(HTML::p(HTML::raw
492                 (str_repeat($indentstr, $indent)), $li));
493             }
494         }
495         $list->setAttr('style', 'display:' . ($jshide ? 'none;' : 'block;'));
496         $open = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowOpen.png");
497         $close = DATA_PATH . '/' . $WikiTheme->_findFile("images/folderArrowClosed.png");
498         if ($noheader) {
499         } else {
500             $toctoggleid = GenerateId("toctoggle");
501             if ($extracollapse)
502                 $toclink = HTML(_("Table of Contents"),
503                     " ",
504                     HTML::a(array('id' => 'TOC')),
505                     HTML::img(array(
506                         'id' => $toctoggleid,
507                         'class' => 'wikiaction',
508                         'title' => _("Click to display to TOC"),
509                         'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')",
510                         'alt' => 'toctoggle',
511                         'src' => $jshide ? $close : $open)));
512             else
513                 $toclink = HTML::a(array('id' => 'TOC',
514                         'class' => 'wikiaction',
515                         'title' => _("Click to display"),
516                         'onclick' => "toggletoc(this, '" . $open . "', '" . $close . "', '" . $toclistid . "')"),
517                     _("Table of Contents"),
518                     HTML::span(array('style' => 'display:none',
519                         'id' => $toctoggleid), " "));
520             $html->pushContent(HTML::p(array('class' => 'toctitle'), $toclink));
521         }
522         $html->pushContent($list);
523         if (count($headers) == 0) {
524             // Do not display an empty TOC
525             $html->setAttr('style', 'display:none;');
526         }
527         return $html;
528     }
529 }
530
531
532
533 // Local Variables:
534 // mode: php
535 // tab-width: 8
536 // c-basic-offset: 4
537 // c-hanging-comment-ender-p: nil
538 // indent-tabs-mode: nil
539 // End: