Index: trunk/extensions/PageTriage/includes/ArticleMetadata.php |
— | — | @@ -303,8 +303,6 @@ |
304 | 304 | * @param $metaData array |
305 | 305 | */ |
306 | 306 | protected function compileArticleBasicData( &$metaData ) { |
307 | | - global $wgLang; |
308 | | - |
309 | 307 | $dbr = wfGetDB( DB_SLAVE ); |
310 | 308 | |
311 | 309 | // Article page length, creation date, number of edit, title, article triage status |
— | — | @@ -376,12 +374,45 @@ |
377 | 375 | __METHOD__ |
378 | 376 | ); |
379 | 377 | foreach ( $res as $row ) { |
380 | | - $metaData[$row->page_id]['snippet'] = $wgLang->truncate( $row->old_text, 150 ); |
| 378 | + $metaData[$row->page_id]['snippet'] = self::generateArticleSnippet( $row->old_text ); |
381 | 379 | } |
382 | 380 | |
383 | 381 | return true; |
384 | 382 | } |
| 383 | + |
| 384 | + /** |
| 385 | + * Generate article snippet for listview from article text |
| 386 | + * @param $text string - page text |
| 387 | + * @return string |
| 388 | + */ |
| 389 | + public static function generateArticleSnippet( $text ) { |
| 390 | + global $wgLang; |
385 | 391 | |
| 392 | + $attempt = 1; |
| 393 | + $openCurPos = strpos($text, '{{'); |
| 394 | + $closeCurPos = strpos($text, '}}'); |
| 395 | + |
| 396 | + while( $openCurPos !== false && $closeCurPos !== false && $openCurPos < $closeCurPos ) { |
| 397 | + // replace all templates with empty string |
| 398 | + $text = preg_replace( '/\{\{[^\{]((?!\{\{).)*?\}\}/is', '', $text ); |
| 399 | + |
| 400 | + $openCurPos = strpos($text, '{{'); |
| 401 | + $closeCurPos = strpos($text, '}}'); |
| 402 | + |
| 403 | + $attempt++; |
| 404 | + // only try 5 nested levels at max |
| 405 | + if ( $attempt > 5 ) { |
| 406 | + break; |
| 407 | + } |
| 408 | + } |
| 409 | + |
| 410 | + // stip out non-useful data for snippet |
| 411 | + $text = str_replace( array('{', '}'), '', $text ); |
| 412 | + $text = trim( strip_tags( MessageCache::singleton()->parse( $text )->getText() ) ); |
| 413 | + |
| 414 | + return $wgLang->truncate( $text, 150 ); |
| 415 | + } |
| 416 | + |
386 | 417 | /** |
387 | 418 | * Compile user basic data like username for the author |
388 | 419 | * @param $metaData array |