Index: branches/salvatoreingala/Gadgets/Gadgets_body.php |
— | — | @@ -729,7 +729,6 @@ |
730 | 730 | return $gadgets; |
731 | 731 | } |
732 | 732 | |
733 | | - |
734 | 733 | //TODO: put the following static methods somewhere else |
735 | 734 | |
736 | 735 | //Checks if the given description of the preferences is valid |
— | — | @@ -809,34 +808,21 @@ |
810 | 809 | } |
811 | 810 | |
812 | 811 | //Gets preferences for gadget $gadget; |
813 | | - // returns * null if the gadget doesn't exists |
814 | | - // * '' if the gadget exists but doesn't have any preferences |
| 812 | + // returns * '' if the gadget exists but doesn't have any preferences (or provided ones are not valid) |
815 | 813 | // * the preference description in JSON format, otherwise |
816 | | - public static function getGadgetPrefsDescription( $gadget ) { |
817 | | - $gadgetsList = Gadget::loadStructuredList(); |
818 | | - foreach ( $gadgetsList as $sectionName => $gadgets ) { |
819 | | - foreach ( $gadgets as $gadgetName => $gadgetData ) { |
820 | | - if ( $gadgetName == $gadget ) { |
821 | | - //Gadget found; are there any prefs? |
822 | | - |
823 | | - $prefsMsg = "Gadget-" . $gadget . ".preferences"; |
824 | | - |
825 | | - //TODO: should we cache? |
826 | | - |
827 | | - $prefsJson = wfMsgForContentNoTrans( $prefsMsg ); |
828 | | - if ( wfEmptyMsg( $prefsMsg, $prefsJson ) ) { |
829 | | - return null; |
830 | | - } |
831 | | - |
832 | | - if ( !self::isGadgetPrefsDescriptionValid( $prefsJson ) ){ |
833 | | - return ''; |
834 | | - } |
835 | | - |
836 | | - return $prefsJson; |
837 | | - } |
838 | | - } |
| 814 | + public function getPrefsDescription() { |
| 815 | + $prefsMsg = "Gadget-{$this->name}.preferences"; |
| 816 | + |
| 817 | + //TODO: use cache |
| 818 | + |
| 819 | + $prefsJson = wfMsgForContentNoTrans( $prefsMsg ); |
| 820 | + if ( wfEmptyMsg( $prefsMsg, $prefsJson ) || |
| 821 | + !self::isGadgetPrefsDescriptionValid( $prefsJson ) ) |
| 822 | + { |
| 823 | + return ''; |
839 | 824 | } |
840 | | - return null; //gadget not found |
| 825 | + |
| 826 | + return $prefsJson; |
841 | 827 | } |
842 | 828 | |
843 | 829 | //Check if a preference is valid, according to description |
— | — | @@ -953,13 +939,13 @@ |
954 | 940 | } |
955 | 941 | } |
956 | 942 | |
957 | | - //Get user's preferences for a specific gadget |
958 | | - public static function getUserPrefs( $user, $gadget ) { |
| 943 | + //Get user's preferences for this gadget |
| 944 | + public function getUserPrefs( $user ) { |
959 | 945 | //TODO: cache! |
960 | 946 | |
961 | | - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
| 947 | + $prefsDescriptionJson = $this->getPrefsDescription(); |
962 | 948 | |
963 | | - if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) { |
| 949 | + if ( $prefsDescriptionJson === '' ) { |
964 | 950 | return null; |
965 | 951 | } |
966 | 952 | |
— | — | @@ -969,7 +955,7 @@ |
970 | 956 | $dbr = wfGetDB( DB_SLAVE ); |
971 | 957 | |
972 | 958 | $id = $user->getId(); |
973 | | - $property = "gadget-{$gadget}-config"; |
| 959 | + $property = "gadget-{$this->name}-config"; |
974 | 960 | |
975 | 961 | $res = $dbr->selectRow( |
976 | 962 | 'user_properties', |
— | — | @@ -993,12 +979,12 @@ |
994 | 980 | return $userPrefs; |
995 | 981 | } |
996 | 982 | |
997 | | - //Set user's preferences for a specific gadget. |
| 983 | + //Set user's preferences for this gadget. |
998 | 984 | //Returns false if preferences are rejected (that is, they don't pass validation) |
999 | | - public static function setUserPrefs( $user, $gadget, &$preferences ) { |
1000 | | - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
| 985 | + public function setUserPrefs( $user, &$preferences ) { |
| 986 | + $prefsDescriptionJson = $this->getPrefsDescription(); |
1001 | 987 | |
1002 | | - if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) { |
| 988 | + if ( $prefsDescriptionJson === '' ) { |
1003 | 989 | return false; //nothing to save |
1004 | 990 | } |
1005 | 991 | |
— | — | @@ -1017,7 +1003,7 @@ |
1018 | 1004 | $dbw = wfGetDB( DB_MASTER ); |
1019 | 1005 | |
1020 | 1006 | $id = $user->getId(); |
1021 | | - $property = "gadget-{$gadget}-config"; |
| 1007 | + $property = "gadget-{$this->name}-config"; |
1022 | 1008 | |
1023 | 1009 | $row = array( |
1024 | 1010 | 'up_user' => $id, |
— | — | @@ -1095,12 +1081,13 @@ |
1096 | 1082 | |
1097 | 1083 | public function getScript( ResourceLoaderContext $context ) { |
1098 | 1084 | $moduleName = $this->getName(); |
1099 | | - $gadget = substr( $moduleName, strlen( 'ext.gadget.' ) ); |
| 1085 | + $gadgetName = substr( $moduleName, strlen( 'ext.gadget.' ) ); |
| 1086 | + $gadgets = Gadget::loadList(); |
| 1087 | + $gadget = $gadgets[$gadgetName]; |
1100 | 1088 | |
1101 | | - |
1102 | 1089 | $user = RequestContext::getMain()->getUser(); |
1103 | 1090 | |
1104 | | - $prefs = Gadget::getUserPrefs( $user, $gadget ); |
| 1091 | + $prefs = $gadget->getUserPrefs( $user ); |
1105 | 1092 | |
1106 | 1093 | //Enclose gadget's code in a closure, with "this" bound to the |
1107 | 1094 | //configuration object (or to "window" for non-configurable gadgets) |
— | — | @@ -1123,7 +1110,7 @@ |
1124 | 1111 | } |
1125 | 1112 | |
1126 | 1113 | |
1127 | | - //TODO: should depend on gadget's last modification time, also |
| 1114 | + //TODO: should depend on last modification time of gadget's configuration page, also |
1128 | 1115 | public function getModifiedTime( ResourceLoaderContext $context ) { |
1129 | 1116 | $touched = RequestContext::getMain()->getUser()->getTouched(); |
1130 | 1117 | |
— | — | @@ -1139,11 +1126,11 @@ |
1140 | 1127 | $configurableGadgets = array(); |
1141 | 1128 | $gadgetsList = Gadget::loadStructuredList(); |
1142 | 1129 | |
1143 | | - foreach ( $gadgetsList as $sectionName => $gadgets ) { |
1144 | | - foreach ( $gadgets as $gadget => $gadgetData ) { |
1145 | | - $prefs = Gadget::getGadgetPrefsDescription( $gadget ); |
1146 | | - if ( $prefs !== null && $prefs !== '' ) { |
1147 | | - $configurableGadgets[] = $gadget; |
| 1130 | + foreach ( $gadgetsList as $section => $gadgets ) { |
| 1131 | + foreach ( $gadgets as $gadgetName => $gadget ) { |
| 1132 | + $prefs = $gadget->getPrefsDescription(); |
| 1133 | + if ( $prefs !== '' ) { |
| 1134 | + $configurableGadgets[] = $gadget->getName(); |
1148 | 1135 | } |
1149 | 1136 | } |
1150 | 1137 | } |
Index: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php |
— | — | @@ -36,30 +36,22 @@ |
37 | 37 | $this->dieUsageMsg( 'sessionfailure' ); |
38 | 38 | } |
39 | 39 | |
40 | | - $gadget = $params['gadget']; |
41 | | - $prefsJson = $params['prefs']; |
42 | | - |
43 | | - //Checks if the gadget actually exists |
44 | | - $gadgetsList = Gadget::loadStructuredList(); |
45 | | - $found = false; |
46 | | - foreach ( $gadgetsList as $section => $gadgets ) { |
47 | | - if ( isset( $gadgets[$gadget] ) ) { |
48 | | - $found = true; |
49 | | - break; |
50 | | - } |
51 | | - } |
| 40 | + $gadgetName = $params['gadget']; |
| 41 | + $gadgets = Gadget::loadList(); |
| 42 | + $gadget = $gadgets && isset( $gadgets[$gadgetName] ) ? $gadgets[$gadgetName] : null; |
52 | 43 | |
53 | | - if ( !$found ) { |
| 44 | + if ( $gadget === null ) { |
54 | 45 | $this->dieUsage( 'Gadget not found', 'notfound' ); |
55 | 46 | } |
56 | 47 | |
| 48 | + $prefsJson = $params['prefs']; |
57 | 49 | $prefs = FormatJson::decode( $prefsJson, true ); |
58 | 50 | |
59 | 51 | if ( !is_array( $prefs ) ) { |
60 | 52 | $this->dieUsage( 'The \'pref\' parameter must be valid JSON', 'notjson' ); |
61 | 53 | } |
62 | 54 | |
63 | | - $result = Gadget::setUserPrefs( $user, $gadget, $prefs ); |
| 55 | + $result = $gadget->setUserPrefs( $user, $prefs ); |
64 | 56 | |
65 | 57 | if ( $result === true ) { |
66 | 58 | $this->getResult()->addValue( |
Index: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php |
— | — | @@ -31,30 +31,22 @@ |
32 | 32 | $this->dieUsage( 'You must be logged-in to get gadget\'s preferences', 'notloggedin' ); |
33 | 33 | } |
34 | 34 | |
35 | | - $gadget = $params['gadget']; |
36 | | - |
37 | | - //Checks if the gadget actually exists |
38 | | - $gadgetsList = Gadget::loadStructuredList(); |
39 | | - $found = false; |
40 | | - foreach ( $gadgetsList as $section => $gadgets ) { |
41 | | - if ( isset( $gadgets[$gadget] ) ) { |
42 | | - $found = true; |
43 | | - break; |
44 | | - } |
45 | | - } |
| 35 | + $gadgetName = $params['gadget']; |
| 36 | + $gadgets = Gadget::loadList(); |
| 37 | + $gadget = $gadgets && isset( $gadgets[$gadgetName] ) ? $gadgets[$gadgetName] : null; |
46 | 38 | |
47 | | - if ( !$found ) { |
| 39 | + if ( $gadget === null ) { |
48 | 40 | $this->dieUsage( 'Gadget not found', 'notfound' ); |
49 | 41 | } |
50 | 42 | |
51 | | - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
| 43 | + $prefsDescriptionJson = $gadget->getPrefsDescription(); |
52 | 44 | $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); |
53 | 45 | |
54 | 46 | if ( $prefsDescription === null ) { |
55 | | - $this->dieUsage( "Gadget $gadget does not have any preference.", 'noprefs' ); |
| 47 | + $this->dieUsage( 'Gadget ' . $gadget->getName() . ' does not have any preference.', 'noprefs' ); |
56 | 48 | } |
57 | 49 | |
58 | | - $userPrefs = Gadget::getUserPrefs( $user, $gadget ); |
| 50 | + $userPrefs = $gadget->getUserPrefs( $user ); |
59 | 51 | |
60 | 52 | //Add user preferences to preference description |
61 | 53 | foreach ( $userPrefs as $pref => $value ) { |