Conditions | 57 |
Paths | > 20000 |
Total Lines | 514 |
Lines | 113 |
Ratio | 21.98 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
736 | function restore_listings($cacheids, $rdate, $roptions, $simulate) |
||
737 | { |
||
738 | global $opt, $login; |
||
739 | |||
740 | sql("SET @restoredby='&1'", $login->userid); // is evaluated by trigger functions |
||
741 | sql_slave("SET @restoredby='&1'", $login->userid); |
||
742 | |||
743 | $restored = array(); |
||
744 | |||
745 | foreach ($cacheids as $cacheid) { |
||
746 | $modified = false; |
||
747 | |||
748 | // get current cache data |
||
749 | $rs = sql("SELECT * FROM `caches` WHERE `cache_id`='&1'", $cacheid); |
||
750 | $cache = sql_fetch_assoc($rs); |
||
751 | sql_free_result($rs); |
||
752 | $wp = $cache['wp_oc']; |
||
753 | $user_id = $cache['user_id']; |
||
754 | |||
755 | // coordinates |
||
756 | View Code Duplication | if (in_array('coords', $roptions) && |
|
757 | sql_value( |
||
758 | "SELECT `cache_id` |
||
759 | FROM `cache_coordinates` |
||
760 | WHERE `cache_id`='&1' |
||
761 | AND `date_created`>='&2'", |
||
762 | 0, |
||
763 | $cacheid, |
||
764 | $rdate |
||
765 | ) |
||
766 | ) { |
||
767 | $rs = sql( |
||
768 | "SELECT `latitude`, |
||
769 | `longitude` |
||
770 | FROM `cache_coordinates` |
||
771 | WHERE `cache_id`='&1' |
||
772 | AND `date_created` < '&2' |
||
773 | ORDER BY `date_created` DESC |
||
774 | LIMIT 1", |
||
775 | $cacheid, |
||
776 | $rdate |
||
777 | ); |
||
778 | if ($r = sql_fetch_assoc($rs)) { // should always be true ... |
||
779 | if (!$simulate) { |
||
780 | sql( |
||
781 | "UPDATE `caches` SET `latitude`='&1', `longitude`='&2' WHERE `cache_id`='&3'", |
||
782 | $r['latitude'], |
||
783 | $r['longitude'], |
||
784 | $cacheid |
||
785 | ); |
||
786 | } |
||
787 | |||
788 | $restored[$wp]['coords'] = true; |
||
789 | } |
||
790 | sql_free_result($rs); |
||
791 | } |
||
792 | |||
793 | // country |
||
794 | View Code Duplication | if (in_array('coords', $roptions) && |
|
795 | sql_value( |
||
796 | "SELECT `cache_id` |
||
797 | FROM `cache_countries` |
||
798 | WHERE `cache_id`='&1' |
||
799 | AND `date_created`>='&2'", |
||
800 | 0, |
||
801 | $cacheid, |
||
802 | $rdate |
||
803 | ) |
||
804 | ) { |
||
805 | $rs = sql( |
||
806 | "SELECT `country` |
||
807 | FROM `cache_countries` |
||
808 | WHERE `cache_id`='&1' |
||
809 | AND `date_created` < '&2' |
||
810 | ORDER BY `date_created` DESC |
||
811 | LIMIT 1", |
||
812 | $cacheid, |
||
813 | $rdate |
||
814 | ); |
||
815 | if ($r = sql_fetch_assoc($rs)) { // should always be true ... |
||
816 | if (!$simulate) { |
||
817 | sql( |
||
818 | "UPDATE `caches` SET `country`='&1' WHERE `cache_id`='&2'", |
||
819 | $r['country'], |
||
820 | $cacheid |
||
821 | ); |
||
822 | } |
||
823 | |||
824 | $restored[$wp]['country'] = true; |
||
825 | } |
||
826 | sql_free_result($rs); |
||
827 | } |
||
828 | |||
829 | // other cache data |
||
830 | $rs = sql( |
||
831 | "SELECT * |
||
832 | FROM `caches_modified` |
||
833 | WHERE `cache_id`='&1' |
||
834 | AND `date_modified` >='&2' |
||
835 | ORDER BY `date_modified` ASC |
||
836 | LIMIT 1", |
||
837 | $cacheid, |
||
838 | $rdate |
||
839 | ); |
||
840 | |||
841 | $fields = [ |
||
842 | 'name' => 'settings', |
||
843 | 'type' => 'settings', |
||
844 | 'size' => 'settings', |
||
845 | 'date_hidden' => 'settings', |
||
846 | 'difficulty' => 'settings', |
||
847 | 'terrain' => 'settings', |
||
848 | 'search_time' => 'settings', |
||
849 | 'way_length' => 'settings', |
||
850 | 'wp_gc' => 'waypoints', |
||
851 | 'wp_nc' => 'waypoints' |
||
852 | ]; |
||
853 | |||
854 | if ($r = sql_fetch_assoc($rs)) { |
||
855 | // can be false |
||
856 | $setfields = ''; |
||
857 | foreach ($fields as $field => $ropt) { |
||
858 | if (in_array($ropt, $roptions) && $r[$field] != $cache[$field]) { |
||
859 | if ($setfields != '') { |
||
860 | $setfields .= ','; |
||
861 | } |
||
862 | $setfields .= "`$field`='" . sql_escape($r[$field]) . "'"; |
||
863 | $restored[$wp][$field] = true; |
||
864 | } |
||
865 | } |
||
866 | if ($setfields != '' && !$simulate) { |
||
867 | sql('UPDATE `caches` SET ' . $setfields . " WHERE `cache_id`='&1'", $cacheid); |
||
868 | } |
||
869 | } |
||
870 | sql_free_result($rs); |
||
871 | |||
872 | // attributes |
||
873 | View Code Duplication | if (in_array('settings', $roptions)) { |
|
874 | $rs = sql( |
||
875 | "SELECT * |
||
876 | FROM `caches_attributes_modified` |
||
877 | WHERE `cache_id`='&1' |
||
878 | AND `date_modified`>='&2' |
||
879 | AND `attrib_id` != 6 /* OConly */ |
||
880 | ORDER BY `date_modified` DESC", |
||
881 | $cacheid, |
||
882 | $rdate |
||
883 | ); |
||
884 | |||
885 | // revert all attribute changes in reverse order. |
||
886 | // recording limit of one change per attribute, cache and day ensures that no exponentially |
||
887 | // growing list of recording entries can emerge from multiple reverts. |
||
888 | |||
889 | while ($r = sql_fetch_assoc($rs)) { |
||
890 | if (!$simulate) { |
||
891 | if ($r['was_set']) { |
||
892 | sql( |
||
893 | "INSERT IGNORE INTO `caches_attributes` (`cache_id`,`attrib_id`) |
||
894 | VALUES ('&1','&2')", |
||
895 | $cacheid, |
||
896 | $r['attrib_id'] |
||
897 | ); |
||
898 | } else { |
||
899 | sql( |
||
900 | "DELETE FROM `caches_attributes` WHERE `cache_id`='&1' AND `attrib_id`='&2'", |
||
901 | $cacheid, |
||
902 | $r['attrib_id'] |
||
903 | ); |
||
904 | } |
||
905 | } |
||
906 | $restored[$wp]['attributes'] = true; |
||
907 | } |
||
908 | sql_free_result($rs); |
||
909 | } |
||
910 | |||
911 | // descriptions |
||
912 | if (in_array('desc', $roptions)) { |
||
913 | $rs = sql( |
||
914 | "SELECT * |
||
915 | FROM `cache_desc_modified` |
||
916 | WHERE `cache_id`='&1' |
||
917 | AND `date_modified`>='&2' |
||
918 | ORDER BY `date_modified` DESC", |
||
919 | $cacheid, |
||
920 | $rdate |
||
921 | ); |
||
922 | |||
923 | // revert all desc changes in reverse order. |
||
924 | // recording limit of one change per language, cache and day ensures that no exponentially |
||
925 | // growing list of recording entries can emerge from restore-reverts. |
||
926 | |||
927 | while ($r = sql_fetch_assoc($rs)) { |
||
928 | if (!$simulate) { |
||
929 | if ($r['desc'] === null) { // was newly created -> delete |
||
930 | sql( |
||
931 | "DELETE FROM `cache_desc` WHERE `cache_id`='&1' AND `language`='&2'", |
||
932 | $cacheid, |
||
933 | $r['language'] |
||
934 | ); |
||
935 | } else { |
||
936 | // id, uuid, date_created and last_modified are set automatically |
||
937 | sql( |
||
938 | "INSERT INTO `cache_desc` |
||
939 | (`node`, `cache_id`, `language`, `desc`, `desc_html`, `desc_htmledit`, `hint`, `short_desc`) |
||
940 | VALUES ('&1','&2','&3','&4','&5','&6','&7','&8') |
||
941 | ON DUPLICATE KEY UPDATE |
||
942 | `desc`='&4', `desc_html`='&5', `desc_htmledit`='&6', `hint`='&7', `short_desc`='&8'", |
||
943 | $opt['logic']['node']['id'], |
||
944 | $cacheid, |
||
945 | $r['language'], |
||
946 | $r['desc'], |
||
947 | $r['desc_html'], |
||
948 | $r['desc_htmledit'], |
||
949 | $r['hint'], |
||
950 | $r['short_desc'] |
||
951 | ); |
||
952 | } |
||
953 | } |
||
954 | |||
955 | $restored[$wp]['description(s)'] = true; |
||
956 | } |
||
957 | sql_free_result($rs); |
||
958 | } |
||
959 | |||
960 | // logs |
||
961 | // ... before pictures, so that restored logpics have a parent |
||
962 | if (in_array('logs', $roptions)) { |
||
963 | $rs = sql( |
||
964 | " |
||
965 | SELECT * FROM ( |
||
966 | SELECT |
||
967 | `id`, |
||
968 | -1 AS `node`, |
||
969 | `date_modified`, |
||
970 | `cache_id`, |
||
971 | 0 AS `user_id`, |
||
972 | 0 AS `type`, |
||
973 | '0' AS `oc_team_comment`, |
||
974 | '0' AS `date`, |
||
975 | '' AS `text`, |
||
976 | 0 AS `text_html`, |
||
977 | 0 AS `text_htmledit`, |
||
978 | 0 AS `needs_maintenance`, |
||
979 | 0 AS `listing_outdated`, |
||
980 | `original_id` |
||
981 | FROM `cache_logs_restored` |
||
982 | WHERE `cache_id`='&1' AND `date_modified` >= '&2' |
||
983 | UNION |
||
984 | SELECT |
||
985 | `id`, |
||
986 | `node`, |
||
987 | `deletion_date`, |
||
988 | `cache_id`, |
||
989 | `user_id`, |
||
990 | `type`, |
||
991 | `oc_team_comment`, |
||
992 | `date`, |
||
993 | `text`, |
||
994 | `text_html`, |
||
995 | `text_htmledit`, |
||
996 | `needs_maintenance`, |
||
997 | `listing_outdated`, |
||
998 | 0 AS `original_id` |
||
999 | FROM `cache_logs_archived` |
||
1000 | WHERE |
||
1001 | `cache_id`='&1' |
||
1002 | AND `deletion_date` >= '&2' |
||
1003 | AND `deleted_by`='&3' |
||
1004 | AND `user_id` != '&3' |
||
1005 | ) `logs` |
||
1006 | ORDER BY `date_modified` ASC", |
||
1007 | $cacheid, |
||
1008 | $rdate, |
||
1009 | $user_id |
||
1010 | ); |
||
1011 | |||
1012 | // We start with the oldest entry and will touch each log ony once: |
||
1013 | // After restoring its state, it is added to $logs_processed (by its last known id), |
||
1014 | // and all further operations on the same log are ignored. This prevents unnecessary |
||
1015 | // operations and flooding pictures_modified on restore-reverts. |
||
1016 | $logs_processed = array(); |
||
1017 | |||
1018 | while ($r = sql_fetch_assoc($rs)) { |
||
1019 | $error = ""; |
||
1020 | $logs_restored = false; |
||
1021 | |||
1022 | // the log's id may have changed by multiple delete-and-restores |
||
1023 | $revert_logid = get_current_logid($r['id']); |
||
1024 | if (!in_array($revert_logid, $logs_processed)) { |
||
1025 | if ($r['node'] == - 1) { |
||
1026 | // if it was not already deleted by a later restore operation ... |
||
1027 | if (sql_value("SELECT `id` FROM `cache_logs` WHERE `id`='&1'", 0, $revert_logid) != 0) { |
||
1028 | if (!$simulate) { |
||
1029 | sql( |
||
1030 | "INSERT INTO `cache_logs_archived` |
||
1031 | SELECT *, '0', '&2', '&3' FROM `cache_logs` WHERE `id`='&1'", |
||
1032 | $revert_logid, |
||
1033 | $user_id, // original deletor's ID and not restoring admin's ID! |
||
1034 | $login->userid |
||
1035 | ); |
||
1036 | sql("DELETE FROM `cache_logs` WHERE `id`='&1'", $revert_logid); |
||
1037 | // This triggers an okapi_syncbase update, if OKAPI is installed: |
||
1038 | sql( |
||
1039 | "UPDATE `cache_logs_archived` SET `deletion_date`=NOW() WHERE `id`='&1'", |
||
1040 | $revert_logid |
||
1041 | ); |
||
1042 | } |
||
1043 | $logs_restored = true; |
||
1044 | } |
||
1045 | // if it was not already restored by a later restore operation ... |
||
1046 | } elseif (sql_value("SELECT `id` FROM `cache_logs` WHERE `id`='&1'", 0, $revert_logid) == 0) { |
||
1047 | // id, uuid, date_created and last_modified are set automatically; |
||
1048 | // picture will be updated automatically on picture-restore |
||
1049 | $log = new cachelog(); |
||
1050 | $log->setNode($r['node']); // cachelog class currently does not initialize node field |
||
1051 | $log->setCacheId($r['cache_id']); |
||
1052 | $log->setUserId($r['user_id']); |
||
1053 | $log->setType($r['type'], true); |
||
1054 | $log->setOcTeamComment($r['oc_team_comment']); |
||
1055 | $log->setDate($r['date']); |
||
1056 | $log->setText($r['text']); |
||
1057 | $log->setTextHtml($r['text_html']); |
||
1058 | $log->setTextHtmlEdit($r['text_htmledit']); |
||
1059 | $log->setNeedsMaintenance($r['needs_maintenance']); |
||
1060 | $log->setListingOutdated($r['listing_outdated']); |
||
1061 | $log->setOwnerNotified(1); |
||
1062 | |||
1063 | if ($simulate) { |
||
1064 | $logs_restored = true; |
||
1065 | } else { |
||
1066 | if (!$log->save()) { |
||
1067 | $error = "restore"; |
||
1068 | } else { |
||
1069 | sql( |
||
1070 | "INSERT IGNORE INTO `cache_logs_restored` |
||
1071 | (`id`, `date_modified`, `cache_id`, `original_id`, `restored_by`) |
||
1072 | VALUES ('&1', NOW(), '&2', '&3', '&4')", |
||
1073 | $log->getLogId(), |
||
1074 | $log->getCacheId(), |
||
1075 | $revert_logid, |
||
1076 | $login->userid |
||
1077 | ); |
||
1078 | sql("DELETE FROM `watches_logqueue` WHERE `log_id`='&1'", $log->getLogId()); |
||
1079 | // watches_logqueue entry was created by trigger |
||
1080 | $logs_processed[] = $log->getLogId(); |
||
1081 | |||
1082 | /* no longer needed after implementing picture deletion in removelog.php |
||
1083 | |||
1084 | // log pic deleting is not completely implemented, orphan pictures are [*p] |
||
1085 | // left over when directly deleting the log. We try to recover them ... |
||
1086 | sql("UPDATE `pictures` SET `object_id`='&1' WHERE `object_type`=1 AND `object_id`='&2'", |
||
1087 | $log->getLogId(), $revert_logid); |
||
1088 | |||
1089 | // ... and then update the stats: |
||
1090 | $log->updatePictureStat(); |
||
1091 | */ |
||
1092 | |||
1093 | $logs_restored = true; |
||
1094 | } |
||
1095 | } |
||
1096 | } // restore deleted |
||
1097 | |||
1098 | $logs_processed[] = $revert_logid; |
||
1099 | } // not already processed |
||
1100 | |||
1101 | if ($error != '') { |
||
1102 | $restored[$wp]['internal error - could not $error log ' . $r['id'] . '/' . $logid]; |
||
1103 | } |
||
1104 | if ($logs_restored) { |
||
1105 | $restored[$wp]['logs'] = true; |
||
1106 | } |
||
1107 | } // while (all relevant log records) |
||
1108 | sql_free_result($rs); |
||
1109 | } // if logs enabled per roptions |
||
1110 | |||
1111 | // pictures |
||
1112 | if (in_array('desc', $roptions) || in_array('logs', $roptions)) { |
||
1113 | $rs = sql( |
||
1114 | "SELECT *FROM `pictures_modified` |
||
1115 | WHERE ((`object_type`=2 AND '&2' AND `object_id`='&3') OR |
||
1116 | (`object_type`=1 AND '&1' |
||
1117 | AND IFNULL((SELECT `user_id` FROM `cache_logs` WHERE `id`=`object_id`),(SELECT `user_id` FROM `cache_logs_archived` WHERE `id`=`object_id`)) != '&5' |
||
1118 | /* ^^ ignore changes of own log pics (shouldnt be in pictures_modified, anyway) */ |
||
1119 | AND IFNULL((SELECT `cache_id` FROM `cache_logs` WHERE `id`=`object_id`),(SELECT `cache_id` FROM `cache_logs_archived` WHERE `id`=`object_id`)) = '&3')) |
||
1120 | AND `date_modified`>='&4' |
||
1121 | ORDER BY `date_modified` ASC", |
||
1122 | in_array("logs", $roptions) ? 1 : 0, |
||
1123 | in_array("desc", $roptions) ? 1 : 0, |
||
1124 | $cacheid, |
||
1125 | $rdate, |
||
1126 | $user_id |
||
1127 | ); |
||
1128 | |||
1129 | // We start with the oldest entry and will touch each picture ony once: |
||
1130 | // After restoring its state, it is added to $pics_processed (by its last known id), |
||
1131 | // and all further operations on the same pic are ignored. This prevents unnecessary |
||
1132 | // operations and flooding the _modified table on restore-reverts. |
||
1133 | $pics_processed = array(); |
||
1134 | |||
1135 | while ($r = sql_fetch_assoc($rs)) { |
||
1136 | $pics_restored = false; |
||
1137 | |||
1138 | // the picture id may have changed by multiple delete-and-restores |
||
1139 | $revert_picid = get_current_picid($r['id']); |
||
1140 | if (!in_array($revert_picid, $pics_processed)) { |
||
1141 | // .. as may have its uuid-based url |
||
1142 | $revert_url = sql_value( |
||
1143 | "SELECT `url` FROM `pictures_modified` WHERE `id`='&1'", |
||
1144 | $r['url'], |
||
1145 | $revert_picid |
||
1146 | ); |
||
1147 | $error = ""; |
||
1148 | |||
1149 | switch ($r['operation']) { |
||
1150 | case 'I': |
||
1151 | if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) != 0) { |
||
1152 | // if it was not already deleted by a later restore operation: |
||
1153 | // delete added (cache) picture |
||
1154 | $pic = new picture($revert_picid); |
||
1155 | View Code Duplication | if ($simulate) { |
|
1156 | $pics_restored = true; |
||
1157 | } else { |
||
1158 | if ($pic->delete(true)) { |
||
1159 | $pics_restored = true; |
||
1160 | } else { |
||
1161 | $error = "delete"; |
||
1162 | } |
||
1163 | } |
||
1164 | } |
||
1165 | break; |
||
1166 | |||
1167 | case 'U': |
||
1168 | if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) != 0) { |
||
1169 | // if it was not deleted by a later restore operation: |
||
1170 | // restore modified (cache) picture properties |
||
1171 | $pic = new picture($revert_picid); |
||
1172 | $pic->setTitle($r['title']); |
||
1173 | $pic->setSpoiler($r['spoiler']); |
||
1174 | $pic->setDisplay($r['display']); |
||
1175 | // mappreview flag is not restored, because it seems unappropriate to |
||
1176 | // advertise for the listing of a vandalizing owner |
||
1177 | |||
1178 | View Code Duplication | if ($simulate) { |
|
1179 | $pics_restored = true; |
||
1180 | } else { |
||
1181 | if ($pic->save(true)) { |
||
1182 | $pics_restored = true; |
||
1183 | } else { |
||
1184 | $error = "update"; |
||
1185 | } |
||
1186 | } |
||
1187 | } |
||
1188 | break; |
||
1189 | |||
1190 | case 'D': |
||
1191 | if (sql_value("SELECT `id` FROM `pictures` WHERE `id`='&1'", 0, $revert_picid) == 0) { |
||
1192 | // if it was not already restored by a later restore operation: |
||
1193 | // restore deleted picture |
||
1194 | // id, uuid, date_created and last_modified are set automatically |
||
1195 | |||
1196 | // the referring log's id may have changed by [multiple] delete-and-restore |
||
1197 | if ($r['object_type'] == 1) { |
||
1198 | $r['object_id'] = get_current_logid($r['object_id']); |
||
1199 | } |
||
1200 | |||
1201 | // id, uuid, node, date_created, date_modified are automatically set; |
||
1202 | // url will be set on save; |
||
1203 | // last_url_check and thumb_last_generated stay at defaults until checked; |
||
1204 | // thumb_url will be set on thumb creation (old thumb was deleted) |
||
1205 | $pic = new picture(); |
||
1206 | $pic->setTitle($r['title']); |
||
1207 | $pic->setObjectId($r['object_id']); |
||
1208 | $pic->setObjectType($r['object_type']); |
||
1209 | $pic->setSpoiler($r['spoiler']); |
||
1210 | $pic->setLocal(1); |
||
1211 | $pic->setUnknownFormat($r['unknown_format']); |
||
1212 | $pic->setDisplay($r['display']); |
||
1213 | // mappreview flag is not restored, because it seems unappropriate to |
||
1214 | // advertise for the listing of a vandalizing owner |
||
1215 | |||
1216 | if ($simulate) { |
||
1217 | $pics_restored = true; |
||
1218 | } else { |
||
1219 | if ($pic->save(true, $revert_picid, $revert_url)) { |
||
1220 | $pics_restored = true; |
||
1221 | $pics_processed[] = $pic->getPictureId(); |
||
1222 | } else { |
||
1223 | $error = "restore"; |
||
1224 | } |
||
1225 | } |
||
1226 | } |
||
1227 | break; |
||
1228 | } // switch |
||
1229 | |||
1230 | $pics_processed[] = $revert_picid; |
||
1231 | } // not already processed |
||
1232 | |||
1233 | if ($error != '') { |
||
1234 | $restored[$wp]['internal error - could not $error picture ' . $r['id'] . '/' . $picid] = true; |
||
1235 | } |
||
1236 | if ($pics_restored) { |
||
1237 | $restored[$wp]['pictures'] = true; |
||
1238 | } |
||
1239 | } // while (all relevant pic records) |
||
1240 | |||
1241 | sql_free_result($rs); |
||
1242 | } // if pics enabled per roptions |
||
1243 | } // foreach cache(id) |
||
1244 | |||
1245 | sql('SET @restoredby=0'); |
||
1246 | sql_slave('SET @restoredby=0'); |
||
1247 | |||
1248 | return $restored; |
||
1249 | } |
||
1250 | |||
1286 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.