Extension:Referenceselector

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

Release status: beta

Referenceselector-0.1-screen.png
Implementation Page action, Special page
Description Adds a button to the edit page for finding mediafiles to "attach" or insert a reference ([[Image:]]) to at the cursor position.
Author(s) mjsTalk
Last version 0.2 (2008-01-20)
MediaWiki 1.11, possible lower
License GPLv2+
Download Download (simply use unzip on it)
Hooks used
EditPage::showEditForm:fields

Check usage (experimental)

Contents

[edit] What can this extension do?

Adds a button to the edit page for finding previously uploaded mediafiles to "attach" or insert a reference ([[Image:]], [[Media:]], {{file|}}) to at the cursor position.

[edit] Usage

[edit] Changelog

0.2 (2008-01-20)
applied changes to make it work with MSIE by Kaizen
0.1 (2007-11-02)
initial version


[edit] Download instructions

Download the file linked in the infobox and unzip, it's a zip file embedded inside an image. Extract it into your $IP/extensions directory

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/referenceselector/referenceselector.php");

[edit] Configuration parameters

currently none. extension needs to be improved to provide more customizability


[edit] Current status

The Informational Wiki Group has displayed the code so the upkeep can be maintained. The code is functional however there is some display errors with,

 < >

(<> or greater than and less than). Post the patch here for all occurrences.

The file structure is as such:
Create a folder called referenceselector that is put within the extensions folder.
In this folder goes 2 more folders gfx and js with the php files:

extensions

  • referenceselector (folder)
  • gfx (folder)
  1. button_rs.png
  2. style.css
  • js (folder)
  1. browser.js
  2. editpage.js
  • referenceselector.php
  • ReferenceSelectorExt.php
  • ReferenceSelectorPage.php
  • SpecialImagelist.php (This file goes outside the extensions folder into the includes folder)

[edit] Patch(es) for the Htmlenites Issue

None yet.



[edit] referenceselector.php

<?php
 
/**
* @author mjs
* @license GPLv2+
*/
 
$wgC7RS_dir = dirname(__FILE__);
$wgC7RS_pubdir = 'referenceselector';
 
$wgAutoloadClasses['ReferenceSelectorExt'] = 'ReferenceSelectorExt.php';
$wgAutoloadClasses['ReferenceSelectorPage'] = 'ReferenceSelectorPage.php';
 
$wgExtensionCredits['specialpage'][] = array(
        'name' => '.core//7 MediaWiki Reference Selector',
        'author' =>'mjs', 
        'url' => 'http://www.mediawiki.org/wiki/Extension:Referenceselector', 
        #'description' => 'This Extension summons dragons'
 'description' => 'Adds a button to the edit page for finding mediafiles to "attach" or insert a reference ([[Image:]]) to at the cursor position.',
        'version' => '0.2'
       );
 
function ReferenceSelector_init() {
        global $wgHooks;
        $obj = new ReferenceSelectorExt();
 
        #$wgHooks['ParserAfterTidy'][] = $obj;
 #$wgHooks['EditPage::showEditForm:initial'][] = array($obj, 'onEditPage__showEditForm_initial');
 $wgHooks['EditPage::showEditForm:fields'][] = array($obj, 'onEditPage__showEditForm_fields');
}
 
 
$wgExtensionFunctions[] = 'ReferenceSelector_init';
$wgSpecialPages['ReferenceSelectorPage'] = 'ReferenceSelectorPage';


[edit] ReferenceSelectorExt.php

<?php
 
/**
* @author mjs
* @license GPLv2+
*/
 
class ReferenceSelectorExt {
        function __construct() {
        }
 
        function onEditPage__showEditForm_fields($editpage, $wgOut) {
                global $wgC7RS_pubdir;
 
                #add reference to the javascript file.. that is enough
         $wgOut->addHTML("<script type='text/javascript' src='$wgC7RS_pubdir/js/editpage.js'></script>");
                return true;
        }
}


[edit] ReferenceSelectorPage.php

<?php
 
/**
* @author mjs
* @license GPLv2+
*/
 
global $IP;
require_once "SpecialImagelist.php";
 
class C7RS_ImageListPager extends ImageListPager {
        function getFieldNames() {
                if ( !$this->mFieldNames ) {
                        $this->mFieldNames = array(
                                'img_timestamp' => wfMsg( 'imagelist_date' ),
                                'img_name' => wfMsg( 'imagelist_name' ),
                                #'img_user_text' => wfMsg( 'imagelist_user' ),
                         #'img_size' => wfMsg( 'imagelist_size' ),
                         #'img_description' => wfMsg( 'imagelist_description' ),
                 );
                }
                return $this->mFieldNames;
        }
 
