Index: trunk/extensions/MobileFrontend/api/ApiQueryExtracts.php |
— | — | @@ -47,6 +47,9 @@ |
48 | 48 | } |
49 | 49 | $text = $this->getExtract( $t ); |
50 | 50 | $text = $this->truncate( $text ); |
| 51 | + if ( $this->params['plaintext'] ) { |
| 52 | + $text = $this->doSections( $text ); |
| 53 | + } |
51 | 54 | |
52 | 55 | if ( $isXml ) { |
53 | 56 | $fit = $result->addValue( array( 'query', 'pages', $id ), 'extract', array( '*' => $text ) ); |
— | — | @@ -283,6 +286,32 @@ |
284 | 287 | return $text; |
285 | 288 | } |
286 | 289 | |
| 290 | + private function doSections( $text ) { |
| 291 | + $text = preg_replace_callback( |
| 292 | + "/" . self::SECTION_MARKER_START . '(\d)'. self::SECTION_MARKER_END . "(.*?)$/m", |
| 293 | + array( $this, 'sectionCallback' ), |
| 294 | + $text |
| 295 | + ); |
| 296 | + return $text; |
| 297 | + } |
| 298 | + |
| 299 | + private function sectionCallback( $matches ) { |
| 300 | + if ( $this->params['sectionformat'] == 'raw' ) { |
| 301 | + return $matches[0]; |
| 302 | + } |
| 303 | + $func = __CLASS__ . "::doSection_{$this->params['sectionformat']}"; |
| 304 | + return call_user_func( $func, $matches[1], trim( $matches[2] ) ); |
| 305 | + } |
| 306 | + |
| 307 | + private static function doSection_wiki( $level, $text ) { |
| 308 | + $bars = str_repeat( '=', $level ); |
| 309 | + return "\n$bars $text $bars"; |
| 310 | + } |
| 311 | + |
| 312 | + private static function doSection_plain( $level, $text ) { |
| 313 | + return "\n$text"; |
| 314 | + } |
| 315 | + |
287 | 316 | public function getAllowedParams() { |
288 | 317 | return array( |
289 | 318 | 'chars' => array( |
— | — | @@ -321,9 +350,9 @@ |
322 | 351 | 'plaintext' => 'Return extracts as plaintext instead of limited HTML', |
323 | 352 | 'sectionformat' => array( |
324 | 353 | 'How to format sections in plaintext mode:', |
325 | | - ' none - No formatting', |
| 354 | + ' plain - No formatting', |
326 | 355 | ' wiki - Wikitext-style formatting == like this ==', |
327 | | - " raw - Return in this module's internal representation (secton titles prefixed with <ASCII 1><ASCII 2><section level><ASCII 2><ASCII 1>", |
| 356 | + " raw - This module's internal representation (secton titles prefixed with <ASCII 1><ASCII 2><section level><ASCII 2><ASCII 1>", |
328 | 357 | ), |
329 | 358 | 'continue' => 'When more results are available, use this to continue', |
330 | 359 | ); |
— | — | @@ -360,7 +389,7 @@ |
361 | 390 | private $sectionFormat; |
362 | 391 | |
363 | 392 | public static $sectionFormats = array( |
364 | | - 'none', |
| 393 | + 'plain', |
365 | 394 | 'wiki', |
366 | 395 | 'raw', |
367 | 396 | ); |
— | — | @@ -388,11 +417,6 @@ |
389 | 418 | $text = html_entity_decode( $text ); |
390 | 419 | $text = str_replace( "\r", "\n", $text ); // for Windows |
391 | 420 | $text = preg_replace( "/\n{3,}/", "\n\n", $text ); // normalise newlines |
392 | | - $text = preg_replace_callback( |
393 | | - "/" . ApiQueryExtracts::SECTION_MARKER_START . '(\d)'. ApiQueryExtracts::SECTION_MARKER_END . "(.*?)$/m", |
394 | | - array( $this, 'sectionCallback' ), |
395 | | - $text |
396 | | - ); |
397 | 421 | } |
398 | 422 | return $text; |
399 | 423 | } |
— | — | @@ -406,21 +430,4 @@ |
407 | 431 | } |
408 | 432 | return $html; |
409 | 433 | } |
410 | | - |
411 | | - private function sectionCallback( $matches ) { |
412 | | - if ( $this->sectionFormat == 'raw' ) { |
413 | | - return $matches[0]; |
414 | | - } |
415 | | - $func = "ExtractFormatter::doSection_{$this->sectionFormat}"; |
416 | | - return call_user_func( $func, $matches[1], trim( $matches[2] ) ); |
417 | | - } |
418 | | - |
419 | | - private static function doSection_wiki( $level, $text ) { |
420 | | - $bars = str_repeat( '=', $level ); |
421 | | - return "\n$bars $text $bars"; |
422 | | - } |
423 | | - |
424 | | - private static function doSection_none( $level, $text ) { |
425 | | - return "\n$text"; |
426 | | - } |
427 | 434 | } |
\ No newline at end of file |