4 * Copyright (C) 2008-2009, 2011 Marc-Etienne Vargenau, Alcatel-Lucent
6 * This file is part of PhpWiki.
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.
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.
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.
24 * Standard Alcatel-Lucent disclaimer for contributing to open source
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
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
46 * WikicreoleTablePlugin
47 * A PhpWiki plugin that allows insertion of tables using the Wikicreole
51 class WikiPlugin_WikicreoleTable
54 function getDescription()
56 return _("Layout tables using the Wikicreole syntax.");
59 function getDefaultArguments()
64 function handle_plugin_args_cruft($argstr, $args)
69 function getWikiPageLinks($argstr, $basepage)
72 if (empty($backlinks)) {
74 $this->run($request->_dbi, $argstr, $request, $basepage);
81 * @param string $argstr
82 * @param WikiRequest $request
83 * @param string $basepage
86 function run($dbi, $argstr, &$request, $basepage)
92 include_once 'lib/InlineParser.php';
96 $lines = preg_split('/\s*?\n\s*/', $argstr);
98 foreach ($lines as $line) {
103 // If line ends with a '|', remove it
104 if ($line[strlen($line) - 1] == '|') {
105 $line = substr($line, 0, -1);
107 if ($line[0] == '|') {
108 $table[] = $this->parse_row($line);
112 $nb_rows = sizeof($table);
113 // If table is empty, do not generate table markup
115 return HTML::raw('');
118 // Number of columns is the number of cells in the longer row
120 for ($i = 0; $i < $nb_rows; $i++) {
121 $nb_cols = max($nb_cols, sizeof($table[$i]));
124 for ($i = 0; $i < $nb_rows; $i++) {
125 for ($j = 0; $j < $nb_cols; $j++) {
126 if (!isset($table[$i][$j])) {
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);
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)));
148 if ($this->is_wiki_numeric($cell)) {
149 $html_row->pushContent(HTML::td(array('style' => "text-align:right"), TransformInline($cell, $basepage)));
151 $html_row->pushContent(HTML::td(TransformInline($cell, $basepage)));
155 $html_table->pushContent($html_row);
160 // $cell is a number, possibly in bold, italics or underlined
161 private function is_wiki_numeric($cell)
163 return is_numeric(trim($cell, "*/_'"));
166 private function parse_row($line)
168 $line = str_replace('|', ' |', $line);
170 $bracket_link = "\\[ .*? [^]\s] .*? \\]";
171 $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_link )*?";
173 preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
174 $line, $matches, PREG_SET_ORDER);
178 foreach ($matches as $m) {
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
194 private function compute_table_cell($table, $i, $j, $imax, $jmax)
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)
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];
221 return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
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];
229 return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
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];
238 $result = $result / $counter;
239 return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
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];
248 $result = $result / $counter;
249 return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
251 } elseif (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
252 for ($index = 0; $index < $imax; $index++) {
253 if (is_numeric($table[$index][$j])) {
256 $result = $table[$index][$j];
258 $result = max($result, $table[$index][$j]);
265 return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
267 } elseif (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
268 for ($index = 0; $index < $jmax; $index++) {
269 if (is_numeric($table[$i][$index])) {
272 $result = $table[$i][$index];
274 $result = max($result, $table[$i][$index]);
281 return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
283 } elseif (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
284 for ($index = 0; $index < $imax; $index++) {
285 if (is_numeric($table[$index][$j])) {
288 $result = $table[$index][$j];
290 $result = min($result, $table[$index][$j]);
297 return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
299 } elseif (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
300 for ($index = 0; $index < $jmax; $index++) {
301 if (is_numeric($table[$i][$index])) {
304 $result = $table[$i][$index];
306 $result = min($result, $table[$i][$index]);
313 return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
315 } elseif (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
316 for ($index = 0; $index < $imax; $index++) {
318 if (!string_starts_with(trim($table[$index][$j]), "=")) {
322 if (string_starts_with(trim($table[$i][$j]), "=")) {
325 $result = $counter - 1; // exclude self
327 return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
329 } elseif (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
330 for ($index = 0; $index < $jmax; $index++) {
332 if (!string_starts_with(trim($table[$i][$index]), "=")) {
336 if (string_starts_with(trim($table[$i][$j]), "=")) {
339 $result = $counter - 1; // exclude self
341 return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
344 return $table[$i][$j];
353 // c-hanging-comment-ender-p: nil
354 // indent-tabs-mode: nil