        function formatValue($field,$value) {
                switch($field) {
                        case 'img_name':
                                /*
                                $name = $this->mCurrentRow->img_name;
                                $link = $this->getSkin()->makeKnownLinkObj( Title::makeTitle( NS_IMAGE, $name ), $value );
                                $image = wfLocalFile( $value );
                                $url = $image->getURL();
                                $download = Xml::element('a', array( "href" => $url ), $this->mMessages['imgfile'] );
                                return "$link ($download)";
                                */
 
                                $name = $this->mCurrentRow->img_name;
                                $link_1 = Xml::element('a',
                                        array( "href" => "javascript:doInsert('[[Image:$value]]', '', '')" ),
                                        $name );
 
                                $link_m = Xml::element('a',
                                        array( "href" => "javascript:doInsert('[[Media:$value]]', '', '')" ),
                                        "M" );
 
                                $link_f = Xml::element('a',
                                        array( "href" => "javascript:doInsert('{{file|$value}}', '', '')" ),
                                        "F" );
 
 
 
 
                                return "$link_1 ($link_m) ($link_f)";
                        default:
                                return parent::formatValue($field,$value);
                }
        }
}
 
class ReferenceSelectorPage extends SpecialPage {
        function __construct() {
                SpecialPage::SpecialPage("ReferenceSelectorPage");
        }
 
        function execute() {
                global $wgRequest, $wgOut;
                global $wgC7RS_pubdir;
 
 
                $this->setHeaders();
 
                //TODO: actually generate real html with head,body etc again - how?
                $wgOut->mArticleBodyOnly = true;
 
 
                $pager = new C7RS_ImageListPager();
 
                $limit = html_entity_decode($pager->getForm());
                $body = html_entity_decode($pager->getBody());
                $nav = html_entity_decode($pager->getNavigationBar());
 
                $wgOut->addHTML("<link rel='stylesheet' type='text/css' href='$wgC7RS_pubdir/gfx/style.css' />");
                $wgOut->addHTML("<script type='text/javascript' src='$wgC7RS_pubdir/js/browser.js'></script>");
 
                $wgOut->addHTML(
                        $limit
                        . '<br/>'
                        . $body
                        . '<br/>'
                        . $nav );
        }
}

[edit] SpecialImagelist.php

Put this file in your, /Your_MediaWiki/includes/specials/, folder.

<?php
/**
 *
 * @addtogroup SpecialPage
 */
 
/**
 *
 */
function wfSpecialImagelist() {
        global $wgOut;
 
        $pager = new ImageListPager;
 
        $limit = $pager->getForm();
        $body = $pager->getBody();
        $nav = $pager->getNavigationBar();
        $wgOut->addHTML(
                $limit
                . '<br/>'
                . $body
                . '<br/>'
                . $nav );
}
 
/**
 * @addtogroup SpecialPage
 * @addtogroup Pager
 */
class ImageListPager extends TablePager {
        var $mFieldNames = null;
        var $mMessages = array();
        var $mQueryConds = array();
 
        function __construct() {
                global $wgRequest, $wgMiserMode;
                if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ){
                        $this->mDefaultDirection = true;
                } else {
                        $this->mDefaultDirection = false;
                }
                $search = $wgRequest->getText( 'ilsearch' );
                if ( $search != '' && !$wgMiserMode ) {
                        $nt = Title::newFromUrl( $search );
                        if( $nt ) {
                                $dbr = wfGetDB( DB_SLAVE );
                                $m = $dbr->strencode( strtolower( $nt->getDBkey() ) );
                                $m = str_replace( "%", "\\%", $m );
                                $m = str_replace( "_", "\\_", $m );
                                $this->mQueryConds = array( "LOWER(img_name) LIKE '%{$m}%'" );
                        }
                }
 
