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