Total Complexity | 68 |
Total Lines | 1003 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Diagnoser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Diagnoser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Diagnoser |
||
18 | { |
||
19 | public const STATUS_OK = 1; |
||
20 | public const STATUS_WARNING = 2; |
||
21 | public const STATUS_ERROR = 3; |
||
22 | public const STATUS_INFORMATION = 4; |
||
23 | |||
24 | /** |
||
25 | * Contructor. |
||
26 | */ |
||
27 | public function __construct() |
||
28 | { |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Show html table. |
||
33 | */ |
||
34 | public function show_html() |
||
35 | { |
||
36 | $sections = [ |
||
37 | 'chamilo' => [ |
||
38 | 'lang' => 'Chamilo', |
||
39 | 'info' => 'State of Chamilo requirements', |
||
40 | ], |
||
41 | 'php' => [ |
||
42 | 'lang' => 'PHP', |
||
43 | 'info' => 'State of PHP settings on the server', |
||
44 | ], |
||
45 | 'database' => [ |
||
46 | 'lang' => 'Database', |
||
47 | 'info' => 'Configuration settings of the database server. To check the database consistency after an upgrade, if you have access to the command line, you can use "php bin/doctrine.php orm:schema-tool:update --dump-sql". This will print a list of database changes that should be applied to your system in order to get the right structure. Index name changes can be ignored. Use "--force" instead of "--dump" to try and execute them in order.', |
||
48 | ], |
||
49 | 'webserver' => [ |
||
50 | 'lang' => get_lang('Web server'), |
||
51 | 'info' => 'Information about your webserver\'s configuration ', |
||
52 | ], |
||
53 | 'paths' => [ |
||
54 | 'lang' => 'Paths', |
||
55 | 'info' => 'The following paths are called by their constant throughout Chamilo\'s code using the api_get_path() function. Here is a list of these paths and what they would be translated to on this portal.', |
||
56 | ], |
||
57 | 'courses_space' => [ |
||
58 | 'lang' => 'Courses space', |
||
59 | 'info' => 'Information about space used by courses on disk. The space used on disk represents the total space used, whereas the quota only limits files in the documents tool. Only 1000 courses are shown, by order of last access and alphabetical code order. For more, please go to the courses folder and use "du -sh *" to get the size of the courses.', |
||
60 | ], |
||
61 | ]; |
||
62 | $currentSection = isset($_GET['section']) ? $_GET['section'] : ''; |
||
63 | if (!in_array(trim($currentSection), array_keys($sections))) { |
||
64 | $currentSection = 'chamilo'; |
||
65 | } |
||
66 | |||
67 | $html = '<div class="tabbable"><ul class="nav nav-tabs">'; |
||
68 | |||
69 | foreach ($sections as $section => $details) { |
||
70 | if ($currentSection === $section) { |
||
71 | $html .= '<li class="active">'; |
||
72 | } else { |
||
73 | $html .= '<li>'; |
||
74 | } |
||
75 | $params['section'] = $section; |
||
76 | $html .= '<a href="system_status.php?section='.$section.'">'.$details['lang'].'</a></li>'; |
||
77 | } |
||
78 | |||
79 | $html .= '</ul><div class="tab-pane">'; |
||
80 | |||
81 | $data = call_user_func([$this, 'get_'.$currentSection.'_data']); |
||
82 | echo $html; |
||
83 | |||
84 | if ('paths' === $currentSection) { |
||
85 | echo '<br />'; |
||
86 | echo Display::return_message($sections[$currentSection]['info'], 'normal'); |
||
87 | |||
88 | $headers = $data['headers']; |
||
89 | $results = $data['data']; |
||
90 | $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']); |
||
91 | |||
92 | $column = 0; |
||
93 | foreach ($headers as $header) { |
||
94 | $table->setHeaderContents(0, $column, $header); |
||
95 | $column++; |
||
96 | } |
||
97 | $row = 1; |
||
98 | foreach ($results as $index => $rowData) { |
||
99 | $table->setCellContents( |
||
100 | $row, |
||
101 | 0, |
||
102 | $rowData |
||
103 | ); |
||
104 | $table->setCellContents( |
||
105 | $row, |
||
106 | 1, |
||
107 | $index |
||
108 | ); |
||
109 | $row++; |
||
110 | } |
||
111 | $table->display(); |
||
112 | } elseif ('courses_space' == $currentSection) { |
||
113 | echo '<br />'; |
||
114 | echo Display::return_message($sections[$currentSection]['info'], 'normal'); |
||
115 | |||
116 | $table = new SortableTableFromArray($data, 1, 1000); |
||
117 | $table->set_additional_parameters(['section' => 'courses_space']); |
||
118 | $table->set_header(0, '', false); |
||
119 | $table->set_header(1, get_lang('Course code'), true); |
||
120 | $table->set_header(2, 'Space used on disk (MB)', true); |
||
121 | $table->set_header(3, 'Set max course space (MB)', false); |
||
122 | $table->set_header(4, get_lang('Edit'), false); |
||
123 | $table->set_header(5, get_lang('Latest visit'), true); |
||
124 | $table->set_header(6, get_lang('Current folder'), false); |
||
125 | |||
126 | $table->display(); |
||
127 | } else { |
||
128 | echo '<br />'; |
||
129 | echo Display::return_message($sections[$currentSection]['info'], 'normal'); |
||
130 | |||
131 | $table = new SortableTableFromArray($data, 1, 100); |
||
132 | $table->set_header(0, '', false); |
||
133 | $table->set_header(1, get_lang('Section'), false); |
||
134 | $table->set_header(2, get_lang('Setting'), false); |
||
135 | $table->set_header(3, get_lang('Current'), false); |
||
136 | $table->set_header(4, get_lang('Expected'), false); |
||
137 | $table->set_header(5, get_lang('Comment'), false); |
||
138 | |||
139 | $table->display(); |
||
140 | } |
||
141 | echo '</div></div>'; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | public function get_paths_data() |
||
157 | ]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Functions to get the data for the chamilo diagnostics. |
||
162 | * |
||
163 | * @return array of data |
||
164 | */ |
||
165 | public function get_chamilo_data() |
||
166 | { |
||
167 | $array = []; |
||
168 | $writable_folders = [ |
||
169 | api_get_path(SYS_ARCHIVE_PATH).'cache', |
||
170 | api_get_path(SYS_PATH).'upload/users/', |
||
171 | ]; |
||
172 | foreach ($writable_folders as $index => $folder) { |
||
173 | $writable = is_writable($folder); |
||
174 | $status = $writable ? self::STATUS_OK : self::STATUS_ERROR; |
||
175 | $array[] = $this->build_setting( |
||
176 | $status, |
||
177 | '[FILES]', |
||
178 | get_lang('Is writable').': '.$folder, |
||
179 | 'http://be2.php.net/manual/en/function.is-writable.php', |
||
180 | $writable, |
||
181 | 1, |
||
182 | 'yes_no', |
||
183 | get_lang('The directory must be writable by the web server') |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | $exists = file_exists(api_get_path(SYS_CODE_PATH).'install'); |
||
188 | $status = $exists ? self::STATUS_WARNING : self::STATUS_OK; |
||
189 | $array[] = $this->build_setting( |
||
190 | $status, |
||
191 | '[FILES]', |
||
192 | get_lang('The directory exists').': /install', |
||
193 | 'http://be2.php.net/file_exists', |
||
194 | $exists, |
||
195 | 0, |
||
196 | 'yes_no', |
||
197 | get_lang('The directory should be removed (it is no longer necessary)') |
||
198 | ); |
||
199 | |||
200 | $app_version = api_get_setting('platform.chamilo_database_version'); |
||
201 | $array[] = $this->build_setting( |
||
202 | self::STATUS_INFORMATION, |
||
203 | '[DB]', |
||
204 | 'chamilo_database_version', |
||
205 | '#', |
||
206 | $app_version, |
||
207 | 0, |
||
208 | null, |
||
209 | 'Chamilo DB version' |
||
210 | ); |
||
211 | |||
212 | $access_url_id = api_get_current_access_url_id(); |
||
213 | |||
214 | if (1 === $access_url_id) { |
||
215 | $size = '-'; |
||
216 | $message2 = ''; |
||
217 | |||
218 | if (api_is_windows_os()) { |
||
219 | $message2 .= get_lang('The space used on disk cannot be measured properly on Windows-based systems.'); |
||
220 | } else { |
||
221 | $dir = api_get_path(SYS_PATH); |
||
222 | $du = exec('du -sh ' . $dir, $err); |
||
223 | list($size, $none) = explode("\t", $du); |
||
224 | unset($none); |
||
225 | |||
226 | $limit = get_hosting_limit($access_url_id, 'hosting_limit_disk_space'); |
||
227 | if ($limit === null) { |
||
228 | $limit = 0; |
||
229 | } |
||
230 | |||
231 | $message2 .= sprintf(get_lang('Total space used by portal %s limit is %s MB'), $size, $limit); |
||
232 | } |
||
233 | |||
234 | $array[] = $this->build_setting( |
||
235 | self::STATUS_OK, |
||
236 | '[FILES]', |
||
237 | 'hosting_limit_disk_space', |
||
238 | '#', |
||
239 | $size, |
||
240 | 0, |
||
241 | null, |
||
242 | $message2 |
||
243 | ); |
||
244 | } |
||
245 | $new_version = '-'; |
||
246 | $new_version_status = ''; |
||
247 | $file = api_get_path(SYS_CODE_PATH).'install/version.php'; |
||
248 | if (is_file($file)) { |
||
249 | @include $file; |
||
250 | } |
||
251 | $array[] = $this->build_setting( |
||
252 | self::STATUS_INFORMATION, |
||
253 | '[CONFIG]', |
||
254 | get_lang('Version from the version file'), |
||
255 | '#', |
||
256 | $new_version.' '.$new_version_status, |
||
257 | '-', |
||
258 | null, |
||
259 | get_lang('The version from the version.php file is updated with each version but only available if the main/install/ directory is present.') |
||
260 | ); |
||
261 | $array[] = $this->build_setting( |
||
262 | self::STATUS_INFORMATION, |
||
263 | '[CONFIG]', |
||
264 | get_lang('Version from the config file'), |
||
265 | '#', |
||
266 | api_get_configuration_value('system_version'), |
||
267 | $new_version, |
||
268 | null, |
||
269 | get_lang('The version from the main configuration file shows on the main administration page, but has to be changed manually on upgrade.') |
||
270 | ); |
||
271 | |||
272 | return $array; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Functions to get the data for the php diagnostics. |
||
277 | * |
||
278 | * @return array of data |
||
279 | */ |
||
280 | public function get_php_data() |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Functions to get the data for the mysql diagnostics. |
||
669 | * |
||
670 | * @return array of data |
||
671 | */ |
||
672 | public function get_database_data() |
||
673 | { |
||
674 | $array = []; |
||
675 | $em = Database::getManager(); |
||
676 | $connection = $em->getConnection(); |
||
677 | $host = $connection->getHost(); |
||
678 | $db = $connection->getDatabase(); |
||
679 | $port = $connection->getPort(); |
||
680 | $driver = $connection->getDriver()->getName(); |
||
681 | |||
682 | $array[] = $this->build_setting( |
||
683 | self::STATUS_INFORMATION, |
||
684 | '[Database]', |
||
685 | 'driver', |
||
686 | '', |
||
687 | $driver, |
||
688 | null, |
||
689 | null, |
||
690 | get_lang('Driver') |
||
691 | ); |
||
692 | |||
693 | $array[] = $this->build_setting( |
||
694 | self::STATUS_INFORMATION, |
||
695 | '[Database]', |
||
696 | 'host', |
||
697 | '', |
||
698 | $host, |
||
699 | null, |
||
700 | null, |
||
701 | get_lang('MySQL server host') |
||
702 | ); |
||
703 | |||
704 | $array[] = $this->build_setting( |
||
705 | self::STATUS_INFORMATION, |
||
706 | '[Database]', |
||
707 | 'port', |
||
708 | '', |
||
709 | $port, |
||
710 | null, |
||
711 | null, |
||
712 | get_lang('Port') |
||
713 | ); |
||
714 | |||
715 | $array[] = $this->build_setting( |
||
716 | self::STATUS_INFORMATION, |
||
717 | '[Database]', |
||
718 | 'Database name', |
||
719 | '', |
||
720 | $db, |
||
721 | null, |
||
722 | null, |
||
723 | get_lang('Name') |
||
724 | ); |
||
725 | |||
726 | return $array; |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * Functions to get the data for the webserver diagnostics. |
||
731 | * |
||
732 | * @return array of data |
||
733 | */ |
||
734 | public function get_webserver_data() |
||
735 | { |
||
736 | $array = []; |
||
737 | |||
738 | $array[] = $this->build_setting( |
||
739 | self::STATUS_INFORMATION, |
||
740 | '[SERVER]', |
||
741 | '$_SERVER["SERVER_NAME"]', |
||
742 | 'http://be.php.net/reserved.variables.server', |
||
743 | $_SERVER["SERVER_NAME"], |
||
744 | null, |
||
745 | null, |
||
746 | get_lang('Server name (as used in your request)') |
||
747 | ); |
||
748 | $array[] = $this->build_setting( |
||
749 | self::STATUS_INFORMATION, |
||
750 | '[SERVER]', |
||
751 | '$_SERVER["SERVER_ADDR"]', |
||
752 | 'http://be.php.net/reserved.variables.server', |
||
753 | $_SERVER["SERVER_ADDR"], |
||
754 | null, |
||
755 | null, |
||
756 | get_lang('Server address') |
||
757 | ); |
||
758 | $array[] = $this->build_setting( |
||
759 | self::STATUS_INFORMATION, |
||
760 | '[SERVER]', |
||
761 | '$_SERVER["SERVER_PORT"]', |
||
762 | 'http://be.php.net/reserved.variables.server', |
||
763 | $_SERVER["SERVER_PORT"], |
||
764 | null, |
||
765 | null, |
||
766 | get_lang('Server port') |
||
767 | ); |
||
768 | $array[] = $this->build_setting( |
||
769 | self::STATUS_INFORMATION, |
||
770 | '[SERVER]', |
||
771 | '$_SERVER["SERVER_SOFTWARE"]', |
||
772 | 'http://be.php.net/reserved.variables.server', |
||
773 | $_SERVER["SERVER_SOFTWARE"], |
||
774 | null, |
||
775 | null, |
||
776 | get_lang('Software running as a web server') |
||
777 | ); |
||
778 | $array[] = $this->build_setting( |
||
779 | self::STATUS_INFORMATION, |
||
780 | '[SERVER]', |
||
781 | '$_SERVER["REMOTE_ADDR"]', |
||
782 | 'http://be.php.net/reserved.variables.server', |
||
783 | $_SERVER["REMOTE_ADDR"], |
||
784 | null, |
||
785 | null, |
||
786 | get_lang('Remote address (your address as received by the server)') |
||
787 | ); |
||
788 | $array[] = $this->build_setting( |
||
789 | self::STATUS_INFORMATION, |
||
790 | '[SERVER]', |
||
791 | '$_SERVER["HTTP_USER_AGENT"]', |
||
792 | 'http://be.php.net/reserved.variables.server', |
||
793 | $_SERVER["HTTP_USER_AGENT"], |
||
794 | null, |
||
795 | null, |
||
796 | get_lang('Your user agent as received by the server') |
||
797 | ); |
||
798 | $array[] = $this->build_setting( |
||
799 | self::STATUS_INFORMATION, |
||
800 | '[SERVER]', |
||
801 | '$_SERVER["SERVER_PROTOCOL"]', |
||
802 | 'http://be.php.net/reserved.variables.server', |
||
803 | $_SERVER["SERVER_PROTOCOL"], |
||
804 | null, |
||
805 | null, |
||
806 | get_lang('Protocol used by this server') |
||
807 | ); |
||
808 | $array[] = $this->build_setting( |
||
809 | self::STATUS_INFORMATION, |
||
810 | '[SERVER]', |
||
811 | 'php_uname()', |
||
812 | 'http://be2.php.net/php_uname', |
||
813 | php_uname(), |
||
814 | null, |
||
815 | null, |
||
816 | get_lang('Information on the system the current server is running on') |
||
817 | ); |
||
818 | $array[] = $this->build_setting( |
||
819 | self::STATUS_INFORMATION, |
||
820 | '[SERVER]', |
||
821 | '$_SERVER["HTTP_X_FORWARDED_FOR"]', |
||
822 | 'http://be.php.net/reserved.variables.server', |
||
823 | (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''), |
||
824 | null, |
||
825 | null, |
||
826 | get_lang('If the server is behind a proxy or firewall (and only in those cases), it might be using the X_FORWARDED_FOR HTTP header to show the remote user IP (yours, in this case).') |
||
827 | ); |
||
828 | |||
829 | return $array; |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * Functions to get the data for the courses space usage. |
||
834 | * |
||
835 | * @throws Exception |
||
836 | * |
||
837 | * @return array of data |
||
838 | */ |
||
839 | public function get_courses_space_data() |
||
840 | { |
||
841 | $array = []; |
||
842 | |||
843 | $em = Database::getManager(); |
||
844 | $connection = $em->getConnection(); |
||
845 | $res = $connection->query('SELECT id, code, directory, disk_quota, last_visit FROM course ORDER BY last_visit DESC, code LIMIT 500'); |
||
846 | $systemPath = api_get_path(SYS_COURSE_PATH); |
||
|
|||
847 | $webPath = api_get_path(WEB_COURSE_PATH); |
||
848 | $courseHomeIcon = Display::getMdiIcon(ObjectIcon::HOME, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('CourseHome')); |
||
849 | $courseEditIcon = Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit')); |
||
850 | $windows = api_is_windows_os(); |
||
851 | $courseEditPath = api_get_path(WEB_CODE_PATH).'admin/course_edit.php?id='; |
||
852 | while ($row = $res->fetch()) { |
||
853 | $quota = $row['disk_quota'] / (1024 * 1024); |
||
854 | $dir = $systemPath.$row['directory'].'/'; |
||
855 | $path = '<a href="'.$webPath.$row['code'].'/index.php?id_session=0">'.$courseHomeIcon.'</a>'; |
||
856 | $size = '-'; |
||
857 | $courseEditLink = '<a href="'.$courseEditPath.$row['id'].'">'.$courseEditIcon.'</a>'; |
||
858 | |||
859 | if (!$windows) { |
||
860 | $err = []; |
||
861 | $du = exec('du -s '.$dir, $err); |
||
862 | list($size, $none) = explode("\t", $du); |
||
863 | unset($none); |
||
864 | $size = intval($size); |
||
865 | if ($size < 1024) { |
||
866 | $size = 1; |
||
867 | } else { |
||
868 | $size = round($size / 1024); |
||
869 | } |
||
870 | } |
||
871 | $array[] = [ |
||
872 | $path, |
||
873 | $row['code'], |
||
874 | $size, |
||
875 | round($quota), |
||
876 | $courseEditLink, |
||
877 | $row['last_visit'], |
||
878 | $dir, |
||
879 | ]; |
||
880 | } |
||
881 | |||
882 | return $array; |
||
883 | } |
||
884 | |||
885 | /** |
||
886 | * Functions to get the number of courses in the database. |
||
887 | * |
||
888 | * @throws Exception |
||
889 | * |
||
890 | * @return array of data |
||
891 | */ |
||
892 | public function get_courses_space_count() |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Additional functions needed for fast integration. |
||
906 | * |
||
907 | * @param int $status Status constant defining which icon to use to illustrate the info |
||
908 | * @param string $section The name of the section this setting is included in |
||
909 | * @param string $title The name of the setting (usually a translated string) |
||
910 | * @param string $url A URL to point the user to regarding this setting, or '#' otherwise |
||
911 | * @param mixed $current_value The current value for this setting |
||
912 | * @param mixed $expected_value The expected value for this setting |
||
913 | * @param string $formatter If this setting is expressed in some kind of format, which format to use |
||
914 | * @param string $comment A translated string explaining what this setting represents |
||
915 | * |
||
916 | * @return array A list of elements to show in an array's row |
||
917 | */ |
||
918 | public function build_setting( |
||
919 | $status, |
||
920 | $section, |
||
921 | $title, |
||
922 | $url, |
||
923 | $current_value, |
||
924 | $expected_value, |
||
925 | $formatter, |
||
926 | $comment |
||
927 | ) { |
||
928 | switch ($status) { |
||
929 | case self::STATUS_OK: |
||
930 | $img = StateIcon::COMPLETE; |
||
931 | break; |
||
932 | case self::STATUS_WARNING: |
||
933 | $img = StateIcon::WARNING; |
||
934 | break; |
||
935 | case self::STATUS_ERROR: |
||
936 | $img = StateIcon::ERROR; |
||
937 | break; |
||
938 | case self::STATUS_INFORMATION: |
||
939 | default: |
||
940 | $img = ActionIcon::INFORMATION; |
||
941 | break; |
||
942 | } |
||
943 | |||
944 | $image = Display::getMdiIcon($img, 'ch-tool-icon', null, ICON_SIZE_SMALL, $title); |
||
945 | $url = $this->get_link($title, $url); |
||
946 | |||
947 | $formatted_current_value = $current_value; |
||
948 | $formatted_expected_value = $expected_value; |
||
949 | |||
950 | if ($formatter) { |
||
951 | if (method_exists($this, 'format_'.$formatter)) { |
||
952 | $formatted_current_value = call_user_func([$this, 'format_'.$formatter], $current_value); |
||
953 | $formatted_expected_value = call_user_func([$this, 'format_'.$formatter], $expected_value); |
||
954 | } |
||
955 | } |
||
956 | |||
957 | return [$image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment]; |
||
958 | } |
||
959 | |||
960 | /** |
||
961 | * Create a link with a url and a title. |
||
962 | * |
||
963 | * @param $title |
||
964 | * @param $url |
||
965 | * |
||
966 | * @return string the url |
||
967 | */ |
||
968 | public function get_link($title, $url) |
||
971 | } |
||
972 | |||
973 | /** |
||
974 | * @param int $value |
||
975 | * |
||
976 | * @return string |
||
977 | */ |
||
978 | public function format_yes_no_optional($value) |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * @param $value |
||
998 | * |
||
999 | * @return string |
||
1000 | */ |
||
1001 | public function format_yes_no($value) |
||
1002 | { |
||
1003 | return $value ? get_lang('Yes') : get_lang('No'); |
||
1004 | } |
||
1005 | |||
1006 | /** |
||
1007 | * @param int $value |
||
1008 | * |
||
1009 | * @return string |
||
1010 | */ |
||
1011 | public function format_on_off($value) |
||
1020 | } |
||
1021 | } |
||
1022 |