]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
Code cleanup:
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageGroup.php,v 1.5 2003-01-18 21:49:00 carstenklapp Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Usage:
25  *
26  * <?plugin PageGroup parent=MyTableOfContents ?>
27  *
28  * <?plugin PageGroup
29  *          parent=MyTableOfContents
30  *          label="Visit more pages in MyTableOfContents"
31  * ?>
32  *
33  * <?plugin PageGroup parent=MyTableOfContents section=PartTwo loop=true ?>
34  *
35  * <?plugin PageGroup parent=MyTableOfContents loop=1 ?>
36  *
37  *
38  * Updated to use new HTML(). It mostly works, but it's still a giant hackish mess.
39  */
40 class WikiPlugin_PageGroup
41 extends WikiPlugin
42 {
43     function getName() {
44         return _("PageGroup");
45     }
46
47     function getDescription() {
48         return sprintf(_("PageGroup for %s"),'[pagename]');
49     }
50
51     function getVersion() {
52         return preg_replace("/[Revision: $]/", '',
53                             "\$Revision: 1.5 $");
54     }
55
56     function getDefaultArguments() {
57         return array(
58                      'parent'  => '',
59                      'rev'     => false,
60                      'section' => _("Contents"),
61                      'label'   => '',
62                      'loop'    => false,
63                      );
64     }
65
66     // Stolen from IncludePage.php
67     function extractSection ($section, $content, $page) {
68         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
69
70         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
71                        . "  \\s*$\\n?"           // possible blank lines
72                        . "  ( (?: ^.*\\n? )*? )" // some lines
73                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
74                        implode("\n", $content),
75                        $match)) {
76             // Strip trailing blanks lines and ---- <hr>s
77             $text = preg_replace("/\\s*^-{4,}\\s*$/m", "", $match[2]);
78             return explode("\n", $text);
79         }
80         return array(sprintf(_("<%s: no such section>"), $page ." ". $section));
81     }
82
83     function run($dbi, $argstr, $request) {
84
85         $args = $this->getArgs($argstr, $request);
86         extract($args);
87         $html="";
88         if (empty($parent)) {
89             // FIXME: WikiPlugin has no way to report when
90             // required args are missing?
91             $error_text = fmt("%s: %s", "WikiPlugin_" .$this->getName(),
92                               $error_text);
93             $error_text .= " " . sprintf(_("A required argument '%s' is missing."), 'parent');
94             $html = $error_text;
95             return $html;
96         }
97
98         $directions = array ('next'     => _("Next"),
99                              'previous' => _("Previous"),
100                              'contents' => _("Contents"),
101                              'first'    => _("First"),
102                              'last'     => _("Last")
103                              );
104
105         global $Theme;
106         $sep = $Theme->getButtonSeparator();
107         if (!$sep)
108             $sep = " | "; // force some kind of separator
109
110         // default label
111         if (!$label)
112             $label = $Theme->makeLinkButton($parent);
113
114         // This is where the list extraction occurs from the named
115         // $section on the $parent page.
116
117         $p = $dbi->getPage($parent);
118         if ($rev) {
119             $r = $p->getRevision($rev);
120             if (!$r) {
121                 $this->error(sprintf(_("%s(%d): no such revision"), $parent,
122                                      $rev));
123                 return '';
124             }
125         } else {
126             $r = $p->getCurrentRevision();
127         }
128
129         $c = $r->getContent();
130         $c = $this->extractSection($section, $c, $parent);
131
132         $pagename = $request->getArg('pagename');
133
134         // The ordered list of page names determines the page
135         // ordering. Right now it doesn't work with a WikiList, only
136         // normal lines of text containing the page names.
137
138         $thispage = array_search($pagename, $c);
139
140         $go = array ('previous','next');
141         $links = HTML();
142         $links->pushcontent($label);
143         $links->pushcontent(" [ "); // an experiment
144         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
145
146         foreach ( $go as $go_item ) {
147             //yuck this smells, needs optimization.
148             if ($go_item == 'previous') {
149                 if ($loop) {
150                     if ($thispage == 0) {
151                         $linkpage  = $c[$lastindex];
152                     } else {
153                         $linkpage  = $c[$thispage - 1];
154                     }
155                     // mind the French : punctuation
156                     $text = fmt("%s: %s", $directions[$go_item],
157                                 $Theme->makeLinkButton($linkpage));
158                     $links->pushcontent($text);
159                     $links->pushcontent($sep); // this works because
160                                                // there are only 2 go
161                                                // items, previous,next
162                 } else {
163                     if ($thispage == 0) {
164                         // skip it
165                     } else {
166                         $linkpage  = $c[$thispage - 1];
167                         $text = fmt("%s: %s", $directions[$go_item],
168                                     $Theme->makeLinkButton($linkpage));
169                         $links->pushcontent($text);
170                         $links->pushcontent($sep); //this works
171                                                    //because there are
172                                                    //only 2 go items,
173                                                    //previous,next
174                     }
175                 }
176             } else if ($go_item == 'next') {
177                 if ($loop) {
178                     if ($thispage == $lastindex) {
179                         $linkpage  = $c[1];
180                     } else {
181                         $linkpage  = $c[$thispage + 1];
182                     }
183                     $text = fmt("%s: %s", $directions[$go_item],
184                                 $Theme->makeLinkButton($linkpage));
185                 } else {
186                     if ($thispage == $lastindex) {
187                         // skip it
188                     } else {
189                         $linkpage = $c[$thispage + 1];
190                         $text = fmt("%s: %s", $directions[$go_item],
191                                     $Theme->makeLinkButton($linkpage));
192                     }
193                 }
194                 $links->pushcontent($text);
195             }
196         }
197         $links->pushcontent(" ] "); // an experiment
198         return $links;
199     }
200 };
201
202 // $Log: not supported by cvs2svn $
203
204 // Local Variables:
205 // mode: php
206 // tab-width: 8
207 // c-basic-offset: 4
208 // c-hanging-comment-ender-p: nil
209 // indent-tabs-mode: nil
210 // End:
211 ?>