]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MediawikiTable.php
Harmonize file footer
[SourceForge/phpwiki.git] / lib / plugin / MediawikiTable.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3 /*
4  * Copyright (C) 2003 Sameer D. Sahasrabuddhe
5  * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
6  * Copyright (C) 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7  *
8  * This file is part of PhpWiki.
9  *
10  * PhpWiki is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * PhpWiki is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with PhpWiki; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  * Standard Alcatel-Lucent disclaimer for contributing to open source
27  *
28  * "The MediawikiTablePlugin ("Contribution") has not been tested and/or
29  * validated for release as or in products, combinations with products or
30  * other commercial use. Any use of the Contribution is entirely made at
31  * the user's own responsibility and the user can not rely on any features,
32  * functionalities or performances Alcatel-Lucent has attributed to the
33  * Contribution.
34  *
35  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
36  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
37  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
38  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
39  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
40  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
41  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
43  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
44  * ALONE BASIS."
45  */
46
47 /**
48  * MediawikiTablePlugin
49  * A PhpWiki plugin that allows insertion of tables using a Mediawiki-like
50  * syntax.
51 */
52 class WikiPlugin_MediawikiTable
53 extends WikiPlugin
54 {
55     function getName() {
56         return _("MediawikiTable");
57     }
58
59     function getDescription() {
60       return _("Layout tables using a Mediawiki-like markup style.");
61     }
62
63     function getDefaultArguments() {
64         return array();
65     }
66
67     function run($dbi, $argstr, &$request, $basepage) {
68         include_once("lib/BlockParser.php");
69         // MediawikiTablePlugin markup is new.
70         $markup = 2.0;
71
72         // We allow the compact Mediawiki syntax with:
73         // - multiple cells on the same line (separated by "||"),
74         // - multiple header cells on the same line (separated by "!!").
75         $argstr = str_replace("||", "\n| ", $argstr);
76         $argstr = str_replace("!!", "\n! ", $argstr);
77
78         $lines = preg_split('/\n/', $argstr);
79         $table = HTML::table();
80
81         // We always generate an Id for the table.
82         // This is convenient for tables of class "sortable".
83         // If user provides an Id, the generated Id will be overwritten below.
84         $table->setAttr("id", GenerateId("MediawikiTable"));
85
86         if (substr($lines[0],0,2) == "{|") {
87             // Start of table
88             $lines[0] = substr($lines[0],2);
89         }
90         if (($lines[0][0] != '|') and ($lines[0][0] != '!')) {
91             $line = array_shift($lines);
92             $attrs = parse_attributes($line);
93             foreach ($attrs as $key => $value) {
94                 if (in_array ($key, array("id", "class", "title", "style",
95                                           "bgcolor", "frame", "rules", "border",
96                                           "cellspacing", "cellpadding",
97                                           "summary", "align", "width"))) {
98                     $table->setAttr($key, $value);
99                 }
100             }
101         }
102
103         if (count($lines) == 1) { // empty table, we only have closing "|}" line
104             return HTML::raw('');
105         }
106
107         foreach ($lines as $line){
108             if (substr($line,0,2) == "|}") {
109                 // End of table
110                 continue;
111             }
112             if (substr($line,0,2) == "|-") {
113                 if (isset($row)) {
114                     if (isset($cell)) {
115                         if (isset($content)) {
116                             if (is_numeric(trim($content))) {
117                                 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
118                             } else {
119                                 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
120                             }
121                             unset($content);
122                         }
123                         $row->pushContent($cell);
124                         unset($cell);
125                     }
126                     if (isset($thead)) {
127                             $thead->pushContent($row);
128                             $table->pushContent($thead);
129                             unset($thead);
130                             $tbody = HTML::tbody();
131                     } else {
132                             $tbody->pushContent($row);
133                     }
134                 }
135                 $row = HTML::tr();
136                 $attrs = parse_attributes(substr($line,2));
137                 foreach ($attrs as $key => $value) {
138                     if (in_array ($key, array("id", "class", "title", "style",
139                                               "bgcolor", "align", "valign"))) {
140                         $row->setAttr($key, $value);
141                     }
142                 }
143                 continue;
144             }
145
146             // Table summary
147             if (substr($line,0,2) == "|=") {
148                 $line = substr($line,2);
149                 $table->setAttr("summary", trim($line));
150             }
151
152             // Table caption
153             if (substr($line,0,2) == "|+") {
154
155                 $caption = HTML::caption();
156                 $line = substr($line,2);
157                 $pospipe = strpos($line, "|");
158                 $posbracket = strpos($line, "[");
159                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
160                     $attrs = parse_attributes(substr($line, 0, $pospipe));
161                     foreach ($attrs as $key => $value) {
162                         if (in_array ($key, array("id", "class", "title", "style",
163                                                   "align", "lang"))) {
164                             $caption->setAttr($key, $value);
165                         }
166                     }
167                     $line=substr($line, $pospipe+1);
168                 }
169
170                 $caption->pushContent(trim($line));
171                 $table->pushContent($caption);
172             }
173
174             if (((substr($line,0,1) == "|") or (substr($line,0,1) == "!")) and isset($row)) {
175                 if (isset($cell)) {
176                     if (isset ($content)) {
177                         if (is_numeric(trim($content))) {
178                             $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
179                         } else {
180                             $cell->pushContent(TransformText(trim($content), $markup, $basepage));
181                         }
182                         unset($content);
183                     }
184                     $row->pushContent($cell);
185                 }
186                 if (substr($line,0,1) == "!") {
187                     $cell = HTML::th();   // Header
188                     $thead = HTML::thead();
189                 } else {
190                     $cell = HTML::td();
191                     if (!isset($tbody)) $tbody = HTML::tbody();
192                 }
193                 $line = substr($line, 1);
194
195                 // If there is a "|" in the line, the start of line
196                 // (before the "|") is made of attributes.
197                 // The end of the line (after the "|") is the cell content
198                 // This is not true if the pipe is inside [], {{}} or {{{}}}
199                 // | [foo|bar]
200                 // The following cases must work:
201                 // | foo
202                 // | [foo|bar]
203                 // | class="xxx" | foo
204                 // | class="xxx" | [foo|bar]
205                 // | {{tmpl|arg=val}}
206                 // | {{image.png|alt}}
207                 // | {{{ xxx | yyy }}}
208                 $pospipe = strpos($line, "|");
209                 $posbracket = strpos($line, "[");
210                 $poscurly = strpos($line, "{");
211                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe)) && (($poscurly === false) || ($poscurly > $pospipe))) {
212                     $attrs = parse_attributes(substr($line, 0, $pospipe));
213                     foreach ($attrs as $key => $value) {
214                         if (in_array ($key, array("id", "class", "title", "style",
215                                                   "colspan", "rowspan", "width", "height",
216                                                   "bgcolor", "align", "valign"))) {
217                             $cell->setAttr($key, $value);
218                         }
219                     }
220                     $line=substr($line, $pospipe+1);
221                     if (is_numeric(trim($line))) {
222                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($line)));
223                     } else {
224                         $cell->pushContent(TransformText(trim($line), $markup, $basepage));
225                     }
226                     continue;
227                 }
228             }
229             if (isset($row) and isset($cell)) {
230                 $line = str_replace("?\>", "?>", $line);
231                 $line = str_replace("\~", "~", $line);
232                 if (empty($content)) $content = '';
233                 $content .= $line . "\n";
234             }
235         }
236         if (isset($row)) {
237             if (isset($cell)) {
238                 if (isset($content)) {
239                     if (is_numeric(trim($content))) {
240                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
241                     } else {
242                         $cell->pushContent(TransformText(trim($content), $markup, $basepage));
243                     }
244
245                 }
246                 $row->pushContent($cell);
247             }
248             $tbody->pushContent($row);
249             $table->pushContent($tbody);
250         }
251         return $table;
252     }
253 }
254
255 // Local Variables:
256 // mode: php
257 // tab-width: 8
258 // c-basic-offset: 4
259 // c-hanging-comment-ender-p: nil
260 // indent-tabs-mode: nil
261 // End:
262 ?>