2012-07-27 22:16:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class EchoDiscussionParser {
|
2014-09-12 03:07:29 +00:00
|
|
|
const HEADER_REGEX = '^(==+)\s*([^=].*)\s*\1$';
|
|
|
|
|
2013-05-19 04:15:45 +00:00
|
|
|
static protected $timestampRegex;
|
|
|
|
static protected $revisionInterpretationCache = array();
|
|
|
|
static protected $diffParser;
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a Revision object, generates EchoEvent objects for
|
|
|
|
* the discussion-related actions that occurred in that Revision.
|
|
|
|
*
|
|
|
|
* @param $revision Revision object
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
static function generateEventsForRevision( $revision ) {
|
|
|
|
$interpretation = self::getChangeInterpretationForRevision( $revision );
|
2012-11-14 21:56:58 +00:00
|
|
|
|
|
|
|
// use slave database if there is a previous revision
|
|
|
|
if ( $revision->getPrevious() ) {
|
|
|
|
$title = Title::newFromID( $revision->getPage() );
|
|
|
|
// use master database for new page
|
|
|
|
} else {
|
|
|
|
$title = Title::newFromID( $revision->getPage(), Title::GAID_FOR_UPDATE );
|
|
|
|
}
|
|
|
|
|
|
|
|
// not a valid title
|
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
$userID = $revision->getUser();
|
|
|
|
$userName = $revision->getUserText();
|
|
|
|
$user = $userID != 0 ? User::newFromId( $userID ) : User::newFromName( $userName, false );
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $interpretation as $action ) {
|
2012-12-20 01:00:19 +00:00
|
|
|
// These two event types are disabled temporarily, there is no need to process them
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( $action['type'] == 'add-comment' ) {
|
|
|
|
$fullSection = $action['full-section'];
|
|
|
|
$header = self::extractHeader( $fullSection );
|
2012-10-28 16:47:41 +00:00
|
|
|
self::generateMentionEvents( $header, $action['content'], $revision, $user );
|
2012-07-27 22:16:19 +00:00
|
|
|
} elseif ( $action['type'] == 'new-section-with-comment' ) {
|
|
|
|
$content = $action['content'];
|
|
|
|
$header = self::extractHeader( $content );
|
2012-10-28 16:47:41 +00:00
|
|
|
self::generateMentionEvents( $header, $content, $revision, $user );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:19:51 +00:00
|
|
|
if ( $title->getNamespace() == NS_USER_TALK ) {
|
2012-08-01 19:53:05 +00:00
|
|
|
$notifyUser = User::newFromName( $title->getText() );
|
2013-03-20 01:10:23 +00:00
|
|
|
// If the recipient is a valid non-anonymous user and hasn't turned
|
2014-08-28 08:50:28 +00:00
|
|
|
// off their notifications, generate a talk page post Echo notification.
|
2015-07-08 00:10:49 +00:00
|
|
|
if ( $notifyUser && $notifyUser->getID() ) {
|
2013-05-01 19:27:32 +00:00
|
|
|
// if this is a minor edit, only notify if the agent doesn't have talk page minor edit notification blocked
|
|
|
|
if ( !$revision->isMinor() || !$user->isAllowed( 'nominornewtalk' ) ) {
|
2015-02-17 10:10:49 +00:00
|
|
|
$section = self::detectSectionTitleAndText( $interpretation, $title );
|
2015-04-26 13:50:44 +00:00
|
|
|
if ( $section['section-text'] === '' ) {
|
|
|
|
$section['section-text'] = $revision->getComment();
|
|
|
|
}
|
2013-05-01 19:27:32 +00:00
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'edit-user-talk',
|
|
|
|
'title' => $title,
|
2013-05-14 22:22:52 +00:00
|
|
|
'extra' => array(
|
|
|
|
'revid' => $revision->getID(),
|
|
|
|
'minoredit' => $revision->isMinor(),
|
2013-07-24 03:50:43 +00:00
|
|
|
'section-title' => $section['section-title'],
|
|
|
|
'section-text' => $section['section-text']
|
2013-05-14 22:22:52 +00:00
|
|
|
),
|
2013-05-01 19:27:32 +00:00
|
|
|
'agent' => $user,
|
|
|
|
) );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
/**
|
|
|
|
* Attempts to determine what section title the edit was performed under (if any)
|
|
|
|
*
|
|
|
|
* @param $interpretation array Results of self::getChangeInterpretationForRevision
|
2013-07-24 03:50:43 +00:00
|
|
|
* @return array Array containing section title and text
|
2015-02-17 10:10:49 +00:00
|
|
|
* @param Title $title
|
2013-05-14 22:22:52 +00:00
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
public static function detectSectionTitleAndText( array $interpretation, Title $title = null ) {
|
2013-07-24 03:50:43 +00:00
|
|
|
$header = $snippet = '';
|
2013-05-17 19:16:46 +00:00
|
|
|
$found = false;
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
foreach ( $interpretation as $action ) {
|
|
|
|
switch( $action['type'] ) {
|
2013-07-24 03:50:43 +00:00
|
|
|
case 'add-comment':
|
|
|
|
$header = self::extractHeader( $action['full-section'] );
|
2015-02-17 10:10:49 +00:00
|
|
|
$snippet = self::getTextSnippet( self::stripSignature( self::stripHeader( $action['content'] ), $title ), 150 );
|
2013-07-24 03:50:43 +00:00
|
|
|
break;
|
|
|
|
case 'new-section-with-comment':
|
|
|
|
$header = self::extractHeader( $action['content'] );
|
2015-02-17 10:10:49 +00:00
|
|
|
$snippet = self::getTextSnippet( self::stripSignature( self::stripHeader( $action['content'] ), $title ), 150 );
|
2013-07-24 03:50:43 +00:00
|
|
|
break;
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
if ( $header ) {
|
2015-04-26 13:37:38 +00:00
|
|
|
// If we find a second header within the same change interpretation then
|
2013-05-17 19:16:46 +00:00
|
|
|
// we cannot choose just 1 to link to
|
|
|
|
if ( $found ) {
|
2015-04-26 13:37:38 +00:00
|
|
|
$found = false;
|
|
|
|
break;
|
2013-05-17 19:16:46 +00:00
|
|
|
}
|
2015-04-26 13:37:38 +00:00
|
|
|
$found = true;
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-26 13:37:38 +00:00
|
|
|
if ( $found === false ) {
|
|
|
|
return array( 'section-title' => '', 'section-text' => '' );
|
2013-05-17 19:16:46 +00:00
|
|
|
}
|
2015-04-26 13:37:38 +00:00
|
|
|
return array( 'section-title' => $header, 'section-text' => $snippet );
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 16:47:41 +00:00
|
|
|
/**
|
|
|
|
* For an action taken on a talk page, notify users whose user pages
|
|
|
|
* are linked.
|
2013-02-28 23:52:12 +00:00
|
|
|
* @param $header string The subject line for the discussion.
|
|
|
|
* @param $content string The content of the post, as a wikitext string.
|
2012-10-28 16:47:41 +00:00
|
|
|
* @param $revision Revision object.
|
2013-02-28 23:52:12 +00:00
|
|
|
* @param $agent User The user who made the comment.
|
2012-10-28 16:47:41 +00:00
|
|
|
*/
|
|
|
|
public static function generateMentionEvents( $header, $content, $revision, $agent ) {
|
|
|
|
$title = $revision->getTitle();
|
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = self::parseNonEditWikitext( $content, new Article( $title ) );
|
|
|
|
$links = $output->getLinks();
|
2013-04-08 23:43:18 +00:00
|
|
|
|
|
|
|
if ( !isset( $links[NS_USER] ) || !is_array( $links[NS_USER] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-28 16:47:41 +00:00
|
|
|
$mentionedUsers = array();
|
2013-03-13 19:30:20 +00:00
|
|
|
$count = 0;
|
2012-10-28 16:47:41 +00:00
|
|
|
|
|
|
|
foreach ( $links[NS_USER] as $dbk => $page_id ) {
|
|
|
|
$user = User::newFromName( $dbk );
|
2014-11-14 23:23:52 +00:00
|
|
|
|
2012-10-28 16:47:41 +00:00
|
|
|
// we should not add user to 'mention' notification list if
|
|
|
|
// 1. the user name is not valid
|
|
|
|
// 2. the user mentions themselves
|
|
|
|
// 3. the user is the owner of the talk page
|
2013-03-12 22:52:00 +00:00
|
|
|
// 4. user is anonymous
|
2012-10-28 16:47:41 +00:00
|
|
|
if (
|
2013-03-12 22:52:00 +00:00
|
|
|
!$user || $user->isAnon() || $user->getId() == $revision->getUser() ||
|
2012-10-28 16:47:41 +00:00
|
|
|
( $title->getNamespace() === NS_USER_TALK && $title->getDBkey() === $dbk )
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-13 19:30:20 +00:00
|
|
|
$mentionedUsers[$user->getId()] = $user->getId();
|
|
|
|
$count++;
|
2014-09-11 18:14:17 +00:00
|
|
|
// If more than 20 users are being pinged this is likely a spam/attack vector
|
|
|
|
// Don't send any mention notifications.
|
|
|
|
if ( $count > 20 ) {
|
|
|
|
return;
|
2013-03-13 19:30:20 +00:00
|
|
|
}
|
2012-10-28 16:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$mentionedUsers ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EchoEvent::create( array(
|
|
|
|
'type' => 'mention',
|
|
|
|
'title' => $title,
|
|
|
|
'extra' => array(
|
|
|
|
'content' => $content,
|
|
|
|
'section-title' => $header,
|
|
|
|
'revid' => $revision->getId(),
|
|
|
|
'mentioned-users' => $mentionedUsers,
|
|
|
|
),
|
|
|
|
'agent' => $agent,
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's like Article::prepareTextForEdit,
|
|
|
|
* but not for editing (old wikitext usually)
|
|
|
|
* Stolen from AbuseFilterVariableHolder
|
|
|
|
*
|
|
|
|
* @param $wikitext String
|
|
|
|
* @param $article Article
|
|
|
|
*
|
2015-01-31 01:00:06 +00:00
|
|
|
* @return ParserOutput
|
2012-10-28 16:47:41 +00:00
|
|
|
*/
|
|
|
|
static function parseNonEditWikitext( $wikitext, $article ) {
|
|
|
|
static $cache = array();
|
|
|
|
|
|
|
|
$cacheKey = md5( $wikitext ) . ':' . $article->getTitle()->getPrefixedText();
|
|
|
|
|
|
|
|
if ( isset( $cache[$cacheKey] ) ) {
|
|
|
|
return $cache[$cacheKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wgParser;
|
|
|
|
$options = new ParserOptions;
|
|
|
|
$options->setTidy( true );
|
|
|
|
$output = $wgParser->parse( $wikitext, $article->getTitle(), $options );
|
|
|
|
$cache[$cacheKey] = $output;
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Given a Revision object, returns a talk-page-centric interpretation
|
|
|
|
* of the changes made in it.
|
|
|
|
*
|
|
|
|
* @param $revision Revision object
|
|
|
|
* @see EchoDiscussionParser::interpretDiff
|
|
|
|
* @return Array, see interpretDiff for details.
|
|
|
|
*/
|
|
|
|
static function getChangeInterpretationForRevision( $revision ) {
|
|
|
|
if ( $revision->getID() && isset( self::$revisionInterpretationCache[$revision->getID()] ) ) {
|
|
|
|
return self::$revisionInterpretationCache[$revision->getID()];
|
|
|
|
}
|
|
|
|
|
|
|
|
$userID = $revision->getUser();
|
|
|
|
$userName = $revision->getUserText();
|
|
|
|
$user = $userID != 0 ? User::newFromId( $userID ) : User::newFromName( $userName, false );
|
2012-08-13 14:57:57 +00:00
|
|
|
$prevText = '';
|
|
|
|
if ( $revision->getParentId() ) {
|
|
|
|
$prevRevision = Revision::newFromId( $revision->getParentId() );
|
|
|
|
if ( $prevRevision ) {
|
|
|
|
$prevText = $prevRevision->getText();
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-08-13 14:57:57 +00:00
|
|
|
$changes = self::getMachineReadableDiff( $prevText, $revision->getText() );
|
2015-02-17 10:10:49 +00:00
|
|
|
$output = self::interpretDiff( $changes, $user->getName(), $revision->getTitle() );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
self::$revisionInterpretationCache[$revision->getID()] = $output;
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a machine-readable diff, interprets the changes
|
|
|
|
* in terms of discussion page actions
|
|
|
|
*
|
|
|
|
* @todo Expand recognisable actions.
|
2015-01-31 01:00:06 +00:00
|
|
|
* @param array $changes Output of EchoEvent::getMachineReadableDiff
|
|
|
|
* @param string $user Username
|
2015-02-17 10:10:49 +00:00
|
|
|
* @param Title $title
|
2012-07-27 22:16:19 +00:00
|
|
|
* @return Array of associative arrays.
|
|
|
|
* Each entry represents an action, which is classified in the 'action' field.
|
|
|
|
* All types contain a 'content' field except 'unknown'
|
|
|
|
* (which instead passes through the machine-readable diff in 'details')
|
|
|
|
* and 'unknown-change' (which provides 'new_content' and 'old_content')
|
|
|
|
* action may be:
|
|
|
|
* - add-comment: A comment signed by the user is added to an
|
|
|
|
* existing section.
|
|
|
|
* - new-section-with-comment: A new section is added, containing
|
|
|
|
* a single comment signed by the user in question.
|
|
|
|
* - unknown-signed-addition: Some signed content is added, but it
|
|
|
|
* includes section headers, is signed by another user or
|
|
|
|
* otherwise confuses the interpretation engine.
|
|
|
|
* - unknown-multi-signed-addition: Some signed content is added,
|
|
|
|
* but it contains multiple signatures.
|
|
|
|
* - unknown-unsigned-addition: Some content is added, but it is
|
|
|
|
* unsigned.
|
|
|
|
* - unknown-subtraction: Some content was removed. These actions are
|
|
|
|
* not currently analysed.
|
|
|
|
* - unknown-change: Some content was replaced with other content.
|
|
|
|
* These actions are not currently analysed.
|
|
|
|
* - unknown: Unrecognised change type.
|
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
static function interpretDiff( $changes, $user, Title $title = null ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
// One extra item in $changes for _info
|
|
|
|
$actions = array();
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $changes as $index => $change ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( !is_numeric( $index ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$change['action'] ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
// Unknown action; skip
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( $change['action'] == 'add' ) {
|
|
|
|
$content = trim( $change['content'] );
|
2014-09-12 03:07:29 +00:00
|
|
|
// The \A means the regex must match at the begining of the string.
|
|
|
|
// This is slightly different than ^ which matches begining of each
|
|
|
|
// line in multiline mode.
|
|
|
|
$startSection = preg_match( "/\A" . self::HEADER_REGEX . '/um', $content );
|
2012-07-27 22:16:19 +00:00
|
|
|
$sectionCount = self::getSectionCount( $content );
|
2015-02-17 10:10:49 +00:00
|
|
|
$signedUsers = array_keys( self::extractSignatures( $content, $title ) );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
count( $signedUsers ) == 1 &&
|
|
|
|
in_array( $user, $signedUsers )
|
|
|
|
) {
|
|
|
|
if ( $sectionCount === 0 ) {
|
|
|
|
$fullSection = self::getFullSection( $changes['_info']['rhs'], $change['right-pos'] );
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'add-comment',
|
|
|
|
'content' => $content,
|
|
|
|
'full-section' => $fullSection,
|
|
|
|
);
|
|
|
|
} elseif ( $startSection && $sectionCount === 1 ) {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'new-section-with-comment',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown-signed-addition',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif ( count( $signedUsers ) >= 1 ) {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown-multi-signed-addition',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown-unsigned-addition',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif ( $change['action'] == 'subtract' ) {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown-subtraction',
|
|
|
|
'content' => $change['content'],
|
|
|
|
);
|
|
|
|
} elseif ( $change['action'] == 'change' ) {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown-change',
|
|
|
|
'old_content' => $change['old_content'],
|
|
|
|
'new_content' => $change['new_content'],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$actions[] = array(
|
|
|
|
'type' => 'unknown',
|
|
|
|
'details' => $change,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// $actions['_diff'] = $changes;
|
|
|
|
// unset( $actions['_diff']['_info'] );
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the section that a given line is in.
|
|
|
|
*
|
|
|
|
* @param $lines Array of lines in the page.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $offset int The line to find the full section for.
|
|
|
|
* @return string Content of the section.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
|
|
|
static function getFullSection( $lines, $offset ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
$content = $lines[$offset - 1];
|
2014-09-12 03:07:29 +00:00
|
|
|
$headerRegex = '/' . self::HEADER_REGEX . '/um';
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
// Expand backwards...
|
2012-08-31 21:50:46 +00:00
|
|
|
$continue = !preg_match( $headerRegex, $lines[$offset - 1] );
|
2012-07-31 00:29:49 +00:00
|
|
|
$i = $offset - 1;
|
2012-07-27 22:16:19 +00:00
|
|
|
while ( $continue && $i > 0 ) {
|
|
|
|
--$i;
|
|
|
|
$line = $lines[$i];
|
|
|
|
$content = "$line\n$content";
|
|
|
|
if ( preg_match( $headerRegex, $line ) ) {
|
|
|
|
$continue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And then forwards...
|
|
|
|
|
|
|
|
$continue = true;
|
2012-07-31 00:29:49 +00:00
|
|
|
$i = $offset - 1;
|
2012-08-30 16:04:39 +00:00
|
|
|
while ( $continue && $i < count( $lines ) - 1 ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
++$i;
|
|
|
|
$line = $lines[$i];
|
|
|
|
if ( preg_match( $headerRegex, $line ) ) {
|
|
|
|
$continue = false;
|
|
|
|
} else {
|
|
|
|
$content .= "\n$line";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-19 04:15:45 +00:00
|
|
|
return trim( $content, "\n" );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of section headers in a string.
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text.
|
|
|
|
* @return int Number of section headers found.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
|
|
|
static function getSectionCount( $text ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$text = trim( $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
$matches = array();
|
2014-09-12 03:07:29 +00:00
|
|
|
preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
return count( $matches[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-17 19:16:46 +00:00
|
|
|
* Gets the title of a section or sub section
|
2012-07-27 22:16:19 +00:00
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text of the section.
|
|
|
|
* @return string The title of the section.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
|
|
|
static function extractHeader( $text ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$text = trim( $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
$matches = array();
|
|
|
|
|
2014-09-12 03:07:29 +00:00
|
|
|
if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches ) ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-17 19:16:46 +00:00
|
|
|
return trim( end( $matches[2] ) );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
/**
|
|
|
|
* Strips out a signature if possible.
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The wikitext to strip
|
2015-02-17 10:10:49 +00:00
|
|
|
* @param Title $title
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return string
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
static function stripSignature( $text, Title $title = null ) {
|
|
|
|
$output = self::getUserFromLine( $text, $title );
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( $output === false ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
$timestampPos = self::getTimestampPosition( $text );
|
2012-07-31 00:29:49 +00:00
|
|
|
return substr( $text, 0, $timestampPos );
|
|
|
|
}
|
|
|
|
|
2013-07-31 21:44:07 +00:00
|
|
|
// Use truncate() instead of truncateHTML() because truncateHTML()
|
2015-01-31 01:00:06 +00:00
|
|
|
// would not strip signature if the text contains < or &
|
2012-07-31 00:29:49 +00:00
|
|
|
global $wgContLang;
|
2015-01-31 01:00:06 +00:00
|
|
|
return $wgContLang->truncate( $text, $output[0], '' );
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips unnecessary indentation and so on from comments
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text to strip from
|
|
|
|
* @return string Stripped wikitext
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
|
|
|
static function stripIndents( $text ) {
|
|
|
|
// First strip all indentation from the beginning of lines
|
|
|
|
$text = preg_replace( '/^\s*\:+/m', '', $text );
|
|
|
|
|
|
|
|
// Now if there is only one list item, strip that too
|
|
|
|
$listRegex = '/^\s*(?:[\:#*]\s*)*[#*]/m';
|
|
|
|
$matches = array();
|
2012-08-01 19:53:05 +00:00
|
|
|
if ( preg_match_all( $listRegex, $text, $matches ) ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( count( $matches ) == 1 ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
$text = preg_replace( $listRegex, '', $text );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Strips out a section header
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text to strip out the section header from.
|
|
|
|
* @return string: The same text, with the section header stripped out.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
|
|
|
static function stripHeader( $text ) {
|
2014-09-12 03:07:29 +00:00
|
|
|
$text = preg_replace( '/' . self::HEADER_REGEX . '/um', '', $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the input is a signed comment.
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text to check.
|
|
|
|
* @param $user User|bool If set, will only return true if the comment is
|
2012-07-27 22:16:19 +00:00
|
|
|
* signed by this user.
|
2015-02-17 10:10:49 +00:00
|
|
|
* @param Title $title
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool: true or false.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
static function isSignedComment( $text, $user = false, Title $title = null ) {
|
|
|
|
$userData = self::getUserFromLine( $text, $title );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
if ( $userData === false ) {
|
|
|
|
return false;
|
|
|
|
} elseif ( $user === false ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-27 18:28:37 +00:00
|
|
|
list( , $foundUser ) = $userData;
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
return User::getCanonicalName( $foundUser, false ) === User::getCanonicalName( $user, false );
|
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
/**
|
|
|
|
* Finds the start position, if any, of the timestamp on a line
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $line string The line to search for a signature on
|
|
|
|
* @return int|bool Integer position
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
|
|
|
static function getTimestampPosition( $line ) {
|
|
|
|
$timestampRegex = self::getTimestampRegex();
|
|
|
|
$endOfLine = self::getLineEndingRegex();
|
|
|
|
$tsMatches = array();
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match(
|
|
|
|
"/$timestampRegex$endOfLine/mu",
|
|
|
|
$line,
|
|
|
|
$tsMatches,
|
|
|
|
PREG_OFFSET_CAPTURE
|
2012-07-31 00:29:49 +00:00
|
|
|
) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-19 13:41:22 +00:00
|
|
|
return $tsMatches[0][1];
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Finds differences between $oldText and $newText
|
|
|
|
* and returns the result in a machine-readable format.
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $oldText string The "left hand side" of the diff.
|
|
|
|
* @param $newText string The "right hand side" of the diff.
|
|
|
|
* @throws MWException
|
2012-07-27 22:16:19 +00:00
|
|
|
* @return Array of changes.
|
|
|
|
* Each change consists of:
|
|
|
|
* * An 'action', one of:
|
|
|
|
* - add
|
|
|
|
* - subtract
|
|
|
|
* - change
|
|
|
|
* * 'content' that was added or removed, or in the case
|
|
|
|
* of a change, 'old_content' and 'new_content'
|
|
|
|
* * 'left_pos' and 'right_pos' (in lines) of the change.
|
|
|
|
*/
|
|
|
|
static function getMachineReadableDiff( $oldText, $newText ) {
|
2013-05-19 04:15:45 +00:00
|
|
|
if ( !isset( self::$diffParser ) ) {
|
|
|
|
self::$diffParser = new EchoDiffParser;
|
2013-04-29 23:24:46 +00:00
|
|
|
}
|
2013-05-19 04:15:45 +00:00
|
|
|
return self::$diffParser->getChangeSet( $oldText, $newText );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds and extracts signatures in $text
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text in which to look for signed comments.
|
2015-02-17 10:10:49 +00:00
|
|
|
* @param Title $title
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return array. Associative array, the key is the username, the value
|
2012-07-27 22:16:19 +00:00
|
|
|
* is the last signature that was found.
|
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
static function extractSignatures( $text, Title $title = null ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$lines = explode( "\n", $text );
|
|
|
|
|
|
|
|
$output = array();
|
|
|
|
|
|
|
|
$lineNumber = 0;
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
++$lineNumber;
|
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// Look for the last user link on the line.
|
2015-02-17 10:10:49 +00:00
|
|
|
$userData = self::getUserFromLine( $line, $title );
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( $userData === false ) {
|
|
|
|
// print "F\t$lineNumber\t$line\n";
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// print "S\t$lineNumber\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $signaturePos, $user ) = $userData;
|
|
|
|
|
|
|
|
$signature = substr( $line, $signaturePos );
|
|
|
|
$output[$user] = $signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-13 01:16:59 +00:00
|
|
|
* From a line in the signature, extract all the users linked to
|
2012-07-27 22:16:19 +00:00
|
|
|
*
|
2015-06-13 01:16:59 +00:00
|
|
|
* @param string $line Line of text potentially including linked user, user talk,
|
|
|
|
* and contribution pages
|
|
|
|
* @return array Array of users; empty array for none detected
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2015-06-13 01:16:59 +00:00
|
|
|
static public function extractUsersFromLine( $line ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
/*
|
|
|
|
* Signatures can look like anything (as defined by i18n messages
|
|
|
|
* "signature" & "signature-anon").
|
|
|
|
* A signature can, e.g., be both a link to user & user-talk page.
|
2015-06-13 01:16:59 +00:00
|
|
|
*
|
2015-01-31 01:00:06 +00:00
|
|
|
*/
|
2015-06-13 01:16:59 +00:00
|
|
|
// match all title-like excerpts in this line
|
|
|
|
if ( !preg_match_all( '/\[\[([^\[]+)\]\]/', $line, $matches ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = $matches[1];
|
|
|
|
|
|
|
|
$usernames = array();
|
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
foreach ( $matches as $match ) {
|
|
|
|
/*
|
|
|
|
* Create an object out of the link title.
|
|
|
|
* In theory, links can be [[text]], [[text|text]] or pipe tricks
|
|
|
|
* [[text|]] or [[|text]].
|
|
|
|
* In the case of reverse pipe trick, the value we use *could* be
|
|
|
|
* empty, but Parser::pstPass2 should have normalized that for us
|
|
|
|
* already.
|
|
|
|
*/
|
|
|
|
$match = explode( '|', $match );
|
|
|
|
$title = Title::newFromText( $match[0] );
|
|
|
|
|
|
|
|
// figure out if we the link is related to a user
|
|
|
|
if ( $title && ( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK ) ) {
|
2015-06-13 01:16:59 +00:00
|
|
|
$usernames[] = $title->getText();
|
2015-01-31 01:00:06 +00:00
|
|
|
} elseif ( $title && $title->isSpecial( 'Contributions' ) ) {
|
|
|
|
$parts = explode( '/', $title->getText(), 2 );
|
2015-06-13 01:16:59 +00:00
|
|
|
$usernames[] = end( $parts );
|
2015-01-31 01:00:06 +00:00
|
|
|
} else {
|
|
|
|
// move on to next matched title-like excerpt
|
2012-07-27 22:16:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-13 01:16:59 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2015-06-13 01:16:59 +00:00
|
|
|
return $usernames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From a line in a wiki page, determine which user, if any,
|
|
|
|
* has signed it.
|
|
|
|
*
|
|
|
|
* @param string $line The line.
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool|array false for none, Array for success.
|
|
|
|
* - First element is the position of the signature.
|
|
|
|
* - Second element is the normalised user name.
|
|
|
|
*/
|
|
|
|
static public function getUserFromLine( $line, Title $title = null ) {
|
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First we call extractUsersFromLine to get all the potential usernames
|
|
|
|
* from the line. Then, we loop backwards through them, figure out which
|
|
|
|
* match to a user, regenera the signature based on that user, and
|
|
|
|
* see if it matches!
|
|
|
|
*/
|
|
|
|
$usernames = self::extractUsersFromLine( $line );
|
|
|
|
$usernames = array_reverse( $usernames );
|
|
|
|
foreach ( $usernames as $username ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
// generate (dateless) signature from the user we think we've
|
|
|
|
// discovered the signature from
|
|
|
|
// don't validate the username - anon (IP) is fine!
|
|
|
|
$user = User::newFromName( $username, false );
|
2015-02-17 10:10:49 +00:00
|
|
|
$sig = $wgParser->preSaveTransform( '~~~', $title ?: Title::newMainPage(), $user, new ParserOptions() );
|
2014-09-29 19:38:01 +00:00
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// see if we can find this user's generated signature in the content
|
|
|
|
$pos = strrpos( $line, $sig );
|
|
|
|
if ( $pos !== false ) {
|
|
|
|
return array( $pos, $username );
|
|
|
|
}
|
|
|
|
|
|
|
|
// couldn't find sig, move on to next link excerpt and try there
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// couldn't find any matching signature
|
|
|
|
return false;
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the last link beginning with a given prefix on a line.
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $line string The line to search.
|
|
|
|
* @param $linkPrefix string The prefix to search for.
|
|
|
|
* @param $failureOffset bool
|
|
|
|
* @return bool false for failure, array for success.
|
2012-07-27 22:16:19 +00:00
|
|
|
* - First element is the string offset of the link.
|
|
|
|
* - Second element is the user the link refers to.
|
|
|
|
*/
|
|
|
|
static function getLinkFromLine( $line, $linkPrefix, $failureOffset = false ) {
|
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
// If extraction failed at another offset, try again.
|
|
|
|
if ( $failureOffset !== false ) {
|
|
|
|
$offset = $failureOffset - strlen( $line ) - 1;
|
|
|
|
}
|
|
|
|
|
2013-11-05 19:02:06 +00:00
|
|
|
// Avoid PHP warning: Offset is greater than the length of haystack string
|
|
|
|
if ( abs( $offset ) > strlen( $line ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
$linkPos = strripos( $line, $linkPrefix, $offset );
|
|
|
|
|
|
|
|
if ( $linkPos === false ) {
|
|
|
|
// print "I\tNo match for $linkPrefix\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$linkUser = self::extractUserFromLink( $line, $linkPrefix, $linkPos );
|
|
|
|
|
|
|
|
if ( $linkUser === false ) {
|
|
|
|
// print "E\tExtraction failed\t$linkPrefix\n";
|
|
|
|
// Look for another place.
|
|
|
|
return self::getLinkFromLine( $line, $linkPrefix, $linkPos );
|
|
|
|
} else {
|
|
|
|
return array( $linkPos, $linkUser );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given text including a link, gives the user that that link refers to
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $text string The text to extract from.
|
|
|
|
* @param $prefix string The link prefix that was used to find the link.
|
|
|
|
* @param $offset int Optionally, the offset of the start of the link.
|
|
|
|
* @return bool|string Type description
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
|
|
|
static function extractUserFromLink( $text, $prefix, $offset = 0 ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$userPart = substr( $text, strlen( $prefix ) + $offset );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
$userMatches = array();
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match(
|
2012-07-27 22:16:19 +00:00
|
|
|
'/^[^\|\]\#]+/u',
|
|
|
|
$userPart,
|
2012-08-01 19:53:05 +00:00
|
|
|
$userMatches
|
2012-07-27 22:16:19 +00:00
|
|
|
) ) {
|
|
|
|
// user link is invalid
|
|
|
|
// print "I\tUser link invalid\t$userPart\n";
|
|
|
|
// print "E\tCannot find user info to extract\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $userMatches[0];
|
|
|
|
|
|
|
|
if (
|
2012-08-31 21:50:46 +00:00
|
|
|
!User::isIP( $user ) &&
|
2012-08-30 16:04:39 +00:00
|
|
|
User::getCanonicalName( $user ) === false
|
2012-07-27 22:16:19 +00:00
|
|
|
) {
|
|
|
|
// Not a real username
|
|
|
|
// print "E\tInvalid username\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return User::getCanonicalName( $userMatches[0], false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a regular expression fragmentmatching characters that
|
|
|
|
* can appear in a line after the signature.
|
2012-08-31 21:50:46 +00:00
|
|
|
*
|
2012-07-27 22:16:19 +00:00
|
|
|
* @return String regular expression fragment.
|
|
|
|
*/
|
|
|
|
static function getLineEndingRegex() {
|
|
|
|
$ignoredEndings = array(
|
|
|
|
'\s*',
|
2012-08-30 16:04:39 +00:00
|
|
|
preg_quote( '}' ),
|
|
|
|
preg_quote( '{' ),
|
2012-07-27 22:16:19 +00:00
|
|
|
'\<[^\>]+\>',
|
2012-08-31 21:50:46 +00:00
|
|
|
preg_quote( '{{' ) . '[^}]+' . preg_quote( '}}' ),
|
2012-07-27 22:16:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$regex = '(?:' . implode( '|', $ignoredEndings ) . ')*';
|
|
|
|
|
|
|
|
return $regex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a regular expression that will match this wiki's
|
|
|
|
* timestamps as given by ~~~~.
|
2012-08-31 21:50:46 +00:00
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2012-07-27 22:16:19 +00:00
|
|
|
* @return String regular expression fragment.
|
|
|
|
*/
|
|
|
|
static function getTimestampRegex() {
|
|
|
|
if ( self::$timestampRegex !== null ) {
|
|
|
|
return self::$timestampRegex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1: Get an exemplar timestamp
|
|
|
|
$title = Title::newMainPage();
|
2012-08-30 16:04:39 +00:00
|
|
|
$user = User::newFromName( 'Test' );
|
2012-07-27 22:16:19 +00:00
|
|
|
$options = new ParserOptions;
|
|
|
|
|
|
|
|
global $wgParser;
|
|
|
|
$exemplarTimestamp =
|
|
|
|
$wgParser->preSaveTransform( '~~~~~', $title, $user, $options );
|
|
|
|
|
|
|
|
// Step 2: Generalise it
|
|
|
|
// Trim off the timezone to replace at the end
|
|
|
|
$output = $exemplarTimestamp;
|
|
|
|
$tzRegex = '/\s*\(\w+\)\s*$/';
|
|
|
|
$tzMatches = array();
|
2014-12-03 17:07:50 +00:00
|
|
|
if ( preg_match( $tzRegex, $output, $tzMatches ) ) {
|
|
|
|
$output = preg_replace( $tzRegex, '', $output );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
$output = preg_quote( $output, '/' );
|
|
|
|
$output = preg_replace( '/[^\d\W]+/u', '[^\d\W]+', $output );
|
|
|
|
$output = preg_replace( '/\d+/u', '\d+', $output );
|
|
|
|
|
2014-12-03 17:07:50 +00:00
|
|
|
if ( $tzMatches ) {
|
|
|
|
$output .= preg_quote( $tzMatches[0] );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match( "/$output/u", $exemplarTimestamp ) ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
throw new MWException( "Timestamp regex does not match exemplar" );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$timestampRegex = $output;
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2012-11-07 01:41:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns plain text snippet, it also removes html tag,
|
|
|
|
* template from text content
|
|
|
|
* @param $text string
|
|
|
|
* @param $length int default 150
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static function getTextSnippet( $text, $length = 150 ) {
|
|
|
|
global $wgLang;
|
|
|
|
|
|
|
|
$text = strip_tags( $text );
|
|
|
|
$attempt = 0;
|
|
|
|
|
|
|
|
// 10 attempts at most, the logic here is to find the first }} and
|
|
|
|
// find the matching {{ for that }}
|
|
|
|
while ( $attempt < 10 ) {
|
|
|
|
$closeCurPos = strpos( $text, '}}' );
|
|
|
|
|
|
|
|
if ( $closeCurPos === false ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$tempStr = substr( $text, 0, $closeCurPos + 2 );
|
|
|
|
|
|
|
|
$openCurPos = strrpos( $tempStr, '{{' );
|
|
|
|
if ( $openCurPos === false ) {
|
|
|
|
$text = substr_replace( $text, '', $closeCurPos, 2 );
|
|
|
|
} else {
|
|
|
|
$text = substr_replace( $text, '', $openCurPos, $closeCurPos - $openCurPos + 2 );
|
|
|
|
}
|
|
|
|
$attempt++;
|
|
|
|
}
|
|
|
|
|
2013-07-24 03:50:43 +00:00
|
|
|
// See Parser::parse() function,   is replaced specifically, replace it back here
|
|
|
|
// with a space as this html entity won't be handled by htmlspecialchars_decode()
|
|
|
|
$text = str_replace( ' ', ' ', MessageCache::singleton()->parse( $text )->getText() );
|
|
|
|
$text = trim( strip_tags( htmlspecialchars_decode( $text ) ) );
|
2012-11-07 01:41:06 +00:00
|
|
|
// strip out non-useful data for snippet
|
|
|
|
$text = str_replace( array( '{', '}' ), '', $text );
|
2013-07-31 21:44:07 +00:00
|
|
|
$text = $wgLang->truncate( $text, $length );
|
2012-11-07 01:41:06 +00:00
|
|
|
|
2013-07-31 21:44:07 +00:00
|
|
|
// Return empty string if there is undecoded char left
|
|
|
|
if ( strpos( $text, '&#' ) !== false ) {
|
|
|
|
$text = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
2012-11-07 01:41:06 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|