00001 <?php
00009 class SDFilter {
00010 var $name;
00011 var $property;
00012 var $escaped_property;
00013 var $is_relation;
00014 var $is_boolean;
00015 var $is_date;
00016 var $category;
00017 var $time_period = null;
00018 var $input_type = null;
00019 var $allowed_values;
00020 var $possible_applied_filters = array();
00021
00022 static function loadAllFromPageSchema( $psSchemaObj ){
00023 $filters_ps = array();
00024 $template_all = $psSchemaObj->getTemplates();
00025 foreach ( $template_all as $template ) {
00026 $field_all = $template->getFields();
00027 foreach( $field_all as $fieldObj ) {
00028 $f = new SDFilter();
00029 $filter_array = $fieldObj->getObject( 'semanticdrilldown_Filter' );
00030 if ( is_null( $filter_array ) ) {
00031 continue;
00032 }
00033 if ( array_key_exists( 'name', $filter_array ) ) {
00034 $f->name = $filter_array['name'];
00035 } else {
00036 $f->name = $fieldObj->getName();
00037 }
00038 $prop_array = $fieldObj->getObject('semanticmediawiki_Property');
00039 $f->property = $prop_array['name'];
00040 $f->escaped_property = str_replace( array( ' ', "'" ), array( '_', "\'" ), $f->property );
00041 $f->is_relation = true;
00042 if ( array_key_exists( 'Type', $prop_array ) && $prop_array['Type'] != 'Page' ) {
00043 $f->is_relation = false;
00044 }
00045 if ( array_key_exists( 'InputType', $filter_array ) ) {
00046 $f->input_type = $filter_array['InputType'];
00047 }
00048 if ( array_key_exists( 'ValuesFromCategory', $filter_array ) ) {
00049 $f->category = $filter_array['ValuesFromCategory'];
00050 $f->allowed_values = SDUtils::getCategoryChildren( $f->category, false, 5 );
00051 } elseif ( array_key_exists( 'TimePeriod', $filter_array ) ) {
00052 $f->time_period = $filter_array['TimePeriod'];
00053 $f->allowed_values = array();
00054 } elseif ( $f->is_boolean ) {
00055 $f->allowed_values = array( '0', '1' );
00056 } elseif ( array_key_exists( 'Values', $filter_array ) ) {
00057 $f->allowed_values = $filter_array['Values'];
00058 } else {
00059 $f->allowed_values = array();
00060 }
00061 $filters_ps[] = $f ;
00062 }
00063 }
00064 return $filters_ps;
00065 }
00066
00067 static function load( $filter_name ) {
00068 $f = new SDFilter();
00069 $f->name = $filter_name;
00070 $properties_used = SDUtils::getValuesForProperty( $filter_name, SD_NS_FILTER, '_SD_CP', SD_SP_COVERS_PROPERTY, SMW_NS_PROPERTY );
00071 if ( count( $properties_used ) > 0 ) {
00072 $f->property = $properties_used[0];
00073 $f->escaped_property = str_replace( array( ' ', "'" ), array( '_', "\'" ), $f->property );
00074 }
00075 $f->is_relation = true;
00076 $proptitle = Title::newFromText( $f->property, SMW_NS_PROPERTY );
00077 if ( $proptitle != null ) {
00078 $store = smwfGetStore();
00079 if ( class_exists( 'SMWDIProperty' ) ) {
00080
00081 $propPage = new SMWDIWikiPage( $f->escaped_property, SMW_NS_PROPERTY, null );
00082 $types = $store->getPropertyValues( $propPage, new SMWDIProperty( '_TYPE' ) );
00083 } elseif ( class_exists( 'SMWPropertyValue' ) ) {
00084 $types = $store->getPropertyValues( $proptitle, SMWPropertyValue::makeUserProperty( 'Has type' ) );
00085 } else {
00086 $types = $store->getSpecialValues( $proptitle, SMW_SP_HAS_TYPE );
00087 }
00088 global $smwgContLang;
00089 $datatypeLabels = $smwgContLang->getDatatypeLabels();
00090 if ( count( $types ) > 0 ) {
00091 if ( $types[0] instanceof SMWDIWikiPage ) {
00092
00093 $typeValue = $types[0]->getDBkey();
00094 } elseif ( $types[0] instanceof SMWDIURI ) {
00095
00096
00097 $typeID = $types[0]->getFragment();
00098 $typeValue = $datatypeLabels[$typeID];
00099 } else {
00100 $typeValue = $types[0]->getWikiValue();
00101 }
00102 if ( $typeValue != $datatypeLabels['_wpg'] ) {
00103 $f->is_relation = false;
00104 }
00105 if ( $typeValue == $datatypeLabels['_boo'] ) {
00106 $f->is_boolean = true;
00107 }
00108 if ( $typeValue == $datatypeLabels['_dat'] ) {
00109 $f->is_date = true;
00110 }
00111 }
00112 }
00113 $categories = SDUtils::getValuesForProperty( $filter_name, SD_NS_FILTER, '_SD_VC', SD_SP_GETS_VALUES_FROM_CATEGORY, NS_CATEGORY );
00114 $time_periods = SDUtils::getValuesForProperty( $filter_name, SD_NS_FILTER, '_SD_TP', SD_SP_USES_TIME_PERIOD, null );
00115 if ( count( $categories ) > 0 ) {
00116 $f->category = $categories[0];
00117 $f->allowed_values = SDUtils::getCategoryChildren( $f->category, false, 5 );
00118 } elseif ( count( $time_periods ) > 0 ) {
00119 $f->time_period = $time_periods[0];
00120 $f->allowed_values = array();
00121 } elseif ( $f->is_boolean ) {
00122 $f->allowed_values = array( '0', '1' );
00123 } else {
00124 $values = SDUtils::getValuesForProperty( $filter_name, SD_NS_FILTER, '_SD_V', SD_SP_HAS_VALUE, null );
00125 $f->allowed_values = $values;
00126 }
00127 $input_types = SDUtils::getValuesForProperty( $filter_name, SD_NS_FILTER, '_SD_IT', SD_SP_HAS_INPUT_TYPE, null );
00128 if ( count( $input_types ) > 0 ) {
00129 $f->input_type = $input_types[0];
00130 }
00131
00132
00133 foreach ( $f->allowed_values as $allowed_value ) {
00134 $f->possible_applied_filters[] = SDAppliedFilter::create( $f, $allowed_value );
00135 }
00136 return $f;
00137 }
00138
00144 function getTimePeriodValues() {
00145 $possible_dates = array();
00146 $property_value = $this->escaped_property;
00147 $dbr = wfGetDB( DB_SLAVE );
00148 if ( $this->time_period == wfMsg( 'sd_filter_month' ) ) {
00149 $fields = "YEAR(value_xsd), MONTH(value_xsd)";
00150 } else {
00151 $fields = "YEAR(value_xsd)";
00152 }
00153 $smw_attributes = $dbr->tableName( 'smw_atts2' );
00154 $smw_ids = $dbr->tableName( 'smw_ids' );
00155 $sql = <<<END
00156 SELECT $fields, count(*)
00157 FROM semantic_drilldown_values sdv
00158 JOIN $smw_attributes a ON sdv.id = a.s_id
00159 JOIN $smw_ids p_ids ON a.p_id = p_ids.smw_id
00160 WHERE p_ids.smw_title = '$property_value'
00161 GROUP BY $fields
00162 ORDER BY $fields
00163
00164 END;
00165 $res = $dbr->query( $sql );
00166 while ( $row = $dbr->fetchRow( $res ) ) {
00167 if ( $this->time_period == wfMsg( 'sd_filter_month' ) ) {
00168 global $sdgMonthValues;
00169 $date_string = SDUtils::monthToString( $row[1] ) . " " . $row[0];
00170 $possible_dates[$date_string] = $row[2];
00171 } else {
00172 $date_string = $row[0];
00173 $possible_dates[$date_string] = $row[1];
00174 }
00175 }
00176 $dbr->freeResult( $res );
00177 return $possible_dates;
00178 }
00179
00185 function getAllValues() {
00186 if ( $this->time_period != null )
00187 return $this->getTimePeriodValues();
00188
00189 $possible_values = array();
00190 $property_value = $this->escaped_property;
00191 $dbr = wfGetDB( DB_SLAVE );
00192 if ( $this->is_relation ) {
00193 $property_table_name = $dbr->tableName( 'smw_rels2' );
00194 $property_table_nickname = "r";
00195 $value_field = 'o_ids.smw_title';
00196 } else {
00197 $property_table_name = $dbr->tableName( 'smw_atts2' );
00198 $property_table_nickname = "a";
00199 $value_field = 'value_xsd';
00200 }
00201 $smw_ids = $dbr->tableName( 'smw_ids' );
00202 $prop_ns = SMW_NS_PROPERTY;
00203 $sql = <<<END
00204 SELECT $value_field, count(DISTINCT sdv.id)
00205 FROM semantic_drilldown_values sdv
00206 JOIN $property_table_name $property_table_nickname ON sdv.id = $property_table_nickname.s_id
00207
00208 END;
00209 if ( $this->is_relation ) {
00210 $sql .= " JOIN $smw_ids o_ids ON r.o_id = o_ids.smw_id";
00211 }
00212 $sql .= <<<END
00213 JOIN $smw_ids p_ids ON $property_table_nickname.p_id = p_ids.smw_id
00214 WHERE p_ids.smw_title = '$property_value'
00215 AND p_ids.smw_namespace = $prop_ns
00216 AND $value_field != ''
00217 GROUP BY $value_field
00218 ORDER BY $value_field
00219
00220 END;
00221 $res = $dbr->query( $sql );
00222 while ( $row = $dbr->fetchRow( $res ) ) {
00223 $value_string = str_replace( '_', ' ', $row[0] );
00224 $possible_values[$value_string] = $row[1];
00225 }
00226 $dbr->freeResult( $res );
00227 return $possible_values;
00228 }
00229
00237 function createTempTable() {
00238 $dbr = wfGetDB( DB_SLAVE );
00239 $smw_ids = $dbr->tableName( 'smw_ids' );
00240 if ( $this->is_relation ) {
00241 $table_name = $dbr->tableName( 'smw_rels2' );
00242 $property_field = 'p_id';
00243 $value_field = 'o_ids.smw_title';
00244 } else {
00245 $table_name = $dbr->tableName( 'smw_atts2' );
00246 $property_field = 'p_id';
00247 $value_field = 'value_xsd';
00248 }
00249 $query_property = $this->escaped_property;
00250 $sql = <<<END
00251 CREATE TEMPORARY TABLE semantic_drilldown_filter_values
00252 AS SELECT s_id AS id, $value_field AS value
00253 FROM $table_name
00254 JOIN $smw_ids p_ids ON $table_name.p_id = p_ids.smw_id
00255
00256 END;
00257 if ( $this->is_relation ) {
00258 $sql .= " JOIN $smw_ids o_ids ON $table_name.o_id = o_ids.smw_id\n";
00259 }
00260 $sql .= " WHERE p_ids.smw_title = '$query_property'";
00261 $dbr->query( $sql );
00262 }
00263
00267 function dropTempTable() {
00268 $dbr = wfGetDB( DB_SLAVE );
00269
00270
00271 $sql = "DROP TABLE semantic_drilldown_filter_values";
00272 $dbr->query( $sql );
00273 }
00274 }