forked from wikimedia/mediawiki-extensions-Cargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CargoLua.library.php
75 lines (66 loc) · 1.81 KB
/
CargoLua.library.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class CargoLuaLibrary extends Scribunto_LuaLibraryBase {
function register() {
$lib = array(
'query' => array( $this, 'cargoQuery' )
);
return $this->getEngine()->registerInterface( __DIR__ . '/cargo.lua', $lib, array() );
}
function cargoQuery( $tables, $fields, $args ) {
$this->checkType( 'query', 1, $tables, 'string' );
$this->checkType( 'query', 2, $fields, 'string' );
$this->checkTypeOptional( 'query', 3, $args, 'table', array() );
if ( isset( $args['where'] ) ) {
$where = $args['where'];
} else {
$where = null;
}
if ( isset( $args['join'] ) ) {
$join = $args['join'];
} else {
$join = null;
}
if ( isset( $args['groupBy'] ) ) {
$groupBy = $args['groupBy'];
} else {
$groupBy = null;
}
if ( isset( $args['having'] ) ) {
$having = $args['having'];
} else {
$having = null;
}
if ( isset( $args['orderBy'] ) ) {
$orderBy = $args['orderBy'];
} else {
$orderBy = null;
}
if ( isset( $args['limit'] ) ) {
$limit = $args['limit'];
} else {
$limit = null;
}
if ( isset( $args['offset'] ) ) {
$offset = $args['offset'];
} else {
$offset = null;
}
$query = CargoSQLQuery::newFromValues( $tables, $fields, $where, $join,
$groupBy, $having, $orderBy, $limit, $offset );
$rows = $query->run();
$result = array();
$fieldArray = CargoUtils::smartSplit( ',', $fields );
$rowIndex = 1; // because Lua arrays start at 1
foreach ( $rows as $row ) {
$values = array();
foreach ( $fieldArray as $fieldString ) {
$alias = $query->getAliasForFieldString( $fieldString );
$nameArray = CargoUtils::smartSplit( '=', $fieldString );
$name = $nameArray[ count( $nameArray ) - 1 ];
$values[$name] = htmlspecialchars_decode( $row[$alias] );
}
$result[$rowIndex++] = $values;
}
return array( $result );
}
}