r24580 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24579‎ | r24580 | r24581 >
Date:14:13, 4 August 2007
Author:robchurch
Status:old
Tags:
Comment:
Make that last commit less fragile; ditch getBoxOption() in favour of passing the whole blob to an Inputbox::extractOptions() method. Also replace all variables in the input text.
Modified paths:
  • /trunk/extensions/inputbox/inputbox.php (modified) (history)

Diff [purge]

Index: trunk/extensions/inputbox/inputbox.php
@@ -37,60 +37,16 @@
3838 $wgParser->setHook('inputbox', 'renderInputbox');
3939 }
4040
41 -
42 -
43 -
44 -/**
45 - * Renders an inputbox based on information provided by $input.
46 - */
47 -function renderInputbox($input, $params, &$parser)
48 -{
49 - $inputbox=new Inputbox( $parser );
50 - getBoxOption($inputbox->type,$input,'type');
51 - getBoxOption($inputbox->width,$input,'width',true);
52 - getBoxOption($inputbox->preload,$input,'preload');
53 - getBoxOption($inputbox->editintro,$input,'editintro');
54 - getBoxOption($inputbox->defaulttext,$input,'default');
55 - getBoxOption($inputbox->bgcolor,$input,'bgcolor');
56 - getBoxOption($inputbox->buttonlabel,$input,'buttonlabel');
57 - getBoxOption($inputbox->searchbuttonlabel,$input,'searchbuttonlabel');
58 - getBoxOption($inputbox->namespaces,$input,'namespaces');
59 - getBoxOption($inputbox->id,$input,'id');
60 - getBoxOption($inputbox->labeltext,$input,'labeltext');
61 - getBoxOption($inputbox->br, $input, 'break');
62 - getBoxOption($inputbox->hidden, $input, 'hidden');
63 - $inputbox->lineBreak();
64 - $inputbox->checkWidth();
65 -
66 - $boxhtml=$inputbox->render();
67 - # Maybe support other useful magic words here
68 - $boxhtml=str_replace("{{PAGENAME}}",$parser->getTitle()->getText(),$boxhtml);
69 - if($boxhtml) {
70 - return $boxhtml;
71 - } else {
72 - return '<div><strong class="error">Input box: type not defined.</strong></div>';
73 - }
 41+function renderInputbox( $input, $params, $parser ) {
 42+ $inputbox = new Inputbox( $parser );
 43+ $inputbox->extractOptions( $parser->replaceVariables( $input ) );
 44+ $html = $inputbox->render();
 45+ // FIXME: Won't somebody please think of the i18n people!?
 46+ return $html
 47+ ? $html
 48+ : '<div><strong class="error">Input box: Type not defined.</strong></div>';
7449 }
7550
76 -
77 -function getBoxOption( &$value, $input, $name, $isNumber = false ) {
78 - static $values = false;
79 - wfProfileIn( __METHOD__ );
80 - if( $values === false ) {
81 - $values = array();
82 - $lines = explode( "\n", $input );
83 - foreach( $lines as $line ) {
84 - if( strpos( $line, '=' ) === false )
85 - continue;
86 - list( $lname, $lval ) = explode( '=', $line, 2 );
87 - $values[ strtolower( trim( $lname ) ) ] = trim( $lval );
88 - }
89 - }
90 - if( isset( $values[$name] ) )
91 - $value = $isNumber ? intval( $values[$name] ) : htmlspecialchars( $values[$name] );
92 - wfProfileOut( __METHOD__ );
93 -}
94 -
9551 class Inputbox {
9652 var $type,$width,$preload,$editintro, $br;
9753 var $defaulttext,$bgcolor,$buttonlabel,$searchbuttonlabel;
@@ -259,6 +215,51 @@
260216 function checkWidth() {
261217 if( !$this->width || trim( $this->width ) == '' )
262218 $this->width = 50;
263 - }
264 -}
265 -?>
 219+ }
 220+
 221+ /**
 222+ * Extract options from a blob of text
 223+ *
 224+ * @param string $text Tag contents
 225+ */
 226+ public function extractOptions( $text ) {
 227+ wfProfileIn( __METHOD__ );
 228+
 229+ // Parse all possible options
 230+ $values = array();
 231+ foreach( explode( "\n", $text ) as $line ) {
 232+ if( strpos( $line, '=' ) === false )
 233+ continue;
 234+ list( $name, $value ) = explode( '=', $line, 2 );
 235+ $values[ strtolower( trim( $name ) ) ] = trim( $value );
 236+ }
 237+
 238+ // Go through and set all the options we found
 239+ $options = array(
 240+ 'type' => 'type',
 241+ 'width' => 'width',
 242+ 'preload' => 'preload',
 243+ 'editintro' => 'editintro',
 244+ 'default' => 'defaulttext',
 245+ 'bgcolor' => 'bgcolor',
 246+ 'buttonlabel' => 'buttonlabel',
 247+ 'searchbuttonlabel' => 'searchbuttonlabel',
 248+ 'namespaces' => 'namespaces',
 249+ 'id' => 'id',
 250+ 'labeltext' => 'labeltext',
 251+ 'break' => 'br',
 252+ 'hidden' => 'hidden',
 253+ );
 254+ foreach( $options as $name => $var ) {
 255+ if( isset( $values[$name] ) )
 256+ $this->$var = $values[$name];
 257+ }
 258+
 259+ // Some special-case fix-ups
 260+ $this->lineBreak();
 261+ $this->checkWidth();
 262+
 263+ wfProfileOut( __METHOD__ );
 264+ }
 265+
 266+}
\ No newline at end of file

Status & tagging log