リビジョン | c0dd212d4150eed589944d7a8bc2b33d0626863e (tree) |
---|---|
日時 | 2007-10-16 22:09:29 |
作者 | henoheno <henoheno> |
コミッター | henoheno |
Simplify:
* Rename variables
* Remove redundant errors
* unset() to rebuild (to be continued)
@@ -1,6 +1,6 @@ | ||
1 | 1 | <?php |
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | -// $Id: tracker.inc.php,v 1.113 2007/10/13 14:47:14 henoheno Exp $ | |
3 | +// $Id: tracker.inc.php,v 1.114 2007/10/16 13:09:29 henoheno Exp $ | |
4 | 4 | // Copyright (C) 2003-2005, 2007 PukiWiki Developers Team |
5 | 5 | // License: GPL v2 or (at your option) any later version |
6 | 6 | // |
@@ -48,9 +48,9 @@ function plugin_tracker_convert() | ||
48 | 48 | |
49 | 49 | if (PKWK_READONLY) return ''; // Show nothing |
50 | 50 | |
51 | - $base = $refer = isset($vars['page']) ? $vars['page'] : ''; | |
52 | - $config_name = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
53 | - $form = PLUGIN_TRACKER_DEFAULT_FORM; | |
51 | + $base = $refer = isset($vars['page']) ? $vars['page'] : ''; | |
52 | + $config = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
53 | + $form = PLUGIN_TRACKER_DEFAULT_FORM; | |
54 | 54 | |
55 | 55 | $args = func_get_args(); |
56 | 56 | $argc = count($args); |
@@ -66,28 +66,31 @@ function plugin_tracker_convert() | ||
66 | 66 | // Config/form |
67 | 67 | if ($args[0] != '') { |
68 | 68 | $arg = explode('/', trim($args[0]), 2); |
69 | - if ($arg[0] != '' ) $config_name = trim($arg[0]); | |
70 | - if (isset($arg[1])) $form = trim($arg[1]); | |
69 | + if ($arg[0] != '' ) $config = trim($arg[0]); | |
70 | + if (isset($arg[1])) $form = trim($arg[1]); | |
71 | 71 | } |
72 | 72 | } |
73 | 73 | unset($args, $argc, $arg); |
74 | 74 | |
75 | - $config = new Config('plugin/tracker/' . $config_name); | |
76 | - if (! $config->read()) { | |
77 | - return '#tracker: Config \'' . htmlspecialchars($config_name) . '\' not found<br />'; | |
75 | + $obj_config = new Config('plugin/tracker/' . $config); | |
76 | + if (! $obj_config->read()) { | |
77 | + return '#tracker: Config \'' . htmlspecialchars($config) . '\' not found<br />'; | |
78 | 78 | } |
79 | - $config->config_name = $config_name; | |
79 | + $obj_config->_local_name = $config; | |
80 | 80 | |
81 | - $form = $config->page . '/' . $form; | |
81 | + $form = $obj_config->page . '/' . $form; | |
82 | 82 | $template = plugin_tracker_get_source($form, TRUE); |
83 | 83 | if ($template === FALSE || empty($template)) { |
84 | 84 | return '#tracker: Form \'' . make_pagelink($form) . '\' not found or seems empty<br />'; |
85 | 85 | } |
86 | 86 | |
87 | - $_form = & new Tracker_form($base, $refer, $config); | |
88 | - $_form->initFields(plugin_tracker_field_pickup($template)); | |
89 | - $_form->initHiddenFields(); | |
90 | - $fields = $_form->fields; | |
87 | + $tracker_form = & new Tracker_form($base, $refer, $obj_config); | |
88 | + if ($tracker_form->initFields(plugin_tracker_field_pickup($template)) === FALSE || | |
89 | + $tracker_form->initHiddenFields() === FALSE) { | |
90 | + return '#tracker: ' . htmlspecialchars($tracker_form->error); | |
91 | + } | |
92 | + $fields = $tracker_form->fields; | |
93 | + unset($tracker_form); | |
91 | 94 | |
92 | 95 | $from = $to = $hidden = array(); |
93 | 96 | foreach (array_keys($fields) as $fieldname) { |
@@ -147,12 +150,15 @@ function plugin_tracker_action() | ||
147 | 150 | } |
148 | 151 | |
149 | 152 | // Loading configuration |
150 | - $config_name = isset($post['_config']) ? $post['_config'] : ''; | |
151 | - $config = new Config('plugin/tracker/' . $config_name); | |
152 | - if (! $config->read()) { | |
153 | - return '<p>config file \'' . htmlspecialchars($config_name) . '\' not found.</p>'; | |
153 | + $config = isset($post['_config']) ? $post['_config'] : ''; | |
154 | + $obj_config = new Config('plugin/tracker/' . $config); | |
155 | + if (! $obj_config->read()) { | |
156 | + return array( | |
157 | + 'msg' => 'Cannot write', | |
158 | + 'body' => 'Config file \'' . htmlspecialchars($config) . '\' not found' | |
159 | + ); | |
154 | 160 | } |
155 | - $config->config_name = $config_name; | |
161 | + $obj_config->_local_name = $config; | |
156 | 162 | |
157 | 163 | // Default |
158 | 164 | $_post = array_merge($post, $_FILES); |
@@ -168,7 +174,7 @@ function plugin_tracker_action() | ||
168 | 174 | $from = $to = array(); |
169 | 175 | |
170 | 176 | // Load $template |
171 | - $template_page = $config->page . '/page'; | |
177 | + $template_page = $obj_config->page . '/page'; | |
172 | 178 | $template = plugin_tracker_get_source($template_page); |
173 | 179 | if ($template === FALSE || empty($template)) { |
174 | 180 | return array( |
@@ -177,9 +183,16 @@ function plugin_tracker_action() | ||
177 | 183 | ); |
178 | 184 | } |
179 | 185 | |
180 | - $form = & new Tracker_form($base, $refer, $config); | |
181 | - $form->initFields(plugin_tracker_field_pickup(implode('', $template))); | |
182 | - $fields = & $form->fields; // unset() | |
186 | + $tracker_form = & new Tracker_form($base, $refer, $obj_config); | |
187 | + if ($tracker_form->initFields(plugin_tracker_field_pickup(implode('', $template))) === FALSE) { | |
188 | + return array( | |
189 | + 'msg' => 'Cannot write', | |
190 | + 'body' => htmlspecialchars($tracker_form->error) | |
191 | + ); | |
192 | + } | |
193 | + $fields = $tracker_form->fields; | |
194 | + unset($tracker_form); | |
195 | + | |
183 | 196 | foreach (array_keys($fields) as $field) { |
184 | 197 | $from[] = '[' . $field . ']'; |
185 | 198 | $to[] = isset($_post[$field]) ? $fields[$field]->format_value($_post[$field]) : ''; |
@@ -475,7 +488,8 @@ class Tracker_field_real extends Tracker_field_text | ||
475 | 488 | |
476 | 489 | function format_cell($value) |
477 | 490 | { |
478 | - // basename(): Rough but work with this(PLUGIN_TRACKER_LIST_EXCLUDE_PATTERN prohibits '/') situation | |
491 | + // basename(): Rough but work with this | |
492 | + // (PLUGIN_TRACKER_LIST_EXCLUDE_PATTERN prohibits '/') situation | |
479 | 493 | return basename($value); |
480 | 494 | } |
481 | 495 | } |
@@ -538,6 +552,7 @@ class Tracker_field_format extends Tracker_field | ||
538 | 552 | function Tracker_field_format(& $tracker_form, $field) |
539 | 553 | { |
540 | 554 | parent::Tracker_field($tracker_form, $field); |
555 | + | |
541 | 556 | foreach ($this->form->config->get($this->name) as $option) { |
542 | 557 | list($key, $style, $format) = array_pad(array_map('trim', $option), 3, ''); |
543 | 558 | if ($style != '') $this->styles[$key] = $style; |
@@ -590,7 +605,6 @@ class Tracker_field_file extends Tracker_field_format | ||
590 | 605 | function format_value() |
591 | 606 | { |
592 | 607 | if (isset($_FILES[$this->name])) { |
593 | - | |
594 | 608 | require_once(PLUGIN_DIR . 'attach.inc.php'); |
595 | 609 | |
596 | 610 | $base = $this->form->base; |
@@ -739,7 +753,7 @@ class Tracker_field_submit extends Tracker_field | ||
739 | 753 | $s_title = htmlspecialchars($this->title); |
740 | 754 | $s_base = htmlspecialchars($form->base); |
741 | 755 | $s_refer = htmlspecialchars($form->refer); |
742 | - $s_config = htmlspecialchars($form->config->config_name); | |
756 | + $s_config = htmlspecialchars($form->config->_local_name); | |
743 | 757 | |
744 | 758 | return <<<EOD |
745 | 759 | <input type="submit" value="$s_title" /> |
@@ -783,11 +797,11 @@ function plugin_tracker_list_convert() | ||
783 | 797 | { |
784 | 798 | global $vars; |
785 | 799 | |
786 | - $base = $refer = isset($vars['page']) ? $vars['page'] : ''; | |
787 | - $config_name = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
788 | - $list = PLUGIN_TRACKER_DEFAULT_LIST; | |
789 | - $limit = PLUGIN_TRACKER_DEFAULT_LIMIT; | |
790 | - $order = PLUGIN_TRACKER_DEFAULT_ORDER; | |
800 | + $base = $refer = isset($vars['page']) ? $vars['page'] : ''; | |
801 | + $config = ''; | |
802 | + $list = ''; | |
803 | + $limit = NULL; | |
804 | + $order = ''; | |
791 | 805 | |
792 | 806 | $args = func_get_args(); |
793 | 807 | $argc = count($args); |
@@ -805,13 +819,13 @@ function plugin_tracker_list_convert() | ||
805 | 819 | // Config/list |
806 | 820 | if ($args[0] != '') { |
807 | 821 | $arg = explode('/', $args[0], 2); |
808 | - if ($arg[0] != '' ) $config_name = $arg[0]; | |
809 | - if (isset($arg[1])) $list = $arg[1]; | |
822 | + if ($arg[0] != '' ) $config = $arg[0]; | |
823 | + if (isset($arg[1])) $list = $arg[1]; | |
810 | 824 | } |
811 | 825 | } |
812 | 826 | unset($args, $argc, $arg); |
813 | 827 | |
814 | - return plugin_tracker_list_render($base, $refer, $config_name, $list, $order, $limit); | |
828 | + return plugin_tracker_list_render($base, $refer, $config, $order, $list, $limit); | |
815 | 829 | } |
816 | 830 | |
817 | 831 | function plugin_tracker_list_action() |
@@ -828,48 +842,44 @@ function plugin_tracker_list_action() | ||
828 | 842 | } |
829 | 843 | |
830 | 844 | $config = isset($get['config']) ? $get['config'] : ''; |
831 | - $list = isset($get['list']) ? $get['list'] : 'list'; | |
832 | - $order = isset($get['order']) ? $get['order'] : PLUGIN_TRACKER_DEFAULT_ORDER; | |
833 | - $limit = isset($get['limit']) ? $get['limit'] : 0; | |
845 | + $list = isset($get['list']) ? $get['list'] : ''; | |
846 | + $order = isset($get['order']) ? $get['order'] : ''; | |
847 | + $limit = isset($get['limit']) ? $get['limit'] : NULL; | |
834 | 848 | |
835 | 849 | $s_refer = make_pagelink($refer); |
836 | 850 | return array( |
837 | 851 | 'msg' => plugin_tracker_message('msg_list'), |
838 | 852 | 'body'=> str_replace('$1', $s_refer, plugin_tracker_message('msg_back')) . |
839 | - plugin_tracker_list_render($base, $refer, $config, $list, $order, $limit) | |
853 | + plugin_tracker_list_render($base, $refer, $config, $order, $list, $limit) | |
840 | 854 | ); |
841 | 855 | } |
842 | 856 | |
843 | -function plugin_tracker_list_render($base, $refer, $config_name, $list, $order_commands = '', $limit = 0) | |
857 | +function plugin_tracker_list_render($base, $refer, $config = '', $order_commands = '', $list = '', $limit = NULL) | |
844 | 858 | { |
845 | - $base = trim($base); | |
846 | - if ($base == '') return '#tracker_list: Base not specified' . '<br />'; | |
847 | - | |
848 | - $refer = trim($refer); | |
849 | - if (! is_page($refer)) { | |
850 | - return '#tracker_list: Refer page not found: ' . htmlspecialchars($refer) . '<br />'; | |
851 | - } | |
859 | + $base = trim($base); | |
860 | + $refer = trim($refer); | |
861 | + $config = trim($config); | |
862 | + $list = trim($list); | |
863 | + if ($config == '' ) $config = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
864 | + if ($list == '' ) $list = PLUGIN_TRACKER_DEFAULT_LIST; | |
865 | + if ($order_commands == '') $order_commands = PLUGIN_TRACKER_DEFAULT_ORDER; | |
866 | + if ($limit == NULL ) $limit = PLUGIN_TRACKER_DEFAULT_LIMIT; | |
852 | 867 | |
853 | - $config_name = trim($config_name); | |
854 | - if ($config_name == '') $config_name = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
868 | + if ($base == '') return '#tracker_list: Base not specified' . '<br />'; | |
869 | + if (! is_page($refer)) return '#tracker_list: Refer page not found: ' . htmlspecialchars($refer) . '<br />'; | |
855 | 870 | |
856 | - $list = trim($list); | |
857 | 871 | if (! is_numeric($limit)) return PLUGIN_TRACKER_LIST_USAGE . '<br />'; |
858 | 872 | $limit = intval($limit); |
859 | 873 | |
860 | - $config = new Config('plugin/tracker/' . $config_name); | |
861 | - if (! $config->read()) { | |
862 | - return '#tracker_list: Config not found: ' . htmlspecialchars($config_name) . '<br />'; | |
863 | - } | |
864 | - $config->config_name = $config_name; | |
865 | - if (! is_page($config->page . '/' . $list)) { | |
866 | - return '#tracker_list: List not found: ' . make_pagelink($config->page . '/' . $list) . '<br />'; | |
867 | - } | |
874 | + $obj_config = new Config('plugin/tracker/' . $config); | |
875 | + if (! $obj_config->read()) return '#tracker_list: Config not found: ' . htmlspecialchars($config) . '<br />'; | |
876 | + $obj_config->_local_name = $config; | |
868 | 877 | |
869 | - $tracker_list = & new Tracker_list($base, $refer, $config); | |
878 | + $tracker_list = & new Tracker_list($base, $refer, $obj_config); | |
870 | 879 | if ($tracker_list->setSortOrder($order_commands) === FALSE) { |
871 | 880 | return '#tracker_list: ' . htmlspecialchars($tracker_list->error) . '<br />'; |
872 | 881 | } |
882 | + | |
873 | 883 | $result = $tracker_list->toString($list, $limit); |
874 | 884 | if ($result === FALSE) { |
875 | 885 | return '#tracker_list: ' . htmlspecialchars($tracker_list->error) . '<br />'; |
@@ -885,7 +895,7 @@ class Tracker_list | ||
885 | 895 | var $form; // class Tracker_form |
886 | 896 | |
887 | 897 | var $rows = array(); |
888 | - var $orders = array(); | |
898 | + var $orders; | |
889 | 899 | var $error = ''; // Error message |
890 | 900 | |
891 | 901 | // _generate_regex() |
@@ -905,7 +915,7 @@ class Tracker_list | ||
905 | 915 | $this->form = & new Tracker_form($base, $refer, $config); |
906 | 916 | } |
907 | 917 | |
908 | - // Generate regex to load a page | |
918 | + // Generate/Regenerate regex to load one page | |
909 | 919 | function _generate_regex() |
910 | 920 | { |
911 | 921 | if (isset($this->pattern) && isset($this->pattern_fields)) return TRUE; |
@@ -1059,11 +1069,12 @@ class Tracker_list | ||
1059 | 1069 | { |
1060 | 1070 | $orders = $this->_order_commands2orders($order_commands); |
1061 | 1071 | if ($orders === FALSE) { |
1062 | - $this->orders = array(); | |
1072 | + unset($this->orders); | |
1063 | 1073 | return FALSE; |
1074 | + } else { | |
1075 | + $this->orders = $orders; | |
1076 | + return TRUE; | |
1064 | 1077 | } |
1065 | - $this->orders = $orders; | |
1066 | - return TRUE; | |
1067 | 1078 | } |
1068 | 1079 | |
1069 | 1080 | // sortRows(): Internal sort type => PHP sort define |
@@ -1095,6 +1106,11 @@ class Tracker_list | ||
1095 | 1106 | // Sort $this->rows by $this->orders |
1096 | 1107 | function sortRows() |
1097 | 1108 | { |
1109 | + if (! isset($this->orders)) { | |
1110 | + $this->error = "Sort order seems not set"; | |
1111 | + return FALSE; | |
1112 | + } | |
1113 | + | |
1098 | 1114 | $fields = $this->form->fields; |
1099 | 1115 | $orders = $this->orders; |
1100 | 1116 | $types = array(); |
@@ -1189,14 +1205,14 @@ class Tracker_list | ||
1189 | 1205 | // toString(): Called within preg_replace_callback() |
1190 | 1206 | function _replace_title($matches = array()) |
1191 | 1207 | { |
1192 | - $form = $this->form; | |
1193 | - $base = $form->base; | |
1194 | - $refer = $form->refer; | |
1195 | - $fields = $form->fields; | |
1196 | - $config_name = $form->config->config_name; | |
1208 | + $form = $this->form; | |
1209 | + $base = $form->base; | |
1210 | + $refer = $form->refer; | |
1211 | + $fields = $form->fields; | |
1212 | + $config = $form->config->_local_name; | |
1197 | 1213 | |
1198 | - $orders = $this->orders; | |
1199 | - $list = $this->_list; | |
1214 | + $orders = $this->orders; | |
1215 | + $list = $this->_list; | |
1200 | 1216 | |
1201 | 1217 | $fieldname = isset($matches[1]) ? $matches[1] : ''; |
1202 | 1218 | if (! isset($fields[$fieldname])) { |
@@ -1239,8 +1255,8 @@ class Tracker_list | ||
1239 | 1255 | $script = get_script_uri(); |
1240 | 1256 | $r_base = ($refer != $base) ? |
1241 | 1257 | '&base=' . rawurlencode($base) : ''; |
1242 | - $r_config = ($config_name != PLUGIN_TRACKER_DEFAULT_CONFIG) ? | |
1243 | - '&config=' . rawurlencode($config_name) : ''; | |
1258 | + $r_config = ($config != PLUGIN_TRACKER_DEFAULT_CONFIG) ? | |
1259 | + '&config=' . rawurlencode($config) : ''; | |
1244 | 1260 | $r_list = ($list != PLUGIN_TRACKER_DEFAULT_LIST) ? |
1245 | 1261 | '&list=' . rawurlencode($list) : ''; |
1246 | 1262 | $r_order = ! empty($_orders) ? |
@@ -1294,7 +1310,7 @@ class Tracker_list | ||
1294 | 1310 | } |
1295 | 1311 | |
1296 | 1312 | // Output a part of Wiki text |
1297 | - function toString($list, $limit = 0) | |
1313 | + function toString($list = PLUGIN_TRACKER_DEFAULT_LIST, $limit = 0) | |
1298 | 1314 | { |
1299 | 1315 | $form = & $this->form; |
1300 | 1316 |
@@ -1307,7 +1323,7 @@ class Tracker_list | ||
1307 | 1323 | // Loading template |
1308 | 1324 | $template = plugin_tracker_get_source($list, TRUE); |
1309 | 1325 | if ($template === FALSE || empty($template)) { |
1310 | - $this->error = 'Page not found or seems empty: ' . $list; | |
1326 | + $this->error = 'List not found: ' . $list; | |
1311 | 1327 | return FALSE; |
1312 | 1328 | } |
1313 | 1329 |
@@ -1318,12 +1334,10 @@ class Tracker_list | ||
1318 | 1334 | return FALSE; |
1319 | 1335 | } |
1320 | 1336 | |
1321 | - // TODO: Check isset($this->rows) or something | |
1322 | 1337 | // Load and sort $this->rows |
1323 | 1338 | if ($this->loadRows() === FALSE || $this->sortRows() === FALSE) return FALSE; |
1324 | 1339 | $rows = $this->rows; |
1325 | 1340 | |
1326 | - | |
1327 | 1341 | // toString() |
1328 | 1342 | $count = count($this->rows); |
1329 | 1343 | $limit = intval($limit); |