Extension:Database/version 1.0

From MediaWiki.org
Jump to: navigation, search

[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 );
}
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox