]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageGroup.php
private --> protected
[SourceForge/phpwiki.git] / lib / plugin / PageGroup.php
1 <?php
2
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     {
46         return _("PageGroup");
47     }
48
49     function getDescription()
50     {
51         return sprintf(_("PageGroup for %s."), '[pagename]');
52     }
53
54     function getDefaultArguments()
55     {
56         return array(
57             'parent' => '',
58             'rev' => false,
59             'section' => _("Contents"),
60             'label' => '',
61             'loop' => false,
62         );
63     }
64
65     // Stolen from IncludePage.php
66     function extractGroupSection($section, $content, $page)
67     {
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         ) {
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
98         $args = $this->getArgs($argstr, $request);
99         extract($args);
100         if (empty($parent)) {
101             return $this->error(sprintf(_("A required argument ā€œ%sā€ is missing."), 'parent'));
102         }
103         $directions = array('next' => _("Next"),
104             'previous' => _("Previous"),
105             'contents' => _("Contents"),
106             'first' => _("First"),
107             'last' => _("Last")
108         );
109
110         global $WikiTheme;
111         $sep = $WikiTheme->getButtonSeparator();
112         if (!$sep)
113             $sep = " | "; // force some kind of separator
114
115         // default label
116         if (!$label)
117             $label = $WikiTheme->makeLinkButton($parent);
118
119         // This is where the list extraction occurs from the named
120         // $section on the $parent page.
121
122         $p = $dbi->getPage($parent);
123         if ($rev) {
124             $r = $p->getRevision($rev);
125             if ((!$r) || ($r->hasDefaultContents())) {
126                 return $this->error(sprintf(_("%s: no such revision %d."),
127                     $parent, $rev));
128             }
129         } else {
130             $r = $p->getCurrentRevision();
131         }
132
133         $c = $r->getContent();
134         $c = $this->extractGroupSection($section, $c, $parent);
135
136         $pagename = $request->getArg('pagename');
137
138         // The ordered list of page names determines the page
139         // ordering. Right now it doesn't work with a WikiList, only
140         // normal lines of text containing the page names.
141
142         $thispage = array_search($pagename, $c);
143
144         $go = array('previous', 'next');
145         $links = HTML();
146         $links->pushcontent($label);
147         $links->pushcontent(" [ "); // an experiment
148         $lastindex = count($c) - 1; // array is 0-based, count is 1-based!
149
150         foreach ($go as $go_item) {
151             //yuck this smells, needs optimization.
152             if ($go_item == 'previous') {
153                 if ($loop) {
154                     if ($thispage == 0) {
155                         $linkpage = $c[$lastindex];
156                     } else {
157                         $linkpage = $c[$thispage - 1];
158                     }
159                     // mind the French : punctuation
160                     $text = fmt("%s: %s", $directions[$go_item],
161                         $WikiTheme->makeLinkButton($linkpage));
162                     $links->pushcontent($text);
163                     $links->pushcontent($sep); // this works because
164                     // there are only 2 go
165                     // items, previous,next
166                 } else {
167                     if ($thispage == 0) {
168                         // skip it
169                     } else {
170                         $linkpage = $c[$thispage - 1];
171                         $text = fmt("%s: %s", $directions[$go_item],
172                             $WikiTheme->makeLinkButton($linkpage));
173                         $links->pushcontent($text);
174                         $links->pushcontent($sep); //this works
175                         //because there are
176                         //only 2 go items,
177                         //previous,next
178                     }
179                 }
180             } elseif ($go_item == 'next') {
181                 if ($loop) {
182                     if ($thispage == $lastindex) {
183                         $linkpage = $c[1];
184                     } else {
185                         $linkpage = $c[$thispage + 1];
186                     }
187                     $text = fmt("%s: %s", $directions[$go_item],
188                         $WikiTheme->makeLinkButton($linkpage));
189                 } else {
190                     if ($thispage == $lastindex) {
191                         // skip it
192                     } else {
193                         $linkpage = $c[$thispage + 1];
194                         $text = fmt("%s: %s", $directions[$go_item],
195                             $WikiTheme->makeLinkButton($linkpage));
196                     }
197                 }
198                 $links->pushcontent($text);
199             }
200         }
201         $links->pushcontent(" ] "); // an experiment
202         return $links;
203     }
204 }
205
206 // Local Variables:
207 // mode: php
208 // tab-width: 8
209 // c-basic-offset: 4
210 // c-hanging-comment-ender-p: nil
211 // indent-tabs-mode: nil
212 // End: