r114536 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114535‎ | r114536 | r114537 >
Date:18:31, 27 March 2012
Author:bsitu
Status:deferred
Tags:
Comment:
Add logic for generating article snippet in pagetriage listview
Modified paths:
  • /trunk/extensions/PageTriage/includes/ArticleMetadata.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PageTriage/includes/ArticleMetadata.php
@@ -303,8 +303,6 @@
304304 * @param $metaData array
305305 */
306306 protected function compileArticleBasicData( &$metaData ) {
307 - global $wgLang;
308 -
309307 $dbr = wfGetDB( DB_SLAVE );
310308
311309 // Article page length, creation date, number of edit, title, article triage status
@@ -376,12 +374,45 @@
377375 __METHOD__
378376 );
379377 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 );
381379 }
382380
383381 return true;
384382 }
 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;
385391
 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+
386417 /**
387418 * Compile user basic data like username for the author
388419 * @param $metaData array

Status & tagging log