-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this adds the parser function ask including tests and documentation
- Loading branch information
Showing
10 changed files
with
505 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
## mw.smw.ask | ||
|
||
With `mw.smw.ask` you can execute an smw query. It returns the result as a lua table for direct use in modules. | ||
For available parameters, please consult the [Semantic Media Wiki documentation hub][smwdoc]. | ||
|
||
Please see the [return format below](#result) for the difference between this and [`mw.smw.getQueryResult`](mw.smw.getQueryResult.md). | ||
|
||
This is a sample call: | ||
```lua | ||
-- Module:SMW | ||
local p = {} | ||
|
||
-- Return results | ||
function p.ask(frame) | ||
|
||
if not mw.smw then | ||
return "mw.smw module not found" | ||
end | ||
|
||
if frame.args[1] == nil then | ||
return "no parameter found" | ||
end | ||
|
||
local queryResult = mw.smw.ask( frame.args ) | ||
|
||
if queryResult == nil then | ||
return "(no values)" | ||
end | ||
|
||
if type( queryResult ) == "table" then | ||
local myResult = "" | ||
for num, row in pairs( queryResult ) do | ||
myResult = myResult .. '* This is result #' .. num .. '\n' | ||
for k, v in pairs( row ) do | ||
myResult = myResult .. '** ' .. k .. ': ' .. v .. '\n' | ||
end | ||
end | ||
return myResult | ||
end | ||
|
||
return queryResult | ||
end | ||
|
||
-- another example, ask used inside another function | ||
function p.inlineAsk( frame ) | ||
|
||
local entityAttributes = { | ||
'has name=name', | ||
'has age=age', | ||
'has color=color' | ||
} | ||
local category = 'thingies' | ||
|
||
-- build query | ||
local query = {} | ||
table.insert(query, '[[Category:' .. category .. ']]') | ||
|
||
for _, v in pairs( entityAttributes ) do | ||
table.insert( query, '?' .. v ) | ||
end | ||
|
||
query.mainlabel = 'origin' | ||
query.limit = 10 | ||
|
||
local result = mw.smw.ask( query ) | ||
|
||
local output = '' | ||
if result and #result then | ||
|
||
for num, entityData in pairs( result ) do | ||
-- further process your data | ||
output = output .. entityData.origin .. ' (' .. num .. ') has name ' .. entityData.name | ||
.. ', color ' .. entityData.color .. ', and age ' .. entityData.age | ||
end | ||
end | ||
|
||
return output | ||
end | ||
|
||
return p | ||
``` | ||
|
||
### <a name="result"></a>Return format | ||
|
||
The return format is a simple collection of tables (one per result set) holding your smw data, | ||
each indexed by property names or labels respectively. You can see an example below: | ||
|
||
```lua | ||
-- assuming sample call | ||
local result = mw.smw.ask( '[[Modification date::+]]|?Modification date|?Last editor is=editor|limit=2|mainlabel=page' ) | ||
-- your result would look something like | ||
{ | ||
{ | ||
['Modification date'] = '1 January 1970 23:59:59', | ||
editor = 'User:Mwjames', | ||
page = 'Main page' | ||
}, | ||
{ | ||
['Modification date'] = '2 January 1970 00:00:42', | ||
editor = 'User:Oetterer', | ||
page = 'User:Oetterer' | ||
}, | ||
} | ||
``` | ||
|
||
This function is meant to be used inside lua modules and should not be directly exposed to #invoke. | ||
|
||
[smwdoc]: https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
namespace SMW\Scribunto; | ||
|
||
/** | ||
* Class LuaAskResultProcessor | ||
* | ||
* @license GNU GPL v2+ | ||
* @since 1.0 | ||
* | ||
* @author Tobias Oetterer | ||
* | ||
* @package SMW\Scribunto | ||
*/ | ||
class LuaAskResultProcessor { | ||
|
||
/** | ||
* Holds the query result for this object | ||
* | ||
* @var \SMWQueryResult | ||
*/ | ||
private $queryResult; | ||
|
||
/** | ||
* Holds all possible representations of "true" in this smw instance | ||
* | ||
* @var array | ||
*/ | ||
private $msgTrue; | ||
|
||
/** | ||
* LuaAskResultProcessor constructor. | ||
* | ||
* @param \SMWQueryResult $queryResult | ||
*/ | ||
public function __construct( \SMWQueryResult $queryResult ) { | ||
$this->queryResult = $queryResult; | ||
$this->msgTrue = explode( ',', wfMessage( 'smw_true_words' )->text() . ',true,t,yes,y' ); | ||
} | ||
|
||
/** | ||
* Extracts the data in {@see $queryResult} and returns it as a table | ||
* usable in lua context | ||
* | ||
* @return array|null | ||
*/ | ||
public function getQueryResultAsLuaTable() { | ||
if ( $this->queryResult == null ) { | ||
return null; | ||
} | ||
|
||
$result = array(); | ||
|
||
while ( $resultRow = $this->queryResult->getNext() ) { | ||
$result[] = $this->getDataFromQueryResultRow( $resultRow ); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Extracts the data of a single result row in the {@see $queryResult} | ||
* and returns it as a table usable in lua context | ||
* | ||
* @param array $resultRow result row as an array of {@see SMWResultArray} objects | ||
* | ||
* @return array | ||
*/ | ||
private function getDataFromQueryResultRow( array $resultRow ) { | ||
|
||
$rowData = array(); | ||
$numberFallBack = 1; | ||
|
||
/** @var \SMWResultArray $resultArray */ | ||
foreach ( $resultRow as $resultArray ) { | ||
// find out, which key to use for this printRequest / query field | ||
if ( $resultArray->getPrintRequest()->getLabel() === '' ) { | ||
$key = $numberFallBack++; | ||
} else { | ||
$key = $resultArray->getPrintRequest()->getText( SMW_OUTPUT_WIKI ); | ||
} | ||
|
||
// and get the data | ||
$rowData[$key] = $this->getDataFromSMWResultArray( $resultArray ); | ||
} | ||
|
||
return $rowData; | ||
} | ||
|
||
/** | ||
* Extracts the data of a single printRequest / query field | ||
* | ||
* @param \SMWResultArray $resultArray | ||
* | ||
* @return mixed | ||
*/ | ||
private function getDataFromSMWResultArray( \SMWResultArray $resultArray ) { | ||
|
||
$resultArrayData = array(); | ||
|
||
// get all data that is stored in this printRequest / query field | ||
/** @var \SMWDataValue $dataValue */ | ||
while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { | ||
|
||
$resultArrayData[] = $this->getValueFromSMWDataValue( $dataValue ); | ||
} | ||
|
||
if ( ! $resultArrayData || ! count( $resultArrayData ) ) { | ||
// this key has no value(s). set to null | ||
$resultArrayData = null; | ||
} elseif ( count( $resultArrayData ) == 1 ) { | ||
// there was only one semantic value found. reduce the array to this value | ||
$resultArrayData = array_shift( $resultArrayData ); | ||
} else { | ||
// $key has multiple values. keep the array, but un-shift it (remember: lua array counting starts with 1) | ||
array_unshift( $resultArrayData, null ); | ||
} | ||
|
||
return $resultArrayData; | ||
} | ||
|
||
/** | ||
* Extracts the data of a single value of the current printRequest / query field | ||
* The value is formatted according to the type of the property | ||
* | ||
* @param \SMWDataValue $dataValue | ||
* | ||
* @return mixed | ||
*/ | ||
private function getValueFromSMWDataValue( \SMWDataValue $dataValue ) { | ||
|
||
$value = null; | ||
|
||
switch ( $dataValue->getTypeID() ) { | ||
case '_boo': | ||
// boolean value found, convert it | ||
$value = in_array( $dataValue->getShortText( SMW_OUTPUT_WIKI ), $this->msgTrue ); | ||
break; | ||
case '_num': | ||
// number value found | ||
/** @var \SMWNumberValue $dataValue */ | ||
$value = $dataValue->getNumber(); | ||
$value = ( $value == (int) $value ) ? intval( $value ) : $value; | ||
break; | ||
case '_wpg' : | ||
/** @var \SMWWikiPageValue $dataValue */ | ||
$value = $dataValue->getShortWikiText( ); | ||
// $value = $dataValue->getShortText( SMW_OUTPUT_WIKI ); | ||
break; | ||
default: | ||
$value = $dataValue->getShortText( SMW_OUTPUT_WIKI ); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Oops, something went wrong.