http://www.mediawiki.org/wiki/Special:Code/MediaWiki/94077
Revision: 94077 Author: kipcool Date: 2011-08-08 19:53:11 +0000 (Mon, 08 Aug 2011) Log Message: ----------- dbr/dbw convention ; added some freeResult and limit 1 Modified Paths: -------------- trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php trunk/extensions/Wikidata/OmegaWiki/WikiDataGlobals.php Modified: trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php =================================================================== --- trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php 2011-08-08 19:38:38 UTC (rev 94076) +++ trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php 2011-08-08 19:53:11 UTC (rev 94077) @@ -1,6 +1,7 @@ <?php require_once( 'Transaction.php' ); +require_once( 'WikiDataGlobals.php' ); class Expression { public $id; @@ -54,6 +55,7 @@ while ( $syntransRecord = $dbr->fetchObject( $queryResult ) ) { $this->meaningIds[] = $syntransRecord->defined_meaning_id; } + $dbr->freeResult( $queryResult ) ; } } @@ -64,9 +66,9 @@ $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT spelling, language_id " . " FROM {$dc}_expression " . - " WHERE {$dc}_expression.expression_id=$expressionId" . - " AND " . getLatestTransactionRestriction( "{$dc}_expression" ) ); + " WHERE {$dc}_expression.expression_id=$expressionId LIMIT 1" ); $expressionRecord = $dbr->fetchObject( $queryResult ); + $dbr->freeResult( $queryResult ) ; if ( $expressionRecord ) { $expression = new Expression( $expressionId, $expressionRecord->spelling, $expressionRecord->language_id ); return $expression; @@ -78,10 +80,10 @@ function newObjectId( $table ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_objects (`table`, `UUID`) VALUES (" . $dbr->addQuotes( $table ) . ", UUID())" ); + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_objects (`table`, `UUID`) VALUES (" . $dbw->addQuotes( $table ) . ", UUID())" ); - return $dbr->insertId(); + return $dbw->insertId(); } function getTableNameWithObjectId( $objectId ) { @@ -107,7 +109,7 @@ $dbr = wfGetDB( DB_SLAVE ); $sql = "SELECT expression_id FROM {$dc}_expression " . 'WHERE spelling=binary ' . $dbr->addQuotes( $spelling ) . ' AND language_id=' . $languageId . - ' AND ' . getLatestTransactionRestriction( "{$dc}_expression" ); + ' AND ' . getLatestTransactionRestriction( "{$dc}_expression" ) . " LIMIT 1 " ; $queryResult = $dbr->query( $sql ); $expression = $dbr->fetchObject( $queryResult ); return isset( $expression->expression_id ) ? $expression->expression_id : null; @@ -119,10 +121,10 @@ $expressionId = newObjectId( "{$dc}_expression" ); - $dbr = wfGetDB( DB_MASTER ); - $spelling = $dbr->addQuotes( $spelling ); + $dbw = wfGetDB( DB_MASTER ); + $spelling = $dbw->addQuotes( $spelling ); - $dbr->query( "INSERT INTO {$dc}_expression(expression_id, spelling, language_id, add_transaction_id) values($expressionId, $spelling, $languageId, " . getUpdateTransactionId() . ")" ); + $dbw->query( "INSERT INTO {$dc}_expression(expression_id, spelling, language_id, add_transaction_id) values($expressionId, $spelling, $languageId, " . getUpdateTransactionId() . ")" ); return $expressionId; } @@ -133,48 +135,47 @@ function createPage( $namespace, $title ) { - $dbr = wfGetDB( DB_MASTER ); - $title = $dbr->addQuotes( $title ); - $timestamp = $dbr->timestamp(); + $dbw = wfGetDB( DB_MASTER ); + $title = $dbw->addQuotes( $title ); + $timestamp = $dbw->timestamp(); $sql = "select page_id from page where page_namespace = $namespace and page_title = $title"; - $queryResult = $dbr->query( $sql ); - $page = $dbr->fetchObject( $queryResult ); + $queryResult = $dbw->query( $sql ); + $page = $dbw->fetchObject( $queryResult ); if ( isset( $page->page_id ) ) { return $page->page_id; } else { $sql = "insert into page(page_namespace,page_title,page_is_new,page_touched) " . "values($namespace, $title, 1, $timestamp)"; - $dbr->query( $sql ); - return $dbr->insertId(); + $dbw->query( $sql ); + return $dbw->insertId(); } } function setPageLatestRevision( $pageId, $latestRevision ) { - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "update page set page_latest=$latestRevision where page_id=$pageId"; - $dbr->query( $sql ); + $dbw->query( $sql ); } function createInitialRevisionForPage( $pageId, $comment ) { - global - $wgUser; + global $wgUser; - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $userId = $wgUser->getID(); - $userName = $dbr->addQuotes( $wgUser->getName() ); - $comment = $dbr->addQuotes( $comment ); - $timestamp = $dbr->timestamp(); + $userName = $dbw->addQuotes( $wgUser->getName() ); + $comment = $dbw->addQuotes( $comment ); + $timestamp = $dbw->timestamp(); $sql = "insert into revision(rev_page,rev_comment,rev_user,rev_user_text,rev_timestamp) " . "values($pageId, $comment, $userId, $userName, $timestamp)"; - $dbr->query( $sql ); + $dbw->query( $sql ); - $revisionId = $dbr->insertId(); + $revisionId = $dbw->insertId(); setPageLatestRevision( $pageId, $revisionId ); return $revisionId; } - + function findExpression( $spelling, $languageId ) { if ( $expressionId = getExpressionId( $spelling, $languageId ) ) { return new Expression( $expressionId, $spelling, $languageId ); @@ -200,13 +201,12 @@ return createExpression( $spelling, $languageId ); } - - function getSynonymId( $definedMeaningId, $expressionId ) { $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT syntrans_sid FROM {$dc}_syntrans " . - "WHERE defined_meaning_id=$definedMeaningId AND expression_id=$expressionId LIMIT 1" ); + "WHERE defined_meaning_id=$definedMeaningId AND expression_id=$expressionId " . + getLatestTransactionRestriction( "{$dc}_syntrans" ) . " LIMIT 1" ); if ( $synonym = $dbr->fetchObject( $queryResult ) ) return $synonym->syntrans_sid; @@ -223,16 +223,15 @@ if ( $synonymId == 0 ) $synonymId = newObjectId( "{$dc}_syntrans" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $identicalMeaningInteger = (int) $identicalMeaning; $sql = "insert into {$dc}_syntrans(syntrans_sid, defined_meaning_id, expression_id, identical_meaning, add_transaction_id) " . "values($synonymId, $definedMeaningId, $expressionId, $identicalMeaningInteger, " . getUpdateTransactionId() . ")"; - $queryResult = $dbr->query( $sql ); + $queryResult = $dbw->query( $sql ); } function expressionIsBoundToDefinedMeaning( $definedMeaningId, $expressionId ) { - global - $dataSet; + global $dataSet; $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); @@ -285,7 +284,7 @@ $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT meaning1_mid FROM {$dc}_meaning_relations " . "WHERE meaning1_mid=$definedMeaning1Id AND meaning2_mid=$definedMeaning2Id AND relationtype_mid=$relationTypeId " . - "AND " . getLatestTransactionRestriction( "{$dc}_meaning_relations" ) ); + "AND " . getLatestTransactionRestriction( "{$dc}_meaning_relations" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; } @@ -298,10 +297,10 @@ if ( $relationId == 0 ) $relationId = newObjectId( "{$dc}_meaning_relations" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "INSERT INTO {$dc}_meaning_relations(relation_id, meaning1_mid, meaning2_mid, relationtype_mid, add_transaction_id) " . " VALUES ($relationId, $definedMeaning1Id, $definedMeaning2Id, $relationTypeId, " . getUpdateTransactionId() . ")"; - $dbr->query( $sql ); + $dbw->query( $sql ); } function addRelation( $definedMeaning1Id, $relationTypeId, $definedMeaning2Id ) { @@ -311,16 +310,16 @@ function removeRelation( $definedMeaning1Id, $relationTypeId, $definedMeaning2Id ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_meaning_relations SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_meaning_relations SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE meaning1_mid=$definedMeaning1Id AND meaning2_mid=$definedMeaning2Id AND relationtype_mid=$relationTypeId " . " AND remove_transaction_id IS NULL" ); } function removeRelationWithId( $relationId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_meaning_relations SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_meaning_relations SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE relation_id=$relationId " . " AND remove_transaction_id IS NULL" ); } @@ -336,13 +335,13 @@ * @param unknown_type $relationTypeId dmid of the relationtype, optional. * @param unknown_type $lhs dmid of the left hand side, optional. * @param unknown_type $dmId dmid of the right hand side, optional. - * * @param unknown_type $dc the dataset, optional + * @param unknown_type $dc the dataset, optional */ function getRelationDefinedMeanings( $relationTypeId = null, $lhs = null, $rhs = null, $dc = null ) { $result = array(); $dc = wdGetDataSetContext( $dc ); - $dbr = wfGetDB( DB_MASTER ); + $dbr = wfGetDB( DB_SLAVE ); if ( $relationTypeId == null ) { if ( $lhs == null ) { if ( $rhs == null ) return $result; @@ -390,7 +389,8 @@ while ( $row = $dbr->fetchRow( $queryResult ) ) { $result[] = $row[0]; } - + $dbr->freeResult( $queryResult ) ; + return $result; } @@ -404,8 +404,8 @@ $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT object_id FROM {$dc}_class_attributes" . " WHERE class_mid=$classMeaningId AND level_mid=$levelMeaningId AND attribute_mid=$attributeMeaningId AND attribute_type = " . $dbr->addQuotes( $attributeType ) . - ' AND ' . getLatestTransactionRestriction( "{$dc}_class_attributes" ) ); - + ' AND ' . getLatestTransactionRestriction( "{$dc}_class_attributes" ) . " LIMIT 1" ); + return $dbr->numRows( $queryResult ) > 0; } @@ -417,10 +417,10 @@ if ( $objectId == 0 ) $objectId = newObjectId( "{$dc}_class_attributes" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "INSERT INTO {$dc}_class_attributes(object_id, class_mid, level_mid, attribute_mid, attribute_type, add_transaction_id) " . - " VALUES ($objectId, $classMeaningId, $levelMeaningId, $attributeMeaningId, " . $dbr->addQuotes( $attributeType ) . ', ' . getUpdateTransactionId() . ')'; - $dbr->query( $sql ); + " VALUES ($objectId, $classMeaningId, $levelMeaningId, $attributeMeaningId, " . $dbw->addQuotes( $attributeType ) . ', ' . getUpdateTransactionId() . ')'; + $dbw->query( $sql ); } function getClassAttributeId( $classMeaningId, $levelMeaningId, $attributeMeaningId, $attributeType ) { @@ -437,8 +437,8 @@ function removeClassAttributeWithId( $classAttributeId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_class_attributes SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_class_attributes SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE object_id=$classAttributeId " . " AND remove_transaction_id IS NULL" ); } @@ -462,10 +462,10 @@ if ( $classMembershipId == 0 ) $classMembershipId = newObjectId( "{$dc}_class_membership" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "INSERT INTO {$dc}_class_membership(class_membership_id, class_mid, class_member_mid, add_transaction_id) " . "VALUES ($classMembershipId, $classId, $classMemberId, " . getUpdateTransactionId() . ")"; - $dbr->query( $sql ); + $dbw->query( $sql ); } function classMembershipExists( $classMemberId, $classId ) { @@ -473,7 +473,7 @@ $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT class_member_mid FROM {$dc}_class_membership " . "WHERE class_mid=$classId AND class_member_mid=$classMemberId " . - "AND " . getLatestTransactionRestriction( "{$dc}_class_membership" ) ); + "AND " . getLatestTransactionRestriction( "{$dc}_class_membership" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; } @@ -485,8 +485,8 @@ function removeClassMembership( $classMemberId, $classId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_class_membership SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_class_membership SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE class_mid=$classId AND class_member_mid=$classMemberId " . " AND remove_transaction_id IS NULL" ); } @@ -494,23 +494,23 @@ function removeClassMembershipWithId( $classMembershipId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_class_membership SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_class_membership SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE class_membership_id=$classMembershipId" . " AND remove_transaction_id IS NULL" ); } function removeSynonymOrTranslation( $definedMeaningId, $expressionId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_syntrans SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_syntrans SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE defined_meaning_id=$definedMeaningId AND expression_id=$expressionId AND remove_transaction_id IS NULL LIMIT 1" ); } function removeSynonymOrTranslationWithId( $syntransId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_syntrans SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_syntrans SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE syntrans_sid=$syntransId AND remove_transaction_id IS NULL LIMIT 1" ); } @@ -524,7 +524,7 @@ $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT defined_meaning_id, expression_id" . " FROM {$dc}_syntrans" . - " WHERE syntrans_sid=$syntransId AND remove_transaction_id IS NULL" ); + " WHERE syntrans_sid=$syntransId AND remove_transaction_id IS NULL LIMIT 1" ); if ( $syntrans = $dbr->fetchObject( $queryResult ) ) updateSynonymOrTranslation( $syntrans->defined_meaning_id, $syntrans->expression_id, $identicalMeaning ); @@ -553,22 +553,22 @@ function createText( $text ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $text = $dbr->addQuotes( $text ); + $dbw = wfGetDB( DB_MASTER ); + $text = $dbw->addQuotes( $text ); $sql = "insert into {$dc}_text(text_text) values($text)"; - $dbr->query( $sql ); + $dbw->query( $sql ); - return $dbr->insertId(); + return $dbw->insertId(); } function createTranslatedContent( $translatedContentId, $languageId, $textId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "insert into {$dc}_translated_content(translated_content_id,language_id,text_id,add_transaction_id) values($translatedContentId, $languageId, $textId, " . getUpdateTransactionId() . ")"; - $dbr->query( $sql ); + $dbw->query( $sql ); - return $dbr->insertId(); + return $dbw->insertId(); } function translatedTextExists( $textId, $languageId ) { @@ -585,7 +585,7 @@ " FROM {$dc}_translated_content" . " WHERE translated_content_id=$textId" . " AND language_id=$languageId" . - " AND " . getLatestTransactionRestriction( "{$dc}_translated_content" ) + " AND " . getLatestTransactionRestriction( "{$dc}_translated_content" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; @@ -605,15 +605,15 @@ $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT meaning_text_tcid FROM {$dc}_defined_meaning WHERE defined_meaning_id=$definedMeaningId " . - " AND " . getLatestTransactionRestriction( "{$dc}_defined_meaning" ) ); + " AND " . getLatestTransactionRestriction( "{$dc}_defined_meaning" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult )->meaning_text_tcid; } function updateDefinedMeaningDefinitionId( $definedMeaningId, $definitionId ) { - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $dc = wdGetDataSetContext(); - $dbr->query( "UPDATE {$dc}_defined_meaning SET meaning_text_tcid=$definitionId WHERE defined_meaning_id=$definedMeaningId" . + $dbw->query( "UPDATE {$dc}_defined_meaning SET meaning_text_tcid=$definitionId WHERE defined_meaning_id=$definedMeaningId" . " AND " . getLatestTransactionRestriction( "{$dc}_defined_meaning" ) ); } @@ -639,8 +639,8 @@ function createDefinedMeaningAlternativeDefinition( $definedMeaningId, $translatedContentId, $sourceMeaningId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_alt_meaningtexts (meaning_mid, meaning_text_tcid, source_id, add_transaction_id) " . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_alt_meaningtexts (meaning_mid, meaning_text_tcid, source_id, add_transaction_id) " . "VALUES ($definedMeaningId, $translatedContentId, $sourceMeaningId, " . getUpdateTransactionId() . ")" ); } @@ -653,15 +653,15 @@ function removeTranslatedText( $translatedContentId, $languageId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_translated_content SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_translated_content SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE translated_content_id=$translatedContentId AND language_id=$languageId AND remove_transaction_id IS NULL" ); } function removeTranslatedTexts( $translatedContentId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_translated_content SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_translated_content SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE translated_content_id=$translatedContentId AND remove_transaction_id IS NULL" ); } @@ -674,8 +674,8 @@ // removeTranslatedTexts($definitionId); $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_alt_meaningtexts SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_alt_meaningtexts SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE meaning_text_tcid=$definitionId AND meaning_mid=$definedMeaningId" . " AND remove_transaction_id IS NULL" ); } @@ -691,16 +691,16 @@ $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT collection_id FROM {$dc}_collection_contents WHERE collection_id=$collectionId AND member_mid=$definedMeaningId " . - "AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) ); + "AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; } function addDefinedMeaningToCollection( $definedMeaningId, $collectionId, $internalId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_collection_contents(collection_id, member_mid, internal_member_id, add_transaction_id) " . - "VALUES ($collectionId, $definedMeaningId, " . $dbr->addQuotes( $internalId ) . ", " . getUpdateTransactionId() . ")" ); + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_collection_contents(collection_id, member_mid, internal_member_id, add_transaction_id) " . + "VALUES ($collectionId, $definedMeaningId, " . $dbw->addQuotes( $internalId ) . ", " . getUpdateTransactionId() . ")" ); } function addDefinedMeaningToCollectionIfNotPresent( $definedMeaningId, $collectionId, $internalId ) { @@ -711,7 +711,7 @@ function getDefinedMeaningFromCollection( $collectionId, $internalMemberId ) { $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); - $query = "SELECT member_mid FROM {$dc}_collection_contents WHERE collection_id=$collectionId AND internal_member_id=" . $dbr->addQuotes( $internalMemberId ) . " AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ); + $query = "SELECT member_mid FROM {$dc}_collection_contents WHERE collection_id=$collectionId AND internal_member_id=" . $dbr->addQuotes( $internalMemberId ) . " AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) . " LIMIT 1" ; $queryResult = $dbr->query( $query ); if ( $definedMeaningObject = $dbr->fetchObject( $queryResult ) ) @@ -722,8 +722,8 @@ function removeDefinedMeaningFromCollection( $definedMeaningId, $collectionId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_collection_contents SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_collection_contents SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE collection_id=$collectionId AND member_mid=$definedMeaningId AND remove_transaction_id IS NULL" ); } @@ -744,7 +744,7 @@ $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT collection_mid FROM {$dc}_collection " . - " WHERE collection_id=$collectionId AND " . getLatestTransactionRestriction( "{$dc}_collection" ) ); + " WHERE collection_id=$collectionId AND " . getLatestTransactionRestriction( "{$dc}_collection" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult )->collection_mid; } @@ -753,7 +753,7 @@ $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT collection_id FROM {$dc}_collection " . - " WHERE collection_mid=$collectionMeaningId AND " . getLatestTransactionRestriction( "{$dc}_collection" ) ); + " WHERE collection_mid=$collectionMeaningId AND " . getLatestTransactionRestriction( "{$dc}_collection" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult )->collection_id; } @@ -762,28 +762,26 @@ $dc = wdGetDataSetContext(); $collectionId = newObjectId( "{$dc}_collection" ); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_collection(collection_id, collection_mid, collection_type, add_transaction_id)" . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_collection(collection_id, collection_mid, collection_type, add_transaction_id)" . " VALUES($collectionId, $definedMeaningId, '$collectionType', " . getUpdateTransactionId() . ")" ); return $collectionId; } function addDefinedMeaning( $definingExpressionId ) { - # FIXME: Replace with method for Namspace::getIndexForName. - $definedMeaningNameSpaceId = 24; $dc = wdGetDataSetContext(); $definedMeaningId = newObjectId( "{$dc}_defined_meaning" ); // wfDebug( "addDefinedMeaning(): $definedMeaningId has to be inserted to the database $dc" ); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_defined_meaning(defined_meaning_id, expression_id, add_transaction_id) values($definedMeaningId, $definingExpressionId, " . getUpdateTransactionId() . ")" ); + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_defined_meaning(defined_meaning_id, expression_id, add_transaction_id) values($definedMeaningId, $definingExpressionId, " . getUpdateTransactionId() . ")" ); // wfDebug( "addDefinedMeaning(): after $definedMeaningId has been inserted in the database" ); $expression = getExpression( $definingExpressionId ); - $pageId = createPage( $definedMeaningNameSpaceId, getPageTitle( "$expression->spelling ($definedMeaningId)" ) ); + $pageId = createPage( NS_DEFINEDMEANING, getPageTitle( "$expression->spelling ($definedMeaningId)" ) ); createInitialRevisionForPage( $pageId, 'Created by adding defined meaning' ); return $definedMeaningId; @@ -807,17 +805,17 @@ function createTextAttributeValue( $textValueAttributeId, $objectId, $textAttributeId, $text ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_text_attribute_values (value_id, object_id, attribute_mid, text, add_transaction_id) " . - "VALUES ($textValueAttributeId, $objectId, $textAttributeId, " . $dbr->addQuotes( $text ) . ", " . getUpdateTransactionId() . ")" + "VALUES ($textValueAttributeId, $objectId, $textAttributeId, " . $dbw->addQuotes( $text ) . ", " . getUpdateTransactionId() . ")" ); } function removeTextAttributeValue( $textValueAttributeId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "UPDATE {$dc}_text_attribute_values SET remove_transaction_id=" . getUpdateTransactionId() . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_text_attribute_values SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE value_id=$textValueAttributeId" . " AND remove_transaction_id IS NULL" ); } @@ -835,7 +833,7 @@ "SELECT object_id, attribute_mid, text" . " FROM {$dc}_text_attribute_values" . " WHERE value_id=$textValueAttributeId " . - " AND " . getLatestTransactionRestriction( "{$dc}_text_attribute_values" ) + " AND " . getLatestTransactionRestriction( "{$dc}_text_attribute_values" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult ); @@ -849,17 +847,17 @@ function createLinkAttributeValue( $linkValueAttributeId, $objectId, $linkAttributeId, $url, $label = "" ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_url_attribute_values (value_id, object_id, attribute_mid, url, label, add_transaction_id) " . - "VALUES ($linkValueAttributeId, $objectId, $linkAttributeId, " . $dbr->addQuotes( $url ) . ", " . $dbr->addQuotes( $label ) . ", " . getUpdateTransactionId() . ")" + "VALUES ($linkValueAttributeId, $objectId, $linkAttributeId, " . $dbw->addQuotes( $url ) . ", " . $dbw->addQuotes( $label ) . ", " . getUpdateTransactionId() . ")" ); } function removeLinkAttributeValue( $linkValueAttributeId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_url_attribute_values SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE value_id=$linkValueAttributeId" . " AND remove_transaction_id IS NULL" @@ -878,7 +876,7 @@ $queryResult = $dbr->query( "SELECT object_id, attribute_mid, url" . " FROM {$dc}_url_attribute_values WHERE value_id=$linkValueAttributeId " . - " AND " . getLatestTransactionRestriction( "{$dc}_url_attribute_values" ) + " AND " . getLatestTransactionRestriction( "{$dc}_url_attribute_values" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult ); @@ -886,8 +884,8 @@ function createTranslatedTextAttributeValue( $valueId, $objectId, $attributeId, $translatedContentId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( "INSERT INTO {$dc}_translated_content_attribute_values (value_id, object_id, attribute_mid, value_tcid, add_transaction_id) " . + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "INSERT INTO {$dc}_translated_content_attribute_values (value_id, object_id, attribute_mid, value_tcid, add_transaction_id) " . "VALUES ($valueId, $objectId, $attributeId, $translatedContentId, " . getUpdateTransactionId() . ")" ); } @@ -904,7 +902,7 @@ $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT value_id, object_id, attribute_mid, value_tcid FROM {$dc}_translated_content_attribute_values WHERE value_id=$valueId " . - " AND " . getLatestTransactionRestriction( "{$dc}_translated_content_attribute_values" ) ); + " AND " . getLatestTransactionRestriction( "{$dc}_translated_content_attribute_values" ) . " LIMIT 1" ); return $dbr->fetchObject( $queryResult ); } @@ -920,8 +918,8 @@ // back easier. // removeTranslatedTexts($translatedTextAttribute->value_tcid); - $dbr = wfGetDB( DB_MASTER ); - $dbr->query( + $dbw = wfGetDB( DB_MASTER ); + $dbw->query( "UPDATE {$dc}_translated_content_attribute_values" . " SET remove_transaction_id=" . getUpdateTransactionId() . " WHERE value_id=$valueId" . @@ -935,7 +933,7 @@ $queryResult = $dbr->query( "SELECT value_id FROM {$dc}_option_attribute_values" . ' WHERE object_id = ' . $objectId . ' AND option_id = ' . $optionId . - ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_values" ) ); + ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_values" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; } @@ -948,23 +946,23 @@ $dc = wdGetDataSetContext(); $valueId = newObjectId( "{$dc}_option_attribute_values" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "INSERT INTO {$dc}_option_attribute_values(value_id,object_id,option_id,add_transaction_id)" . ' VALUES(' . $valueId . ',' . $objectId . ',' . $optionId . ',' . getUpdateTransactionId() . ')'; - $dbr->query( $sql ); + $dbw->query( $sql ); } function removeOptionAttributeValue( $valueId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "UPDATE {$dc}_option_attribute_values" . ' SET remove_transaction_id = ' . getUpdateTransactionId() . ' WHERE value_id = ' . $valueId . ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_values" ); - $dbr->query( $sql ); + $dbw->query( $sql ); } function optionAttributeOptionExists( $attributeId, $optionMeaningId, $languageId ) { @@ -974,7 +972,7 @@ ' WHERE attribute_id = ' . $attributeId . ' AND option_mid = ' . $optionMeaningId . ' AND language_id = ' . $languageId . - ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_options" ) ); + ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_options" ) . " LIMIT 1" ); return $dbr->numRows( $queryResult ) > 0; } @@ -987,27 +985,27 @@ $dc = wdGetDataSetContext(); $optionId = newObjectId( "{$dc}_option_attribute_options" ); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); $sql = "INSERT INTO {$dc}_option_attribute_options(option_id,attribute_id,option_mid,language_id,add_transaction_id)" . ' VALUES(' . $optionId . ',' . $attributeId . ',' . $optionMeaningId . ',' . $languageId . ',' . getUpdateTransactionId() . ')'; - $dbr->query( $sql ); + $dbw->query( $sql ); } function removeOptionAttributeOption( $optionId ) { $dc = wdGetDataSetContext(); - $dbr = wfGetDB( DB_MASTER ); + $dbw = wfGetDB( DB_MASTER ); // first check if the option attribute option is still in use $sql = "SELECT * FROM {$dc}_option_attribute_values" . ' WHERE option_id = ' . $optionId . - ' AND remove_transaction_id IS NULL' ; - $queryResult = $dbr->query( $sql ); + ' AND remove_transaction_id IS NULL LIMIT 1' ; + $queryResult = $dbw->query( $sql ); - if ( $dbr->numRows( $queryResult ) > 0 ) { + if ( $dbw->numRows( $queryResult ) > 0 ) { echo "\nThe option $optionId cannot be deleted because it is still in use!\n" ; } else { // option not used, can proceed to delete @@ -1016,7 +1014,7 @@ ' SET remove_transaction_id = ' . $transactionId . ' WHERE option_id = ' . $optionId . ' AND ' . getLatestTransactionRestriction( "{$dc}_option_attribute_options" ); - $dbr->query( $sql ); + $dbw->query( $sql ); } // alternatively to checking if the attribute option is in use @@ -1040,7 +1038,6 @@ function getDefinedMeaningDefinitionForLanguage( $definedMeaningId, $languageId, $dc = null ) { if ( is_null( $dc ) ) { $dc = wdGetDataSetContext(); - } $dbr = wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( "SELECT text_text FROM {$dc}_defined_meaning as dm, {$dc}_translated_content as tc, {$dc}_text as t " . @@ -1048,7 +1045,7 @@ " AND " . getLatestTransactionRestriction( 'dm' ) . " AND " . getLatestTransactionRestriction( 'tc' ) . " AND dm.meaning_text_tcid=tc.translated_content_id AND tc.language_id=$languageId " . - " AND t.text_id=tc.text_id" ); + " AND t.text_id=tc.text_id LIMIT 1" ); if ( $definition = $dbr->fetchObject( $queryResult ) ) return $definition->text_text; @@ -1115,17 +1112,17 @@ $definedMeaningId = $dbr->addQuotes( $definedMeaningId ); $userLanguageId = $dbr->addQuotes( $userLanguageId ); - $fallbackLanguageId = $dbr->addQuotes( $fallbackLanguageId ); if ( $userLanguageId ) { - $actual_query = "select spelling from {$dc}_syntrans,{$dc}_expression where {$dc}_syntrans.defined_meaning_id=$definedMeaningId and {$dc}_expression.expression_id={$dc}_syntrans.expression_id and language_id=$userLanguageId and {$dc}_expression.remove_transaction_id is NULL"; + $actual_query = "select spelling from {$dc}_syntrans,{$dc}_expression where {$dc}_syntrans.defined_meaning_id=$definedMeaningId and {$dc}_expression.expression_id={$dc}_syntrans.expression_id and language_id=$userLanguageId and {$dc}_expression.remove_transaction_id is NULL LIMIT 1"; $res = $dbr->query( $actual_query ); $row = $dbr->fetchObject( $res ); if ( isset( $row->spelling ) ) return $row->spelling; } - $fallback_query = "select spelling from {$dc}_syntrans,{$dc}_expression where {$dc}_syntrans.defined_meaning_id=$definedMeaningId and {$dc}_expression.expression_id={$dc}_syntrans.expression_id and language_id=$fallbackLanguageId and {$dc}_expression.remove_transaction_id is NULL"; + $fallbackLanguageId = $dbr->addQuotes( $fallbackLanguageId ); + $fallback_query = "select spelling from {$dc}_syntrans,{$dc}_expression where {$dc}_syntrans.defined_meaning_id=$definedMeaningId and {$dc}_expression.expression_id={$dc}_syntrans.expression_id and language_id=$fallbackLanguageId and {$dc}_expression.remove_transaction_id is NULL LIMIT 1"; $res = $dbr->query( $fallback_query ); $row = $dbr->fetchObject( $res ); @@ -1153,7 +1150,7 @@ " FROM ({$dc}_collection_contents INNER JOIN {$dc}_collection ON {$dc}_collection.collection_id = {$dc}_collection_contents.collection_id) " . " WHERE {$dc}_collection_contents.member_mid = $objectId AND {$dc}_collection.collection_type = 'CLAS' " . " AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) . " " . - " AND " . getLatestTransactionRestriction( "{$dc}_collection" ); + " AND " . getLatestTransactionRestriction( "{$dc}_collection" ) . " LIMIT 1" ; $queryResult = $dbr->query( $query ); $result = $dbr->numRows( $queryResult ) > 0; @@ -1178,9 +1175,8 @@ } function getCollectionContents( $collectionId ) { - global - $dataSet; - + global $dataSet; + $dc = wdGetDataSetContext(); $dbr = & wfGetDB( DB_SLAVE ); $queryResult = $dbr->query( @@ -1217,7 +1213,8 @@ while ( $row = $dbr->fetchRow( $result ) ) { $memberMids[] = $row['member_mid']; } - + $dbr->freeResult( $result ) ; + return $memberMids; } @@ -1229,7 +1226,7 @@ " FROM {$dc}_collection_contents " . " WHERE collection_id=$collectionId" . " AND internal_member_id=" . $dbr->addQuotes( $sourceIdentifier ) . - " AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) + " AND " . getLatestTransactionRestriction( "{$dc}_collection_contents" ) . " LIMIT 1" ); if ( $collectionEntry = $dbr->fetchObject( $queryResult ) ) @@ -1271,8 +1268,10 @@ $result = array(); - while ( $synonymRecord = $dbr->fetchObject( $queryResult ) ) - $result[] = $synonymRecord->defined_meaning_id; + while ( $synonymRecord = $dbr->fetchObject( $queryResult ) ) { + $result[] = $synonymRecord->defined_meaning_id; + } + $dbr->freeResult( $queryResult ) ; return $result; } @@ -1298,6 +1297,7 @@ $result[] = $synonymRecord->defined_meaning_id; } } + $dbr->freeResult( $queryResult ) ; return $result; } @@ -1324,7 +1324,7 @@ function getMapping( $dc, $collid, $dm_id ) { $dbr = wfGetDB( DB_SLAVE ); - $queryResult = $dbr->query( "select internal_member_id from {$dc}_collection_contents where collection_id = $collid AND member_mid = $dm_id" ); + $queryResult = $dbr->query( "select internal_member_id from {$dc}_collection_contents where collection_id = $collid AND member_mid = $dm_id LIMIT 1" ); if ( $record = $dbr->fetchObject( $queryResult ) ) { return $record->internal_member_id; } @@ -1371,7 +1371,7 @@ /** this funtion assumes that there is only a single mapping collection */ function getCollectionIdForDC( $dc ) { - $dbr = & wfGetDB( DB_SLAVE ); + $dbr = & wfGetDB( DB_SLAVE ); $query = " SELECT collection_id FROM {$dc}_collection WHERE collection_type=\"MAPP\" @@ -1391,12 +1391,12 @@ // if(is_null($dc)) { // $dc=wdGetDataSetContext(); // } - $dbr = & wfGetDB( DB_SLAVE ); + $dbw = & wfGetDB( DB_MASTER ); $collection_contents = "{$dc}_collection_contents"; - $collid = $dbr->addQuotes( $collid ); - $uuid = $dbr->addQuotes( $uuid ); - $dm_id = $dbr->addQuotes( $dm_id ); + $collid = $dbw->addQuotes( $collid ); + $uuid = $dbw->addQuotes( $uuid ); + $dm_id = $dbw->addQuotes( $dm_id ); $add_transaction_id = $override_transaction; if ( is_null( $add_transaction_id ) ) { @@ -1411,7 +1411,7 @@ member_mid=$dm_id, add_transaction_id=$add_transaction_id "; - $result = $dbr->query( $sql ); + $result = $dbw->query( $sql ); } /**read a ConceptMapping from the database. @@ -1440,7 +1440,7 @@ $query = " SELECT member_mid FROM $collection_contents WHERE collection_id = $collection_id - AND internal_member_id=$concept_id + AND internal_member_id=$concept_id LIMIT 1 "; $queryResult = $dbr->query( $query ); $row = $dbr->fetchObject( $queryResult ); @@ -1462,7 +1462,7 @@ SELECT internal_member_id AS concept_id FROM {$dc}_collection_contents WHERE member_mid=$dm - AND collection_id=$collection_id; + AND collection_id=$collection_id LIMIT 1 "; $queryResult = $dbr->query( $query ); $row = $dbr->fetchObject( $queryResult ); @@ -1506,7 +1506,7 @@ " WHERE {$dc}_defined_meaning.defined_meaning_id=$definedMeaningId " . " AND {$dc}_expression.expression_id={$dc}_defined_meaning.expression_id" . " AND " . getLatestTransactionRestriction( "{$dc}_defined_meaning" ) . - " AND " . getLatestTransactionRestriction( "{$dc}_expression" ) ); + " AND " . getLatestTransactionRestriction( "{$dc}_expression" ) . " LIMIT 1" ); $expression = $dbr->fetchObject( $queryResult ); return array( $expression->expression_id, $expression->spelling, $expression->language_id ); } @@ -1521,7 +1521,7 @@ " WHERE {$dc}_defined_meaning.defined_meaning_id=$definedMeaningId " . " AND {$dc}_expression.expression_id={$dc}_defined_meaning.expression_id" . " AND " . getLatestTransactionRestriction( "{$dc}_defined_meaning" ) . - " AND " . getLatestTransactionRestriction( "{$dc}_expression" ) ); + " AND " . getLatestTransactionRestriction( "{$dc}_expression" ) . " LIMIT 1" ); $expression = $dbr->fetchObject( $queryResult ); if ( $expression ) { return $expression->spelling; @@ -1616,7 +1616,7 @@ function getTextValue( $textId ) { $dc = wdGetDataSetContext(); $dbr = wfGetDB( DB_SLAVE ); - $queryResult = $dbr->query( "SELECT text_text from {$dc}_text where text_id=$textId" ); + $queryResult = $dbr->query( "SELECT text_text from {$dc}_text where text_id=$textId LIMIT 1" ); return $dbr->fetchObject( $queryResult )->text_text; } @@ -1628,12 +1628,13 @@ $dbr = wfGetDB( DB_SLAVE ); $spelling = $dbr->addQuotes( $spelling ); - $queryResult = $dbr->query( "SELECT * FROM {$dc}_expression WHERE {$dc}_expression.spelling=$spelling AND " . getLatestTransactionRestriction( "{$dc}_expression" ) ); + $queryResult = $dbr->query( "SELECT * FROM {$dc}_expression WHERE spelling=$spelling AND " . getLatestTransactionRestriction( "{$dc}_expression" ) ); $rv = array(); while ( $expressionRecord = $dbr->fetchObject( $queryResult ) ) { $rv[] = new Expression( $expressionRecord->expression_id, $expressionRecord->spelling, $expressionRecord->language_id ); } + $dbr->freeResult( $queryResult ) ; return $rv; } @@ -1672,7 +1673,7 @@ $qry .= 'AND ' . getLatestTransactionRestriction( 'trans' ); $qry .= 'AND ' . getLatestTransactionRestriction( 'dm' ); - + $definitions = $dbr->query( $qry ); while ( $row = $dbr->fetchRow( $definitions ) ) { // $key becomes something like def_23 Modified: trunk/extensions/Wikidata/OmegaWiki/WikiDataGlobals.php =================================================================== --- trunk/extensions/Wikidata/OmegaWiki/WikiDataGlobals.php 2011-08-08 19:38:38 UTC (rev 94076) +++ trunk/extensions/Wikidata/OmegaWiki/WikiDataGlobals.php 2011-08-08 19:53:11 UTC (rev 94077) @@ -1,6 +1,7 @@ <?php define( 'NS_EXPRESSION', 16 ); +define( 'NS_DEFINEDMEANING', 24 ); require_once( "Wikidata.php" ); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs