mediawiki-extensions-InputBox/InputBox.classes.php

392 lines
9.1 KiB
PHP
Raw Normal View History

2008-10-27 20:33:18 +00:00
<?php
/**
* Classes for InputBox extension
*
* @file
* @ingroup Extensions
*/
// InputBox class
class InputBox {
/* Fields */
private $mParser;
private $mType;
private $mWidth;
private $mPreload;
private $mEditIntro;
private $mBR;
private $mDefaultText;
private $mBGColor;
private $mButtonLabel;
private $mSearchButtonLabel;
private $mFullTextButton;
private $mLabelText;
private $mHidden;
private $mNamespaces;
private $mID;
private $mInline;
2008-10-27 20:33:18 +00:00
/* Functions */
2008-10-27 20:33:18 +00:00
public function __construct( $parser ) {
$this->_parser = $parser;
}
2008-10-27 20:33:18 +00:00
public function render() {
// Internationalization
wfLoadExtensionMessages( 'InputBox' );
2008-10-27 20:33:18 +00:00
// Handle various types
switch( $this->mType ) {
2008-10-27 20:33:18 +00:00
case 'create':
case 'comment':
return $this->getCreateForm();
case 'search':
return $this->getSearchForm();
case 'search2':
return $this->getSearchForm2();
default:
return Xml::tags( 'div', null,
Xml::element( 'strong',
array(
'class' => 'error'
),
strlen( $this->mtype ) > 0
? htmlspecialchars( wfMsgForContent( 'inputbox-error-bad-type', $this->mType ) )
2008-10-27 20:33:18 +00:00
: htmlspecialchars( wfMsgForContent( 'inputbox-error-no-type' ) )
)
);
}
}
/**
* Generate search form
*/
public function getSearchForm() {
global $wgContLang;
2008-10-27 20:33:18 +00:00
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( 'tryexact' );
2008-10-27 20:33:18 +00:00
}
if ( !$this->mSearchButtonLabel ) {
$this->mSearchButtonLabel = wfMsgHtml( 'searchfulltext' );
2008-10-27 20:33:18 +00:00
}
2008-10-27 20:33:18 +00:00
// Build HTML
$htmlOut = Xml::openElement( 'div',
array(
'align' => 'center',
'style' => 'background-color:' . $this->mBGColor
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::openElement( 'form',
array(
'name' => 'searchbox',
'id' => 'searchbox',
'class' => 'searchbox',
'action' => SpecialPage::getTitleFor( 'Search' )->escapeLocalUrl(),
)
);
$htmlOut .= Xml::element( 'input',
array(
'class' => 'searchboxInput',
'name' => 'search',
'type' => $this->mHidden ? 'hidden' : 'text',
'value' => $this->mDefaultText,
'size' => $this->mWidth,
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= $this->mBR;
2008-10-27 20:33:18 +00:00
// Determine namespace checkboxes
$namespaces = $wgContLang->getNamespaces();
$namespacesArray = explode( ',', $this->mNamespaces );
if ( $this->mNamespaces ) {
2008-10-27 20:33:18 +00:00
foreach ( $namespacesArray as $userNamespace ) {
$checked = array();
// Namespace needs to be checked if flagged with "**" or if it's the only one
if ( strstr( $userNamespace, '**' ) || count( $namespacesArray ) == 1 ) {
$userNamespace = str_replace( '**', '', $userNamespace );
$checked = array( 'checked' => 'checked' );
}
2008-10-27 20:33:18 +00:00
// Namespace checkboxes
foreach ( $namespaces as $i => $name ) {
if ( $i < 0 ) {
continue;
} elseif ( $i == 0 ) {
$name = 'Main';
}
if ( $userNamespace == $name ) {
2008-10-27 20:33:18 +00:00
// Checkbox
$htmlOut .= Xml::element( 'input',
array(
'type' => 'checkbox',
'name' => 'ns' . $i,
'value' => 1
) + $checked
);
// Label
$htmlOut .= '&nbsp;' . htmlspecialchars( $userNamespace );
}
}
}
// Line break
$htmlOut .= $this->mBR;
2008-10-27 20:33:18 +00:00
} else {
// Go button
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'go',
'class' => 'searchboxGoButton',
'value' => $this->mButtonLabel
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= '&nbsp;';
}
2008-10-27 20:33:18 +00:00
// Search button
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'searchboxSearchButton',
'value' => $this->mSearchButtonLabel
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
2008-10-27 20:33:18 +00:00
// Return HTML
return $htmlOut;
}
2008-10-27 20:33:18 +00:00
/*
* Generate search form version 2
*/
public function getSearchForm2() {
2008-10-27 20:33:18 +00:00
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( 'tryexact' );
2008-10-27 20:33:18 +00:00
}
$id = Sanitizer::escapeId( $this->mID );
2008-10-27 20:33:18 +00:00
$htmlLabel = '';
if ( isset( $this->mLabelText ) && strlen( trim( $this->mLabelText ) ) ) {
$output = $this->mParser->parse(
$this->mLabelText,
$this->mParser->getTitle(),
$this->mParser->getOptions(),
2008-10-27 20:33:18 +00:00
false,
false
);
$this->mLabelText = $output->getText();
$this->mLabelText = str_replace( '<p>', '', $this->mLabelText );
$this->mLabelText = str_replace( '</p>', '', $this->mLabelText );
2008-10-27 20:33:18 +00:00
$htmlLabel = Xml::element( 'label',
array(
'for' => 'bodySearchIput' . $id
),
$this->mLabelText
2008-10-27 20:33:18 +00:00
);
}
$htmlOut = Xml::openElement( 'form',
array(
'name' => 'bodySearch' . $id,
'id' => 'bodySearch' . $id,
'class' => 'bodySearch',
'action' => SpecialPage::getTitleFor( 'Search' )->escapeLocalUrl(),
'style' => $this->mInline ? 'display: inline;' : ''
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::openElement( 'div',
array(
'class' => 'bodySearchWrap',
'style' => 'background-color:' . $this->mBGColor . ';' .
$this->mInline ? 'display: inline;' : ''
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= $htmlLabel;
$htmlOut .= Xml::element( 'input',
array(
'type' => $this->mHidden ? 'hidden' : 'text',
2008-10-27 20:33:18 +00:00
'name' => 'search',
'size' => $this->mWidth,
2008-10-27 20:33:18 +00:00
'class' => 'bodySearchInput' . $id
)
);
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'go',
'value' => $this->mButtonLabel,
2008-10-27 20:33:18 +00:00
'class' => 'bodySearchBtnGo' . $id
)
);
// Better testing needed here!
if ( !empty( $this->mFullTextButton ) ) {
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'bodySearchBtnSearch',
'value' => $this->mSearchButtonLabel
2008-10-27 20:33:18 +00:00
)
);
}
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::closeElement( 'div' );
$htmlOut .= Xml::closeElement( 'form' );
// Return HTML
return $htmlOut;
}
/**
* Generate create page form
*/
public function getCreateForm() {
global $wgScript;
2008-10-27 20:33:18 +00:00
if ( $this->mType == "comment" ) {
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( "postcomment" );
2008-10-27 20:33:18 +00:00
}
} else {
$comment = '';
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( 'createarticle' );
2008-10-27 20:33:18 +00:00
}
}
2008-10-27 20:33:18 +00:00
$htmlOut = Xml::openElement( 'div',
array(
'align' => 'center',
'style' => 'background-color:' . $this->mBGColor
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::openElement( 'form',
array(
'name' => 'createbox',
'id' => 'createbox',
'class' => 'createbox',
'action' => $wgScript,
'method' => 'get'
)
);
$htmlOut .= Xml::openElement( 'input',
array(
'type' => 'hidden',
'name' => 'action',
'value' => 'edit',
)
);
$htmlOut .= Xml::openElement( 'input',
array(
'type' => 'hidden',
'name' => 'preload',
'value' => $this->mPreload,
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::openElement( 'input',
array(
'type' => 'hidden',
'name' => 'editintro',
'value' => $this->mEditIntro,
2008-10-27 20:33:18 +00:00
)
);
if ( $this->mType == 'comment' ) {
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::openElement( 'input',
array(
'type' => 'hidden',
'name' => 'section',
'value' => 'new',
)
);
}
$htmlOut .= Xml::openElement( 'input',
array(
'type' => $this->mHidden ? 'hidden' : 'text',
2008-10-27 20:33:18 +00:00
'name' => 'title',
'class' => 'createboxInput',
'value' => $this->mDefaultText,
'size' => $this->mWidth
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= $this->mBR;
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::openElement( 'input',
array(
'type' => 'submit',
'name' => 'create',
'class' => 'createboxButton',
'value' => $this->mButtonLabel
2008-10-27 20:33:18 +00:00
)
);
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
2008-10-27 20:33:18 +00:00
// Return HTML
return $htmlOut;
}
/**
* Extract options from a blob of text
*
* @param string $text Tag contents
*/
public function extractOptions( $text ) {
wfProfileIn( __METHOD__ );
2008-10-27 20:33:18 +00:00
// Parse all possible options
$values = array();
foreach ( explode( "\n", $text ) as $line ) {
if ( strpos( $line, '=' ) === false )
2008-10-27 20:33:18 +00:00
continue;
list( $name, $value ) = explode( '=', $line, 2 );
$values[ strtolower( trim( $name ) ) ] = trim( $value );
}
2008-10-27 22:52:05 +00:00
// Build list of options, with local member names and deafults
2008-10-27 20:33:18 +00:00
$options = array(
2008-10-27 22:52:05 +00:00
'type' => array( 'mType', '' ),
'width' => array( 'mWidth', 50 ),
'preload' => array( 'mPreload', '' ),
'editintro' => array( 'mEditIntro', '' ),
'break' => array( 'mBR', 'yes' ),
'default' => array( 'mDefaultText', '' ),
'bgcolor' => array( 'mBGColor', 'transparent' ),
'buttonlabel' => array( 'mButtonLabel', '' ),
'searchbuttonlabel' => array( 'mSearchButtonLabel', '' ),
'fulltextbutton' => array( 'mFullTextButton', '' ),
'namespaces' => array( '_namespaces', '' ),
'labeltext' => array( 'mLabelText', '' ),
'hidden' => array( 'mHidden', '' ),
'id' => array( 'mID', '' ),
'inline' => array( 'mInline', false )
2008-10-27 20:33:18 +00:00
);
foreach ( $options as $name => $var ) {
if ( isset( $values[$name] ) ) {
2008-10-27 22:52:05 +00:00
$this->$var[0] = $values[$name];
} else {
$this->$var[0] = $var[1];
2008-10-27 20:33:18 +00:00
}
}
2008-10-27 22:52:05 +00:00
2008-10-27 20:33:18 +00:00
// Insert a line break if configured to do so
$this->mBR = ( strtolower( $this->mBR ) == "no" ) ? '' : '<br />';
2008-10-27 20:33:18 +00:00
// Validate the width; make sure it's a valid, positive integer
$this->mWidth = intval( $this->mWidth <= 0 ? 50 : $this->mWidth );
2008-10-27 22:52:05 +00:00
2008-10-27 20:33:18 +00:00
wfProfileOut( __METHOD__ );
}
}