r59337 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59336‎ | r59337 | r59338 >
Date:16:09, 22 November 2009
Author:kipcool
Status:deferred
Tags:
Comment:
Faster sql query for attributes
Modified paths:
  • /trunk/extensions/Wikidata/OmegaWiki/SpecialSuggest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/OmegaWiki/SpecialSuggest.php
@@ -238,14 +238,64 @@
239239 return $sql;
240240 }
241241
 242+/**
 243+ * Returns the list of attributes of a given $attributesType (DM, TEXT, TRNS, URL, OPTN)
 244+ * in the user language or in English
 245+ *
 246+ * @param $language the 2 letter wikimedia code
 247+ */
 248+
242249 function getSQLToSelectPossibleAttributes( $definedMeaningId, $attributesLevel, $annotationAttributeId, $attributesType ) {
243 - global
244 - $wgUser;
245 -
246 - $sqlActual = getSQLToSelectPossibleAttributesForLanguage( $definedMeaningId, $attributesLevel, $annotationAttributeId, $attributesType, $wgUser->getOption( 'language' ) );
247 - $sqlFallback = getSQLToSelectPossibleAttributesForLanguage( $definedMeaningId, $attributesLevel, $annotationAttributeId, $attributesType, 'en' );
248 -
249 - return constructSQLWithFallback( $sqlActual, $sqlFallback, array( "attribute_mid", "spelling" ) );
 250+
 251+ global $wgDefaultClassMids;
 252+ global $wgUser;
 253+ $dc = wdGetDataSetContext();
 254+ $dbr =& wfGetDB( DB_SLAVE );
 255+
 256+ $language = $wgUser->getOption( 'language' ) ;
 257+ $lng = ' ( SELECT language_id FROM language WHERE wikimedia_key = ' . $dbr->addQuotes( $language ) . ' ) ';
 258+
 259+ if ( count( $wgDefaultClassMids ) > 0 )
 260+ $defaultClassRestriction = " OR {$dc}_class_attributes.class_mid IN (" . join( $wgDefaultClassMids, ", " ) . ")";
 261+ else
 262+ $defaultClassRestriction = "";
 263+
 264+ $filteredAttributesRestriction = getFilteredAttributesRestriction( $annotationAttributeId );
 265+
 266+ $sql =
 267+ 'SELECT attribute_mid, MAX(spelling) as spelling FROM (' .
 268+ 'SELECT attribute_mid, spelling' .
 269+ " FROM {$dc}_bootstrapped_defined_meanings, {$dc}_class_attributes, {$dc}_syntrans, {$dc}_expression" .
 270+ " WHERE {$dc}_bootstrapped_defined_meanings.name = " . $dbr->addQuotes( $attributesLevel ) .
 271+ " AND {$dc}_bootstrapped_defined_meanings.defined_meaning_id = {$dc}_class_attributes.level_mid" .
 272+ " AND {$dc}_class_attributes.attribute_type = " . $dbr->addQuotes( $attributesType ) .
 273+ " AND {$dc}_syntrans.defined_meaning_id = {$dc}_class_attributes.attribute_mid" .
 274+ " AND {$dc}_expression.expression_id = {$dc}_syntrans.expression_id" .
 275+ $filteredAttributesRestriction . " ";
 276+
 277+ $sql .=
 278+ " AND ( language_id=$lng " .
 279+ ' OR ( ' .
 280+ ' language_id=85 ' .
 281+ " AND {$dc}_syntrans.defined_meaning_id NOT IN ( SELECT defined_meaning_id FROM {$dc}_syntrans synt, {$dc}_expression exp WHERE exp.expression_id = synt.expression_id AND exp.language_id=$lng ) " .
 282+ ' ) ) ' ;
 283+
 284+ $sql .=
 285+ ' AND ' . getLatestTransactionRestriction( "{$dc}_class_attributes" ) .
 286+ ' AND ' . getLatestTransactionRestriction( "{$dc}_expression" ) .
 287+ ' AND ' . getLatestTransactionRestriction( "{$dc}_syntrans" ) .
 288+ " AND ({$dc}_class_attributes.class_mid IN (" .
 289+ ' SELECT class_mid ' .
 290+ " FROM {$dc}_class_membership" .
 291+ " WHERE {$dc}_class_membership.class_member_mid = " . $definedMeaningId .
 292+ ' AND ' . getLatestTransactionRestriction( "{$dc}_class_membership" ) .
 293+ ' )' .
 294+ $defaultClassRestriction .
 295+ ')';
 296+
 297+ $sql .= ') AS filtered GROUP BY attribute_mid';
 298+
 299+ return $sql ;
250300 }
251301
252302 function getPropertyToColumnFilterForAttribute( $annotationAttributeId ) {
@@ -310,65 +360,7 @@
311361 return $result;
312362 }
313363
314 -# language is the 2 letter wikimedia code. use "<ANY>" if you don't want language filtering
315 -# (any does set limit 1 hmph)
316 -function getSQLToSelectPossibleAttributesForLanguage( $definedMeaningId, $attributesLevel, $annotationAttributeId, $attributesType, $language = "<ANY>" ) {
317 - global $wgDefaultClassMids;
318 - global $wgUser;
319 - $dc = wdGetDataSetContext();
320364
321 - if ( count( $wgDefaultClassMids ) > 0 )
322 - $defaultClassRestriction = " OR {$dc}_class_attributes.class_mid IN (" . join( $wgDefaultClassMids, ", " ) . ")";
323 - else
324 - $defaultClassRestriction = "";
325 -
326 - $filteredAttributesRestriction = getFilteredAttributesRestriction( $annotationAttributeId );
327 -
328 - $dbr =& wfGetDB( DB_SLAVE );
329 - $sql =
330 - 'SELECT attribute_mid, MAX(spelling) as spelling FROM (' .
331 - 'SELECT attribute_mid, spelling' .
332 - " FROM {$dc}_bootstrapped_defined_meanings, {$dc}_class_attributes, {$dc}_syntrans, {$dc}_expression" .
333 - " WHERE {$dc}_bootstrapped_defined_meanings.name = " . $dbr->addQuotes( $attributesLevel ) .
334 - " AND {$dc}_bootstrapped_defined_meanings.defined_meaning_id = {$dc}_class_attributes.level_mid" .
335 - " AND {$dc}_class_attributes.attribute_type = " . $dbr->addQuotes( $attributesType ) .
336 - " AND {$dc}_syntrans.defined_meaning_id = {$dc}_class_attributes.attribute_mid" .
337 - " AND {$dc}_expression.expression_id = {$dc}_syntrans.expression_id" .
338 - $filteredAttributesRestriction . " ";
339 -
340 - if ( $language != "<ANY>" ) {
341 - $sql .=
342 - ' AND language_id=( ' .
343 - ' SELECT language_id' .
344 - ' FROM language' .
345 - ' WHERE wikimedia_key = ' . $dbr->addQuotes( $language ) .
346 - ' )';
347 - }
348 -
349 - $sql .=
350 - ' AND ' . getLatestTransactionRestriction( "{$dc}_class_attributes" ) .
351 - ' AND ' . getLatestTransactionRestriction( "{$dc}_expression" ) .
352 - ' AND ' . getLatestTransactionRestriction( "{$dc}_syntrans" ) .
353 - " AND ({$dc}_class_attributes.class_mid IN (" .
354 - ' SELECT class_mid ' .
355 - " FROM {$dc}_class_membership" .
356 - " WHERE {$dc}_class_membership.class_member_mid = " . $definedMeaningId .
357 - ' AND ' . getLatestTransactionRestriction( "{$dc}_class_membership" ) .
358 - ' )' .
359 - $defaultClassRestriction .
360 - ')';
361 -
362 - $sql .= ') AS filtered GROUP BY attribute_mid';
363 -
364 - // if ($language="<ANY>") {
365 - // $sql .=
366 - // ' LIMIT 1 ';
367 - // }
368 -
369 -
370 - return $sql;
371 -}
372 -
373365 /**
374366 * Returns the name of all classes and their spelling in the user language or in English
375367 *

Status & tagging log