http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89153
Revision: 89153
Author: freakolowsky
Date: 2011-05-30 07:37:38 +0000 (Mon, 30 May 2011)
Log Message:
-----------
* Initial commit
Added Paths:
-----------
trunk/extensions/SQL2Wiki/
trunk/extensions/SQL2Wiki/SQL2Wiki.body.php
trunk/extensions/SQL2Wiki/SQL2Wiki.i18n.php
trunk/extensions/SQL2Wiki/SQL2Wiki.php
trunk/extensions/SQL2Wiki/SQL2Wiki.php~
trunk/extensions/SQL2Wiki/SpecialSQL2Wiki.php
Added: trunk/extensions/SQL2Wiki/SQL2Wiki.body.php
===================================================================
--- trunk/extensions/SQL2Wiki/SQL2Wiki.body.php (rev 0)
+++ trunk/extensions/SQL2Wiki/SQL2Wiki.body.php 2011-05-30 07:37:38 UTC (rev
89153)
@@ -0,0 +1,270 @@
+<?php
+
+class SQL2Wiki {
+
+ private $mDB;
+ private $mDBType;
+
+ private $mDBMSOutput;
+
+ private $mArgs;
+ private $mPLSQLMode;
+ private $mInlineTag = null;
+
+ private $mResult = null;
+ private $mOutput = null;
+ private $mError = null;
+
+ private $ignoreAttributes = array( 'database', 'inline',
'fieldseparator', 'lineseparator', 'cache', 'expand', 'preexpand', 'quiet' );
+ private $ignoteTabAttributes = array( 'tablestyle', 'hrowstyle',
'hcellstyle', 'rowstyle', 'cellstyle', 'style', 'noheader' );
+
+ public static function initTags( &$parser ) {
+ $parser->setHook( "sql2wiki", "SQL2Wiki::renderSQL" );
+ $parser->setHook( "plsql2wiki", "SQL2Wiki::renderPLSQL" );
+ return false;
+ }
+
+ public static function renderSQL( $input, $args, $parser, $frame ) {
+ try {
+ $s2w = new SQL2Wiki( $args );
+
+ $sql = ( $s2w->shouldPreexpand() ) ?
$parser->recursiveTagParse( $input ) : $input;
+
+ $s2w->execute( $sql );
+
+ if ( ( $inline = $s2w->getInlineTag() ) !== false )
+ $ret = $s2w->parseInline();
+ else
+ $ret = $s2w->parseTable();
+
+ $ret = ( $s2w->shouldExpand() ) ?
$parser->recursiveTagParse( $ret ) : $ret;
+ $ret .= $s2w->handleCache( $parser );
+
+ return $ret;
+ } catch ( Exception $ex ) {
+ return $ex->getMessage();
+ }
+ }
+
+ public static function renderPLSQL( $input, $args, $parser, $frame ) {
+ try {
+ $s2w = new SQL2Wiki( $args, true );
+
+ if ( !$s2w->isPLSQLSupported() ) {
+ return wfMsgForContent(
'sql2wiki-err-feature_not_supported', $s2w->getDBType() );
+ }
+
+ $sql = ( $s2w->shouldPreexpand() ) ?
$parser->recursiveTagParse( $input ) : $input;
+
+ $s2w->execute( $sql );
+
+ $ret = $s2w->parseInline();
+
+ $ret = ( $s2w->shouldExpand() ) ?
$parser->recursiveTagParse( $ret ) : $ret;
+ $ret .= $s2w->handleCache( $parser );
+
+ return $ret;
+ } catch ( Exception $ex ) {
+ return $ex->getMessage();
+ }
+ }
+
+ public function __construct( $args, $PLSQLMode = false ) {
+ global $wgExSql2WikiDatabases;
+
+ $this->mArgs = $args;
+ $this->mPLSQLMode = $PLSQLMode;
+
+ if ( !isset( $this->mArgs["database"] ) || !isset(
$wgExSql2WikiDatabases[$this->mArgs["database"]] ) )
+ throw new Exception( wfMsgForContent(
'sql2wiki-err-invalid_db_id' ) );
+
+ $db = $wgExSql2WikiDatabases[$this->mArgs["database"]];
+ $this->mDBType = $db['type'];
+ $this->mDB = Database::newFromType( $this->mDBType, $db );
+
+ if ( !$this->mDB->isOpen() ) {
+ throw new Exception( wfMsgForContent(
'sql2wiki-err-failed_to_connect', $this->mArgs["database"] ) );
+
+ }
+ }
+
+ public function isPLSQLSupported() {
+ return ($this->mDBType == 'oracle');
+ }
+
+ public function isDBMSOutputSupported() {
+ return ($this->mDBType == 'oracle');
+ }
+
+ public function shouldPreexpand() {
+ return ( isset( $this->mArgs["preexpand"] ) &&
$this->mArgs["preexpand"] == 'true' );
+ }
+
+ public function shouldExpand() {
+ return ( isset( $this->mArgs["expand"] ) &&
$this->mArgs["expand"] == 'true' );
+ }
+
+ public function getInlineTag() {
+ if ( $this->mInlineTag == null ) {
+ $this->mInlineTag = ( isset( $this->mArgs["inline"] ) )
+ ? $this->mArgs["inline"]
+ : ( $this->mPLSQLMode ? 'span' : false ) ;
+ }
+ return $this->mInlineTag;
+ }
+
+ public function getDBType() {
+ return $this->mDBType;
+ }
+
+ public function execute( $sql ) {
+
+ $ignore = $this->mDB->ignoreErrors( true );
+ $this->initDBMSOutput();
+
+ $this->mResult = $this->mDB->doQuery( $sql );
+
+ $this->getDBMSOutput();
+ $this->mDB->ignoreErrors( $ignore );
+
+ if ( $this->mDB->lastError() != null ) {
+ if ( ( isset( $this->mArgs["quiet"] ) &&
$this->mArgs["quiet"] == 'true' ) ) {
+ $this->mError = wfMsgForContent(
'sql2wiki-err-failed_to_execute', $sql, $this->mDB->lastError() );
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+
+ public function handleCache ( $parser ) {
+ $cache = isset( $this->mArgs["cache"] ) ? $this->mArgs["cache"]
: 'on';
+
+ if ( $cache == 'off') {
+ $parser->disableCache();
+ } elseif ( $cache == 'manual' && ($this->mPLSQLMode ||
$this->getInlineTag() === false) ) {
+ return '<br />'.
+ Xml::openElement( 'a',
+
array( 'href' => $parser->getTitle()->getLocalURL( array( 'action' =>
'purge' ) ) ) ).
+ Xml::element( 'small', null,
wfMsgForContent( 'sql2wiki-cache_refresh' ) ).
+ Xml::closeElement( 'a' );
+ }
+
+ return '';
+ }
+
+ private function initDBMSOutput() {
+ if ( !$this->isDBMSOutputSupported() ) {
+ $this->mDBMSOutput = false;
+ return;
+ }
+
+ $this->mDbmsOutput = ( isset( $this->mArgs["dbmsoutput"] ) &&
$this->mArgs["dbmsoutput"] == 'true' );
+ if ($this->mDBMSOutput) {
+ $this->enableDBMSOutput();
+ }
+ }
+
+ private function enableDBMSOutput() {
+ if ( !$this->isDBMSOutputSupported() ) {
+ return;
+ }
+
+ $this->mDB->doQuery ('BEGIN dbms_output.enable(null); END;');
+ }
+
+ private function disableDBMSOutput() {
+ if ( !$this->isDBMSOutputSupported() ) {
+ return;
+ }
+
+ $this->mDB->doQuery ('BEGIN dbms_output.disable; END;');
+ }
+
+ private function getDBMSOutput() {
+ if ( !$this->mDBMSOutput ) {
+ return false;
+ }
+
+ $this->mOutput = array();
+
+ $ret = $dbObj->doQuery('SELECT column_value FROM
TABLE(get_output_lines())');
+ while(($line = $ret->fetchObject()) !== FALSE) {
+ $this->mOutput[] = $line->column_value;
+ }
+ }
+
+ private function parseInline() {
+ $fieldSeparator = isset( $this->mArgs["fieldseparator"] ) ?
$this->mArgs["fieldseparator"] : '';
+ $lineSeparator = isset( $this->mArgs["lineseparator"] ) ?
$this->mArgs["lineseparator"] : '';
+
+ $output = '';
+ while ( ($line = $this->mResult->fetchObject() ) !== FALSE) {
+ if ( $output != '' ) {
+ $output .= $lineSeparator."\n";
+ }
+
+ foreach ( $line as $value ){
+ $output .= $value.$fieldSeparator;
+ }
+ $output = substr( $output, 0, strlen( $output )-strlen(
$fieldSeparator ) );
+ }
+
+ $attributes = array_diff_key( $this->mArgs, array_flip(
$this->ignoreAttributes ) );
+ return Xml::openElement( $this->getInlineTag(), $attributes).
+ $output.
+ Xml::closeElement( $this->getInlineTag() );
+ }
+
+ private function parseTable() {
+ $line = $this->mResult->fetchObject();
+ if ( $line === false ) {
+ return '';
+ }
+
+ $tableStyle = isset( $this->mArgs["tablestyle"] ) ?
$this->mArgs["tablestyle"] : 'border: black solid 1px;';
+
+ $attributes = array_diff_key( $this->mArgs, array_flip(
$this->ignoreAttributes ), array_flip( $this->ignoteTabAttributes ) );
+
+ $output = Xml::openElement( 'table', array_merge( $attributes,
array( 'style' => $tableStyle ) ) );
+
+ if ( !isset( $this->mArgs["noheader"] ) ||
$this->mArgs["noheader"] != 'true' ) {
+ $headerRowStyle = isset( $this->mArgs["hrowstyle"] ) ?
$this->mArgs["hrow_style"] : '';
+ $headerCellStyle = isset( $this->mArgs["hcellstyle"] )
? $this->mArgs["hcellstyle"] : 'font-weight: bold;';
+ $headerCellStyle = array_merge( $attributes, array(
'style' => $headerCellStyle ) );
+
+ $output .= Xml::openElement( 'tr', array_merge(
$attributes, array( 'style' => $headerRowStyle ) ) );
+
+ foreach ( $line as $key=>$value ) {
+ $output .= Xml::openElement( 'th',
$headerCellStyle ).
+ $key.
+ Xml::closeElement( 'th'
);
+ }
+
+ $output .= Xml::closeElement( 'tr' );
+ }
+
+ $rowStyle = isset( $this->mArgs["rowstyle"] ) ?
$this->mArgs["rowstyle"] : '';
+ $cellStyle = isset( $this->mArgs["cellstyle"] ) ?
$this->mArgs["cellstyle"] : '';
+ $cellStyle = array_merge( $attributes, array( 'style' =>
$cellStyle ) );
+
+ do {
+ $output .= Xml::openElement( 'tr', array_merge(
$attributes, array( 'style' => $rowStyle ) ) );
+
+ foreach ( $line as $key=>$value ) {
+ $output .= Xml::openElement( 'td', $cellStyle
).
+ $value.
+ Xml::closeElement( 'td'
);
+ }
+
+ $output .= Xml::closeElement( 'tr' );
+ } while ( ( $line = $this->mResult->fetchObject() ) !== false );
+
+
+ $output .= Xml::closeElement( 'table' );
+
+ return $output;
+ }
+
+}
Property changes on: trunk/extensions/SQL2Wiki/SQL2Wiki.body.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/extensions/SQL2Wiki/SQL2Wiki.i18n.php
===================================================================
--- trunk/extensions/SQL2Wiki/SQL2Wiki.i18n.php (rev 0)
+++ trunk/extensions/SQL2Wiki/SQL2Wiki.i18n.php 2011-05-30 07:37:38 UTC (rev
89153)
@@ -0,0 +1,17 @@
+<?php
+$messages = array();
+
+$messages['en'] = array(
+ 'sql2wiki' => 'SQL2Wiki',
+ 'sql2wiki-desc' => 'Show SQL data directly in the page contents.',
+
+ 'sql2wiki-cache_refresh' => 'Refresh',
+
+ 'sql2wiki-err-invalid_db_id' => 'SQL2Wiki: Missing or invalid database
ID',
+ 'sql2wiki-err-invalid_type' => 'SQL2Wiki: Missing or invalid type',
+ 'sql2wiki-err-failed_to_connect' => 'SQL2Wiki: Failed to connect to
$1!',
+ 'sql2wiki-err-failed_to_execute' => 'SQL2Wiki: Failed to execute the
statement:<br /> "$1" <br />with error message: "$2"!',
+ 'sql2wiki-err-feature_not_supported' => 'SQL2Wiki: Feature is not
supported by databases of type $1!',
+);
+
+?>
Property changes on: trunk/extensions/SQL2Wiki/SQL2Wiki.i18n.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/extensions/SQL2Wiki/SQL2Wiki.php
===================================================================
--- trunk/extensions/SQL2Wiki/SQL2Wiki.php (rev 0)
+++ trunk/extensions/SQL2Wiki/SQL2Wiki.php 2011-05-30 07:37:38 UTC (rev
89153)
@@ -0,0 +1,283 @@
+<?php
+# Alert the user that this is not a valid entry point to MediaWiki if they try
to access the special pages file directly.
+if (!defined('MEDIAWIKI')) {
+ echo <<<EOT
+To install this extension put the following line in LocalSettings.php:
+require_once( "\$IP/extensions/SQL2Wiki/SQL2Wiki.php" );
+EOT;
+ exit( 1 );
+}
+$wgExtensionCredits['parserhook'][] = array(
+ 'name' => 'SQL2Wiki',
+ 'author' => 'freakolowsky [Jure Kajzer] original version by Patrick
M\xFCller',
+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SQL2Wiki',
+ 'description' => 'Show SQL data directly in the page contents..',
+ 'descriptionmsg' => 'sql2wiki-desc',
+ 'version' => '1.0.0',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SQL2Wiki',
+ 'author' => 'Jure Kajzer',
+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SQL2Wiki',
+ 'description' => 'Run SQL2Wiki code on-click',
+ 'descriptionmsg' => 'sql2wiki-special',
+ 'version' => '1.0.0',
+);
+
+$dir = dirname(__FILE__) . '/';
+$wgExtensionMessagesFiles['SQL2Wiki'] = $dir . 'SQL2Wiki.i18n.php';
+
+$wgAutoloadClasses['SQL2Wiki'] = $dir . 'SQL2Wiki.body.php';
+$wgHooks['ParserFirstCallInit'][] = 'SQL2Wiki::initTags';
+
+$wgAutoloadClasses['SpecialSQL2Wiki'] = $dir . 'SpecialSQL2Wiki.php';
+$wgSpecialPages['SQL2Wiki'] = 'SpecialSQL2Wiki';
+
+
+// list of database contact data
+$wgExSql2WikiDatabases = array();
+
+
+
+/*
+
+
+
+
+
+
+
+
+$wgExtensionFunctions[] = "wfSQL2Wiki";
+
+$wgAutoloadClasses['sql2wiki'] = $dir . 'SQL2Wiki_body.php';
+$wgExtensionMessagesFiles['sql2wiki'] = $dir . 'SQL2Wiki.i18n.php';
+$wgSpecialPages['sql2wiki'] = 'SQL2Wiki';
+
+$sql2wiki_DB_handles = array();
+
+require_once($dir . 'SQL2Wiki_body.php');
+
+$wgOracleDBMSEnabled = false;
+
+function SQL2Wiki_enableDBMSOutput($dbObj, $state = true) {
+ global $wgOracleDBMSEnabled;
+ $wgOracleDBMSEnabled = $state;
+ if ($state)
+ $dbObj->doQuery ('begin dbms_output.enable(null); end;');
+ else
+ $dbObj->doQuery ('begin dbms_output.disable; end;');
+}
+
+function SQL2Wiki_getDBMSOutput($dbObj) {
+ global $wgOracleDBMSEnabled;
+
+ if ($wgOracleDBMSEnabled === false)
+ return false;
+
+ $out = array();
+ $qReturn = null;
+ $wdc = 0;
+ $qReturn = $dbObj->doQuery('select column_value from
table(get_output_lines())');
+ while(($qLine = $qReturn->fetchObject()) !== FALSE) {
+ $out[] = $qLine->column_value;
+ }
+
+ return $out;
+}
+
+function wfSQL2Wiki() {
+ global $wgParser;
+ $wgParser->setHook( "sql2wiki", "renderSQL" );
+ $wgParser->setHook( "plsql2wiki", "renderPLSQL" );
+}
+
+function
SQL2Wiki_execute($db,$input,$enable_output,&$dbObj,&$result,&$output,&$error){
+ global $wgDBtype;
+ global $sql2wiki_DB_handles;
+
+ reset ($sql2wiki_DB_handles);
+ $bFound = false;
+ while(list($index,$val)=each($sql2wiki_DB_handles)) {
+ if ( $db == $index ) {
+ $aParams = explode(";", $val);
+
+ foreach($aParams as $parameter) {
+ if( count( $aParams ) < 3 ){
+ $error="Error in DB_handler
definition !";
+ return false;
+ }
+ $host = trim($aParams[0]);
+ $user = trim($aParams[1]);
+ $pass = trim($aParams[2]);
+ $charset = trim($aParams[3]);
+ $bFound = true;
+ }
+ }
+ }
+ if ( !$bFound ){
+ $error="Error in DB_handler definition !";
+ return false;
+ }
+
+ $dbObj = new DatabaseOracle($db, $user, $pass, $host, false, 128);
+ if (!$dbObj->isOpen()){
+ $error='<b>SQL2Wiki failed to connect to DB
"'.$db.'"</b>';
+ return false;
+ }
+
+ $ignore = $dbObj->ignoreErrors(true);
+
+ SQL2Wiki_enableDBMSOutput($dbObj, $enable_output);
+
+ $result = $dbObj->doQuery ($input);
+
+ $output='';
+ if ($enable_output){
+ $output = implode(SQL2Wiki_getDBMSOutput($dbObj), "\n");
+ if ($output === false){
+ $error='<b>SQL2Wiki completed successfully</b>';
+ return false;
+ }
+ }
+
+ $dbObj->ignoreErrors($ignore);
+
+ if ($dbObj->lastError() != null){
+ $error='<b>SQL2Wiki exited with error: '.$dbObj->lastError().'
'.$input.'</b>';
+ return false;
+ }
+ elseif (isset($argv["quiet"])){
+ $error='';
+ return false;
+ }
+
+ return true;
+}
+
+
+function renderSQL( $input, $argv, &$parser ) {
+ global $wgOut;
+
+ $db = $argv["database"];
+
+ if (isset($argv["preexpand"]) && $argv["preexpand"] == 'true') {
+ $input = $parser->recursiveTagParse($input);
+ }
+
+ if (!SQL2Wiki_execute($db,$input,false,$dbObj,$result,$output,$error))
+ return $error;
+
+ if (isset($argv["inline"])) {
+ $field_separator = isset($argv["fieldseparator"]) ?
$argv["fieldseparator"] : '';
+ $line_separator = isset($argv["lineseparator"]) ?
$argv["lineseparator"] : '';
+
+ while (($line = $result->fetchObject()) !== FALSE) {
+ if ($output != '')
+ $output .= $line_separator."\n";
+
+ foreach ($line as $value){
+ $output .= $value.$field_separator;
+ }
+ $output = substr($output, 0,
strlen($output)-strlen($field_separator));
+ }
+
+ if ($argv["inline"] != '') {
+ $other_params = '';
+ foreach($argv as $key=>$value)
+ if (!in_array($key, array('database', 'inline',
'fieldseparator', 'lineseparator', 'cache', 'expand', 'preexpand')))
+ $other_params .= '
'.$key.'="'.trim($value, '"').'"';
+ $output =
'<'.$argv["inline"].$other_params.'>'.$output.'</'.$argv["inline"].'>';
+ }
+ } else {
+ $table_style = isset($argv["tablestyle"]) ? '
style="'.$argv["tablestyle"].'" ' : ' style="border: black solid 1px;" ';
+ $h_row_style = isset($argv["hrowstyle"]) ? '
style="'.$argv["hrow_style"].'" ' : '';
+ $h_cell_style = isset($argv["hcellstyle"]) ? '
style="'.$argv["hcellstyle"].'" ' : ' style="font-weight: bold;" ';
+ $row_style = isset($argv["rowstyle"]) ? '
style="'.$argv["rowstyle"].'" ' : '';
+ $cell_style = isset($argv["cellstyle"]) ? '
style="'.$argv["cellstyle"].'" ' : '';
+
+ # Create Table Header
+ $output .= '<table border=1 cellspacing=0
cellpadding=3'.$table_style.'>';
+ $output .= '<tr'.$h_row_style.'>';
+ $line = $result->fetchObject();
+ foreach ($line as $key=>$value) {
+ $output .= '<th'.$h_cell_style.'>'.$key.'</th>';
+ }
+ $output .= '</tr>';
+
+ # Create Table Data Rows
+ do {
+ $output .= '<tr'.$row_style.'>';
+ foreach ($line as $value){
+ $output .= '<td'.$cell_style.'>'.$value.'</td>';
+
+ }
+ $output .= '</tr>';
+ } while (($line = $result->fetchObject()) !== FALSE);
+ # End of Table Tag
+ $output .= '</table>';
+ }
+
+
+ if (isset($argv["cache"]) && $argv["cache"] == 'off') {
+ $parser->disableCache();
+ } elseif (isset($argv["cache"]) && $argv["cache"] == 'manual') {
+ if (!isset($argv["inline"])) {
+ $refresh_url =
preg_replace('/(.*?)&action=[^&]*(.*)/i', '$1$2', $_SERVER['REQUEST_URI']).
+ '&action=purge';
+ $output .= '<a
href="'.$refresh_url.'"><small>Refresh</small></a>';
+ }
+ } elseif (isset($argv["inline"])) {
+ $parser->disableCache();
+ }
+
+ if ($wgOut->getPageTitle() != 'SQL2Wiki' && isset($argv["expand"]) &&
$argv["expand"] == 'true') {
+ $output = $parser->recursiveTagParse($output);
+ }
+
+ @$dbObj->close();
+ return $output;
+}
+
+function renderPLSQL( $input, $argv, &$parser ) {
+ global $wgDBtype;
+ global $wgOut;
+
+ $db = $argv["database"];
+
+ if (strtolower($wgDBtype) != 'oracle' && strtolower($wgDBtype) !=
'oracless')
+ return '<b>This function is available only for Oracle and
OracleSS DB class.</b>';
+
+ $dbms_output = isset($argv["dbmsoutput"]) ? $argv["dbmsoutput"] : false;
+
+ if (isset($argv["preexpand"]) && $argv["preexpand"] == 'true') {
+ $input = $parser->recursiveTagParse($input);
+ }
+
+ if
(!SQL2Wiki_execute($db,$input,$dbms_output,$dbObj,$result,$output,$error))
+ return $error;
+
+ $wrapper = isset($argv["wrapper"]) ? $argv["wrapper"] : '';
+ if ($wrapper != '') {
+ $other_params = '';
+ foreach($argv as $key=>$value)
+ if (!in_array($key, array('database', 'wrapper',
'quiet', 'cache', 'expand', 'preexpand', 'dbmsoutput')))
+ $other_params .= ' '.$key.'="'.trim($value,
'"').'"';
+
+ $output =
'<'.$wrapper.$other_params.'>'.$output.'</'.$wrapper.'>';
+ }
+
+ if (!isset($argv["cache"]) || $argv["cache"] != 'on') {
+ $parser->disableCache();
+ }
+
+ if ($wgOut->getPageTitle() != 'SQL2Wiki' && isset($argv["expand"]) &&
$argv["expand"] == 'true' ) {
+ $output = $parser->recursiveTagParse($output);
+ }
+
+ @$dbObj->close();
+ return $output;
+}
+
+*/
Property changes on: trunk/extensions/SQL2Wiki/SQL2Wiki.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/extensions/SQL2Wiki/SQL2Wiki.php~
===================================================================
--- trunk/extensions/SQL2Wiki/SQL2Wiki.php~ (rev 0)
+++ trunk/extensions/SQL2Wiki/SQL2Wiki.php~ 2011-05-30 07:37:38 UTC (rev
89153)
@@ -0,0 +1,260 @@
+<?php
+
+if (!defined('MEDIAWIKI')) die();
+
+$wgExtensionCredits['parserhook'][] = array(
+ 'name' => 'SQL2Wiki',
+ 'author' => 'Patrick M\xFCller (Jure Kajzer - Oracle port)',
+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SQL2Wiki',
+ 'description' => 'This is modified Oracle version of this extension',
+ 'descriptionmsg' => 'sql2wiki-oracle_mod',
+ 'version' => '0.0.1',
+);
+
+$wgExtensionCredits['specialpage'][] = array(
+ 'name' => 'SQL2Wiki',
+ 'author' => 'Jure Kajzer',
+ 'url' => 'http://www.mediawiki.org/wiki/Extension:SQL2Wiki',
+ 'description' => 'Run SQL2Wiki code on-click',
+ 'descriptionmsg' => 'sql2wiki-special',
+ 'version' => '0.0.1',
+);
+
+$wgExtensionFunctions[] = "wfSQL2Wiki";
+
+$dir = dirname(__FILE__) . '/';
+$wgAutoloadClasses['sql2wiki'] = $dir . 'SQL2Wiki_body.php';
+$wgExtensionMessagesFiles['sql2wiki'] = $dir . 'SQL2Wiki.i18n.php';
+$wgSpecialPages['sql2wiki'] = 'SQL2Wiki';
+
+$sql2wiki_DB_handles = array(
+ "orac" =>
"orac.abakus.si;abakus_wiki;abakus_wiki",
+ "abakus" =>
"abakus.abakus.si;abakus_wiki;abakus_wiki",
+ "ipa" => "abakus.abakus.si;ipa_wiki;ipa_wiki");
+
+require_once($dir . 'SQL2Wiki_body.php');
+
+$wgOracleDBMSEnabled = false;
+
+function SQL2Wiki_enableDBMSOutput($dbObj, $state = true) {
+ global $wgOracleDBMSEnabled;
+ $wgOracleDBMSEnabled = $state;
+ if ($state)
+ $dbObj->doQuery ('begin dbms_output.enable(null); end;');
+ else
+ $dbObj->doQuery ('begin dbms_output.disable; end;');
+}
+
+function SQL2Wiki_getDBMSOutput($dbObj) {
+ global $wgOracleDBMSEnabled;
+
+ if ($wgOracleDBMSEnabled === false)
+ return false;
+
+ $out = array();
+ $qReturn = null;
+ $wdc = 0;
+ $qReturn = $dbObj->doQuery('select column_value from
table(get_output_lines())');
+ while(($qLine = $qReturn->fetchObject()) !== FALSE) {
+ $out[] = $qLine->column_value;
+ }
+
+ return $out;
+}
+
+function wfSQL2Wiki() {
+ global $wgParser;
+ $wgParser->setHook( "sql2wiki", "renderSQL" );
+ $wgParser->setHook( "plsql2wiki", "renderPLSQL" );
+}
+
+function
SQL2Wiki_execute($db,$input,$enable_output,&$dbObj,&$result,&$output,&$error){
+ global $wgDBtype;
+ global $sql2wiki_DB_handles;
+
+ reset ($sql2wiki_DB_handles);
+ $bFound = false;
+ while(list($index,$val)=each($sql2wiki_DB_handles)) {
+ if ( $db == $index ) {
+ $aParams = explode(";", $val);
+
+ foreach($aParams as $parameter) {
+ if( count( $aParams ) < 3 ){
+ $error="Error in DB_handler
definition !";
+ return false;
+ }
+ $host = trim($aParams[0]);
+ $user = trim($aParams[1]);
+ $pass = trim($aParams[2]);
+ $charset = trim($aParams[3]);
+ $bFound = true;
+ }
+ }
+ }
+ if ( !$bFound ){
+ $error="Error in DB_handler definition !";
+ return false;
+ }
+
+ $dbObj = new DatabaseOracle($db, $user, $pass, $host, false, 128);
+ if (!$dbObj->isOpen()){
+ $error='<b>SQL2Wiki failed to connect to DB
"'.$db.'"</b>';
+ return false;
+ }
+
+ $ignore = $dbObj->ignoreErrors(true);
+
+ SQL2Wiki_enableDBMSOutput($dbObj, $enable_output);
+
+ $result = $dbObj->doQuery ($input);
+
+ $output='';
+ if ($enable_output){
+ $output = implode(SQL2Wiki_getDBMSOutput($dbObj), "\n");
+ if ($output === false){
+ $error='<b>SQL2Wiki completed successfully</b>';
+ return false;
+ }
+ }
+
+ $dbObj->ignoreErrors($ignore);
+
+ if ($dbObj->lastError() != null){
+ $error='<b>SQL2Wiki exited with error: '.$dbObj->lastError().'
'.$input.'</b>';
+ return false;
+ }
+ elseif (isset($argv["quiet"])){
+ $error='';
+ return false;
+ }
+
+ return true;
+}
+
+
+function renderSQL( $input, $argv, &$parser ) {
+ global $wgOut;
+
+ $db = $argv["database"];
+
+ if (isset($argv["preexpand"]) && $argv["preexpand"] == 'true') {
+ $input = $parser->recursiveTagParse($input);
+ }
+
+ if (!SQL2Wiki_execute($db,$input,false,$dbObj,$result,$output,$error))
+ return $error;
+
+ if (isset($argv["inline"])) {
+ $field_separator = isset($argv["fieldseparator"]) ?
$argv["fieldseparator"] : '';
+ $line_separator = isset($argv["lineseparator"]) ?
$argv["lineseparator"] : '';
+
+ while (($line = $result->fetchObject()) !== FALSE) {
+ if ($output != '')
+ $output .= $line_separator."\n";
+
+ foreach ($line as $value){
+ $output .= $value.$field_separator;
+ }
+ $output = substr($output, 0,
strlen($output)-strlen($field_separator));
+ }
+
+ if ($argv["inline"] != '') {
+ $other_params = '';
+ foreach($argv as $key=>$value)
+ if (!in_array($key, array('database', 'inline',
'fieldseparator', 'lineseparator', 'cache', 'expand', 'preexpand')))
+ $other_params .= '
'.$key.'="'.trim($value, '"').'"';
+ $output =
'<'.$argv["inline"].$other_params.'>'.$output.'</'.$argv["inline"].'>';
+ }
+ } else {
+ $table_style = isset($argv["tablestyle"]) ? '
style="'.$argv["tablestyle"].'" ' : ' style="border: black solid 1px;" ';
+ $h_row_style = isset($argv["hrowstyle"]) ? '
style="'.$argv["hrow_style"].'" ' : '';
+ $h_cell_style = isset($argv["hcellstyle"]) ? '
style="'.$argv["hcellstyle"].'" ' : ' style="font-weight: bold;" ';
+ $row_style = isset($argv["rowstyle"]) ? '
style="'.$argv["rowstyle"].'" ' : '';
+ $cell_style = isset($argv["cellstyle"]) ? '
style="'.$argv["cellstyle"].'" ' : '';
+
+ # Create Table Header
+ $output .= '<table border=1 cellspacing=0
cellpadding=3'.$table_style.'>';
+ $output .= '<tr'.$h_row_style.'>';
+ $line = $result->fetchObject();
+ foreach ($line as $key=>$value) {
+ $output .= '<th'.$h_cell_style.'>'.$key.'</th>';
+ }
+ $output .= '</tr>';
+
+ # Create Table Data Rows
+ do {
+ $output .= '<tr'.$row_style.'>';
+ foreach ($line as $value){
+ $output .= '<td'.$cell_style.'>'.$value.'</td>';
+
+ }
+ $output .= '</tr>';
+ } while (($line = $result->fetchObject()) !== FALSE);
+ # End of Table Tag
+ $output .= '</table>';
+ }
+
+
+ if (isset($argv["cache"]) && $argv["cache"] == 'off') {
+ $parser->disableCache();
+ } elseif (isset($argv["cache"]) && $argv["cache"] == 'manual') {
+ if (!isset($argv["inline"])) {
+ $refresh_url =
preg_replace('/(.*?)&action=[^&]*(.*)/i', '$1$2', $_SERVER['REQUEST_URI']).
+ '&action=purge';
+ $output .= '<a
href="'.$refresh_url.'"><small>Refresh</small></a>';
+ }
+ } elseif (isset($argv["inline"])) {
+ $parser->disableCache();
+ }
+
+ if ($wgOut->getPageTitle() != 'SQL2Wiki' && isset($argv["expand"]) &&
$argv["expand"] == 'true') {
+wfDebug(strlen($output)." ---------------------\n$output\n\n\n");
+ $output = $parser->recursiveTagParse($output);
+wfDebug(strlen($output)." ---------------------\n");
+ }
+
+ @$dbObj->close();
+ return $output;
+}
+
+function renderPLSQL( $input, $argv, &$parser ) {
+ global $wgDBtype;
+ global $wgOut;
+
+ $db = $argv["database"];
+
+ if (strtolower($wgDBtype) != 'oracle' && strtolower($wgDBtype) !=
'oracless')
+ return '<b>This function is available only for Oracle and
OracleSS DB class.</b>';
+
+ $dbms_output = isset($argv["dbmsoutput"]) ? $argv["dbmsoutput"] : false;
+
+ if (isset($argv["preexpand"]) && $argv["preexpand"] == 'true') {
+ $input = $parser->recursiveTagParse($input);
+ }
+
+ if
(!SQL2Wiki_execute($db,$input,$dbms_output,$dbObj,$result,$output,$error))
+ return $error;
+
+ $wrapper = isset($argv["wrapper"]) ? $argv["wrapper"] : '';
+ if ($wrapper != '') {
+ $other_params = '';
+ foreach($argv as $key=>$value)
+ if (!in_array($key, array('database', 'wrapper',
'quiet', 'cache', 'expand', 'preexpand', 'dbmsoutput')))
+ $other_params .= ' '.$key.'="'.trim($value,
'"').'"';
+
+ $output =
'<'.$wrapper.$other_params.'>'.$output.'</'.$wrapper.'>';
+ }
+
+ if (!isset($argv["cache"]) || $argv["cache"] != 'on') {
+ $parser->disableCache();
+ }
+
+ if ($wgOut->getPageTitle() != 'SQL2Wiki' && isset($argv["expand"]) &&
$argv["expand"] == 'true' ) {
+ $output = $parser->recursiveTagParse($output);
+ }
+
+ @$dbObj->close();
+ return $output;
+}
+
+?>
Added: trunk/extensions/SQL2Wiki/SpecialSQL2Wiki.php
===================================================================
--- trunk/extensions/SQL2Wiki/SpecialSQL2Wiki.php
(rev 0)
+++ trunk/extensions/SQL2Wiki/SpecialSQL2Wiki.php 2011-05-30 07:37:38 UTC
(rev 89153)
@@ -0,0 +1,37 @@
+<?php
+
+class SpecialSQL2Wiki extends SpecialPage {
+
+ public function __construct() {
+ parent::__construct( 'SQL2Wiki' );
+ }
+
+ function execute($par) {
+ global $wgOut, $wgParser;
+
+ $this->setHeaders();
+
+ $pars = explode('/', str_replace('\\', ' ', $par));
+
+ $type = array_shift($pars);
+ $value = array_shift($pars);
+
+ $argv = array();
+ foreach ($pars as $arg) {
+ $subarg = explode('=', $arg);
+ $argv[$subarg[0]] = $subarg[1];
+ }
+ $argv['cache'] = 'on';
+ $argv['preexpand'] = 'false';
+ $argv['expand'] = 'false';
+
+ if (strtolower($type) == 'sql')
+ $wgOut->addHTML(SQL2Wiki::renderSQL($value, $argv,
$wgParser, null));
+ elseif (strtolower($type) == 'plsql')
+ $wgOut->addHTML(SQL2Wiki::renderPLSQL($value, $argv,
$wgParser, null));
+ else
+
$wgOut->addWikiText('<b>'.wfMsg('sql2wiki-err-invalid_type').'</b>');
+
+ }
+
+}
Property changes on: trunk/extensions/SQL2Wiki/SpecialSQL2Wiki.php
___________________________________________________________________
Added: svn:eol-style
+ native
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs