]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
minor cleanup for localization
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php rcs_id('$Id: PageGroup.php,v 1.4 2002-02-16 02:08:26 carstenklapp Exp $');
2 /**
3  * Usage:
4  *
5  * <?plugin PageGroup parent=MyTableOfContents ?>
6  *
7  * <?plugin PageGroup parent=MyTableOfContents label="Visit more pages in MyTableOfContents" ?>
8  *
9  * <?plugin PageGroup parent=MyTableOfContents section=PartTwo loop=true ?>
10  *
11  * <?plugin PageGroup parent=MyTableOfContents loop=1 ?>
12  *
13  *
14  * Updated to use new HTML(). It mostly works, but it's still a giant hackish mess.
15  */
16 class WikiPlugin_PageGroup
17 extends WikiPlugin
18 {
19     function getName() {
20         return _("PageGroup");
21     }
22
23     function getDescription() {
24         return sprintf(_("PageGroup for %s"),'[pagename]');
25     }
26
27     function getDefaultArguments() {
28         return array(
29                      'parent'  => '',
30                      'rev'     => false,
31                      'section' => _("Contents"),
32                      'label'   => '',
33                      'loop'    => false,
34                      );
35     }
36
37     // Stolen from IncludePage.php
38     function extractSection ($section, $content, $page) {
39         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
40
41         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
42                        . "  \\s*$\\n?"           // possible blank lines
43                        . "  ( (?: ^.*\\n? )*? )" // some lines
44                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
45                        implode("\n", $content),
46                        $match)) {
47             // Strip trailing blanks lines and ---- <hr>s
48             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
49             return explode("\n", $text);
50         }
51         return array(sprintf(_("<%s: no such section>"), $page ." ". $section));
52     }
53
54     function run($dbi, $argstr, $request) {
55
56         $args = $this->getArgs($argstr, $request);
57         extract($args);
58         $html="";
59         if (empty($parent)) {
60             // FIXME: WikiPlugin has no way to report when
61             // required args are missing?
62             $error_text = fmt("%s: %s", "WikiPlugin_" .$this->getName(),
63                               $error_text);
64             $error_text .= " " . sprintf(_("A required argument '%s' is missing."),
65                                          'parent');
66             $html = $error_text;
67             return $html;
68         }
69
70         $directions = array ('next'     => _("Next"),
71                              'previous' => _("Previous"),
72                              'contents' => _("Contents"),
73                              'first'    => _("First"),
74                              'last'     => _("Last")
75                              );
76
77         global $Theme;
78         $sep = $Theme->getButtonSeparator();
79         if (!$sep)
80             $sep = " | "; // force some kind of separator
81
82         // default label
83         if (!$label)
84             $label = $Theme->makeLinkButton($parent);
85
86         // This is where the list extraction occurs from the named
87         // $section on the $parent page.
88
89         $p = $dbi->getPage($parent);
90         if ($rev) {
91             $r = $p->getRevision($rev);
92             if (!$r) {
93                 $this->error(sprintf(_("%s(%d): no such revision"), $parent,
94                                      $rev));
95                 return '';
96             }
97         } else {
98             $r = $p->getCurrentRevision();
99         }
100
101         $c = $r->getContent();
102         $c = $this->extractSection($section, $c, $parent);
103
104         $pagename = $request->getArg('pagename');
105
106         // The ordered list of page names determines the page
107         // ordering. Right now it doesn't work with a WikiList, only
108         // normal lines of text containing the page names.
109
110         $thispage = array_search($pagename, $c);
111
112         $go = array ('previous','next');
113         $links = HTML();
114         $links->pushcontent($label);
115         $links->pushcontent(" [ "); // an experiment
116         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
117
118         foreach ( $go as $go_item ) {
119             //yuck this smells, needs optimization.
120             if ($go_item == 'previous') {
121                 if ($loop) {
122                     if ($thispage == 0) {
123                         $linkpage  = $c[$lastindex];
124                     } else {
125                         $linkpage  = $c[$thispage - 1];
126                     }
127                     // mind the French : punctuation
128                     $text = fmt("%s: %s", $directions[$go_item], $Theme->makeLinkButton($linkpage));
129                     $links->pushcontent($text);
130                     $links->pushcontent($sep); //this works because there are only 2 go items, previous,next
131                 } else {
132                     if ($thispage == 0) {
133                         // skip it
134                     } else {
135                         $linkpage  = $c[$thispage - 1];
136                         $text = fmt("%s: %s", $directions[$go_item], $Theme->makeLinkButton($linkpage));
137                         $links->pushcontent($text);
138                         $links->pushcontent($sep); //this works because there are only 2 go items, previous,next
139                     }
140                 }
141             } else if ($go_item == 'next') {
142                 if ($loop) {
143                     if ($thispage == $lastindex) {
144                         $linkpage  = $c[1];
145                     } else {
146                         $linkpage  = $c[$thispage + 1];
147                     }
148                     $text = fmt("%s: %s", $directions[$go_item], $Theme->makeLinkButton($linkpage));
149                 } else {
150                     if ($thispage == $lastindex) {
151                         // skip it
152                     } else {
153                         $linkpage = $c[$thispage + 1];
154                         $text = fmt("%s: %s", $directions[$go_item], $Theme->makeLinkButton($linkpage));
155                     }
156                 }
157                 $links->pushcontent($text);
158             }
159         }
160         $links->pushcontent(" ] "); // an experiment
161         return $links;
162
163     }
164
165 };
166
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End:
174 ?>