                parent::__construct();
        }
 
        function getFieldNames() {
                if ( !$this->mFieldNames ) {
                        $this->mFieldNames = array(
                                'img_timestamp' => wrapWikiMsg( 'Image List Date' ),
                                'img_name' => wrapWikiMsg( 'Image List name' ),
                                'img_user_text' => wrapWikiMsg( 'imagelist_user' ),
                                'img_size' => wrapWikiMsg( 'imagelist_size' ),
                                'img_description' => wrapWikiMsg( 'imagelist_description' ),
                        );
                }
 
                return $this->mFieldNames;
        }
 
        function isFieldSortable( $field ) {
                static $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
                return in_array( $field, $sortable );
        }
 
        function getQueryInfo() {
                $fields = $this->getFieldNames();
                $fields = array_keys( $fields );
                $fields[] = 'img_user';
                return array(
                        'tables' => 'image',
                        'fields' => $fields,
                        'conds' => $this->mQueryConds
                );
        }
 
        function getDefaultSort() {
                return 'img_timestamp';
        }
 
        function getStartBody() {
                # Do a link batch query for user pages
         if ( $this->mResult->numRows() ) {
                        $lb = new LinkBatch;
                        $this->mResult->seek( 0 );
                        while ( $row = $this->mResult->fetchObject() ) {
                                if ( $row->img_user ) {
                                        @$lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
                                }
                        }
                        $lb->execute();
                }
 
                # Cache messages used in each row
         $this->mMessages['imgdesc'] = wfMsgHtml( 'imgdesc' );
                $this->mMessages['imgfile'] = wfMsgHtml( 'imgfile' );
 
                return parent::getStartBody();
        }
 
        function formatValue( $field, $value ) {
                global $wgLang;
                switch ( $field ) {
                        case 'img_timestamp':
                                return $wgLang->timeanddate( $value, true );
                        case 'img_name':
                                $name = $this->mCurrentRow->img_name;
                                $link = $this->getSkin()->makeKnownLinkObj( Title::makeTitle( NS_IMAGE, $name ), $value );
                                $image = wfLocalFile( $value );
                                $url = $image->getURL();
                                $download = Xml::element('a', array( "href" => $url ), $this->mMessages['imgfile'] );
                                return "$link ($download)";
                        case 'img_user_text':
                                if ( $this->mCurrentRow->img_user ) {
                                        $link = $this->getSkin()->makeLinkObj( Title::makeTitle( NS_USER, $value ), 
                                                htmlspecialchars( $value ) );
                                } else {
                                        $link = htmlspecialchars( $value );
                                }
                                return $link;
                        case 'img_size':
                                return $this->getSkin()->formatSize( $value );
                        case 'img_description':
                                return $this->getSkin()->commentBlock( $value );
                }
        }
 
        function getForm() {
                global $wgRequest, $wgMiserMode;
                $url = $this->getTitle()->escapeLocalURL();
                $search = $wgRequest->getText( 'ilsearch' );
                $s = "<form method=\"get\" action=\"$url\">\n" .
                        wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() );
                if ( !$wgMiserMode ) {
                        $s .= "<br/>\n" .
                        Xml::inputLabel( wfMsg ( 'imagelist_search_for' ), 'ilsearch', 'mw-ilsearch', 20, $search );
                }
                $s .= " " . Xml::submitButton( wfMsg( 'table_pager_limit_submit' ) ) ." \n" .
                        $this->getHiddenFields( array( 'limit', 'ilsearch' ) ) .
                        "</form>\n";
                return $s;
        }
 
        function getTableClass() {
                return 'imagelist ' . parent::getTableClass();
        }
 
        function getNavClass() {
                return 'imagelist_nav ' . parent::getNavClass();
        }
 
        function getSortHeaderClass() {
                return 'imagelist_sort ' . parent::getSortHeaderClass();
        }
}

[edit] browser.js

/**
* @author mjs
* @license GPLv2+
*/
 
function doInsert(pre,post,inner) {
        handlerdoc.insertTags(pre, post, inner);
}
 
var handlerdoc = self;
if(parent && parent.document && parent.document.getElementById("wikiPreview")) {
        handlerdoc = parent;
}
if(opener && opener.document && opener.document.getElementById("wikiPreview")) {
        handlerdoc = opener;
}
 
function insertTags() {
        alert("not in edit page");
}

[edit] editpage.js

/**
* @author mjs, Kaizen
* @license GPLv2+
*/
 
var c7rs_basedir = wgScriptPath + '/extensions/referenceselector';
 
function c7rs_init() {
        var Etoolbar = document.getElementById('toolbar');
 
        var Ebu = document.createElement('img');
        Ebu.src = c7rs_basedir + '/gfx/button_rs.png';
        Ebu.style.cursor = "pointer";
 
        if (Ebu.addEventListener) { //sane DOM-compatible browsers
                Ebu.addEventListener('click', c7rs_show, false);
        } else if(Ebu.attachEvent) { //IE
                Ebu.attachEvent("onclick", c7rs_show);
    }
 
        Etoolbar.appendChild(Ebu);
}
 
function c7rs_show() {
        var url = wgArticlePath.replace("$1", "Special:ReferenceSelectorPage");
 
        window.open(url, 'c7rs_popup', 
                "width=500,height=400,left=0,top=0,location=no,menubar=no,toolbar=no,status=no,resizable=yes,scrollable=yes,scrollbars=yes");
 
}
 
if (window.addEventListener) { //sane DOM-compatible browsers
        window.addEventListener('load', c7rs_init, false);
} else if (window.attachEvent) { //IE
        window.attachEvent("onload", c7rs_init);
}

[edit] style.css

/**
 * @author mjs
 * @license GPLv2+
*/
 
html, body {
        margin: 0;
        padding: 0;
 
        font-size: 12px;
}
 
img {
        border: none;
}
 
input, table, select {
        font-size: 100%;
}
 
table.imagelist {
        border: none;
        border-collapse: collapse;
}
 
table.imagelist td, table.imagelist th {
        border: none;
}


[edit] Edit Button

Button rs.png rs edit button

[edit] See also

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