r23249 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23248‎ | r23249 | r23250 >
Date:20:31, 22 June 2007
Author:magnusmanske
Status:old
Tags:
Comment:
Now supporting #if and #switch; some template fixes
Modified paths:
  • /trunk/wiki2xml/php/w2x.php (modified) (history)
  • /trunk/wiki2xml/php/wiki2xml.php (modified) (history)

Diff [purge]

Index: trunk/wiki2xml/php/wiki2xml.php
@@ -293,24 +293,35 @@
294294 else $replace_template = false ;
295295 }
296296
297 - if ( $replace_template ) { # Do not generate <template> sections, but rather replace the template call with the template text
298 - # Get template text
299 - $between = trim ( $content_provider->get_template_text ( $target ) ) ;
300 - add_authors ( $content_provider->authors ) ;
 297+ if ( substr ( $target , 0 , 1 ) == '#' ) { # Try template logic
 298+ $between = $this->process_template_logic ( $target , $variables ) ;
 299+ # Change source (!)
 300+ $w1 = substr ( $this->w , 0 , $a ) ;
 301+ $w2 = substr ( $this->w , $b ) ;
 302+ $this->w = $w1 . $between . $w2 ;
 303+ $this->wl = strlen ( $this->w ) ;
 304+ } else if ( $replace_template ) { # Do not generate <template> sections, but rather replace the template call with the template text
 305+
 306+ # Get template text
 307+ $between = trim ( $content_provider->get_template_text ( $target ) ) ;
 308+ add_authors ( $content_provider->authors ) ;
301309
302 -# if ( $a > 0 && explode ( "\n" , $between , 2 ) > 1 ) # Multi-lined templates get leading newline
303 -# $between = "\n" . $between ;
304 -
305310 # Removing <noinclude> stuff
306311 $between = preg_replace( '?<noinclude>.*</noinclude>?msU', '', $between);
307312 $between = str_replace ( "<include>" , "" , $between ) ;
308313 $between = str_replace ( "</include>" , "" , $between ) ;
309314 $between = str_replace ( "<includeonly>" , "" , $between ) ;
310315 $between = str_replace ( "</includeonly>" , "" , $between ) ;
311 -
 316+
 317+ # Remove HTML comments
 318+ $between = preg_replace( '?<!--.*-->?msU', '', $between) ;
 319+
312320 # Replacing template variables.
313321 # ATTENTION: Template variables within <nowiki> sections of templates will be replaced as well!
314322
 323+ if ( $a > 0 && substr ( $between , 0 , 2 ) == '{|' )
 324+ $between = "\n" . $between ;
 325+
315326 $this->replace_template_variables ( $between , $variables ) ;
316327
317328 # Change source (!)
@@ -324,7 +335,31 @@
325336 }
326337 return true ;
327338 }
 339+
 340+ function process_template_logic ( $title , $variables ) {
328341
 342+ # TODO : Process title and variables for sub-template-replacements
 343+
 344+ if ( substr ( $title , 0 , 4 ) == "#if:" ) {
 345+ $title = trim ( substr ( $title , 4 ) ) ;
 346+ if ( $title == '' ) return array_pop ( $variables ) ; # ELSE
 347+ return array_shift ( $variables ) ; # THEN
 348+ }
 349+
 350+ if ( substr ( $title , 0 , 8 ) == "#switch:" ) {
 351+ $title = trim ( array_pop ( explode ( ':' , $title , 2 ) ) ) ;
 352+ foreach ( $variables AS $v ) {
 353+ $v = explode ( '=' , $v , 2 ) ;
 354+ $key = trim ( array_shift ( $v ) ) ;
 355+ if ( $key != $title ) continue ; # Wrong key
 356+ return array_pop ( $v ) ; # Correct key, return value
 357+ }
 358+ }
 359+
 360+ # BAD PARSER FUNCTION! Ignoring...
 361+ return $title ;
 362+ }
 363+
329364 function replace_template_variables ( &$text , &$variables ) {
330365 for ( $a = 0 ; $a+3 < strlen ( $text ) ; $a++ ) {
331366 if ( $text[$a] != '{' ) continue ;
Index: trunk/wiki2xml/php/w2x.php
@@ -255,6 +255,7 @@
256256 }
257257
258258 if ( $whatsthis == "wikitext" ) {
 259+ $content_provider->first_title = "Raw wikitext page" ;
259260 $wiki2xml_authors = array () ;
260261 $xml = $converter->article2xml ( "" , $wikitext , $xmlg ) ;
261262 } else {

Status & tagging log