r111305 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111304‎ | r111305 | r111306 >
Date:15:51, 12 February 2012
Author:jeroendedauw
Status:reverted (Comments)
Tags:
Comment:
follow up to r111264 - get rid of wgDBDataObjects as per suggestion by Platonides
Modified paths:
  • /trunk/phase3/includes/DBDataObject.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/DBDataObject.php
@@ -7,6 +7,8 @@
88 * array of fields and various methods to do common interaction with the database.
99 *
1010 * These methods must be implemented in deriving classes:
 11+ * * getDBTable
 12+ * * getFieldPrefix
1113 * * getFieldTypes
1214 *
1315 * These methods are likely candidates for overriding:
@@ -17,10 +19,6 @@
1820 * * loadSummaryFields
1921 * * getSummaryFields
2022 *
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 - *
2523 * Main instance methods:
2624 * * getField(s)
2725 * * setField(s)
@@ -95,13 +93,7 @@
9694 * @return string
9795 */
9896 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' );
10698 }
10799
108100 /**
@@ -113,13 +105,7 @@
114106 * @return string
115107 */
116108 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' );
124110 }
125111
126112 /**
@@ -675,9 +661,6 @@
676662
677663 /**
678664 * 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.
682665 *
683666 * @since 1.20
684667 *
@@ -687,28 +670,7 @@
688671 * @throws MWException
689672 */
690673 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;
713675 }
714676
715677 /**
@@ -873,12 +835,11 @@
874836 * @param array|string|null $fields
875837 * @param array $conditions
876838 * @param array $options
877 - * @param array $joinConds
878839 *
879840 * @return array of self
880841 */
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 );
883844
884845 $objects = array();
885846
@@ -906,12 +867,11 @@
907868 * @param array|string|null $fields
908869 * @param array $conditions
909870 * @param array $options
910 - * @param array $joinConds
911871 * @param boolean $collapse Set to false to always return each result row as associative array.
912872 *
913873 * @return array of array
914874 */
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 ) {
916876 if ( is_null( $fields ) ) {
917877 $fields = array_keys( static::getFieldTypes() );
918878 }
@@ -919,15 +879,10 @@
920880 $fields = (array)$fields;
921881 }
922882
923 - $tables = array( static::getDBTable() );
924 - $joinConds = static::getProcessedJoinConds( $joinConds, $tables );
925 -
926883 $result = static::rawSelect(
927884 static::getPrefixedFields( $fields ),
928885 static::getPrefixedValues( $conditions ),
929 - $options,
930 - $joinConds,
931 - $tables
 886+ $options
932887 );
933888
934889 $objects = array();
@@ -955,50 +910,6 @@
956911 }
957912
958913 /**
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 - /**
1003914 * Selects the the specified fields of the first matching record.
1004915 * Field names get prefixed.
1005916 *
@@ -1007,14 +918,13 @@
1008919 * @param array|string|null $fields
1009920 * @param array $conditions
1010921 * @param array $options
1011 - * @param array $joinConds
1012922 *
1013923 * @return DBObject|bool False on failure
1014924 */
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() ) {
1016926 $options['LIMIT'] = 1;
1017927
1018 - $objects = static::select( $fields, $conditions, $options, $joinConds );
 928+ $objects = static::select( $fields, $conditions, $options );
1019929
1020930 return count( $objects ) > 0 ? $objects[0] : false;
1021931 }
@@ -1031,15 +941,14 @@
1032942 * @param array|string|null $fields
1033943 * @param array $conditions
1034944 * @param array $options
1035 - * @param array $joinConds
1036945 * @param boolean $collapse Set to false to always return each result row as associative array.
1037946 *
1038947 * @return mixed|array|bool False on failure
1039948 */
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 ) {
1041950 $options['LIMIT'] = 1;
1042951
1043 - $objects = static::selectFields( $fields, $conditions, $options, $joinConds, $collapse );
 952+ $objects = static::selectFields( $fields, $conditions, $options, $collapse );
1044953
1045954 return count( $objects ) > 0 ? $objects[0] : false;
1046955 }
@@ -1088,25 +997,18 @@
1089998 * @param array $fields
1090999 * @param array $conditions
10911000 * @param array $options
1092 - * @param array $joinConds
1093 - * @param array $tables
10941001 *
10951002 * @return ResultWrapper
10961003 */
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() ) {
11021005 $dbr = wfGetDB( static::getReadDb() );
11031006
11041007 return $dbr->select(
1105 - $tables,
 1008+ static::getDBTable(),
11061009 $fields,
11071010 count( $conditions ) == 0 ? '' : $conditions,
11081011 __METHOD__,
1109 - $options,
1110 - $joinConds
 1012+ $options
11111013 );
11121014 }
11131015

Follow-up revisions

RevisionCommit summaryAuthorDate
r111306follow up to r111305jeroendedauw15:51, 12 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r111264adding DBDataObject class after having some people review it and posting on t...jeroendedauw21:34, 11 February 2012

Comments

#Comment by Platonides (talk | contribs)   16:33, 12 February 2012

What about making abstract those functions that are only throwing exceptions now?

#Comment by Jeroen De Dauw (talk | contribs)   16:37, 12 February 2012

You cannot make static methods abstract. Would have been nicer, but in the end it does not matter much.

#Comment by Jeroen De Dauw (talk | contribs)   19:17, 14 February 2012

Abstract and non-static in r111468

Status & tagging log