]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
Support [] links, but no [name|page] links yet
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageGroup.php,v 1.7 2004-05-03 15:53:20 rurban Exp $');
3 /**
4  Copyright 1999,2000,2001,2002,2004 $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.7 $");
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         if (preg_match("/ ^(!{1,})\\s*$qsection" // section header
70                        . "  \\s*$\\n?"           // possible blank lines
71                        . "  ( (?: ^.*\\n? )*? )" // some lines
72                        . "  (?= ^\\1 | \\Z)/xm", // sec header (same or higher level) (or EOF)
73                        implode("\n", $content),
74                        $match)) {
75             $result = array();                  
76             //FIXME: return list of Wiki_Pagename objects
77             foreach (explode("\n", $match[2]) as $line) {
78                 $text = trim($line);
79                 // Strip trailing blanks lines and ---- <hr>s
80                 $text = preg_replace("/\\s*^-{4,}\\s*$/", "", $text);
81                 // Strip leading list chars: * or #
82                 $text = preg_replace("/^[\*#]+\s*(\S.+)$/", "\\1", $text);
83                 // Strip surrounding [] 
84                 // FIXME: parse [ name | link ]
85                 $text = preg_replace("/^\[\s*(\S.+)\s*\]$/", "\\1", $text);
86                 if (!empty($text))
87                     $result[] = $text;
88             }
89             return $result;
90         }
91         return array(sprintf(_("<%s: no such section>"), $page ." ". $section));
92     }
93
94     function run($dbi, $argstr, &$request, $basepage) {
95
96         $args = $this->getArgs($argstr, $request);
97         extract($args);
98         $html="";
99         if (empty($parent)) {
100             // FIXME: WikiPlugin has no way to report when
101             // required args are missing?
102             $error_text = fmt("%s: %s", "WikiPlugin_" .$this->getName(),
103                               $error_text);
104             $error_text .= " " . sprintf(_("A required argument '%s' is missing."), 'parent');
105             $html = $error_text;
106             return $html;
107         }
108         $directions = array ('next'     => _("Next"),
109                              'previous' => _("Previous"),
110                              'contents' => _("Contents"),
111                              'first'    => _("First"),
112                              'last'     => _("Last")
113                              );
114
115         global $Theme;
116         $sep = $Theme->getButtonSeparator();
117         if (!$sep)
118             $sep = " | "; // force some kind of separator
119
120         // default label
121         if (!$label)
122             $label = $Theme->makeLinkButton($parent);
123
124         // This is where the list extraction occurs from the named
125         // $section on the $parent page.
126
127         $p = $dbi->getPage($parent);
128         if ($rev) {
129             $r = $p->getRevision($rev);
130             if (!$r) {
131                 $this->error(sprintf(_("%s(%d): no such revision"), $parent,
132                                      $rev));
133                 return '';
134             }
135         } else {
136             $r = $p->getCurrentRevision();
137         }
138
139         $c = $r->getContent();
140         $c = $this->extractSection($section, $c, $parent);
141
142         $pagename = $request->getArg('pagename');
143
144         // The ordered list of page names determines the page
145         // ordering. Right now it doesn't work with a WikiList, only
146         // normal lines of text containing the page names.
147
148         $thispage = array_search($pagename, $c);
149
150         $go = array ('previous','next');
151         $links = HTML();
152         $links->pushcontent($label);
153         $links->pushcontent(" [ "); // an experiment
154         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
155
156         foreach ( $go as $go_item ) {
157             //yuck this smells, needs optimization.
158             if ($go_item == 'previous') {
159                 if ($loop) {
160                     if ($thispage == 0) {
161                         $linkpage  = $c[$lastindex];
162                     } else {
163                         $linkpage  = $c[$thispage - 1];
164                     }
165                     // mind the French : punctuation
166                     $text = fmt("%s: %s", $directions[$go_item],
167                                 $Theme->makeLinkButton($linkpage));
168                     $links->pushcontent($text);
169                     $links->pushcontent($sep); // this works because
170                                                // there are only 2 go
171                                                // items, previous,next
172                 } else {
173                     if ($thispage == 0) {
174                         // skip it
175                     } else {
176                         $linkpage  = $c[$thispage - 1];
177                         $text = fmt("%s: %s", $directions[$go_item],
178                                     $Theme->makeLinkButton($linkpage));
179                         $links->pushcontent($text);
180                         $links->pushcontent($sep); //this works
181                                                    //because there are
182                                                    //only 2 go items,
183                                                    //previous,next
184                     }
185                 }
186             } else if ($go_item == 'next') {
187                 if ($loop) {
188                     if ($thispage == $lastindex) {
189                         $linkpage  = $c[1];
190                     } else {
191                         $linkpage  = $c[$thispage + 1];
192                     }
193                     $text = fmt("%s: %s", $directions[$go_item],
194                                 $Theme->makeLinkButton($linkpage));
195                 } else {
196                     if ($thispage == $lastindex) {
197                         // skip it
198                     } else {
199                         $linkpage = $c[$thispage + 1];
200                         $text = fmt("%s: %s", $directions[$go_item],
201                                     $Theme->makeLinkButton($linkpage));
202                     }
203                 }
204                 $links->pushcontent($text);
205             }
206         }
207         $links->pushcontent(" ] "); // an experiment
208         return $links;
209     }
210 };
211
212 // $Log: not supported by cvs2svn $
213 // Revision 1.6  2004/02/17 12:11:36  rurban
214 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
215 //
216 // Revision 1.5  2003/01/18 21:49:00  carstenklapp
217 // Code cleanup:
218 // Reformatting & tabs to spaces;
219 // Added copyleft, getVersion, getDescription, rcs_id.
220 //
221
222 // Local Variables:
223 // mode: php
224 // tab-width: 8
225 // c-basic-offset: 4
226 // c-hanging-comment-ender-p: nil
227 // indent-tabs-mode: nil
228 // End:
229 ?>