]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php // -*-php-*-
2 rcs_id('$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
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Usage:
26  *
27  * <?plugin PageGroup parent=MyTableOfContents ?>
28  *
29  * <?plugin PageGroup
30  *          parent=MyTableOfContents
31  *          label="Visit more pages in MyTableOfContents"
32  * ?>
33  *
34  * <?plugin PageGroup parent=MyTableOfContents section=PartTwo loop=true ?>
35  *
36  * <?plugin 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 getVersion() {
53         return preg_replace("/[Revision: $]/", '',
54                             "\$Revision$");
55     }
56
57     function getDefaultArguments() {
58         return array(
59                      'parent'  => '',
60                      'rev'     => false,
61                      'section' => _("Contents"),
62                      'label'   => '',
63                      'loop'    => false,
64                      );
65     }
66
67     // Stolen from IncludePage.php
68     function extractGroupSection ($section, $content, $page) {
69         $qsection = preg_replace('/\s+/', '\s+', preg_quote($section, '/'));
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             $result = array();
77             //FIXME: return list of Wiki_Pagename objects
78             foreach (explode("\n", $match[2]) as $line) {
79                     $text = trim($line);
80                 // Strip trailing blanks lines and ---- <hr>s
81                 $text = preg_replace("/\\s*^-{4,}\\s*$/", "", $text);
82                 // Strip leading list chars: * or #
83                 $text = preg_replace("/^[\*#]+\s*(\S.+)$/", "\\1", $text);
84                 // Strip surrounding []
85                 // FIXME: parse [ name | link ]
86                 $text = preg_replace("/^\[\s*(\S.+)\s*\]$/", "\\1", $text);
87                 if (!empty($text))
88                     $result[] = $text;
89             }
90             return $result;
91         }
92         return array(sprintf(_("<%s: no such section>"), $page ." ". $section));
93     }
94
95     function run($dbi, $argstr, &$request, $basepage) {
96
97         $args = $this->getArgs($argstr, $request);
98         extract($args);
99         if (empty($parent)) {
100             // FIXME: WikiPlugin has no way to report when
101             // required args are missing?
102             $error_text = sprintf("%s: ", "WikiPlugin_" .$this->getName());
103             $error_text .= sprintf(_("A required argument '%s' is missing."), 'parent');
104             return HTML::div(array('class' => "error"), $error_text);
105         }
106         $directions = array ('next'     => _("Next"),
107                              'previous' => _("Previous"),
108                              'contents' => _("Contents"),
109                              'first'    => _("First"),
110                              'last'     => _("Last")
111                              );
112
113         global $WikiTheme;
114         $sep = $WikiTheme->getButtonSeparator();
115         if (!$sep)
116             $sep = " | "; // force some kind of separator
117
118         // default label
119         if (!$label)
120             $label = $WikiTheme->makeLinkButton($parent);
121
122         // This is where the list extraction occurs from the named
123         // $section on the $parent page.
124
125         $p = $dbi->getPage($parent);
126         if ($rev) {
127             $r = $p->getRevision($rev);
128             if (!$r) {
129                 $this->error(sprintf(_("%s(%d): no such revision"), $parent,
130                                      $rev));
131                 return '';
132             }
133         } else {
134             $r = $p->getCurrentRevision();
135         }
136
137         $c = $r->getContent();
138         $c = $this->extractGroupSection($section, $c, $parent);
139
140         $pagename = $request->getArg('pagename');
141
142         // The ordered list of page names determines the page
143         // ordering. Right now it doesn't work with a WikiList, only
144         // normal lines of text containing the page names.
145
146         $thispage = array_search($pagename, $c);
147
148         $go = array ('previous','next');
149         $links = HTML();
150         $links->pushcontent($label);
151         $links->pushcontent(" [ "); // an experiment
152         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
153
154         foreach ( $go as $go_item ) {
155             //yuck this smells, needs optimization.
156             if ($go_item == 'previous') {
157                 if ($loop) {
158                     if ($thispage == 0) {
159                         $linkpage  = $c[$lastindex];
160                     } else {
161                         $linkpage  = $c[$thispage - 1];
162                     }
163                     // mind the French : punctuation
164                     $text = fmt("%s: %s", $directions[$go_item],
165                                 $WikiTheme->makeLinkButton($linkpage));
166                     $links->pushcontent($text);
167                     $links->pushcontent($sep); // this works because
168                                                // there are only 2 go
169                                                // items, previous,next
170                 } else {
171                     if ($thispage == 0) {
172                         // skip it
173                     } else {
174                         $linkpage  = $c[$thispage - 1];
175                         $text = fmt("%s: %s", $directions[$go_item],
176                                     $WikiTheme->makeLinkButton($linkpage));
177                         $links->pushcontent($text);
178                         $links->pushcontent($sep); //this works
179                                                    //because there are
180                                                    //only 2 go items,
181                                                    //previous,next
182                     }
183                 }
184             } else if ($go_item == 'next') {
185                 if ($loop) {
186                     if ($thispage == $lastindex) {
187                         $linkpage  = $c[1];
188                     } else {
189                         $linkpage  = $c[$thispage + 1];
190                     }
191                     $text = fmt("%s: %s", $directions[$go_item],
192                                 $WikiTheme->makeLinkButton($linkpage));
193                 } else {
194                     if ($thispage == $lastindex) {
195                         // skip it
196                     } else {
197                         $linkpage = $c[$thispage + 1];
198                         $text = fmt("%s: %s", $directions[$go_item],
199                                     $WikiTheme->makeLinkButton($linkpage));
200                     }
201                 }
202                 $links->pushcontent($text);
203             }
204         }
205         $links->pushcontent(" ] "); // an experiment
206         return $links;
207     }
208 };
209
210 // Local Variables:
211 // mode: php
212 // tab-width: 8
213 // c-basic-offset: 4
214 // c-hanging-comment-ender-p: nil
215 // indent-tabs-mode: nil
216 // End:
217 ?>