]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php
2
3 /*
4  * Copyright (C) 2008-2009, 2011 Marc-Etienne Vargenau, Alcatel-Lucent
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /*
24  * Standard Alcatel-Lucent disclaimer for contributing to open source
25  *
26  * "The WikicreoleTablePlugin ("Contribution") has not been tested and/or
27  * validated for release as or in products, combinations with products or
28  * other commercial use. Any use of the Contribution is entirely made at
29  * the user's own responsibility and the user can not rely on any features,
30  * functionalities or performances Alcatel-Lucent has attributed to the
31  * Contribution.
32  *
33  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
34  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
35  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
36  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
37  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
38  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
39  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
41  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
42  * ALONE BASIS."
43  */
44
45 /**
46  * WikicreoleTablePlugin
47  * A PhpWiki plugin that allows insertion of tables using the Wikicreole
48  * syntax.
49  */
50
51 class WikiPlugin_WikicreoleTable
52     extends WikiPlugin
53 {
54     function getDescription()
55     {
56         return _("Layout tables using the Wikicreole syntax.");
57     }
58
59     function getDefaultArguments()
60     {
61         return array();
62     }
63
64     function handle_plugin_args_cruft($argstr, $args)
65     {
66         return;
67     }
68
69     function getWikiPageLinks($argstr, $basepage)
70     {
71         global $backlinks;
72         if (empty($backlinks)) {
73             global $request;
74             $this->run($request->_dbi, $argstr, $request, $basepage);
75         }
76         return $backlinks;
77     }
78
79     /**
80      * @param WikiDB $dbi
81      * @param string $argstr
82      * @param WikiRequest $request
83      * @param string $basepage
84      * @return mixed
85      */
86     function run($dbi, $argstr, &$request, $basepage)
87     {
88         global $backlinks;
89
90         $backlinks = array();
91
92         include_once 'lib/InlineParser.php';
93
94         $table = array();
95
96         $lines = preg_split('/\s*?\n\s*/', $argstr);
97
98         foreach ($lines as $line) {
99             if (!$line) {
100                 continue;
101             }
102             $line = trim($line);
103             // If line ends with a '|', remove it
104             if ($line[strlen($line) - 1] == '|') {
105                 $line = substr($line, 0, -1);
106             }
107             if ($line[0] == '|') {
108                 $table[] = $this->parse_row($line);
109             }
110         }
111
112         $nb_rows = sizeof($table);
113         // If table is empty, do not generate table markup
114         if ($nb_rows == 0) {
115             return HTML::raw('');
116         }
117
118         // Number of columns is the number of cells in the longer row
119         $nb_cols = 0;
120         for ($i = 0; $i < $nb_rows; $i++) {
121             $nb_cols = max($nb_cols, sizeof($table[$i]));
122         }
123
124         for ($i = 0; $i < $nb_rows; $i++) {
125             for ($j = 0; $j < $nb_cols; $j++) {
126                 if (!isset($table[$i][$j])) {
127                     $table[$i][$j] = '';
128                 }
129             }
130         }
131
132         for ($i = 0; $i < $nb_rows; $i++) {
133             for ($j = 0; $j < $nb_cols; $j++) {
134                 if (preg_match('/@@/', $table[$i][$j])) {
135                     $table[$i][$j] = $this->compute_table_cell($table, $i, $j, $nb_rows, $nb_cols);
136                 }
137             }
138         }
139
140         $html_table = HTML::table(array('class' => "bordered"));
141         foreach ($table as $row) {
142             $html_row = HTML::tr();
143             foreach ($row as $cell) {
144                 if ($cell && $cell[0] == '=') {
145                     $cell = trim(substr($cell, 1));
146                     $html_row->pushContent(HTML::th(TransformInline($cell, $basepage)));
147                 } else {
148                     if ($this->is_wiki_numeric($cell)) {
149                         $html_row->pushContent(HTML::td(array('style' => "text-align:right"), TransformInline($cell, $basepage)));
150                     } else {
151                         $html_row->pushContent(HTML::td(TransformInline($cell, $basepage)));
152                     }
153                 }
154             }
155             $html_table->pushContent($html_row);
156         }
157         return $html_table;
158     }
159
160     // $cell is a number, possibly in bold, italics or underlined
161     private function is_wiki_numeric($cell)
162     {
163         return is_numeric(trim($cell, "*/_'"));
164     }
165
166     private function parse_row($line)
167     {
168         $line = str_replace('|', ' |', $line);
169
170         $bracket_link = "\\[ .*? [^]\s] .*? \\]";
171         $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_link )*?";
172
173         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
174             $line, $matches, PREG_SET_ORDER);
175
176         $row = array();
177
178         foreach ($matches as $m) {
179             $cell = $m[2];
180             $row[] = $cell;
181         }
182         return $row;
183     }
184
185     /**
186      * Compute cell in spreadsheet table
187      * @param array $table: two-dimensional table
188      * @param int $i: first index of cell to compute
189      * @param int $j: second index of cell to compute
190      * @param int $imax: first table dimension
191      * @param int $jmax: second table dimension
192      * @return int
193      */
194     private function compute_table_cell($table, $i, $j, $imax, $jmax)
195     {
196
197         // What is implemented:
198         // @@=SUM(R)@@ : sum of cells in current row
199         // @@=SUM(C)@@ : sum of cells in current column
200         // @@=AVERAGE(R)@@ : average of cells in current row
201         // @@=AVERAGE(C)@@ : average of cells in current column
202         // @@=MAX(R)@@ : maximum value of cells in current row
203         // @@=MAX(C)@@ : maximum value of cells in current column
204         // @@=MIN(R)@@ : minimum value of cells in current row
205         // @@=MIN(C)@@ : minimum value of cells in current column
206         // @@=COUNT(R)@@ : number of cells in current row
207         //                (numeric or not, excluding headers and current cell)
208         // @@=COUNT(C)@@ : number of cells in current column
209         //                (numeric or not, excluding headers and current cell)
210
211         $result = 0;
212         $counter = 0;
213         $found = false;
214
215         if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
216             for ($index = 0; $index < $imax; $index++) {
217                 if (is_numeric($table[$index][$j])) {
218                     $result += $table[$index][$j];
219                 }
220             }
221             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
222
223         } elseif (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
224             for ($index = 0; $index < $jmax; $index++) {
225                 if (is_numeric($table[$i][$index])) {
226                     $result += $table[$i][$index];
227                 }
228             }
229             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
230
231         } elseif (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
232             for ($index = 0; $index < $imax; $index++) {
233                 if (is_numeric($table[$index][$j])) {
234                     $result += $table[$index][$j];
235                     $counter++;
236                 }
237             }
238             $result = $result / $counter;
239             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
240
241         } elseif (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
242             for ($index = 0; $index < $jmax; $index++) {
243                 if (is_numeric($table[$i][$index])) {
244                     $result += $table[$i][$index];
245                     $counter++;
246                 }
247             }
248             $result = $result / $counter;
249             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
250
251         } elseif (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
252             for ($index = 0; $index < $imax; $index++) {
253                 if (is_numeric($table[$index][$j])) {
254                     if (!$found) {
255                         $found = true;
256                         $result = $table[$index][$j];
257                     } else {
258                         $result = max($result, $table[$index][$j]);
259                     }
260                 }
261             }
262             if (!$found) {
263                 $result = "";
264             }
265             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
266
267         } elseif (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
268             for ($index = 0; $index < $jmax; $index++) {
269                 if (is_numeric($table[$i][$index])) {
270                     if (!$found) {
271                         $found = true;
272                         $result = $table[$i][$index];
273                     } else {
274                         $result = max($result, $table[$i][$index]);
275                     }
276                 }
277             }
278             if (!$found) {
279                 $result = "";
280             }
281             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
282
283         } elseif (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
284             for ($index = 0; $index < $imax; $index++) {
285                 if (is_numeric($table[$index][$j])) {
286                     if (!$found) {
287                         $found = true;
288                         $result = $table[$index][$j];
289                     } else {
290                         $result = min($result, $table[$index][$j]);
291                     }
292                 }
293             }
294             if (!$found) {
295                 $result = "";
296             }
297             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
298
299         } elseif (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
300             for ($index = 0; $index < $jmax; $index++) {
301                 if (is_numeric($table[$i][$index])) {
302                     if (!$found) {
303                         $found = true;
304                         $result = $table[$i][$index];
305                     } else {
306                         $result = min($result, $table[$i][$index]);
307                     }
308                 }
309             }
310             if (!$found) {
311                 $result = "";
312             }
313             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
314
315         } elseif (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
316             for ($index = 0; $index < $imax; $index++) {
317                 // exclude header
318                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
319                     $counter++;
320                 }
321             }
322             if (string_starts_with(trim($table[$i][$j]), "=")) {
323                 $result = $counter;
324             } else {
325                 $result = $counter - 1; // exclude self
326             }
327             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
328
329         } elseif (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
330             for ($index = 0; $index < $jmax; $index++) {
331                 // exclude header
332                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
333                     $counter++;
334                 }
335             }
336             if (string_starts_with(trim($table[$i][$j]), "=")) {
337                 $result = $counter;
338             } else {
339                 $result = $counter - 1; // exclude self
340             }
341             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
342         }
343
344         return $table[$i][$j];
345     }
346
347 }
348
349 // Local Variables:
350 // mode: php
351 // tab-width: 8
352 // c-basic-offset: 4
353 // c-hanging-comment-ender-p: nil
354 // indent-tabs-mode: nil
355 // End: