Extension:SQLselect

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
SQLselect

Release status: unknown

Implementation  Tag
Description Embed the output of an SQL query in a wiki page
Author(s)  stixuser
Last Version  1.0 (2007-02-22)
MediaWiki  1.9
License No license specified
Download this page

check usage (experimental)

With this simple extension you can include the output of SQL queries in a wiki page.

I use it to create pages with links to booktitles. The booktitles and associated filenames are stored in an SQL database. In my wiki pages I use a command like:

<SQLselect>title like '%ubuntu%' or title like '%linux%'</SQLselect>

to list all books about Ubuntu or Linux.

Enough talk, here's what you have to do to get it going:

[edit] Update LocalSettings.php

Add the following line at the bottom of LocalSettings.php:

include("extensions/SQLselect.php");

[edit] Create SQLselect.php

Copy the following (quick and dirty) code and save it to ../extensions/SQLselect.php
Note that you need to customize this code: specify your own DB user and password, and tailor the SQL processing code to your needs.

 <?php
 # Tag : 
 #   <sqlselect>valid sql query</sqlselect>
 #   i.e. <sqlselect>title like '%ubuntu%' or title like '%linux%'</sqlselect> 
 #
 # Requires :
 #   -
 # 
 # To activate the extension :
 # - include it at the end of your LocalSettings.php: include("extensions/SQLselect.php");
 # 
 $wgExtensionFunctions[] = 'wfsqlselect';
 $wgExtensionCredits['parserhook'][] = array(
        'name' => 'sqlselect',
        'description' => 'embed the output of SQL queries in wiki pages' ,                        
        'author' => 'stixuser',
        'url' => 'http://www.mediawiki.org/wiki/Extension:SQLselect'
 );
 function wfsqlselect() {
        global $wgParser;
        $wgParser->setHook('sqlselect', 'rendersqlselect');
 }
 # The callback function for converting the input SQL query to HTML output
 function rendersqlselect($input, $argv, &$parser )
 {
  #important: caching must be disabled for this page!
  $parser->disableCache();
  global $wgScriptPath, $wgUploadPath;
  if (!defined(title))
  {
    define("title","title"); 
    define("filename","filename"); 
  }
  # enter your own SQL DB definitions here (or use an include)
  $hostname = "localhost"; 
  $username = "myuser"; 
  $password = "mypassword"; 
  $dbname   = "books"; 
  # open the database
  mysql_connect($hostname, $username, $password) or die(mysql_error()); 
  mysql_select_db("$dbname") or die(mysql_error()); 
  # execute the SQL query ; note that there's no errorchecking on the inputstring in arr[0]
  # see the Discussion page for comments on the next line
  $query = "select * from `books` where ".$arr[0]." order by title"; 
  $result = mysql_query($query) or die(mysql_error()); 
  $number = mysql_num_rows($result); 
  $output = '';
  # the following code processes the results of the query. Adapt to your own needs.
  $i = 0; 
  while ($i < $number)
  { 
    $row = mysql_fetch_array($result); 
    $filename = $row[filename]; 
    $title    = $row[title]; 
    $icon     = "file_icon.gif";
    $dotpos   = strrpos($filename,".");
    if ($dotpos)
    {
      $ext = substr($filename,$dotpos+1);
      if ($ext == "pdf") $icon = "pdf_icon.gif";
    }
    $output = $output.'<img src="'.$wgUploadPath.'/'.$icon.'">&nbsp;<a href="'.$wgUploadPath.$filename.'" target="_book">'.$title.'</a><br>';
    $i++;
  } 
  return $output;
 }