Index: trunk/phase3/includes/DBDataObject.php |
— | — | @@ -7,6 +7,8 @@ |
8 | 8 | * array of fields and various methods to do common interaction with the database. |
9 | 9 | * |
10 | 10 | * These methods must be implemented in deriving classes: |
| 11 | + * * getDBTable |
| 12 | + * * getFieldPrefix |
11 | 13 | * * getFieldTypes |
12 | 14 | * |
13 | 15 | * These methods are likely candidates for overriding: |
— | — | @@ -17,10 +19,6 @@ |
18 | 20 | * * loadSummaryFields |
19 | 21 | * * getSummaryFields |
20 | 22 | * |
21 | | - * Deriving classes must register their table and field prefix in $wgDBDataObjects. |
22 | | - * Syntax: $wgDBDataObjects['DrivingClassName'] = array( 'table' => 'table_name', 'prefix' => 'fieldprefix_' ); |
23 | | - * Example: $wgDBDataObjects['EPOrg'] = array( 'table' => 'ep_orgs', 'prefix' => 'org_' ); |
24 | | - * |
25 | 23 | * Main instance methods: |
26 | 24 | * * getField(s) |
27 | 25 | * * setField(s) |
— | — | @@ -95,13 +93,7 @@ |
96 | 94 | * @return string |
97 | 95 | */ |
98 | 96 | public static function getDBTable() { |
99 | | - global $wgDBDataObjects; |
100 | | - if ( array_key_exists( get_called_class(), $wgDBDataObjects ) ) { |
101 | | - return $wgDBDataObjects[get_called_class()]['table']; |
102 | | - } |
103 | | - else { |
104 | | - throw new MWException( 'Class "' . get_called_class() . '" not found in $wgDBDataObjects' ); |
105 | | - } |
| 97 | + throw new MWException( 'Class "' . get_called_class() . '" did not implement getDBTable' ); |
106 | 98 | } |
107 | 99 | |
108 | 100 | /** |
— | — | @@ -113,13 +105,7 @@ |
114 | 106 | * @return string |
115 | 107 | */ |
116 | 108 | protected static function getFieldPrefix() { |
117 | | - global $wgDBDataObjects; |
118 | | - if ( array_key_exists( get_called_class(), $wgDBDataObjects ) ) { |
119 | | - return $wgDBDataObjects[get_called_class()]['prefix']; |
120 | | - } |
121 | | - else { |
122 | | - throw new MWException( 'Class "' . get_called_class() . '" not found in $wgDBDataObjects' ); |
123 | | - } |
| 109 | + throw new MWException( 'Class "' . get_called_class() . '" did not implement getFieldPrefix' ); |
124 | 110 | } |
125 | 111 | |
126 | 112 | /** |
— | — | @@ -675,9 +661,6 @@ |
676 | 662 | |
677 | 663 | /** |
678 | 664 | * Takes in a field and returns an it's prefixed version, ready for db usage. |
679 | | - * If the field needs to be prefixed for another table, provide an array in the form |
680 | | - * array( 'tablename', 'fieldname' ) |
681 | | - * Where table name is registered in $wgDBDataObjects. |
682 | 665 | * |
683 | 666 | * @since 1.20 |
684 | 667 | * |
— | — | @@ -687,28 +670,7 @@ |
688 | 671 | * @throws MWException |
689 | 672 | */ |
690 | 673 | public static function getPrefixedField( $field ) { |
691 | | - static $prefixes = false; |
692 | | - |
693 | | - if ( $prefixes === false ) { |
694 | | - foreach ( $GLOBALS['wgDBDataObjects'] as $classInfo ) { |
695 | | - $prefixes[$classInfo['table']] = $classInfo['prefix']; |
696 | | - } |
697 | | - } |
698 | | - |
699 | | - if ( is_array( $field ) && count( $field ) > 1 ) { |
700 | | - if ( array_key_exists( $field[0], $prefixes ) ) { |
701 | | - $prefix = $prefixes[$field[0]]; |
702 | | - $field = $field[1]; |
703 | | - } |
704 | | - else { |
705 | | - throw new MWException( 'Tried to prefix field with unknown table "' . $field[0] . '"' ); |
706 | | - } |
707 | | - } |
708 | | - else { |
709 | | - $prefix = static::getFieldPrefix(); |
710 | | - } |
711 | | - |
712 | | - return $prefix . $field; |
| 674 | + return static::getFieldPrefix() . $field; |
713 | 675 | } |
714 | 676 | |
715 | 677 | /** |
— | — | @@ -873,12 +835,11 @@ |
874 | 836 | * @param array|string|null $fields |
875 | 837 | * @param array $conditions |
876 | 838 | * @param array $options |
877 | | - * @param array $joinConds |
878 | 839 | * |
879 | 840 | * @return array of self |
880 | 841 | */ |
881 | | - public static function select( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array() ) { |
882 | | - $result = static::selectFields( $fields, $conditions, $options, $joinConds, false ); |
| 842 | + public static function select( $fields = null, array $conditions = array(), array $options = array() ) { |
| 843 | + $result = static::selectFields( $fields, $conditions, $options, false ); |
883 | 844 | |
884 | 845 | $objects = array(); |
885 | 846 | |
— | — | @@ -906,12 +867,11 @@ |
907 | 868 | * @param array|string|null $fields |
908 | 869 | * @param array $conditions |
909 | 870 | * @param array $options |
910 | | - * @param array $joinConds |
911 | 871 | * @param boolean $collapse Set to false to always return each result row as associative array. |
912 | 872 | * |
913 | 873 | * @return array of array |
914 | 874 | */ |
915 | | - public static function selectFields( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), $collapse = true ) { |
| 875 | + public static function selectFields( $fields = null, array $conditions = array(), array $options = array(), $collapse = true ) { |
916 | 876 | if ( is_null( $fields ) ) { |
917 | 877 | $fields = array_keys( static::getFieldTypes() ); |
918 | 878 | } |
— | — | @@ -919,15 +879,10 @@ |
920 | 880 | $fields = (array)$fields; |
921 | 881 | } |
922 | 882 | |
923 | | - $tables = array( static::getDBTable() ); |
924 | | - $joinConds = static::getProcessedJoinConds( $joinConds, $tables ); |
925 | | - |
926 | 883 | $result = static::rawSelect( |
927 | 884 | static::getPrefixedFields( $fields ), |
928 | 885 | static::getPrefixedValues( $conditions ), |
929 | | - $options, |
930 | | - $joinConds, |
931 | | - $tables |
| 886 | + $options |
932 | 887 | ); |
933 | 888 | |
934 | 889 | $objects = array(); |
— | — | @@ -955,50 +910,6 @@ |
956 | 911 | } |
957 | 912 | |
958 | 913 | /** |
959 | | - * Process the join conditions. This includes prefixing table and field names, |
960 | | - * and adding of needed tables. |
961 | | - * |
962 | | - * @since 1.20 |
963 | | - * |
964 | | - * @param array $joinConds Join conditions without prefixes and fields in array rather then string with equals sign. |
965 | | - * @param array $tables List of tables to which the extra needed ones get added. |
966 | | - * |
967 | | - * @return array Join conditions ready to be fed to MediaWikis native select function. |
968 | | - */ |
969 | | - protected static function getProcessedJoinConds( array $joinConds, array &$tables ) { |
970 | | - $conds = array(); |
971 | | - |
972 | | - foreach ( $joinConds as $table => $joinCond ) { |
973 | | - if ( !in_array( $table, $tables ) ) { |
974 | | - $tables[] = $table; |
975 | | - } |
976 | | - |
977 | | - $cond = array( $joinCond[0], array() ); |
978 | | - |
979 | | - foreach ( $joinCond[1] as $joinCondPart ) { |
980 | | - $parts = array( |
981 | | - static::getPrefixedField( $joinCondPart[0] ), |
982 | | - static::getPrefixedField( $joinCondPart[1] ), |
983 | | - ); |
984 | | - |
985 | | - if ( !in_array( $joinCondPart[0][0], $tables ) ) { |
986 | | - $tables[] = $joinCondPart[0][0]; |
987 | | - } |
988 | | - |
989 | | - if ( !in_array( $joinCondPart[1][0], $tables ) ) { |
990 | | - $tables[] = $joinCondPart[1][0]; |
991 | | - } |
992 | | - |
993 | | - $cond[1][] = implode( '=', $parts ); |
994 | | - } |
995 | | - |
996 | | - $conds[$table] = $cond; |
997 | | - } |
998 | | - |
999 | | - return $conds; |
1000 | | - } |
1001 | | - |
1002 | | - /** |
1003 | 914 | * Selects the the specified fields of the first matching record. |
1004 | 915 | * Field names get prefixed. |
1005 | 916 | * |
— | — | @@ -1007,14 +918,13 @@ |
1008 | 919 | * @param array|string|null $fields |
1009 | 920 | * @param array $conditions |
1010 | 921 | * @param array $options |
1011 | | - * @param array $joinConds |
1012 | 922 | * |
1013 | 923 | * @return DBObject|bool False on failure |
1014 | 924 | */ |
1015 | | - public static function selectRow( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array() ) { |
| 925 | + public static function selectRow( $fields = null, array $conditions = array(), array $options = array() ) { |
1016 | 926 | $options['LIMIT'] = 1; |
1017 | 927 | |
1018 | | - $objects = static::select( $fields, $conditions, $options, $joinConds ); |
| 928 | + $objects = static::select( $fields, $conditions, $options ); |
1019 | 929 | |
1020 | 930 | return count( $objects ) > 0 ? $objects[0] : false; |
1021 | 931 | } |
— | — | @@ -1031,15 +941,14 @@ |
1032 | 942 | * @param array|string|null $fields |
1033 | 943 | * @param array $conditions |
1034 | 944 | * @param array $options |
1035 | | - * @param array $joinConds |
1036 | 945 | * @param boolean $collapse Set to false to always return each result row as associative array. |
1037 | 946 | * |
1038 | 947 | * @return mixed|array|bool False on failure |
1039 | 948 | */ |
1040 | | - public static function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), array $joinConds = array(), $collapse = true ) { |
| 949 | + public static function selectFieldsRow( $fields = null, array $conditions = array(), array $options = array(), $collapse = true ) { |
1041 | 950 | $options['LIMIT'] = 1; |
1042 | 951 | |
1043 | | - $objects = static::selectFields( $fields, $conditions, $options, $joinConds, $collapse ); |
| 952 | + $objects = static::selectFields( $fields, $conditions, $options, $collapse ); |
1044 | 953 | |
1045 | 954 | return count( $objects ) > 0 ? $objects[0] : false; |
1046 | 955 | } |
— | — | @@ -1088,25 +997,18 @@ |
1089 | 998 | * @param array $fields |
1090 | 999 | * @param array $conditions |
1091 | 1000 | * @param array $options |
1092 | | - * @param array $joinConds |
1093 | | - * @param array $tables |
1094 | 1001 | * |
1095 | 1002 | * @return ResultWrapper |
1096 | 1003 | */ |
1097 | | - public static function rawSelect( array $fields, array $conditions = array(), array $options = array(), array $joinConds = array(), array $tables = null ) { |
1098 | | - if ( is_null( $tables ) ) { |
1099 | | - $tables = static::getDBTable(); |
1100 | | - } |
1101 | | - |
| 1004 | + public static function rawSelect( array $fields, array $conditions = array(), array $options = array() ) { |
1102 | 1005 | $dbr = wfGetDB( static::getReadDb() ); |
1103 | 1006 | |
1104 | 1007 | return $dbr->select( |
1105 | | - $tables, |
| 1008 | + static::getDBTable(), |
1106 | 1009 | $fields, |
1107 | 1010 | count( $conditions ) == 0 ? '' : $conditions, |
1108 | 1011 | __METHOD__, |
1109 | | - $options, |
1110 | | - $joinConds |
| 1012 | + $options |
1111 | 1013 | ); |
1112 | 1014 | } |
1113 | 1015 | |