Kipcool has submitted this change and it was merged.

Change subject: Various db query function fix
......................................................................


Various db query function fix

converter's query adds the MySQL prefix and need not be fixed. Search.php
seems not used, if it is, I do not know how to test it.

Patch 2: language.php's query function need not be changed since the SQL
it uses was created using selectSQLText.

Query functions not yet checked are contained in
SpecialNeedTranslation.php, SpecialSuggest.php and WikiDataAPI.php.

There are PHP mysql_query functions at SpecialNeedsTranslation.php and
copytest.php and Copy.php which if I am correct, might need to be checked
for prefixed database. I am not sure, kindly check.

Kindly merge this if there are no more issues. Thanks.

Bug: 56220
Change-Id: Ia43d86734b2f59c39497946a236185c431c88700
---
M OmegaWiki/RecordSetQueries.php
M OmegaWiki/Search.php
M OmegaWiki/converter.php
M OmegaWiki/languages.php
4 files changed, 82 insertions(+), 61 deletions(-)

Approvals:
  Kipcool: Verified; Looks good to me, approved



diff --git a/OmegaWiki/RecordSetQueries.php b/OmegaWiki/RecordSetQueries.php
index 5dcd476..82fbc24 100644
--- a/OmegaWiki/RecordSetQueries.php
+++ b/OmegaWiki/RecordSetQueries.php
@@ -6,16 +6,16 @@
 class TableColumnsToAttribute {
        protected $tableColumns;
        protected $attribute;
-       
+
        public function __construct( array $tableColumns, Attribute $attribute 
) {
                $this->tableColumns = $tableColumns;
                $this->attribute = $attribute;
        }
-       
+
        public function getTableColumns() {
                return $this->tableColumns;
        }
-       
+
        public function getAttribute() {
                return $this->attribute;
        }
@@ -23,7 +23,7 @@
 
 class TableColumnsToAttributesMapping {
        protected $tableColumnsToAttributes;
-       
+
        public function __construct( $tableColumnsToAttributes ) {
                if ( is_array( $tableColumnsToAttributes ) ) {
                        $this->tableColumnsToAttributes = 
$tableColumnsToAttributes;
@@ -31,10 +31,10 @@
                        $this->tableColumnsToAttributes = func_get_args();
                }
        }
-       
+
        public function getSelectColumns() {
                $result = array();
-               
+
                foreach ( $this->tableColumnsToAttributes as 
$tableColumnToAttribute ) {
                        foreach ( $tableColumnToAttribute->getTableColumns() as 
$tableColumn ) {
                                $result[] = $tableColumn;
@@ -45,17 +45,17 @@
 
        public function getAttributes() {
                $result = array();
-               
+
                foreach ( $this->tableColumnsToAttributes as 
$tableColumnToAttribute ) {
                        $result[] = $tableColumnToAttribute->getAttribute();
                }
                return $result;
        }
-       
+
        public function getCount() {
                return count( $this->tableColumnsToAttributes );
        }
-       
+
        public function getMapping( $index ) {
                return $this->tableColumnsToAttributes[$index];
        }
@@ -78,7 +78,7 @@
        else {
                $groupBy = array();
        }
-       
+
        $query =
                "SELECT " . implode( ", ", $selectFields ) .
                " FROM " . implode( ", ", $tableNames );
@@ -101,18 +101,18 @@
 
 function getRecordFromRow( $row, $columnIndex, Structure $structure ) {
        $result = new ArrayRecord( $structure );
-       
+
        foreach ( $structure->getAttributes() as $attribute ) {
                $result->setAttributeValue( $attribute, $row[$columnIndex] );
                $columnIndex++;
        }
-       
+
        return $result;
 }
 
 function queryRecordSet( $recordSetStructureId, QueryTransactionInformation 
$transactionInformation, Attribute $keyAttribute, 
TableColumnsToAttributesMapping $tableColumnsToAttributeMapping, Table $table, 
array $restrictions, array $orderBy = array(), $count = - 1, $offset = 0 ) {
        $dbr = wfGetDB( DB_SLAVE );
-       
+
        $selectFields =  $tableColumnsToAttributeMapping->getSelectColumns();
        $attributes = $tableColumnsToAttributeMapping->getAttributes();
 
@@ -121,11 +121,13 @@
        } else {
                $allAttributes = $attributes;
        }
-       
+
        // create and run the sql query to get the "$selectFields" fields from 
the "$table" table
        $query = getTransactedSQL( $transactionInformation, $selectFields, 
$table, $restrictions, $orderBy, $count, $offset );
        $queryResult = $dbr->query( $query );
-       
+       // @note Even though the above uses the query function, we need not 
convert this
+       // to select function, since the generated SQL adds the mySQL prefix 
automatically. ~he
+
        if ( !is_null( $recordSetStructureId ) ) {
                $structure = new Structure( $recordSetStructureId, 
$allAttributes );
        } else {
@@ -142,35 +144,35 @@
                        $mapping = $tableColumnsToAttributeMapping->getMapping( 
$i );
                        $attribute = $mapping->getAttribute();
                        $tableColumns = $mapping->getTableColumns();
-                       
+
                        if ( count( $tableColumns ) == 1 ) {
                                $value = $row[$columnIndex];
                        } else {
                                $value = getRecordFromRow( $row, $columnIndex, 
$attribute->type );
                        }
-                       
+
                        $record->setAttributeValue( $attribute, $value );
                        $columnIndex += count( $tableColumns );
                }
-                       
+
                $transactionInformation->setVersioningAttributes( $record, $row 
);
                $recordSet->add( $record );
        }
-               
+
        return $recordSet;
 }
 
 function getUniqueIdsInRecordSet( RecordSet $recordSet, array $idAttributes ) {
        $ids = array();
-       
+
        for ( $i = 0; $i < $recordSet->getRecordCount(); $i++ ) {
                $record = $recordSet->getRecord( $i );
-               
+
                foreach ( $idAttributes as $idAttribute ) {
                        $ids[] = $record->getAttributeValue( $idAttribute );
                }
        }
-       
+
        return array_unique( $ids );
 }
 
diff --git a/OmegaWiki/Search.php b/OmegaWiki/Search.php
index d82021a..7993074 100644
--- a/OmegaWiki/Search.php
+++ b/OmegaWiki/Search.php
@@ -10,11 +10,15 @@
 require_once( "OmegaWikiEditors.php" );
 require_once( "WikiDataGlobals.php" );
 
+/**
+ * @todo Check if this class is used or not, I can not find how to use this.
+ *     Was this default app replaced by special page Data search? ~he
+ */
 class Search extends DefaultWikidataApplication {
        function view() {
                global
                        $wgOut, $wgTitle;
-               
+
                parent::view();
 
                $spelling = $wgTitle->getText();
@@ -22,20 +26,36 @@
                $wgOut->addHTML( '<p>Showing only a maximum of 100 
matches.</p>' );
                $wgOut->addHTML( $this->searchText( $spelling ) );
        }
-       
+
        function searchText( $text ) {
                $dc = wdGetDataSetContext();
                $dbr = wfGetDB( DB_SLAVE );
-               
-               $sql = "SELECT INSTR(LCASE({$dc}_expression.spelling), LCASE(" 
. $dbr->addQuotes( "$text" ) . ")) as position, 
{$dc}_syntrans.defined_meaning_id AS defined_meaning_id, 
{$dc}_expression.spelling AS spelling, {$dc}_expression.language_id AS 
language_id " .
-                               "FROM {$dc}_expression, {$dc}_syntrans " .
-                   "WHERE 
{$dc}_expression.expression_id={$dc}_syntrans.expression_id AND 
{$dc}_syntrans.identical_meaning=1 " .
-                   " AND " . getLatestTransactionRestriction( "{$dc}_syntrans" 
) .
-                   " AND " . getLatestTransactionRestriction( 
"{$dc}_expression" ) .
-                               " AND spelling LIKE " . $dbr->addQuotes( 
"%$text%" ) .
-                               " ORDER BY position ASC, 
{$dc}_expression.spelling ASC limit 100";
-               
-               $queryResult = $dbr->query( $sql );
+
+               $queryResult = $dbr->selectSQLText(
+                       array(
+                               'exp' => '{$dc}_expression',
+                               'synt' => '{$dc}_syntrans'
+                       ),
+                       array(
+                               'INSTR( LOWER( exp.spelling ), LOWER( {$text} ) 
)  AS position', // LCASE replaced with LOWER for SQLite compatibility
+                               'synt.defined_meaning_id AS defined_meaning_id',
+                               'exp.spelling AS spelling',
+                               'exp.language_id AS language_id'
+                       ),
+                       array(
+                               'exp.expression_id = synt.expression_id',
+                               'synt.identical_meaning = 1',
+                               'exp.remove_transaction_id' => null,
+                               'synt.remove_transaction_id' => null,
+                               'spelling LIKE ' . $dbr->addQuotes( "%$text%" )
+                       ), __METHOD__,
+                       array(
+                               'ORDER BY' => 'position ASC',
+                               'ORDER BY' => 'exp.spelling ASC',
+                               'LIMIT' => 100
+                       )
+               );
+       var_dump( $queryResult ); die;
                list( $recordSet, $editor ) = getSearchResultAsRecordSet( 
$queryResult );
 //             return $sql;
                return $editor->view( new IdStack( "expression" ), $recordSet );
@@ -50,23 +70,23 @@
        $dbr = wfGetDB( DB_SLAVE );
        $spellingAttribute = new Attribute( "found-word", "Found word", 
"short-text" );
        $languageAttribute = new Attribute( "language", "Language", "language" 
);
-       
+
        $expressionStructure = new Structure( $spellingAttribute, 
$languageAttribute );
        $expressionAttribute = new Attribute( "expression", "Expression", 
$expressionStructure );
-       
+
        $definedMeaningAttribute = new Attribute( WLD_DEFINED_MEANING, "Defined 
meaning", $definedMeaningReferenceType );
        $definitionAttribute = new Attribute( "definition", "Definition", 
"definition" );
-       
+
        $meaningStructure = new Structure( $definedMeaningAttribute, 
$definitionAttribute );
        $meaningAttribute = new Attribute( "meaning", "Meaning", 
$meaningStructure );
 
        $recordSet = new ArrayRecordSet( new Structure( $o->definedMeaningId, 
$expressionAttribute, $meaningAttribute ), new Structure( $o->definedMeaningId 
) );
-       
+
        while ( $row = $dbr->fetchObject( $queryResult ) ) {
                $expressionRecord = new ArrayRecord( $expressionStructure );
                $expressionRecord->setAttributeValue( $spellingAttribute, 
$row->spelling );
                $expressionRecord->setAttributeValue( $languageAttribute, 
$row->language_id );
-               
+
                $meaningRecord = new ArrayRecord( $meaningStructure );
                $meaningRecord->setAttributeValue( $definedMeaningAttribute, 
getDefinedMeaningReferenceRecord( $row->defined_meaning_id ) );
                $meaningRecord->setAttributeValue( $definitionAttribute, 
getDefinedMeaningDefinition( $row->defined_meaning_id ) );
diff --git a/OmegaWiki/converter.php b/OmegaWiki/converter.php
index 6e507c9..5fa3c2e 100644
--- a/OmegaWiki/converter.php
+++ b/OmegaWiki/converter.php
@@ -12,21 +12,21 @@
 
 class ProjectConverter implements Converter {
        protected $structure;
-       
+
        public function __construct( $structure ) {
                $this->structure = $structure;
        }
-       
+
        public function getStructure() {
                return $this->structure;
        }
-       
+
        public function convert( $record ) {
                $result = new ArrayRecord( $this->structure );
-               
+
                foreach ( $this->structure->getStructure() as $attribute )
                        $result->setAttributeValue( $attribute, 
$record->getAttributeValue( $attribute ) );
-                       
+
                return $result;
        }
 }
@@ -34,19 +34,19 @@
 class DefaultConverter implements Converter {
        protected $attribute;
        protected $structure;
-       
+
        public function __construct( $attribute ) {
                $this->attribute = $attribute;
                $this->structure = new Structure( $attribute );
        }
-       
+
        public function convert( $record ) {
                $result = new ArrayRecord( $this->structure );
                $result->setAttributeValue( $this->attribute, convertToHTML( 
$record->getAttributeValue( $this->attribute ), $this->attribute->type ) );
-               
+
                return $result;
        }
-       
+
        public function getStructure() {
                return $this->structure;
        }
@@ -54,38 +54,35 @@
 
 class ExpressionIdConverter extends DefaultConverter {
        protected $attributes = array();
-       
+
        public function __construct( $attribute ) {
 
                $o = OmegaWikiAttributes::getInstance();
-                       
+
                parent::__construct( $attribute );
                $this->structure = new Structure( $o->expression );
        }
-       
+
        public function getStructure() {
                return $this->structure;
        }
-       
+
        public function convert( $record ) {
                $dc = wdGetDataSetContext();
 
 
                $o = OmegaWikiAttributes::getInstance();
-               
-               $dbr = wfGetDB( DB_SLAVE );
+
                $expressionId = $record->getAttributeValue( $this->attribute );
-               $queryResult = $dbr->query( "SELECT language_id, spelling from 
{$dc}_expression WHERE expression_id=$expressionId" .
-                                                                       " AND " 
. getLatestTransactionRestriction( "{$dc}_expression" ) );
-               $expression = $dbr->fetchObject( $queryResult );
+               $expression = getExpression( $expressionId );
 
                $expressionRecord = new ArrayRecord( new Structure( 
$o->language, $o->spelling ) );
-               $expressionRecord->language = $expression->language_id;
+               $expressionRecord->language = $expression->languageId;
                $expressionRecord->spelling = $expression->spelling;
 
                $result = new ArrayRecord( $this->structure );
                $result->expression = $expressionRecord;
-       
+
                return $result;
        }
 }
diff --git a/OmegaWiki/languages.php b/OmegaWiki/languages.php
index 7a355bd..9ba74b5 100644
--- a/OmegaWiki/languages.php
+++ b/OmegaWiki/languages.php
@@ -1,5 +1,7 @@
 <?php
-
+/** @file
+ * @todo create a language class for owDatabaseAPI class
+ */
 require_once( 'WikiDataGlobals.php' );
 
 /**
@@ -22,7 +24,7 @@
        $dbr = wfGetDB( DB_SLAVE );
        $names = array();
        $sql = getSQLForLanguageNames( $code );
-       $lang_res = $dbr->query( $sql );
+       $lang_res = $dbr->query( $sql ); // function getSQLForLanguageNames 
creates SQL with MySQL prefix
        while ( $lang_row = $dbr->fetchObject( $lang_res ) )
                $names[$lang_row->row_id] = $lang_row->language_name;
        return $names;

-- 
To view, visit https://gerrit.wikimedia.org/r/130568
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia43d86734b2f59c39497946a236185c431c88700
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <hiong3.e...@gmail.com>
Gerrit-Reviewer: Kipcool <kipmas...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to