]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
elseif
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright 1999,2000,2001,2002,2004 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 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  * Usage:
26  *
27  * <<PageGroup parent=MyTableOfContents >>
28  *
29  * <<PageGroup
30  *          parent=MyTableOfContents
31  *          label="Visit more pages in MyTableOfContents"
32  * >>
33  *
34  * <<PageGroup parent=MyTableOfContents section=PartTwo loop=true >>
35  *
36  * <<PageGroup parent=MyTableOfContents loop=1 >>
37  *
38  *
39  * Updated to use new HTML(). It mostly works, but it's still a giant hackish mess.
40  */
41 class WikiPlugin_PageGroup
42 extends WikiPlugin
43 {
44     function getName() {
45         return _("PageGroup");
46     }
47
48     function getDescription() {
49         return sprintf(_("PageGroup for %s"),'[pagename]');
50     }
51
52     function getDefaultArguments() {
53         return array(
54                      'parent'  => '',
55                      'rev'     => false,
56                      'section' => _("Contents"),
57                      'label'   => '',
58                      'loop'    => false,
59                      );
60     }
61
62     // Stolen from IncludePage.php
63     function extractGroupSection ($section, $content, $page) {
64         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
65         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
66                        . "  \\s*$\\n?"           // possible blank lines
67                        . "  ( (?: ^.*\\n? )*? )" // some lines
68                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
69                        implode("\n", $content),
70                        $match)) {
71             $result = array();
72             //FIXME: return list of Wiki_Pagename objects
73             foreach (explode("\n", $match[2]) as $line) {
74                     $text = trim($line);
75                 // Strip trailing blanks lines and ---- <hr>s
76                 $text = preg_replace("/\\s*^-{4,}\\s*$/", "", $text);
77                 // Strip leading list chars: * or #
78                 $text = preg_replace("/^[\*#]+\s*(\S.+)$/", "\\1", $text);
79                 // Strip surrounding []
80                 // FIXME: parse [ name | link ]
81                 $text = preg_replace("/^\[\s*(\S.+)\s*\]$/", "\\1", $text);
82                 if (!empty($text))
83                     $result[] = $text;
84             }
85             return $result;
86         }
87         return array(sprintf(_("<%s: no such section>"), $page ." ". $section));
88     }
89
90     function run($dbi, $argstr, &$request, $basepage) {
91
92         $args = $this->getArgs($argstr, $request);
93         extract($args);
94         if (empty($parent)) {
95             return $this->error(sprintf(_("A required argument '%s' is missing."), 'parent'));
96         }
97         $directions = array ('next'     => _("Next"),
98                              'previous' => _("Previous"),
99                              'contents' => _("Contents"),
100                              'first'    => _("First"),
101                              'last'     => _("Last")
102                              );
103
104         global $WikiTheme;
105         $sep = $WikiTheme->getButtonSeparator();
106         if (!$sep)
107             $sep = " | "; // force some kind of separator
108
109         // default label
110         if (!$label)
111             $label = $WikiTheme->makeLinkButton($parent);
112
113         // This is where the list extraction occurs from the named
114         // $section on the $parent page.
115
116         $p = $dbi->getPage($parent);
117         if ($rev) {
118             $r = $p->getRevision($rev);
119             if ((!$r) || ($r->hasDefaultContents())) {
120                 return $this->error(sprintf(_("%s: no such revision %d."),
121                                             $parent, $rev));
122             }
123         } else {
124             $r = $p->getCurrentRevision();
125         }
126
127         $c = $r->getContent();
128         $c = $this->extractGroupSection($section, $c, $parent);
129
130         $pagename = $request->getArg('pagename');
131
132         // The ordered list of page names determines the page
133         // ordering. Right now it doesn't work with a WikiList, only
134         // normal lines of text containing the page names.
135
136         $thispage = array_search($pagename, $c);
137
138         $go = array ('previous','next');
139         $links = HTML();
140         $links->pushcontent($label);
141         $links->pushcontent(" [ "); // an experiment
142         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
143
144         foreach ( $go as $go_item ) {
145             //yuck this smells, needs optimization.
146             if ($go_item == 'previous') {
147                 if ($loop) {
148                     if ($thispage == 0) {
149                         $linkpage  = $c[$lastindex];
150                     } else {
151                         $linkpage  = $c[$thispage - 1];
152                     }
153                     // mind the French : punctuation
154                     $text = fmt("%s: %s", $directions[$go_item],
155                                 $WikiTheme->makeLinkButton($linkpage));
156                     $links->pushcontent($text);
157                     $links->pushcontent($sep); // this works because
158                                                // there are only 2 go
159                                                // items, previous,next
160                 } else {
161                     if ($thispage == 0) {
162                         // skip it
163                     } else {
164                         $linkpage  = $c[$thispage - 1];
165                         $text = fmt("%s: %s", $directions[$go_item],
166                                     $WikiTheme->makeLinkButton($linkpage));
167                         $links->pushcontent($text);
168                         $links->pushcontent($sep); //this works
169                                                    //because there are
170                                                    //only 2 go items,
171                                                    //previous,next
172                     }
173                 }
174             } elseif ($go_item == 'next') {
175                 if ($loop) {
176                     if ($thispage == $lastindex) {
177                         $linkpage  = $c[1];
178                     } else {
179                         $linkpage  = $c[$thispage + 1];
180                     }
181                     $text = fmt("%s: %s", $directions[$go_item],
182                                 $WikiTheme->makeLinkButton($linkpage));
183                 } else {
184                     if ($thispage == $lastindex) {
185                         // skip it
186                     } else {
187                         $linkpage = $c[$thispage + 1];
188                         $text = fmt("%s: %s", $directions[$go_item],
189                                     $WikiTheme->makeLinkButton($linkpage));
190                     }
191                 }
192                 $links->pushcontent($text);
193             }
194         }
195         $links->pushcontent(" ] "); // an experiment
196         return $links;
197     }
198 };
199
200 // Local Variables:
201 // mode: php
202 // tab-width: 8
203 // c-basic-offset: 4
204 // c-hanging-comment-ender-p: nil
205 // indent-tabs-mode: nil
206 // End:
207 ?>