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