00001 <?php
00015 class SFAutocompleteAPI extends ApiBase {
00016
00017 public function __construct( $query, $moduleName ) {
00018 parent::__construct( $query, $moduleName );
00019 }
00020
00021 public function execute() {
00022 $params = $this->extractRequestParams();
00023 $substr = $params['substr'];
00024 $namespace = $params['namespace'];
00025 $property = $params['property'];
00026 $category = $params['category'];
00027 $concept = $params['concept'];
00028 $external_url = $params['external_url'];
00029 $baseprop = $params['baseprop'];
00030 $basevalue = $params['basevalue'];
00031
00032
00033 if ( is_null( $baseprop ) && strlen( $substr ) == 0 ) {
00034 $this->dieUsage( 'The substring must be specified', 'param_substr' );
00035 }
00036
00037 if ( !is_null( $baseprop ) ) {
00038 if ( !is_null( $property ) ) {
00039 $data = self::getAllValuesForProperty( $property, null, $baseprop, $basevalue );
00040 }
00041 } elseif ( !is_null( $property ) ) {
00042 $data = self::getAllValuesForProperty( $property, $substr );
00043 } elseif ( !is_null( $category ) ) {
00044 $data = SFUtils::getAllPagesForCategory( $category, 3, $substr );
00045 } elseif ( !is_null( $concept ) ) {
00046 $data = SFUtils::getAllPagesForConcept( $concept, $substr );
00047 } elseif ( !is_null( $namespace ) ) {
00048 $data = SFUtils::getAllPagesForNamespace( $namespace, $substr );
00049 } elseif ( !is_null( $external_url ) ) {
00050 $data = SFUtils::getValuesFromExternalURL( $external_url, $substr );
00051 } else {
00052 $data = array();
00053 }
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 $formattedData = array();
00065 foreach ( $data as $value ) {
00066 $formattedData[] = array( 'title' => $value );
00067 }
00068
00069
00070 $result = $this->getResult();
00071 $result->setIndexedTagName( $formattedData, 'p' );
00072 $result->addValue( null, $this->getModuleName(), $formattedData );
00073 }
00074
00075 protected function getAllowedParams() {
00076 return array (
00077 'limit' => array (
00078 ApiBase::PARAM_TYPE => 'limit',
00079 ApiBase::PARAM_DFLT => 10,
00080 ApiBase::PARAM_MIN => 1,
00081 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00082 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00083 ),
00084 'substr' => null,
00085 'property' => null,
00086 'category' => null,
00087 'concept' => null,
00088 'namespace' => null,
00089 'external_url' => null,
00090 'baseprop' => null,
00091 'basevalue' => null,
00092 );
00093 }
00094
00095 protected function getParamDescription() {
00096 return array (
00097 'substr' => 'Search substring',
00098 'property' => 'Semantic property for which to search values',
00099 'category' => 'Category for which to search values',
00100 'concept' => 'Concept for which to search values',
00101 'namespace' => 'Namespace for which to search values',
00102 'external_url' => 'Alias for external URL from which to get values',
00103 'baseprop' => 'A previous property in the form to check against',
00104 'basevalue' => 'The value to check for the previous property',
00105
00106 );
00107 }
00108
00109 protected function getDescription() {
00110 return 'Autocompletion call used by the Semantic Forms extension (http://www.mediawiki.org/Extension:Semantic_Forms)';
00111 }
00112
00113 protected function getExamples() {
00114 return array (
00115 'api.php?action=sfautocomplete&substr=te',
00116 'api.php?action=sfautocomplete&substr=te&property=Has_author',
00117 'api.php?action=sfautocomplete&substr=te&category=Authors',
00118 );
00119 }
00120
00121 public function getVersion() {
00122 return __CLASS__ . ': $Id: SF_AutocompleteAPI.php 112139 2012-02-22 19:52:49Z yaron $';
00123 }
00124
00125 private static function getAllValuesForProperty( $property_name, $substring, $base_property_name = null, $base_value = null ) {
00126 global $sfgMaxAutocompleteValues;
00127
00128 $values = array();
00129 $db = wfGetDB( DB_SLAVE );
00130 $sql_options = array();
00131 $sql_options['LIMIT'] = $sfgMaxAutocompleteValues;
00132
00133 $property = SMWPropertyValue::makeUserProperty( $property_name );
00134 $is_relation = ( $property->getPropertyTypeID() == '_wpg' );
00135 $property_name = str_replace( ' ', '_', $property_name );
00136 $conditions = array( 'p_ids.smw_title' => $property_name );
00137
00138 if ( $is_relation ) {
00139 $value_field = 'o_ids.smw_title';
00140 $from_clause = $db->tableName( 'smw_rels2' ) . " r JOIN " . $db->tableName( 'smw_ids' ) . " p_ids ON r.p_id = p_ids.smw_id JOIN " . $db->tableName( 'smw_ids' ) . " o_ids ON r.o_id = o_ids.smw_id";
00141 } else {
00142 $value_field = 'a.value_xsd';
00143 $from_clause = $db->tableName( 'smw_atts2' ) . " a JOIN " . $db->tableName( 'smw_ids' ) . " p_ids ON a.p_id = p_ids.smw_id";
00144 }
00145
00146 if ( !is_null( $base_property_name ) ) {
00147 $base_property = SMWPropertyValue::makeUserProperty( $base_property_name );
00148 $base_is_relation = ( $base_property->getPropertyTypeID() == '_wpg' );
00149
00150 $base_property_name = str_replace( ' ', '_', $base_property_name );
00151 $conditions['base_p_ids.smw_title'] = $base_property_name;
00152 $main_prop_alias = ( $is_relation ) ? 'r' : 'a';
00153 if ( $base_is_relation ) {
00154 $from_clause .= " JOIN " . $db->tableName( 'smw_rels2' ) . " r_base ON $main_prop_alias.s_id = r_base.s_id";
00155 $from_clause .= " JOIN " . $db->tableName( 'smw_ids' ) . " base_p_ids ON r_base.p_id = base_p_ids.smw_id JOIN " . $db->tableName( 'smw_ids' ) . " base_o_ids ON r_base.o_id = base_o_ids.smw_id";
00156 $base_value = str_replace( ' ', '_', $base_value );
00157 $conditions['base_o_ids.smw_title'] = $base_value;
00158 } else {
00159 $from_clause .= " JOIN " . $db->tableName( 'smw_atts2' ) . " a_base ON $main_prop_alias.s_id = a_base.s_id";
00160 $from_clause .= " JOIN " . $db->tableName( 'smw_ids' ) . " base_p_ids ON a_base.p_id = base_p_ids.smw_id";
00161 $conditions['a_base.value_xsd'] = $base_value;
00162 }
00163 }
00164
00165 if ( !is_null( $substring ) ) {
00166 $conditions[] = SFUtils::getSQLConditionForAutocompleteInColumn( $value_field, $substring );
00167 }
00168
00169 $sql_options['ORDER BY'] = $value_field;
00170 $res = $db->select( $from_clause, "DISTINCT $value_field",
00171 $conditions, __METHOD__, $sql_options );
00172
00173 while ( $row = $db->fetchRow( $res ) ) {
00174 $values[] = str_replace( '_', ' ', $row[0] );
00175 }
00176 $db->freeResult( $res );
00177
00178 return $values;
00179 }
00180
00181 }