r90127 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90126‎ | r90127 | r90128 >
Date:18:03, 15 June 2011
Author:salvatoreingala
Status:deferred (Comments)
Tags:
Comment:
Replaced static methods to get/set preferences and preference descriptions with member functions.
Modified paths:
  • /branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_body.php
@@ -729,7 +729,6 @@
730730 return $gadgets;
731731 }
732732
733 -
734733 //TODO: put the following static methods somewhere else
735734
736735 //Checks if the given description of the preferences is valid
@@ -809,34 +808,21 @@
810809 }
811810
812811 //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)
815813 // * 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 '';
839824 }
840 - return null; //gadget not found
 825+
 826+ return $prefsJson;
841827 }
842828
843829 //Check if a preference is valid, according to description
@@ -953,13 +939,13 @@
954940 }
955941 }
956942
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 ) {
959945 //TODO: cache!
960946
961 - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget );
 947+ $prefsDescriptionJson = $this->getPrefsDescription();
962948
963 - if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) {
 949+ if ( $prefsDescriptionJson === '' ) {
964950 return null;
965951 }
966952
@@ -969,7 +955,7 @@
970956 $dbr = wfGetDB( DB_SLAVE );
971957
972958 $id = $user->getId();
973 - $property = "gadget-{$gadget}-config";
 959+ $property = "gadget-{$this->name}-config";
974960
975961 $res = $dbr->selectRow(
976962 'user_properties',
@@ -993,12 +979,12 @@
994980 return $userPrefs;
995981 }
996982
997 - //Set user's preferences for a specific gadget.
 983+ //Set user's preferences for this gadget.
998984 //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();
1001987
1002 - if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) {
 988+ if ( $prefsDescriptionJson === '' ) {
1003989 return false; //nothing to save
1004990 }
1005991
@@ -1017,7 +1003,7 @@
10181004 $dbw = wfGetDB( DB_MASTER );
10191005
10201006 $id = $user->getId();
1021 - $property = "gadget-{$gadget}-config";
 1007+ $property = "gadget-{$this->name}-config";
10221008
10231009 $row = array(
10241010 'up_user' => $id,
@@ -1095,12 +1081,13 @@
10961082
10971083 public function getScript( ResourceLoaderContext $context ) {
10981084 $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];
11001088
1101 -
11021089 $user = RequestContext::getMain()->getUser();
11031090
1104 - $prefs = Gadget::getUserPrefs( $user, $gadget );
 1091+ $prefs = $gadget->getUserPrefs( $user );
11051092
11061093 //Enclose gadget's code in a closure, with "this" bound to the
11071094 //configuration object (or to "window" for non-configurable gadgets)
@@ -1123,7 +1110,7 @@
11241111 }
11251112
11261113
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
11281115 public function getModifiedTime( ResourceLoaderContext $context ) {
11291116 $touched = RequestContext::getMain()->getUser()->getTouched();
11301117
@@ -1139,11 +1126,11 @@
11401127 $configurableGadgets = array();
11411128 $gadgetsList = Gadget::loadStructuredList();
11421129
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();
11481135 }
11491136 }
11501137 }
Index: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php
@@ -36,30 +36,22 @@
3737 $this->dieUsageMsg( 'sessionfailure' );
3838 }
3939
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;
5243
53 - if ( !$found ) {
 44+ if ( $gadget === null ) {
5445 $this->dieUsage( 'Gadget not found', 'notfound' );
5546 }
5647
 48+ $prefsJson = $params['prefs'];
5749 $prefs = FormatJson::decode( $prefsJson, true );
5850
5951 if ( !is_array( $prefs ) ) {
6052 $this->dieUsage( 'The \'pref\' parameter must be valid JSON', 'notjson' );
6153 }
6254
63 - $result = Gadget::setUserPrefs( $user, $gadget, $prefs );
 55+ $result = $gadget->setUserPrefs( $user, $prefs );
6456
6557 if ( $result === true ) {
6658 $this->getResult()->addValue(
Index: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
@@ -31,30 +31,22 @@
3232 $this->dieUsage( 'You must be logged-in to get gadget\'s preferences', 'notloggedin' );
3333 }
3434
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;
4638
47 - if ( !$found ) {
 39+ if ( $gadget === null ) {
4840 $this->dieUsage( 'Gadget not found', 'notfound' );
4941 }
5042
51 - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget );
 43+ $prefsDescriptionJson = $gadget->getPrefsDescription();
5244 $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true );
5345
5446 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' );
5648 }
5749
58 - $userPrefs = Gadget::getUserPrefs( $user, $gadget );
 50+ $userPrefs = $gadget->getUserPrefs( $user );
5951
6052 //Add user preferences to preference description
6153 foreach ( $userPrefs as $pref => $value ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r90216Code simplification, as per comment in r90127.salvatoreingala17:14, 16 June 2011

Comments

#Comment by MaxSem (talk | contribs)   01:42, 16 June 2011
 	public function getScript( ResourceLoaderContext $context ) {
 		$moduleName = $this->getName();
-		$gadget = substr( $moduleName, strlen( 'ext.gadget.' ) );
+		$gadgetName = substr( $moduleName, strlen( 'ext.gadget.' ) );
+		$gadgets = Gadget::loadList();
+		$gadget = $gadgets[$gadgetName];

Why not pass the gadget to constructor instead?

#Comment by Salvatore Ingala (talk | contribs)   17:30, 16 June 2011

Dofinitely true; done in r90216, thank you.

Status & tagging log