Index: branches/salvatoreingala/Gadgets/Gadgets.php |
— | — | @@ -48,6 +48,7 @@ |
49 | 49 | $wgAutoloadClasses['Gadget'] = $dir . 'backend/Gadget.php'; |
50 | 50 | $wgAutoloadClasses['GadgetHooks'] = $dir . 'backend/GadgetHooks.php'; |
51 | 51 | $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'backend/GadgetResourceLoaderModule.php'; |
| 52 | +$wgAutoloadClasses['GadgetPrefs'] = $dir . 'backend/GadgetPrefs.php'; |
52 | 53 | $wgAutoloadClasses['GadgetsMainModule'] = $dir . 'ui/GadgetsMainModule.php'; |
53 | 54 | $wgAutoloadClasses['SpecialGadgets'] = $dir . 'ui/SpecialGadgets.php'; |
54 | 55 | |
Index: branches/salvatoreingala/Gadgets/backend/Gadget.php |
— | — | @@ -35,154 +35,6 @@ |
36 | 36 | $prefsDescription = null, |
37 | 37 | $preferences = null; |
38 | 38 | |
39 | | - |
40 | | - //Syntax specifications of preference description language |
41 | | - private static $prefsDescriptionSpecifications = array( |
42 | | - 'boolean' => array( |
43 | | - 'default' => array( |
44 | | - 'isMandatory' => true, |
45 | | - 'checker' => 'is_bool' |
46 | | - ), |
47 | | - 'label' => array( |
48 | | - 'isMandatory' => true, |
49 | | - 'checker' => 'is_string' |
50 | | - ) |
51 | | - ), |
52 | | - 'string' => array( |
53 | | - 'default' => array( |
54 | | - 'isMandatory' => true, |
55 | | - 'checker' => 'is_string' |
56 | | - ), |
57 | | - 'label' => array( |
58 | | - 'isMandatory' => true, |
59 | | - 'checker' => 'is_string' |
60 | | - ), |
61 | | - 'required' => array( |
62 | | - 'isMandatory' => false, |
63 | | - 'checker' => 'is_bool' |
64 | | - ), |
65 | | - 'minlength' => array( |
66 | | - 'isMandatory' => false, |
67 | | - 'checker' => 'is_integer' |
68 | | - ), |
69 | | - 'maxlength' => array( |
70 | | - 'isMandatory' => false, |
71 | | - 'checker' => 'is_integer' |
72 | | - ) |
73 | | - ), |
74 | | - 'number' => array( |
75 | | - 'default' => array( |
76 | | - 'isMandatory' => true, |
77 | | - 'checker' => 'Gadget::isFloatOrIntOrNull' |
78 | | - ), |
79 | | - 'label' => array( |
80 | | - 'isMandatory' => true, |
81 | | - 'checker' => 'is_string' |
82 | | - ), |
83 | | - 'required' => array( |
84 | | - 'isMandatory' => false, |
85 | | - 'checker' => 'is_bool' |
86 | | - ), |
87 | | - 'integer' => array( |
88 | | - 'isMandatory' => false, |
89 | | - 'checker' => 'is_bool' |
90 | | - ), |
91 | | - 'min' => array( |
92 | | - 'isMandatory' => false, |
93 | | - 'checker' => 'Gadget::isFloatOrInt' |
94 | | - ), |
95 | | - 'max' => array( |
96 | | - 'isMandatory' => false, |
97 | | - 'checker' => 'Gadget::isFloatOrInt' |
98 | | - ) |
99 | | - ), |
100 | | - 'select' => array( |
101 | | - 'default' => array( |
102 | | - 'isMandatory' => true |
103 | | - ), |
104 | | - 'label' => array( |
105 | | - 'isMandatory' => true, |
106 | | - 'checker' => 'is_string' |
107 | | - ), |
108 | | - 'options' => array( |
109 | | - 'isMandatory' => true, |
110 | | - 'checker' => 'is_array' |
111 | | - ) |
112 | | - ) |
113 | | - ); |
114 | | - |
115 | | - //Type-specific checkers for finer validation |
116 | | - private static $typeCheckers = array( |
117 | | - 'string' => 'Gadget::checkStringOptionDefinition', |
118 | | - 'number' => 'Gadget::checkNumberOptionDefinition', |
119 | | - 'select' => 'Gadget::checkSelectOptionDefinition' |
120 | | - ); |
121 | | - |
122 | | - //Further checks for 'string' options |
123 | | - private static function checkStringOptionDefinition( $option ) { |
124 | | - if ( isset( $option['minlength'] ) && $option['minlength'] < 0 ) { |
125 | | - return false; |
126 | | - } |
127 | | - |
128 | | - if ( isset( $option['maxlength'] ) && $option['maxlength'] <= 0 ) { |
129 | | - return false; |
130 | | - } |
131 | | - |
132 | | - if ( isset( $option['minlength']) && isset( $option['maxlength'] ) ) { |
133 | | - if ( $option['minlength'] > $option['maxlength'] ) { |
134 | | - return false; |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - return true; |
139 | | - } |
140 | | - |
141 | | - private static function isFloatOrInt( $param ) { |
142 | | - return is_float( $param ) || is_int( $param ); |
143 | | - } |
144 | | - |
145 | | - private static function isFloatOrIntOrNull( $param ) { |
146 | | - return is_float( $param ) || is_int( $param ) || $param === null; |
147 | | - } |
148 | | - |
149 | | - //Further checks for 'number' options |
150 | | - private static function checkNumberOptionDefinition( $option ) { |
151 | | - if ( isset( $option['integer'] ) && $option['integer'] === true ) { |
152 | | - //Check if 'min', 'max' and 'default' are integers (if given) |
153 | | - if ( intval( $option['default'] ) != $option['default'] ) { |
154 | | - return false; |
155 | | - } |
156 | | - if ( isset( $option['min'] ) && intval( $option['min'] ) != $option['min'] ) { |
157 | | - return false; |
158 | | - } |
159 | | - if ( isset( $option['max'] ) && intval( $option['max'] ) != $option['max'] ) { |
160 | | - return false; |
161 | | - } |
162 | | - } |
163 | | - |
164 | | - return true; |
165 | | - } |
166 | | - |
167 | | - private static function checkSelectOptionDefinition( $option ) { |
168 | | - $options = $option['options']; |
169 | | - |
170 | | - foreach ( $options as $opt => $optVal ) { |
171 | | - //Correct value for $optVal are NULL, boolean, integer, float or string |
172 | | - if ( $optVal !== NULL && |
173 | | - !is_bool( $optVal ) && |
174 | | - !is_int( $optVal ) && |
175 | | - !is_float( $optVal ) && |
176 | | - !is_string( $optVal ) ) |
177 | | - { |
178 | | - return false; |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - $values = array_values( $options ); |
183 | | - |
184 | | - return true; |
185 | | - } |
186 | | - |
187 | 39 | /** |
188 | 40 | * Creates an instance of this class from definition in MediaWiki:Gadgets-definition |
189 | 41 | * @param $definition String: Gadget definition |
— | — | @@ -541,102 +393,7 @@ |
542 | 394 | |
543 | 395 | return $gadgets; |
544 | 396 | } |
545 | | - |
546 | | - //TODO: put the following static methods somewhere else |
547 | | - |
548 | | - //Checks if the given description of the preferences is valid |
549 | | - public static function isPrefsDescriptionValid( $prefsDescription ) { |
550 | | - if ( !is_array( $prefsDescription ) |
551 | | - || !isset( $prefsDescription['fields'] ) |
552 | | - || !is_array( $prefsDescription['fields'] ) |
553 | | - || count( $prefsDescription['fields'] ) == 0 ) |
554 | | - { |
555 | | - return false; |
556 | | - } |
557 | | - |
558 | | - //Count of mandatory members for each type |
559 | | - $mandatoryCount = array(); |
560 | | - foreach ( self::$prefsDescriptionSpecifications as $type => $typeSpec ) { |
561 | | - $mandatoryCount[$type] = 0; |
562 | | - foreach ( $typeSpec as $fieldName => $fieldSpec ) { |
563 | | - if ( $fieldSpec['isMandatory'] === true ) { |
564 | | - ++$mandatoryCount[$type]; |
565 | | - } |
566 | | - } |
567 | | - } |
568 | | - |
569 | | - //TODO: validation of members other than $prefs['fields'] |
570 | | - |
571 | | - foreach ( $prefsDescription['fields'] as $option => $optionDefinition ) { |
572 | | - |
573 | | - //Check if 'type' is set and valid |
574 | | - if ( !isset( $optionDefinition['type'] ) ) { |
575 | | - return false; |
576 | | - } |
577 | | - |
578 | | - $type = $optionDefinition['type']; |
579 | | - |
580 | | - if ( !isset( self::$prefsDescriptionSpecifications[$type] ) ) { |
581 | | - return false; |
582 | | - } |
583 | | - |
584 | | - //check $option name compliance |
585 | | - if ( strlen( $option ) > 40 |
586 | | - || !preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $option ) ) |
587 | | - { |
588 | | - return false; |
589 | | - } |
590 | | - |
591 | | - //Check if all fields satisfy specification |
592 | | - $typeSpec = self::$prefsDescriptionSpecifications[$type]; |
593 | | - $count = 0; //count of present mandatory members |
594 | | - foreach ( $optionDefinition as $fieldName => $fieldValue ) { |
595 | | - |
596 | | - if ( $fieldName == 'type' ) { |
597 | | - continue; //'type' must not be checked |
598 | | - } |
599 | | - |
600 | | - if ( !isset( $typeSpec[$fieldName] ) ) { |
601 | | - return false; |
602 | | - } |
603 | | - |
604 | | - if ( $typeSpec[$fieldName]['isMandatory'] ) { |
605 | | - ++$count; |
606 | | - } |
607 | | - |
608 | | - if ( isset( $typeSpec[$fieldName]['checker'] ) ) { |
609 | | - $checker = $typeSpec[$fieldName]['checker']; |
610 | | - if ( !call_user_func( $checker, $fieldValue ) ) { |
611 | | - return false; |
612 | | - } |
613 | | - } |
614 | | - } |
615 | | - |
616 | | - if ( $count != $mandatoryCount[$type] ) { |
617 | | - return false; //not all mandatory members are given |
618 | | - } |
619 | | - |
620 | | - if ( isset( self::$typeCheckers[$type] ) ) { |
621 | | - //Call type-specific checker for finer validation |
622 | | - if ( !call_user_func( self::$typeCheckers[$type], $optionDefinition ) ) { |
623 | | - return false; |
624 | | - } |
625 | | - } |
626 | | - |
627 | | - //Finally, check that the 'default' fields exists and is valid |
628 | | - if ( !array_key_exists( 'default', $optionDefinition ) ) { |
629 | | - return false; |
630 | | - } |
631 | | - |
632 | | - $prefs = array( 'dummy' => $optionDefinition['default'] ); |
633 | | - if ( !self::checkSinglePref( $optionDefinition, $prefs, 'dummy' ) ) { |
634 | | - return false; |
635 | | - } |
636 | | - } |
637 | | - |
638 | | - return true; |
639 | | - } |
640 | | - |
| 397 | + |
641 | 398 | /** |
642 | 399 | * Gets description of preferences for this gadget. |
643 | 400 | * |
— | — | @@ -656,7 +413,7 @@ |
657 | 414 | * |
658 | 415 | */ |
659 | 416 | public function setPrefsDescription( $prefsDescription ) { |
660 | | - if ( self::isPrefsDescriptionValid( $prefsDescription ) ) { |
| 417 | + if ( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ) { |
661 | 418 | $this->prefsDescription = $prefsDescription; |
662 | 419 | } else { |
663 | 420 | $this->prefsDescription = null; |
— | — | @@ -666,133 +423,7 @@ |
667 | 424 | $this->preferences = null; |
668 | 425 | } |
669 | 426 | |
670 | | - //Check if a preference is valid, according to description |
671 | | - //NOTE: we pass both $prefs and $prefName (instead of just $prefs[$prefName]) |
672 | | - // to allow checking for null. |
673 | | - private static function checkSinglePref( $prefDescription, $prefs, $prefName ) { |
674 | | - |
675 | | - //isset( $prefs[$prefName] ) would return false for null values |
676 | | - if ( !array_key_exists( $prefName, $prefs ) ) { |
677 | | - return false; |
678 | | - } |
679 | | - |
680 | | - $pref = $prefs[$prefName]; |
681 | | - |
682 | | - switch ( $prefDescription['type'] ) { |
683 | | - case 'boolean': |
684 | | - return is_bool( $pref ); |
685 | | - case 'string': |
686 | | - if ( !is_string( $pref ) ) { |
687 | | - return false; |
688 | | - } |
689 | | - |
690 | | - $len = strlen( $pref ); |
691 | | - |
692 | | - //Checks the "required" option, if present |
693 | | - $required = isset( $prefDescription['required'] ) ? $prefDescription['required'] : true; |
694 | | - if ( $required === true && $len == 0 ) { |
695 | | - return false; |
696 | | - } elseif ( $required === false && $len == 0 ) { |
697 | | - return true; //overriding 'minlength' |
698 | | - } |
699 | | - |
700 | | - //Checks the "minlength" option, if present |
701 | | - $minlength = isset( $prefDescription['minlength'] ) ? $prefDescription['minlength'] : 0; |
702 | | - if ( $len < $minlength ){ |
703 | | - return false; |
704 | | - } |
705 | | - |
706 | | - //Checks the "maxlength" option, if present |
707 | | - $maxlength = isset( $prefDescription['maxlength'] ) ? $prefDescription['maxlength'] : 1024; //TODO: what big integer here? |
708 | | - if ( $len > $maxlength ){ |
709 | | - return false; |
710 | | - } |
711 | | - |
712 | | - return true; |
713 | | - case 'number': |
714 | | - if ( !is_float( $pref ) && !is_int( $pref ) && $pref !== null ) { |
715 | | - return false; |
716 | | - } |
717 | | - |
718 | | - $required = isset( $prefDescription['required'] ) ? $prefDescription['required'] : true; |
719 | | - if ( $required === false && $pref === null ) { |
720 | | - return true; |
721 | | - } |
722 | | - |
723 | | - if ( $pref === null ) { |
724 | | - return false; //$required === true, so null is not acceptable |
725 | | - } |
726 | | - |
727 | | - $integer = isset( $prefDescription['integer'] ) ? $prefDescription['integer'] : false; |
728 | | - |
729 | | - if ( $integer === true && intval( $pref ) != $pref ) { |
730 | | - return false; //not integer |
731 | | - } |
732 | | - |
733 | | - if ( isset( $prefDescription['min'] ) ) { |
734 | | - $min = $prefDescription['min']; |
735 | | - if ( $pref < $min ) { |
736 | | - return false; //value below minimum |
737 | | - } |
738 | | - } |
739 | | - |
740 | | - if ( isset( $prefDescription['max'] ) ) { |
741 | | - $max = $prefDescription['max']; |
742 | | - if ( $pref > $max ) { |
743 | | - return false; //value above maximum |
744 | | - } |
745 | | - } |
746 | | - |
747 | | - return true; |
748 | | - case 'select': |
749 | | - $values = array_values( $prefDescription['options'] ); |
750 | | - return in_array( $pref, $values, true ); |
751 | | - default: |
752 | | - return false; //unexisting type |
753 | | - } |
754 | | - } |
755 | | - |
756 | 427 | /** |
757 | | - * Checks if $prefs is an array of preferences that passes validation |
758 | | - * |
759 | | - * @param $prefsDescription Array: the preferences description to use. |
760 | | - * @param $prefs Array: reference of the array of preferences to check. |
761 | | - * |
762 | | - * @return boolean true if $prefs passes validation against $prefsDescription, false otherwise. |
763 | | - */ |
764 | | - public static function checkPrefsAgainstDescription( $prefsDescription, $prefs ) { |
765 | | - foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
766 | | - if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
767 | | - return false; |
768 | | - } |
769 | | - } |
770 | | - return true; |
771 | | - } |
772 | | - |
773 | | - /** |
774 | | - * Fixes $prefs so that it matches the description given by $prefsDescription. |
775 | | - * All values of $prefs that fail validation are replaced with default values. |
776 | | - * |
777 | | - * @param $prefsDescription Array: the preferences description to use. |
778 | | - * @param &$prefs Array: reference of the array of preferences to match. |
779 | | - */ |
780 | | - public static function matchPrefsWithDescription( $prefsDescription, &$prefs ) { |
781 | | - //Remove unexisting preferences from $prefs |
782 | | - foreach ( $prefs as $prefName => $value ) { |
783 | | - if ( !isset( $prefsDescription['fields'][$prefName] ) ) { |
784 | | - unset( $prefs[$prefName] ); |
785 | | - } |
786 | | - } |
787 | | - |
788 | | - //Fix preferences that fail validation |
789 | | - foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
790 | | - if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
791 | | - $prefs[$prefName] = $prefDescription['default']; |
792 | | - } |
793 | | - } |
794 | | - } |
795 | | - |
796 | | - /** |
797 | 428 | * Returns current user's preferences for this gadget. |
798 | 429 | * |
799 | 430 | * @return Mixed the array of preferences if they have been set, null otherwise. |
— | — | @@ -821,7 +452,7 @@ |
822 | 453 | return false; //nothing to save |
823 | 454 | } |
824 | 455 | |
825 | | - if ( !self::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ) { |
| 456 | + if ( !GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ) { |
826 | 457 | return false; //validation failed |
827 | 458 | } |
828 | 459 | |
Index: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php |
— | — | @@ -0,0 +1,379 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Static methods for gadget preferences parsing, validation and so on. |
| 6 | + * |
| 7 | + * @author Salvatore Ingala |
| 8 | + * @license GNU General Public Licence 2.0 or later |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +class GadgetPrefs { |
| 13 | + |
| 14 | + //Syntax specifications of preference description language |
| 15 | + private static $prefsDescriptionSpecifications = array( |
| 16 | + 'boolean' => array( |
| 17 | + 'default' => array( |
| 18 | + 'isMandatory' => true, |
| 19 | + 'checker' => 'is_bool' |
| 20 | + ), |
| 21 | + 'label' => array( |
| 22 | + 'isMandatory' => true, |
| 23 | + 'checker' => 'is_string' |
| 24 | + ) |
| 25 | + ), |
| 26 | + 'string' => array( |
| 27 | + 'default' => array( |
| 28 | + 'isMandatory' => true, |
| 29 | + 'checker' => 'is_string' |
| 30 | + ), |
| 31 | + 'label' => array( |
| 32 | + 'isMandatory' => true, |
| 33 | + 'checker' => 'is_string' |
| 34 | + ), |
| 35 | + 'required' => array( |
| 36 | + 'isMandatory' => false, |
| 37 | + 'checker' => 'is_bool' |
| 38 | + ), |
| 39 | + 'minlength' => array( |
| 40 | + 'isMandatory' => false, |
| 41 | + 'checker' => 'is_integer' |
| 42 | + ), |
| 43 | + 'maxlength' => array( |
| 44 | + 'isMandatory' => false, |
| 45 | + 'checker' => 'is_integer' |
| 46 | + ) |
| 47 | + ), |
| 48 | + 'number' => array( |
| 49 | + 'default' => array( |
| 50 | + 'isMandatory' => true, |
| 51 | + 'checker' => 'GadgetPrefs::isFloatOrIntOrNull' |
| 52 | + ), |
| 53 | + 'label' => array( |
| 54 | + 'isMandatory' => true, |
| 55 | + 'checker' => 'is_string' |
| 56 | + ), |
| 57 | + 'required' => array( |
| 58 | + 'isMandatory' => false, |
| 59 | + 'checker' => 'is_bool' |
| 60 | + ), |
| 61 | + 'integer' => array( |
| 62 | + 'isMandatory' => false, |
| 63 | + 'checker' => 'is_bool' |
| 64 | + ), |
| 65 | + 'min' => array( |
| 66 | + 'isMandatory' => false, |
| 67 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 68 | + ), |
| 69 | + 'max' => array( |
| 70 | + 'isMandatory' => false, |
| 71 | + 'checker' => 'GadgetPrefs::isFloatOrInt' |
| 72 | + ) |
| 73 | + ), |
| 74 | + 'select' => array( |
| 75 | + 'default' => array( |
| 76 | + 'isMandatory' => true |
| 77 | + ), |
| 78 | + 'label' => array( |
| 79 | + 'isMandatory' => true, |
| 80 | + 'checker' => 'is_string' |
| 81 | + ), |
| 82 | + 'options' => array( |
| 83 | + 'isMandatory' => true, |
| 84 | + 'checker' => 'is_array' |
| 85 | + ) |
| 86 | + ) |
| 87 | + ); |
| 88 | + |
| 89 | + //Type-specific checkers for finer validation |
| 90 | + private static $typeCheckers = array( |
| 91 | + 'string' => 'GadgetPrefs::checkStringOptionDefinition', |
| 92 | + 'number' => 'GadgetPrefs::checkNumberOptionDefinition', |
| 93 | + 'select' => 'GadgetPrefs::checkSelectOptionDefinition' |
| 94 | + ); |
| 95 | + |
| 96 | + //Further checks for 'string' options |
| 97 | + private static function checkStringOptionDefinition( $option ) { |
| 98 | + if ( isset( $option['minlength'] ) && $option['minlength'] < 0 ) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if ( isset( $option['maxlength'] ) && $option['maxlength'] <= 0 ) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if ( isset( $option['minlength']) && isset( $option['maxlength'] ) ) { |
| 107 | + if ( $option['minlength'] > $option['maxlength'] ) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + private static function isFloatOrInt( $param ) { |
| 116 | + return is_float( $param ) || is_int( $param ); |
| 117 | + } |
| 118 | + |
| 119 | + private static function isFloatOrIntOrNull( $param ) { |
| 120 | + return is_float( $param ) || is_int( $param ) || $param === null; |
| 121 | + } |
| 122 | + |
| 123 | + //Further checks for 'number' options |
| 124 | + private static function checkNumberOptionDefinition( $option ) { |
| 125 | + if ( isset( $option['integer'] ) && $option['integer'] === true ) { |
| 126 | + //Check if 'min', 'max' and 'default' are integers (if given) |
| 127 | + if ( intval( $option['default'] ) != $option['default'] ) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + if ( isset( $option['min'] ) && intval( $option['min'] ) != $option['min'] ) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + if ( isset( $option['max'] ) && intval( $option['max'] ) != $option['max'] ) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + private static function checkSelectOptionDefinition( $option ) { |
| 142 | + $options = $option['options']; |
| 143 | + |
| 144 | + foreach ( $options as $opt => $optVal ) { |
| 145 | + //Correct value for $optVal are NULL, boolean, integer, float or string |
| 146 | + if ( $optVal !== NULL && |
| 147 | + !is_bool( $optVal ) && |
| 148 | + !is_int( $optVal ) && |
| 149 | + !is_float( $optVal ) && |
| 150 | + !is_string( $optVal ) ) |
| 151 | + { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + $values = array_values( $options ); |
| 157 | + |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + //Checks if the given description of the preferences is valid |
| 162 | + public static function isPrefsDescriptionValid( $prefsDescription ) { |
| 163 | + if ( !is_array( $prefsDescription ) |
| 164 | + || !isset( $prefsDescription['fields'] ) |
| 165 | + || !is_array( $prefsDescription['fields'] ) |
| 166 | + || count( $prefsDescription['fields'] ) == 0 ) |
| 167 | + { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + //Count of mandatory members for each type |
| 172 | + $mandatoryCount = array(); |
| 173 | + foreach ( self::$prefsDescriptionSpecifications as $type => $typeSpec ) { |
| 174 | + $mandatoryCount[$type] = 0; |
| 175 | + foreach ( $typeSpec as $fieldName => $fieldSpec ) { |
| 176 | + if ( $fieldSpec['isMandatory'] === true ) { |
| 177 | + ++$mandatoryCount[$type]; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + //TODO: validation of members other than $prefs['fields'] |
| 183 | + |
| 184 | + foreach ( $prefsDescription['fields'] as $option => $optionDefinition ) { |
| 185 | + |
| 186 | + //Check if 'type' is set and valid |
| 187 | + if ( !isset( $optionDefinition['type'] ) ) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + $type = $optionDefinition['type']; |
| 192 | + |
| 193 | + if ( !isset( self::$prefsDescriptionSpecifications[$type] ) ) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + //check $option name compliance |
| 198 | + if ( strlen( $option ) > 40 |
| 199 | + || !preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $option ) ) |
| 200 | + { |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + //Check if all fields satisfy specification |
| 205 | + $typeSpec = self::$prefsDescriptionSpecifications[$type]; |
| 206 | + $count = 0; //count of present mandatory members |
| 207 | + foreach ( $optionDefinition as $fieldName => $fieldValue ) { |
| 208 | + |
| 209 | + if ( $fieldName == 'type' ) { |
| 210 | + continue; //'type' must not be checked |
| 211 | + } |
| 212 | + |
| 213 | + if ( !isset( $typeSpec[$fieldName] ) ) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + if ( $typeSpec[$fieldName]['isMandatory'] ) { |
| 218 | + ++$count; |
| 219 | + } |
| 220 | + |
| 221 | + if ( isset( $typeSpec[$fieldName]['checker'] ) ) { |
| 222 | + $checker = $typeSpec[$fieldName]['checker']; |
| 223 | + if ( !call_user_func( $checker, $fieldValue ) ) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if ( $count != $mandatoryCount[$type] ) { |
| 230 | + return false; //not all mandatory members are given |
| 231 | + } |
| 232 | + |
| 233 | + if ( isset( self::$typeCheckers[$type] ) ) { |
| 234 | + //Call type-specific checker for finer validation |
| 235 | + if ( !call_user_func( self::$typeCheckers[$type], $optionDefinition ) ) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + //Finally, check that the 'default' fields exists and is valid |
| 241 | + if ( !array_key_exists( 'default', $optionDefinition ) ) { |
| 242 | + return false; |
| 243 | + } |
| 244 | + |
| 245 | + $prefs = array( 'dummy' => $optionDefinition['default'] ); |
| 246 | + if ( !self::checkSinglePref( $optionDefinition, $prefs, 'dummy' ) ) { |
| 247 | + return false; |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return true; |
| 252 | + } |
| 253 | + |
| 254 | + //Check if a preference is valid, according to description |
| 255 | + //NOTE: we pass both $prefs and $prefName (instead of just $prefs[$prefName]) |
| 256 | + // to allow checking for null. |
| 257 | + private static function checkSinglePref( $prefDescription, $prefs, $prefName ) { |
| 258 | + |
| 259 | + //isset( $prefs[$prefName] ) would return false for null values |
| 260 | + if ( !array_key_exists( $prefName, $prefs ) ) { |
| 261 | + return false; |
| 262 | + } |
| 263 | + |
| 264 | + $pref = $prefs[$prefName]; |
| 265 | + |
| 266 | + switch ( $prefDescription['type'] ) { |
| 267 | + case 'boolean': |
| 268 | + return is_bool( $pref ); |
| 269 | + case 'string': |
| 270 | + if ( !is_string( $pref ) ) { |
| 271 | + return false; |
| 272 | + } |
| 273 | + |
| 274 | + $len = strlen( $pref ); |
| 275 | + |
| 276 | + //Checks the "required" option, if present |
| 277 | + $required = isset( $prefDescription['required'] ) ? $prefDescription['required'] : true; |
| 278 | + if ( $required === true && $len == 0 ) { |
| 279 | + return false; |
| 280 | + } elseif ( $required === false && $len == 0 ) { |
| 281 | + return true; //overriding 'minlength' |
| 282 | + } |
| 283 | + |
| 284 | + //Checks the "minlength" option, if present |
| 285 | + $minlength = isset( $prefDescription['minlength'] ) ? $prefDescription['minlength'] : 0; |
| 286 | + if ( $len < $minlength ){ |
| 287 | + return false; |
| 288 | + } |
| 289 | + |
| 290 | + //Checks the "maxlength" option, if present |
| 291 | + $maxlength = isset( $prefDescription['maxlength'] ) ? $prefDescription['maxlength'] : 1024; //TODO: what big integer here? |
| 292 | + if ( $len > $maxlength ){ |
| 293 | + return false; |
| 294 | + } |
| 295 | + |
| 296 | + return true; |
| 297 | + case 'number': |
| 298 | + if ( !is_float( $pref ) && !is_int( $pref ) && $pref !== null ) { |
| 299 | + return false; |
| 300 | + } |
| 301 | + |
| 302 | + $required = isset( $prefDescription['required'] ) ? $prefDescription['required'] : true; |
| 303 | + if ( $required === false && $pref === null ) { |
| 304 | + return true; |
| 305 | + } |
| 306 | + |
| 307 | + if ( $pref === null ) { |
| 308 | + return false; //$required === true, so null is not acceptable |
| 309 | + } |
| 310 | + |
| 311 | + $integer = isset( $prefDescription['integer'] ) ? $prefDescription['integer'] : false; |
| 312 | + |
| 313 | + if ( $integer === true && intval( $pref ) != $pref ) { |
| 314 | + return false; //not integer |
| 315 | + } |
| 316 | + |
| 317 | + if ( isset( $prefDescription['min'] ) ) { |
| 318 | + $min = $prefDescription['min']; |
| 319 | + if ( $pref < $min ) { |
| 320 | + return false; //value below minimum |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + if ( isset( $prefDescription['max'] ) ) { |
| 325 | + $max = $prefDescription['max']; |
| 326 | + if ( $pref > $max ) { |
| 327 | + return false; //value above maximum |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + return true; |
| 332 | + case 'select': |
| 333 | + $values = array_values( $prefDescription['options'] ); |
| 334 | + return in_array( $pref, $values, true ); |
| 335 | + default: |
| 336 | + return false; //unexisting type |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * Checks if $prefs is an array of preferences that passes validation |
| 342 | + * |
| 343 | + * @param $prefsDescription Array: the preferences description to use. |
| 344 | + * @param $prefs Array: reference of the array of preferences to check. |
| 345 | + * |
| 346 | + * @return boolean true if $prefs passes validation against $prefsDescription, false otherwise. |
| 347 | + */ |
| 348 | + public static function checkPrefsAgainstDescription( $prefsDescription, $prefs ) { |
| 349 | + foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
| 350 | + if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
| 351 | + return false; |
| 352 | + } |
| 353 | + } |
| 354 | + return true; |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * Fixes $prefs so that it matches the description given by $prefsDescription. |
| 359 | + * All values of $prefs that fail validation are replaced with default values. |
| 360 | + * |
| 361 | + * @param $prefsDescription Array: the preferences description to use. |
| 362 | + * @param &$prefs Array: reference of the array of preferences to match. |
| 363 | + */ |
| 364 | + public static function matchPrefsWithDescription( $prefsDescription, &$prefs ) { |
| 365 | + //Remove unexisting preferences from $prefs |
| 366 | + foreach ( $prefs as $prefName => $value ) { |
| 367 | + if ( !isset( $prefsDescription['fields'][$prefName] ) ) { |
| 368 | + unset( $prefs[$prefName] ); |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + //Fix preferences that fail validation |
| 373 | + foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
| 374 | + if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
| 375 | + $prefs[$prefName] = $prefDescription['default']; |
| 376 | + } |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | +} |
Property changes on: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 381 | + native |
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php |
— | — | @@ -206,7 +206,7 @@ |
207 | 207 | $userPrefs = array(); //no saved prefs (or invalid entry in DB), use defaults |
208 | 208 | } |
209 | 209 | |
210 | | - Gadget::matchPrefsWithDescription( $prefsDescription, $userPrefs ); |
| 210 | + GadgetPrefs::matchPrefsWithDescription( $prefsDescription, $userPrefs ); |
211 | 211 | |
212 | 212 | $gadget->setPrefs( $userPrefs ); |
213 | 213 | } |