Extension:Database

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Database

Release status: stable

CloudComputingIncidentsDatabase.png
Implementation Special page
Description Database functionality creates sequential, preloaded articles as 'records' (eg KB-0001)
Last version 1.01 (2008-09-20)
License GPLv3
Download Extension:Database/version_1.0#Code
Example Cloud Computing Incidents Database (CCID)
Parameters

$wgDatabaseConfigNameSpace, $wgDatabaseConfigPrefix, $wgDatabaseConfigPadding, $wgDatabaseConfigPreload

Check usage (experimental)

Contents

[edit] What can this extension do?

Special page that provides a records based database simply by redirecting to the next sequential identifier (eg DB-001, DB-002) and preloading from a specified template

[edit] Usage

This is a simple, secure and elegant solution to the problem of turning MediaWiki into a simple database. It creates new records sequentially (eg KB-0001, KB-0002 ... KB-nnnn) by scanning for the next available ID.

It requires ZERO configuration (in which case your records will be numeric with no padding, no preloaded text and stored in the main namespace) but can be configured to taste (eg prefixed with 'KB-', zero padded to 4 digits, preloaded with a knowledge base template and stored in the 'KB' namespace).

Any requests for Special:Database will result in a single select on the database to determine the next available ID, and then redirect the user to the edit form for that page. It fails safe (in the worst case you will edit an existing article) and does not accept any user input so should be secure.

[edit] Download instructions

Please cut and paste the below and place it in $IP/extensions/Database/Database.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension:

  1. Download the code as above
  2. Set up dedicated custom namespace (Optional)
  3. Add the following to LocalSettings.php:
  #$wgDatabaseConfigNameSpace="KB"; // optional namespace (eg "KB")
  #$wgDatabaseConfigPrefix="KB-";   // optional article prefix (eg "KB-")
  #$wgDatabaseConfigPadding=4;      // pad numbers eg 007 instead of 7; 0 for no padding
  #$wgDatabaseConfigPreload="new";  // optional article to be preloaded
  require_once("extensions/Database/Database.php");
  1. Uncomment and configure to taste (Optional)
  2. Create the article to be preloaded (eg KB:new) (Optional)
  3. Browse to Special:Database and verify that you are redirected to a new page (eg KB-0001)
  4. Create the new page and browse to Special:Database again to see that it is incremented (eg KB-0002)

[edit] Configuration parameters

  • $wgDatabaseConfigNameSpace is the namespace in which articles will be created and from which the ID is generated (eg "KB") (Optional)
  • $wgDatabaseConfigPrefix is the article prefix which among other things allows database records to cohabitate with other articles (eg "KB-") (Optional)
  • $wgDatabaseConfigPadding pad numbers eg set to '3' you'll get 007 instead of 7; 0 for no padding (Optional)
  • $wgDatabaseConfigPreload article to be preloaded when creating new records (Optional)

[edit] Code

<?php
/**
 * Special page that provides a records based database simply by redirecting to the next sequential
 * identifier (eg DB-001, DB-002) and preloading from a specified template
 * 
 * Use of custom namespaces (rather than the main namespace) is optional, but usually a good idea:
 *   Add this to LocalSettings.php:
 *      $wgExtraNamespaces[100] = "KB";
 *      $wgContentNamespaces[] = 100;
 * For more info see http://www.mediawiki.org/wiki/Manual:Using_custom_namespaces
 *
 * To install, copy to your extensions directory and add this line to LocalSettings.php: 
 *   $wgDatabaseConfigNameSpace="KB"; // optional namespace (eg "KB")
 *   $wgDatabaseConfigPrefix="KB-";   // optional article prefix (eg "KB-")
 *   $wgDatabaseConfigPadding=4;      // optional id padding eg 007 instead of 7; 0 for no padding
 *   $wgDatabaseConfigPreload="new";  // optional article to be preloaded
 *   require_once("extensions/Database/Database.php");
 *
 * Author: Sam Johnston <samj@samj.net>, Australian Online Solutions (September 2008)
 * Original Author: Barry Brannan (August 2007)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
if( defined( 'MEDIAWIKI' ) ) {
 
  require_once( 'SpecialPage.php' );
 
  $wgExtensionFunctions[] = 'efDatabaseExtn';
  $wgExtensionCredits['specialpage'][] = array( 
    'name' => 'Database', 
    'author' => array('Sam Johnston', 'Barry Brannan'),
    'url' => 'http://www.mediawiki.org/wiki/Extension:Database',
    'description' => 'Create a database of articles from a template and numbered sequentially (eg knowledgebase)' );
 
  function efDatabaseExtn() {
    global $wgMessageCache;
    SpecialPage::addPage( new DatabaseExtn() );
    $wgMessageCache->addMessage( 'database', 'Add sequential database record from template' );
  }
 
  class DatabaseExtn extends IncludableSpecialPage {
 
    function DatabaseExtn() {
      SpecialPage::SpecialPage( 'Database', '', true, false, 'default', true );
    }
 
    function execute( $par ) {        
      global $wgOut, $wgContLang, $wgDatabaseConfigNameSpace, $wgDatabaseConfigPrefix,
        $wgDatabaseConfigPadding, $wgDatabaseConfigPreload;
      /* optional fields don't need to be set */
      $wgDatabaseConfigNameSpace = isset($wgDatabaseConfigNameSpace) ? $wgDatabaseConfigNameSpace : '';
      $wgDatabaseConfigPrefix = isset($wgDatabaseConfigPrefix) ? $wgDatabaseConfigPrefix : '';
      $wgDatabaseConfigPadding = isset($wgDatabaseConfigPadding) ? $wgDatabaseConfigPadding : 0;
 
      $ns_id = $wgContLang->getNsIndex( $wgDatabaseConfigNameSpace );
      if ($ns_id == '')
        $ns_id = NS_MAIN;
 
      $dbr = wfGetDB( DB_SLAVE );
      $page = $dbr->tableName( 'page' );
      $id_start = strlen($wgDatabaseConfigPrefix) + 1;
      $id_end   = $id_start + $wgDatabaseConfigPadding - 1;
      $sql = "SELECT MAX(CAST(SUBSTR(page_title, $id_start, $id_end) AS unsigned))+1 AS next_page
                FROM $page 
                WHERE page_namespace = $ns_id AND page_title LIKE '$wgDatabaseConfigPrefix%'";
 
      $res = $dbr->query( $sql );
      $row = $dbr->fetchObject( $res );
 
      if ($row->next_page == '')  // no exiting records, create starting with number 1 
        $title_text = '1';
      else
        $title_text = $row->next_page;
 
      $title_text = $wgDatabaseConfigPrefix .
        str_pad($title_text, $wgDatabaseConfigPadding, '0', STR_PAD_LEFT);
 
      $title = Title::makeTitleSafe( $ns_id, $title_text );
      $title_url = $title->getFullUrl();
      if (isset($wgDatabaseConfigPreload)) {
            $title_url .= '?action=edit&preload=';
            if ($wgDatabaseConfigNameSpace) {
                  $title_url .= $wgDatabaseConfigNameSpace . ':';
            }
        $title_url .= $wgDatabaseConfigPreload;
      }
      $wgOut->redirect( $title_url );
    }
 
  }
 
} else {
  echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
  die( -1 );
}

[edit] Feature requests

  • Suffixes so that a suffix can be appended to the title in the same way that a prefix can. (Added in 1.01)

[edit] Production installations

[edit] See also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox