Extension:EasyGallery

From Organic Design wiki

<?php

  1. Extension:EasyGallery
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:SpecialPage

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: Jack Henderson

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('EASYGALLERY_VERSION','1.0.0, 2009-06-15');

$wgExtensionFunctions[] = 'wfSetupEasyGallery';

$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:EasyGallery', 'author' => 'Jack Henderson', 'description' => 'An special page for creating a gallery, made with Template:SpecialPage.', 'url' => 'http://www.organicdesign.co.nz/Extension:EasyGallery', 'version' => EASYGALLERY_VERSION );

require_once "$IP/includes/SpecialPage.php";

  1. Define a new class based on the SpecialPage class

class SpecialEasyGallery extends SpecialPage {

# Constructor function __construct() { global $wgHooks, $wgParser; SpecialPage::SpecialPage( 'EasyGallery', # name as seen in links etc 'sysop', # user rights required true, # listed in special:specialpages false, # function called by execute() - defaults to wfSpecial{$name} false, # file included by execute() - defaults to Special{$name}.php, only used if no function false # includable ); }

# Override SpecialPage::execute() # $param is from the URL, eg Special:{{{name}}}/param

function execute($param) { global $wgOut, $wgRequest, $wgTitle, $wgUser, $wgMessageCache; $this->setHeaders(); $title = Title::makeTitle(NS_SPECIAL,'EasyGallery'); $par = 8;

# The $param is the number of text inputs to be replicated in the form if (isset($param)) $par = $param;

# If posted, we want to keep $par from the previous execution so we extract data from the hidden field else if ($wgRequest->getText('wpParam', "")) $par = $wgRequest->getText('wpParam', "");

# A low default to encourage users to use the switch else $par = 8;

# Extract any posted data $posted = $wgRequest->getText('wpSubmit', false);

# Extract any contents of fields $gtitle = $wgRequest->getText('wpGtitle', ""); $width = $wgRequest->getText('wpWidth', ""); $column = $wgRequest->getText('wpColumn', "");

# Render the form $wgOut->addWikiText(wfMsg('easygalleryMessage', 'easygalleryParameter')); $wgOut->addHTML( wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null)

. "

Usage

This form creates a gallery in the page you enter below, whether the page exists or not.

If there is already text in the page it will be retained but an existing gallery will be replaced by this one. You can have one gallery per page.

This extension defaults to 8 pictures (plus captions), unless the number of pictures in your intended gallery is added to the address bar title, for example for five images: http://localhost/wiki/index.php/Special:EasyGallery/5

Here is the expected form of the picture and the caption: Image:Example.jpg Example Caption

You can upload your pictures by clicking <a href='" . $wgServer . $wgScript . "/Special:Upload' target='_blank'>Special:Upload</a>.

You can access the names of images already uploaded by clicking <a href='" . $wgServer . $wgScript . "/Special:ImageList' target='_blank'>Special:ImageList</a>.

Press the Submit Query button, then you can go to the page and edit it normally if you want.


Page Setup

What is the title of the page your gallery will be in? This is a compulsory field.

<input name='wpGtitle' size = '35' />

How many columns do you want the easygallery to have? If you leave the field empty it is 2.

<input name='wpColumn' size = '10' />

How wide do you want each thumbnail to be (in pixels)? Empty field is 150.

<input name='wpWidth' size = '10' /> <input type='hidden' name='wpParam' value=$par>

" );

# Render multiple inputs using the /$par from the url for ($ctr = 1; $ctr <= $par; $ctr++) { $wgOut->addHTML(

"

Gallery " . $ctr ."

The image you want for the thumbnail.

<input name='wpUrl" . $ctr . "' size = '50' />

The caption for this image.

<input name='wpCaption" . $ctr . "' size = '50' />" ); }

# Post $wgOut->addHTML( "

" . wfElement('input', array('name' => 'wpSubmit', 'type' => 'submit')) . "

" );

# Process results if data posted if ($posted) { # Defaults for posted style elements if ($width == "") $width = 200; if ($column == "") $column = 2;

# Build tag $tag = "< gallery widths='".$width."px' perrow='".$column."'>\n";

# Some arrays to populate with loops $url[] = array(); $caption[] = array();

# Make some variables out of the posted fields for ($n = 1; $n <= $par; $n++) { $url[$n] = $wgRequest->getText('wpUrl' . $n, ""); $caption[$n] = $wgRequest->getText('wpCaption' . $n, ""); } for ($n = 1; $n <= $par; $n++) { if ($url[$n] != "") {

$tag .= $url[$n] . "|

". $caption[$n] ."

\n";

} } $tag .= "</gallery>"; if ($wgUser->isAllowed('edit')) { $title = Title::newFromText( $gtitle ); if (trim($wgRequest->getText('wpGtitle'))==) { $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) ); $wgOut->errorpage( 'badtitle', 'badtitletext'); }

# Make the new page if no page exists. if ((isset($title)) && ($title->getArticleID() == 0)) { $article = new Article($title); $article->doEdit($tag, "", EDIT_NEW); $wgOut->addHTML(wfMsg('easygallerySuccessMessage')); }

#If the page exists, extract the existing text and replace elseif ((isset($title)) && ($title->getArticleID() != 0)) { $article = new Article($title, 0); $original = $article->fetchContent(0); $intro = preg_replace("%<gallery.*%is","",$original); $outro = preg_replace("%.*</gallery>%is","",$original); $tag = $intro.$tag.$outro; $article->doEdit($tag, "", EDIT_UPDATE); $wgOut->addHTML(wfMsg('easygallerySuccessMessage')); }

else { $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) ); $wgOut->errorpage( 'error', 'badtitletext'); } } else { $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) ); $wgOut->errorpage( 'error', 'badarticleerror'); } } } }

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupEasyGallery() { global $wgLanguageCode,$wgMessageCache,$wgUser,$wgRequest;

# Ensure the code only runs if it is a Easygallery and the user is logged in $sgnewtitle = Title::newFromText($wgRequest->getText('title')); if (preg_match('%(EasyGallery)(.*)%',$sgnewtitle) && !$wgUser->isAnon()) {

# Add the messages used by the specialpage # if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'easygallery' => 'EasyGallery Specialpage', # The friendly page title 'easygalleryMessage' => "EasyGallery: Use this special page to create a gallery", 'easygallerySuccessMessage' => "
EasyGallery has successfully posted your data.
") ); # }

# Add the special page to the environment SpecialPage::addPage(new SpecialEasyGallery()); } }