User:StasR/CargoLua

From mediawiki.org

CargoLua.library.php[edit]

<?php

class CargoLuaLibrary extends Scribunto_LuaLibraryBase {

    function register() {
        $lib = array(
            'get' => array( $this, 'cargoGet' ),
        );
        return $this->getEngine()->registerInterface( __DIR__ . '/cargo.lua', $lib, array() );
    }

    function cargoGet() {
        global $wgCargo_Lua;
        $val = $wgCargo_Lua;
        if ( $val ) {
            array_unshift( $val, NULL );
            $wgCargo_Lua = NULL;
            return array( $val );
        }
        return array( NULL );
    }
}

cargo.lua[edit]

local cargo = {}
local php

function cargo.setupInterface( options )
    -- Remove setup function
    cargo.setupInterface = nil

    -- Copy the PHP callbacks to a local variable, and remove the global
    php = mw_interface
    mw_interface = nil

    -- Do any other setup here

    -- Install into the mw global
    mw = mw or {}
    mw.ext = mw.ext or {}
    mw.ext.cargo = cargo

    -- Indicate that we're loaded
    package.loaded['mw.ext.cargo'] = cargo
end

function cargo.get()
    return php.get()
end

return cargo

formats/CargoLuaDisplay.php[edit]

<?php
/**
 * @ingroup Cargo
 */

class CargoLuaFormat extends CargoDisplayFormat {

	function allowedParameters() {
		return array(  );
	}
	/**
	 *
	 * @param array $valuesTable
	 * @param array $formattedValuesTable Unused
	 * @param array $fieldDescriptions
	 * @param array $displayParams
	 * @return string
	 * @throws MWException
	 */
	function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) {
		global $wgCargo_Lua;
		$wgCargo_Lua = $valuesTable;
		return '';
	}

}

Patches[edit]

Cargo.php[edit]

$wgHooks['ScribuntoExternalLibraries'][] = 'CargoHooks::onScribuntoExternalLibraries';
// . . . .
$wgAutoloadClasses['CargoLuaLibrary'] = $dir . '/CargoLua.library.php';
// . . . .
$wgAutoloadClasses['CargoLuaFormat'] = $dir . '/formats/CargoLuaFormat.php';

Cargo.hooks.php[edit]

        public static function onScribuntoExternalLibraries( $engine, &$extraLibraries ) {
                $extraLibraries['mw.ext.cargo'] = 'CargoLuaLibrary';
                return true;
        }

CargoQueryDisplayer.php[edit]

//	public static function getAllFormatClasses() {
//		$formatClasses = array(  
			'lua' => 'CargoLuaFormat',

extension.json[edit]

        "AutoloadClasses": {
                "CargoLuaLibrary": "CargoLua.library.php",

                "CargoLuaFormat": "formats/CargoLuaFormat.php",
        },

        "Hooks": {
                "ScribuntoExternalLibraries": [
                        "CargoHooks::onScribuntoExternalLibraries"
        },

Usage[edit]

local p = {}
local cargo = mw.ext.cargo

function p.Main( frame )
    local z = frame:callParserFunction{ name = '#cargo_query', args = { '',
       tables = 'table1, table2, etc.',
       fields = 'field1=alias1,field2=alias2, etc.',
       where = 'table1.fieldE="some value", etc.',
       -- other parameters here
       format = 'lua',
    } }
    local result = cargo.get()
--  . . .
end