2004-02-26 13:37:26 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-09-21 05:49:12 +00:00
|
|
|
* File for Parser and related classes
|
2004-09-02 23:28:24 +00:00
|
|
|
*
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2005-04-12 01:29:21 +00:00
|
|
|
* @subpackage Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
2005-03-04 08:41:28 +00:00
|
|
|
/** */
|
2005-02-06 06:44:48 +00:00
|
|
|
require_once( 'Sanitizer.php' );
|
|
|
|
|
|
2004-11-25 22:02:30 +00:00
|
|
|
/**
|
|
|
|
|
* Update this version number when the ParserOutput format
|
|
|
|
|
* changes in an incompatible way, so the parser cache
|
|
|
|
|
* can automatically discard old data.
|
|
|
|
|
*/
|
2005-03-25 07:25:49 +00:00
|
|
|
define( 'MW_PARSER_VERSION', '1.5.0' );
|
2004-11-25 22:02:30 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Variable substitution O(N^2) attack
|
|
|
|
|
*
|
2004-09-21 05:49:12 +00:00
|
|
|
* Without countermeasures, it would be possible to attack the parser by saving
|
|
|
|
|
* a page filled with a large number of inclusions of large pages. The size of
|
|
|
|
|
* the generated page would be proportional to the square of the input size.
|
|
|
|
|
* Hence, we limit the number of inclusions of any given page, thus bringing any
|
|
|
|
|
* attack back to O(N).
|
|
|
|
|
*/
|
2004-10-08 04:27:07 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'MAX_INCLUDE_REPEAT', 100 );
|
|
|
|
|
define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2004-10-15 17:39:10 +00:00
|
|
|
define( 'RLH_FOR_UPDATE', 1 );
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Allowed values for $mOutputType
|
2004-08-22 17:24:50 +00:00
|
|
|
define( 'OT_HTML', 1 );
|
|
|
|
|
define( 'OT_WIKI', 2 );
|
|
|
|
|
define( 'OT_MSG' , 3 );
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-04-28 04:50:35 +00:00
|
|
|
# string parameter for extractTags which will cause it
|
|
|
|
|
# to strip HTML comments in addition to regular
|
|
|
|
|
# <XML>-style tags. This should not be anything we
|
|
|
|
|
# may want to use in wikisyntax
|
2004-08-07 18:24:12 +00:00
|
|
|
define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
|
2004-04-28 04:50:35 +00:00
|
|
|
|
2004-04-09 07:31:35 +00:00
|
|
|
# prefix for escaping, used in two functions at least
|
2004-08-07 18:24:12 +00:00
|
|
|
define( 'UNIQ_PREFIX', 'NaodW29');
|
|
|
|
|
|
|
|
|
|
# Constants needed for external link processing
|
|
|
|
|
define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
|
|
|
|
|
define( 'HTTP_PROTOCOLS', 'http|https' );
|
|
|
|
|
# Everything except bracket, space, or control characters
|
2004-10-11 22:10:14 +00:00
|
|
|
define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
|
2004-08-07 18:24:12 +00:00
|
|
|
# Including space
|
|
|
|
|
define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
|
|
|
|
|
define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
|
|
|
|
|
define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
|
2005-01-08 03:03:10 +00:00
|
|
|
define( 'EXT_LINK_BRACKETED', '/\[(\b('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
|
2004-08-14 22:38:46 +00:00
|
|
|
define( 'EXT_IMAGE_REGEX',
|
2004-08-07 18:24:12 +00:00
|
|
|
'/^('.HTTP_PROTOCOLS.':)'. # Protocol
|
|
|
|
|
'('.EXT_LINK_URL_CLASS.'+)\\/'. # Hostname and path
|
|
|
|
|
'('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
|
|
|
|
|
);
|
2004-04-09 07:31:35 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-09-21 05:49:12 +00:00
|
|
|
* PHP Parser
|
|
|
|
|
*
|
|
|
|
|
* Processes wiki markup
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* There are three main entry points into the Parser class:
|
|
|
|
|
* parse()
|
|
|
|
|
* produces HTML output
|
|
|
|
|
* preSaveTransform().
|
|
|
|
|
* produces altered wiki markup.
|
|
|
|
|
* transformMsg()
|
|
|
|
|
* performs brace substitution on MediaWiki messages
|
|
|
|
|
*
|
|
|
|
|
* Globals used:
|
2005-04-20 15:42:08 +00:00
|
|
|
* objects: $wgLang, $wgLinkCache
|
2004-09-21 05:49:12 +00:00
|
|
|
*
|
|
|
|
|
* NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
|
|
|
|
|
*
|
|
|
|
|
* settings:
|
|
|
|
|
* $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
|
|
|
|
|
* $wgNamespacesWithSubpages, $wgAllowExternalImages*,
|
|
|
|
|
* $wgLocaltimezone
|
|
|
|
|
*
|
|
|
|
|
* * only within ParserOptions
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-02-26 13:37:26 +00:00
|
|
|
class Parser
|
|
|
|
|
{
|
2004-09-21 05:49:12 +00:00
|
|
|
/**#@+
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
# Persistent:
|
|
|
|
|
var $mTagHooks;
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
# Cleared with clearState():
|
2004-04-15 14:25:34 +00:00
|
|
|
var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
|
|
|
|
|
var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
|
2005-04-27 07:48:14 +00:00
|
|
|
var $mInterwikiLinkHolders, $mLinkHolders;
|
2004-02-29 08:43:29 +00:00
|
|
|
|
|
|
|
|
# Temporary:
|
2004-07-24 00:37:24 +00:00
|
|
|
var $mOptions, $mTitle, $mOutputType,
|
|
|
|
|
$mTemplates, // cache of already loaded templates, avoids
|
2004-08-16 20:01:21 +00:00
|
|
|
// multiple SQL queries for the same string
|
2004-07-24 00:37:24 +00:00
|
|
|
$mTemplatePath; // stores an unsorted hash of all the templates already loaded
|
2004-08-16 20:01:21 +00:00
|
|
|
// in this path. Used for loop detection.
|
2004-09-21 08:31:25 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**#@-*/
|
2004-02-29 08:43:29 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function Parser() {
|
2005-04-27 07:48:14 +00:00
|
|
|
global $wgContLang;
|
2004-07-24 00:37:24 +00:00
|
|
|
$this->mTemplates = array();
|
|
|
|
|
$this->mTemplatePath = array();
|
2004-06-12 06:15:09 +00:00
|
|
|
$this->mTagHooks = array();
|
2004-02-26 13:37:26 +00:00
|
|
|
$this->clearState();
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Clear Parser state
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function clearState() {
|
2004-02-26 13:37:26 +00:00
|
|
|
$this->mOutput = new ParserOutput;
|
|
|
|
|
$this->mAutonumber = 0;
|
2005-01-31 22:59:55 +00:00
|
|
|
$this->mLastSection = '';
|
2004-02-26 13:37:26 +00:00
|
|
|
$this->mDTopen = false;
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->mVariables = false;
|
|
|
|
|
$this->mIncludeCount = array();
|
2004-03-26 21:25:48 +00:00
|
|
|
$this->mStripState = array();
|
2004-04-11 16:46:06 +00:00
|
|
|
$this->mArgStack = array();
|
2004-06-01 22:19:22 +00:00
|
|
|
$this->mInPre = false;
|
2005-04-27 07:48:14 +00:00
|
|
|
$this->mInterwikiLinkHolders = array();
|
|
|
|
|
$this->mLinkHolders = array(
|
|
|
|
|
'namespaces' => array(),
|
|
|
|
|
'dbkeys' => array(),
|
|
|
|
|
'queries' => array(),
|
|
|
|
|
'texts' => array(),
|
|
|
|
|
'titles' => array()
|
|
|
|
|
);
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* First pass--just handle <nowiki> sections, pass the rest off
|
|
|
|
|
* to internalParse() which does all the real work.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
2005-01-31 22:59:55 +00:00
|
|
|
* @param string $text Text we want to parse
|
|
|
|
|
* @param Title &$title A title object
|
|
|
|
|
* @param array $options
|
|
|
|
|
* @param boolean $linestart
|
|
|
|
|
* @param boolean $clearState
|
2004-09-21 05:49:12 +00:00
|
|
|
* @return ParserOutput a ParserOutput
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
|
2004-10-08 04:27:07 +00:00
|
|
|
global $wgUseTidy, $wgContLang;
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'Parser::parse';
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
if ( $clearState ) {
|
|
|
|
|
$this->clearState();
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
$this->mOptions = $options;
|
|
|
|
|
$this->mTitle =& $title;
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->mOutputType = OT_HTML;
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2005-04-21 06:30:48 +00:00
|
|
|
$this->mStripState = NULL;
|
2005-04-27 07:48:14 +00:00
|
|
|
|
2004-12-19 08:00:50 +00:00
|
|
|
//$text = $this->strip( $text, $this->mStripState );
|
|
|
|
|
// VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
|
|
|
|
|
$x =& $this->mStripState;
|
|
|
|
|
$text = $this->strip( $text, $x );
|
2004-10-08 04:27:07 +00:00
|
|
|
|
2005-04-21 06:30:48 +00:00
|
|
|
$text = $this->internalParse( $text );
|
2005-03-15 06:07:58 +00:00
|
|
|
|
|
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
$text = $this->unstrip( $text, $this->mStripState );
|
2005-04-21 01:33:32 +00:00
|
|
|
|
2004-04-15 14:59:46 +00:00
|
|
|
# Clean up special characters, only run once, next-to-last before doBlockLevels
|
2005-04-21 01:33:32 +00:00
|
|
|
$fixtags = array(
|
|
|
|
|
# french spaces, last one Guillemet-left
|
|
|
|
|
# only if there is something before the space
|
|
|
|
|
'/(.) (?=\\?|:|;|!|\\302\\273)/' => '\\1 \\2',
|
|
|
|
|
# french spaces, Guillemet-right
|
|
|
|
|
'/(\\302\\253) /' => '\\1 ',
|
|
|
|
|
'/<hr *>/i' => '<hr />',
|
|
|
|
|
'/<br *>/i' => '<br />',
|
|
|
|
|
'/<center *>/i' => '<div class="center">',
|
|
|
|
|
'/<\\/center *>/i' => '</div>',
|
|
|
|
|
);
|
|
|
|
|
$text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
|
|
|
|
|
|
2004-04-15 14:59:46 +00:00
|
|
|
# only once and last
|
|
|
|
|
$text = $this->doBlockLevels( $text, $linestart );
|
2004-10-08 04:27:07 +00:00
|
|
|
|
2004-10-15 17:39:10 +00:00
|
|
|
$this->replaceLinkHolders( $text );
|
2005-04-26 20:50:16 +00:00
|
|
|
|
2005-05-03 04:49:58 +00:00
|
|
|
$dashReplace = array(
|
|
|
|
|
'/ - /' => " – ", # N dash
|
2005-05-04 01:53:04 +00:00
|
|
|
'/(?<=[\d])-(?=[\d])/' => "–", # N dash between numbers
|
2005-05-03 04:49:58 +00:00
|
|
|
'/ -- /' => " — " # M dash
|
|
|
|
|
);
|
|
|
|
|
$text = preg_replace( array_keys($dashReplace), array_values($dashReplace), $text );
|
|
|
|
|
|
2005-04-26 20:50:16 +00:00
|
|
|
# the position of the convert() call should not be changed. it
|
|
|
|
|
# assumes that the links are all replaces and the only thing left
|
|
|
|
|
# is the <nowiki> mark.
|
2004-10-08 04:27:07 +00:00
|
|
|
$text = $wgContLang->convert($text);
|
2005-04-07 23:04:08 +00:00
|
|
|
$this->mOutput->setTitleText($wgContLang->getParsedTitle());
|
2005-04-26 20:50:16 +00:00
|
|
|
|
2004-06-02 12:29:15 +00:00
|
|
|
$text = $this->unstripNoWiki( $text, $this->mStripState );
|
2005-04-21 01:33:32 +00:00
|
|
|
|
2005-04-21 11:40:58 +00:00
|
|
|
$text = Sanitizer::normalizeCharReferences( $text );
|
2005-04-21 01:33:32 +00:00
|
|
|
global $wgUseTidy;
|
2004-10-15 17:39:10 +00:00
|
|
|
if ($wgUseTidy) {
|
|
|
|
|
$text = Parser::tidy($text);
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
$this->mOutput->setText( $text );
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $this->mOutput;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Get a random string
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function getRandomString() {
|
2004-03-06 01:49:16 +00:00
|
|
|
return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
|
|
|
|
|
}
|
2004-03-26 17:14:23 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces all occurrences of <$tag>content</$tag> in the text
|
|
|
|
|
* with a random marker and returns the new text. the output parameter
|
|
|
|
|
* $content will be an associative array filled with data on the form
|
|
|
|
|
* $unique_marker => content.
|
|
|
|
|
*
|
|
|
|
|
* If $content is already set, the additional entries will be appended
|
|
|
|
|
* If $tag is set to STRIP_COMMENTS, the function will extract
|
|
|
|
|
* <!-- HTML comments -->
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function extractTags($tag, $text, &$content, $uniq_prefix = ''){
|
2004-04-07 17:56:13 +00:00
|
|
|
$rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
|
2004-04-09 15:29:33 +00:00
|
|
|
if ( !$content ) {
|
|
|
|
|
$content = array( );
|
|
|
|
|
}
|
2004-03-26 17:14:23 +00:00
|
|
|
$n = 1;
|
2004-06-08 18:11:28 +00:00
|
|
|
$stripped = '';
|
2004-03-26 17:14:23 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
while ( '' != $text ) {
|
2004-04-28 04:50:35 +00:00
|
|
|
if($tag==STRIP_COMMENTS) {
|
2005-03-11 02:32:19 +00:00
|
|
|
$p = preg_split( '/<!--/', $text, 2 );
|
2004-04-28 04:50:35 +00:00
|
|
|
} else {
|
|
|
|
|
$p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
|
|
|
|
|
}
|
2004-03-26 17:14:23 +00:00
|
|
|
$stripped .= $p[0];
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
|
|
|
|
|
$text = '';
|
2004-03-26 17:14:23 +00:00
|
|
|
} else {
|
2004-04-28 04:50:35 +00:00
|
|
|
if($tag==STRIP_COMMENTS) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$q = preg_split( '/-->/i', $p[1], 2 );
|
2004-04-28 04:50:35 +00:00
|
|
|
} else {
|
|
|
|
|
$q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
$marker = $rnd . sprintf('%08X', $n++);
|
2004-03-26 17:14:23 +00:00
|
|
|
$content[$marker] = $q[0];
|
|
|
|
|
$stripped .= $marker;
|
|
|
|
|
$text = $q[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $stripped;
|
2004-04-12 23:59:37 +00:00
|
|
|
}
|
2004-03-26 17:14:23 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Strips and renders nowiki, pre, math, hiero
|
|
|
|
|
* If $render is set, performs necessary rendering operations on plugins
|
|
|
|
|
* Returns the text, and fills an array with data needed in unstrip()
|
|
|
|
|
* If the $state is already a valid strip state, it adds to the state
|
|
|
|
|
*
|
|
|
|
|
* @param bool $stripcomments when set, HTML comments <!-- like this -->
|
|
|
|
|
* will be stripped in addition to other tags. This is important
|
|
|
|
|
* for section editing, where these comments cause confusion when
|
|
|
|
|
* counting the sections in the wikisource
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function strip( $text, &$state, $stripcomments = false ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
$render = ($this->mOutputType == OT_HTML);
|
2004-07-24 22:59:44 +00:00
|
|
|
$html_content = array();
|
2004-04-12 23:59:37 +00:00
|
|
|
$nowiki_content = array();
|
2004-04-11 16:46:06 +00:00
|
|
|
$math_content = array();
|
|
|
|
|
$pre_content = array();
|
2004-04-28 04:50:35 +00:00
|
|
|
$comment_content = array();
|
2004-06-09 12:15:42 +00:00
|
|
|
$ext_content = array();
|
2004-11-13 10:53:46 +00:00
|
|
|
$gallery_content = array();
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
# Replace any instances of the placeholders
|
2004-04-09 07:31:35 +00:00
|
|
|
$uniq_prefix = UNIQ_PREFIX;
|
2004-04-11 16:46:06 +00:00
|
|
|
#$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
|
2004-07-24 22:59:44 +00:00
|
|
|
# html
|
2004-09-14 05:49:15 +00:00
|
|
|
global $wgRawHtml, $wgWhitelistEdit;
|
|
|
|
|
if( $wgRawHtml && $wgWhitelistEdit ) {
|
2004-07-24 22:59:44 +00:00
|
|
|
$text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
|
|
|
|
|
foreach( $html_content as $marker => $content ) {
|
|
|
|
|
if ($render ) {
|
|
|
|
|
# Raw and unchecked for validity.
|
|
|
|
|
$html_content[$marker] = $content;
|
|
|
|
|
} else {
|
2004-08-22 17:24:50 +00:00
|
|
|
$html_content[$marker] = '<html>'.$content.'</html>';
|
2004-07-24 22:59:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
# nowiki
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
|
2004-07-24 22:59:44 +00:00
|
|
|
foreach( $nowiki_content as $marker => $content ) {
|
2004-06-02 12:29:15 +00:00
|
|
|
if( $render ){
|
|
|
|
|
$nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
|
|
|
|
|
} else {
|
2004-08-22 17:24:50 +00:00
|
|
|
$nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
|
2004-06-02 12:29:15 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
# math
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
|
2004-04-28 04:50:35 +00:00
|
|
|
foreach( $math_content as $marker => $content ){
|
2004-05-09 12:04:59 +00:00
|
|
|
if( $render ) {
|
|
|
|
|
if( $this->mOptions->getUseTeX() ) {
|
|
|
|
|
$math_content[$marker] = renderMath( $content );
|
|
|
|
|
} else {
|
2004-08-22 17:24:50 +00:00
|
|
|
$math_content[$marker] = '<math>'.$content.'<math>';
|
2004-05-09 12:04:59 +00:00
|
|
|
}
|
2004-04-28 04:50:35 +00:00
|
|
|
} else {
|
2004-08-22 17:24:50 +00:00
|
|
|
$math_content[$marker] = '<math>'.$content.'</math>';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
# pre
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
|
2004-03-26 17:14:23 +00:00
|
|
|
foreach( $pre_content as $marker => $content ){
|
2004-06-02 08:01:48 +00:00
|
|
|
if( $render ){
|
2004-06-08 18:11:28 +00:00
|
|
|
$pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
|
2004-03-06 01:49:16 +00:00
|
|
|
} else {
|
2004-08-22 17:24:50 +00:00
|
|
|
$pre_content[$marker] = '<pre>'.$content.'</pre>';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-09 12:15:42 +00:00
|
|
|
|
2004-11-13 10:53:46 +00:00
|
|
|
# gallery
|
|
|
|
|
$text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
|
|
|
|
|
foreach( $gallery_content as $marker => $content ) {
|
|
|
|
|
require_once( 'ImageGallery.php' );
|
|
|
|
|
if ( $render ) {
|
2004-11-13 12:04:31 +00:00
|
|
|
$gallery_content[$marker] = Parser::renderImageGallery( $content );
|
2004-11-13 10:53:46 +00:00
|
|
|
} else {
|
|
|
|
|
$gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
# Comments
|
2004-04-28 04:50:35 +00:00
|
|
|
if($stripcomments) {
|
|
|
|
|
$text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
|
|
|
|
|
foreach( $comment_content as $marker => $content ){
|
2004-08-22 17:24:50 +00:00
|
|
|
$comment_content[$marker] = '<!--'.$content.'-->';
|
2004-04-28 04:50:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
# Extensions
|
2004-06-12 06:15:09 +00:00
|
|
|
foreach ( $this->mTagHooks as $tag => $callback ) {
|
2005-04-21 06:30:48 +00:00
|
|
|
$ext_content[$tag] = array();
|
2004-06-09 12:15:42 +00:00
|
|
|
$text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
|
|
|
|
|
foreach( $ext_content[$tag] as $marker => $content ) {
|
|
|
|
|
if ( $render ) {
|
|
|
|
|
$ext_content[$tag][$marker] = $callback( $content );
|
|
|
|
|
} else {
|
|
|
|
|
$ext_content[$tag][$marker] = "<$tag>$content</$tag>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
# Merge state with the pre-existing state, if there is one
|
|
|
|
|
if ( $state ) {
|
2004-07-24 22:59:44 +00:00
|
|
|
$state['html'] = $state['html'] + $html_content;
|
2004-04-11 16:46:06 +00:00
|
|
|
$state['nowiki'] = $state['nowiki'] + $nowiki_content;
|
|
|
|
|
$state['math'] = $state['math'] + $math_content;
|
|
|
|
|
$state['pre'] = $state['pre'] + $pre_content;
|
2004-04-28 04:50:35 +00:00
|
|
|
$state['comment'] = $state['comment'] + $comment_content;
|
2004-11-13 10:53:46 +00:00
|
|
|
$state['gallery'] = $state['gallery'] + $gallery_content;
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-06-09 12:15:42 +00:00
|
|
|
foreach( $ext_content as $tag => $array ) {
|
|
|
|
|
if ( array_key_exists( $tag, $state ) ) {
|
|
|
|
|
$state[$tag] = $state[$tag] + $array;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-11 16:46:06 +00:00
|
|
|
} else {
|
2004-04-12 23:59:37 +00:00
|
|
|
$state = array(
|
2004-07-24 22:59:44 +00:00
|
|
|
'html' => $html_content,
|
2004-04-11 16:46:06 +00:00
|
|
|
'nowiki' => $nowiki_content,
|
2004-04-12 23:59:37 +00:00
|
|
|
'math' => $math_content,
|
|
|
|
|
'pre' => $pre_content,
|
2004-06-09 12:15:42 +00:00
|
|
|
'comment' => $comment_content,
|
2004-11-13 10:53:46 +00:00
|
|
|
'gallery' => $gallery_content,
|
2004-06-09 12:15:42 +00:00
|
|
|
) + $ext_content;
|
2004-04-11 16:46:06 +00:00
|
|
|
}
|
2004-03-26 17:14:23 +00:00
|
|
|
return $text;
|
2004-03-06 01:49:16 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
2004-10-21 05:04:14 +00:00
|
|
|
* restores pre, math, and hiero removed by strip()
|
2004-09-21 05:49:12 +00:00
|
|
|
*
|
|
|
|
|
* always call unstripNoWiki() after this one
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function unstrip( $text, &$state ) {
|
2004-04-09 15:29:33 +00:00
|
|
|
# Must expand in reverse order, otherwise nested tags will be corrupted
|
|
|
|
|
$contentDict = end( $state );
|
2004-04-12 23:59:37 +00:00
|
|
|
for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
|
2004-07-24 22:59:44 +00:00
|
|
|
if( key($state) != 'nowiki' && key($state) != 'html') {
|
2004-06-02 12:29:15 +00:00
|
|
|
for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
|
|
|
|
|
$text = str_replace( key( $contentDict ), $content, $text );
|
|
|
|
|
}
|
2004-04-09 15:29:33 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
return $text;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-09-21 05:49:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* always call this after unstrip() to preserve the order
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function unstripNoWiki( $text, &$state ) {
|
2004-06-02 12:29:15 +00:00
|
|
|
# Must expand in reverse order, otherwise nested tags will be corrupted
|
|
|
|
|
for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
|
|
|
|
|
$text = str_replace( key( $state['nowiki'] ), $content, $text );
|
|
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-07-24 22:59:44 +00:00
|
|
|
global $wgRawHtml;
|
|
|
|
|
if ($wgRawHtml) {
|
|
|
|
|
for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
|
|
|
|
|
$text = str_replace( key( $state['html'] ), $content, $text );
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-06-02 12:29:15 +00:00
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Add an item to the strip state
|
|
|
|
|
* Returns the unique tag which must be inserted into the stripped text
|
|
|
|
|
* The tag will be replaced with the original text in unstrip()
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function insertStripItem( $text, &$state ) {
|
2004-04-09 15:29:33 +00:00
|
|
|
$rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
|
|
|
|
|
if ( !$state ) {
|
2004-04-12 23:59:37 +00:00
|
|
|
$state = array(
|
2004-07-24 22:59:44 +00:00
|
|
|
'html' => array(),
|
2004-04-09 15:29:33 +00:00
|
|
|
'nowiki' => array(),
|
|
|
|
|
'math' => array(),
|
2004-04-28 04:50:35 +00:00
|
|
|
'pre' => array()
|
2004-04-09 15:29:33 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$state['item'][$rnd] = $text;
|
|
|
|
|
return $rnd;
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
2005-04-20 21:58:57 +00:00
|
|
|
* Interface with html tidy, used if $wgUseTidy = true.
|
|
|
|
|
* If tidy isn't able to correct the markup, the original will be
|
|
|
|
|
* returned in all its glory with a warning comment appended.
|
2004-09-21 05:49:12 +00:00
|
|
|
*
|
2005-04-20 21:58:57 +00:00
|
|
|
* Either the external tidy program or the in-process tidy extension
|
|
|
|
|
* will be used depending on availability. Override the default
|
|
|
|
|
* $wgTidyInternal setting to disable the internal if it's not working.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text Hideous HTML input
|
|
|
|
|
* @return string Corrected HTML output
|
2004-09-25 18:22:21 +00:00
|
|
|
* @access public
|
|
|
|
|
* @static
|
2004-09-21 05:49:12 +00:00
|
|
|
*/
|
2005-04-20 21:58:57 +00:00
|
|
|
function tidy( $text ) {
|
|
|
|
|
global $wgTidyInternal;
|
|
|
|
|
$wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
|
|
|
|
|
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
|
|
|
|
|
'<head><title>test</title></head><body>'.$text.'</body></html>';
|
|
|
|
|
if( $wgTidyInternal ) {
|
|
|
|
|
$correctedtext = Parser::internalTidy( $wrappedtext );
|
|
|
|
|
} else {
|
|
|
|
|
$correctedtext = Parser::externalTidy( $wrappedtext );
|
|
|
|
|
}
|
|
|
|
|
if( is_null( $correctedtext ) ) {
|
|
|
|
|
wfDebug( "Tidy error detected!\n" );
|
|
|
|
|
return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
|
|
|
|
|
}
|
|
|
|
|
return $correctedtext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spawn an external HTML tidy process and get corrected markup back from it.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function externalTidy( $text ) {
|
2004-05-06 12:50:04 +00:00
|
|
|
global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
|
2005-04-20 21:58:57 +00:00
|
|
|
$fname = 'Parser::externalTidy';
|
2004-05-29 04:51:29 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-05-06 12:50:04 +00:00
|
|
|
$cleansource = '';
|
2005-03-26 22:23:48 +00:00
|
|
|
$opts = ' -utf8';
|
2004-05-07 09:06:45 +00:00
|
|
|
|
2004-05-06 12:50:04 +00:00
|
|
|
$descriptorspec = array(
|
2004-06-08 18:11:28 +00:00
|
|
|
0 => array('pipe', 'r'),
|
|
|
|
|
1 => array('pipe', 'w'),
|
|
|
|
|
2 => array('file', '/dev/null', 'a')
|
2004-05-06 12:50:04 +00:00
|
|
|
);
|
2005-04-21 06:30:48 +00:00
|
|
|
$pipes = array();
|
2004-09-25 18:22:21 +00:00
|
|
|
$process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts$opts", $descriptorspec, $pipes);
|
2004-05-06 12:50:04 +00:00
|
|
|
if (is_resource($process)) {
|
2005-04-20 21:58:57 +00:00
|
|
|
fwrite($pipes[0], $text);
|
2004-05-06 12:50:04 +00:00
|
|
|
fclose($pipes[0]);
|
|
|
|
|
while (!feof($pipes[1])) {
|
|
|
|
|
$cleansource .= fgets($pipes[1], 1024);
|
|
|
|
|
}
|
|
|
|
|
fclose($pipes[1]);
|
2005-04-21 06:30:48 +00:00
|
|
|
proc_close($process);
|
2004-05-06 12:50:04 +00:00
|
|
|
}
|
2004-05-29 04:51:29 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-05-06 21:06:23 +00:00
|
|
|
if( $cleansource == '' && $text != '') {
|
2005-04-20 21:58:57 +00:00
|
|
|
// Some kind of error happened, so we couldn't get the corrected text.
|
|
|
|
|
// Just give up; we'll use the source text and append a warning.
|
|
|
|
|
return null;
|
2004-05-06 13:38:23 +00:00
|
|
|
} else {
|
2004-05-06 19:01:05 +00:00
|
|
|
return $cleansource;
|
2004-05-06 13:38:23 +00:00
|
|
|
}
|
2004-05-06 12:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-20 21:58:57 +00:00
|
|
|
/**
|
|
|
|
|
* Use the HTML tidy PECL extension to use the tidy library in-process,
|
|
|
|
|
* saving the overhead of spawning a new process. Currently written to
|
|
|
|
|
* the PHP 4.3.x version of the extension, may not work on PHP 5.
|
|
|
|
|
*
|
|
|
|
|
* 'pear install tidy' should be able to compile the extension module.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function internalTidy( $text ) {
|
|
|
|
|
global $wgTidyConf;
|
|
|
|
|
$fname = 'Parser::internalTidy';
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
tidy_load_config( $wgTidyConf );
|
|
|
|
|
tidy_set_encoding( 'utf8' );
|
|
|
|
|
tidy_parse_string( $text );
|
|
|
|
|
tidy_clean_repair();
|
|
|
|
|
if( tidy_get_status() == 2 ) {
|
|
|
|
|
// 2 is magic number for fatal error
|
|
|
|
|
// http://www.php.net/manual/en/function.tidy-get-status.php
|
|
|
|
|
$cleansource = null;
|
|
|
|
|
} else {
|
|
|
|
|
$cleansource = tidy_get_output();
|
|
|
|
|
}
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $cleansource;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* parse the wiki syntax used to render tables
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function doTableStuff ( $t ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
$fname = 'Parser::doTableStuff';
|
|
|
|
|
wfProfileIn( $fname );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
$t = explode ( "\n" , $t ) ;
|
|
|
|
|
$td = array () ; # Is currently a td tag open?
|
2004-07-12 19:49:20 +00:00
|
|
|
$ltd = array () ; # Was it TD or TH?
|
|
|
|
|
$tr = array () ; # Is currently a tr tag open?
|
|
|
|
|
$ltr = array () ; # tr attributes
|
2004-08-04 16:37:48 +00:00
|
|
|
$indent_level = 0; # indent level of the table
|
2004-07-12 19:49:20 +00:00
|
|
|
foreach ( $t AS $k => $x )
|
|
|
|
|
{
|
|
|
|
|
$x = trim ( $x ) ;
|
|
|
|
|
$fc = substr ( $x , 0 , 1 ) ;
|
2004-08-16 20:01:21 +00:00
|
|
|
if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
|
2004-08-04 16:37:48 +00:00
|
|
|
$indent_level = strlen( $matches[1] );
|
2005-04-09 08:36:29 +00:00
|
|
|
$t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
|
2005-02-06 12:46:31 +00:00
|
|
|
'<table' . Sanitizer::fixTagAttributes ( $matches[2], 'table' ) . '>' ;
|
2004-07-12 19:49:20 +00:00
|
|
|
array_push ( $td , false ) ;
|
|
|
|
|
array_push ( $ltd , '' ) ;
|
|
|
|
|
array_push ( $tr , false ) ;
|
|
|
|
|
array_push ( $ltr , '' ) ;
|
|
|
|
|
}
|
|
|
|
|
else if ( count ( $td ) == 0 ) { } # Don't do any of the following
|
2004-08-16 20:01:21 +00:00
|
|
|
else if ( '|}' == substr ( $x , 0 , 2 ) ) {
|
2005-04-09 08:36:29 +00:00
|
|
|
$z = "</table>" . substr ( $x , 2);
|
2004-07-12 19:49:20 +00:00
|
|
|
$l = array_pop ( $ltd ) ;
|
|
|
|
|
if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
|
2004-08-16 20:01:21 +00:00
|
|
|
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
|
2004-07-12 19:49:20 +00:00
|
|
|
array_pop ( $ltr ) ;
|
2004-08-22 17:24:50 +00:00
|
|
|
$t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
|
2004-07-12 19:49:20 +00:00
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
|
2004-07-12 19:49:20 +00:00
|
|
|
$x = substr ( $x , 1 ) ;
|
|
|
|
|
while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
|
|
|
|
|
$z = '' ;
|
|
|
|
|
$l = array_pop ( $ltd ) ;
|
|
|
|
|
if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
|
2004-08-16 20:01:21 +00:00
|
|
|
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
|
2004-07-12 19:49:20 +00:00
|
|
|
array_pop ( $ltr ) ;
|
|
|
|
|
$t[$k] = $z ;
|
|
|
|
|
array_push ( $tr , false ) ;
|
|
|
|
|
array_push ( $td , false ) ;
|
|
|
|
|
array_push ( $ltd , '' ) ;
|
2005-02-06 12:46:31 +00:00
|
|
|
array_push ( $ltr , Sanitizer::fixTagAttributes ( $x, 'tr' ) ) ;
|
2004-07-12 19:49:20 +00:00
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
|
2004-10-01 21:20:47 +00:00
|
|
|
# $x is a table row
|
2004-08-16 20:01:21 +00:00
|
|
|
if ( '|+' == substr ( $x , 0 , 2 ) ) {
|
2004-07-12 19:49:20 +00:00
|
|
|
$fc = '+' ;
|
2004-02-29 08:43:29 +00:00
|
|
|
$x = substr ( $x , 1 ) ;
|
|
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
$after = substr ( $x , 1 ) ;
|
|
|
|
|
if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
|
|
|
|
|
$after = explode ( '||' , $after ) ;
|
|
|
|
|
$t[$k] = '' ;
|
2004-10-01 21:20:47 +00:00
|
|
|
|
|
|
|
|
# Loop through each table cell
|
2004-07-12 19:49:20 +00:00
|
|
|
foreach ( $after AS $theline )
|
2004-02-29 08:43:29 +00:00
|
|
|
{
|
2004-07-12 19:49:20 +00:00
|
|
|
$z = '' ;
|
|
|
|
|
if ( $fc != '+' )
|
2004-02-29 08:43:29 +00:00
|
|
|
{
|
2004-07-12 19:49:20 +00:00
|
|
|
$tra = array_pop ( $ltr ) ;
|
2005-02-06 12:46:31 +00:00
|
|
|
if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
|
2004-07-12 19:49:20 +00:00
|
|
|
array_push ( $tr , true ) ;
|
|
|
|
|
array_push ( $ltr , '' ) ;
|
2004-02-29 08:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
2004-07-12 19:49:20 +00:00
|
|
|
$l = array_pop ( $ltd ) ;
|
2004-08-16 20:01:21 +00:00
|
|
|
if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
|
2004-07-12 19:49:20 +00:00
|
|
|
if ( $fc == '|' ) $l = 'td' ;
|
|
|
|
|
else if ( $fc == '!' ) $l = 'th' ;
|
|
|
|
|
else if ( $fc == '+' ) $l = 'caption' ;
|
|
|
|
|
else $l = '' ;
|
|
|
|
|
array_push ( $ltd , $l ) ;
|
2004-10-01 21:20:47 +00:00
|
|
|
|
|
|
|
|
# Cell parameters
|
2004-07-12 19:49:20 +00:00
|
|
|
$y = explode ( '|' , $theline , 2 ) ;
|
2004-10-01 21:20:47 +00:00
|
|
|
# Note that a '|' inside an invalid link should not
|
|
|
|
|
# be mistaken as delimiting cell parameters
|
|
|
|
|
if ( strpos( $y[0], '[[' ) !== false ) {
|
|
|
|
|
$y = array ($theline);
|
|
|
|
|
}
|
|
|
|
|
if ( count ( $y ) == 1 )
|
|
|
|
|
$y = "{$z}<{$l}>{$y[0]}" ;
|
2005-02-06 12:46:31 +00:00
|
|
|
else $y = $y = "{$z}<{$l}".Sanitizer::fixTagAttributes($y[0], $l).">{$y[1]}" ;
|
2004-07-12 19:49:20 +00:00
|
|
|
$t[$k] .= $y ;
|
|
|
|
|
array_push ( $td , true ) ;
|
2004-02-28 05:55:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
}
|
2004-02-29 08:43:29 +00:00
|
|
|
|
|
|
|
|
# Closing open td, tr && table
|
|
|
|
|
while ( count ( $td ) > 0 )
|
|
|
|
|
{
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( array_pop ( $td ) ) $t[] = '</td>' ;
|
|
|
|
|
if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
|
|
|
|
|
$t[] = '</table>' ;
|
2004-02-28 05:55:13 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
$t = implode ( "\n" , $t ) ;
|
2004-07-10 03:09:26 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-02-29 08:43:29 +00:00
|
|
|
return $t ;
|
2004-02-28 05:55:13 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function for parse() that transforms wiki markup into
|
|
|
|
|
* HTML. Only called for $mOutputType == OT_HTML.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2005-04-21 06:30:48 +00:00
|
|
|
function internalParse( $text ) {
|
2004-10-11 16:16:27 +00:00
|
|
|
global $wgContLang;
|
2005-04-21 06:30:48 +00:00
|
|
|
$args = array();
|
|
|
|
|
$isMain = true;
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::internalParse';
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-04-11 16:46:06 +00:00
|
|
|
|
2005-02-06 06:44:48 +00:00
|
|
|
$text = Sanitizer::removeHTMLtags( $text );
|
2004-04-11 16:46:06 +00:00
|
|
|
$text = $this->replaceVariables( $text, $args );
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
|
2004-02-26 13:37:26 +00:00
|
|
|
|
|
|
|
|
$text = $this->doHeadings( $text );
|
2004-02-29 08:43:29 +00:00
|
|
|
if($this->mOptions->getUseDynamicDates()) {
|
2005-04-20 15:42:08 +00:00
|
|
|
$df =& DateFormatter::getInstance();
|
|
|
|
|
$text = $df->reformat( $this->mOptions->getDateFormat(), $text );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
$text = $this->doAllQuotes( $text );
|
2004-11-28 03:29:50 +00:00
|
|
|
$text = $this->replaceInternalLinks( $text );
|
2004-10-25 05:24:23 +00:00
|
|
|
$text = $this->replaceExternalLinks( $text );
|
|
|
|
|
|
|
|
|
|
# replaceInternalLinks may sometimes leave behind
|
|
|
|
|
# absolute URLs, which have to be masked to hide them from replaceExternalLinks
|
|
|
|
|
$text = str_replace("http-noparse://","http://",$text);
|
|
|
|
|
|
2004-09-26 17:59:08 +00:00
|
|
|
$text = $this->doMagicLinks( $text );
|
2004-08-04 01:53:29 +00:00
|
|
|
$text = $this->doTableStuff( $text );
|
2004-05-13 17:16:50 +00:00
|
|
|
$text = $this->formatHeadings( $text, $isMain );
|
2004-04-12 16:10:17 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replace special strings like "ISBN xxx" and "RFC xxx" with
|
|
|
|
|
* magic external links.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function &doMagicLinks( &$text ) {
|
2004-08-04 01:53:29 +00:00
|
|
|
$text = $this->magicISBN( $text );
|
2004-11-20 11:28:37 +00:00
|
|
|
$text = $this->magicRFC( $text, 'RFC ', 'rfcurl' );
|
|
|
|
|
$text = $this->magicRFC( $text, 'PMID ', 'pubmedurl' );
|
2004-08-04 01:53:29 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Parse ^^ tokens and return html
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-11-20 11:28:37 +00:00
|
|
|
function doExponent( $text ) {
|
2004-06-19 06:48:02 +00:00
|
|
|
$fname = 'Parser::doExponent';
|
2004-11-20 11:28:37 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-06-17 17:29:54 +00:00
|
|
|
$text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
|
2004-11-20 11:28:37 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-06-17 17:29:54 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Parse headers and return html
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function doHeadings( $text ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::doHeadings';
|
2004-06-05 04:51:24 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-02-26 13:37:26 +00:00
|
|
|
for ( $i = 6; $i >= 1; --$i ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$h = substr( '======', 0, $i );
|
2004-03-16 03:58:36 +00:00
|
|
|
$text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
|
2004-02-26 13:37:26 +00:00
|
|
|
"<h{$i}>\\1</h{$i}>\\2", $text );
|
|
|
|
|
}
|
2004-06-05 04:51:24 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-02-26 13:37:26 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replace single quotes with HTML markup
|
|
|
|
|
* @access private
|
|
|
|
|
* @return string the altered text
|
|
|
|
|
*/
|
|
|
|
|
function doAllQuotes( $text ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::doAllQuotes';
|
2004-06-05 04:51:24 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-06-08 18:11:28 +00:00
|
|
|
$outtext = '';
|
2004-05-27 16:08:12 +00:00
|
|
|
$lines = explode( "\n", $text );
|
2004-05-26 16:29:04 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2004-08-06 20:47:21 +00:00
|
|
|
$outtext .= $this->doQuotes ( $line ) . "\n";
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
2004-06-05 04:51:24 +00:00
|
|
|
$outtext = substr($outtext, 0,-1);
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $outtext;
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function for doAllQuotes()
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function doQuotes( $text ) {
|
2004-11-20 11:28:37 +00:00
|
|
|
$arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
|
|
|
|
if ( count( $arr ) == 1 )
|
2004-08-06 20:47:21 +00:00
|
|
|
return $text;
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-08-06 21:13:30 +00:00
|
|
|
# First, do some preliminary work. This may shift some apostrophes from
|
|
|
|
|
# being mark-up to being text. It also counts the number of occurrences
|
|
|
|
|
# of bold and italics mark-ups.
|
2004-08-06 20:47:21 +00:00
|
|
|
$i = 0;
|
2004-08-06 21:13:30 +00:00
|
|
|
$numbold = 0;
|
|
|
|
|
$numitalics = 0;
|
2004-11-20 11:28:37 +00:00
|
|
|
foreach ( $arr as $r )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( ( $i % 2 ) == 1 )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
|
|
|
|
# If there are ever four apostrophes, assume the first is supposed to
|
|
|
|
|
# be text, and the remaining three constitute mark-up for bold text.
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( strlen( $arr[$i] ) == 4 )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
|
|
|
|
$arr[$i-1] .= "'";
|
|
|
|
|
$arr[$i] = "'''";
|
|
|
|
|
}
|
|
|
|
|
# If there are more than 5 apostrophes in a row, assume they're all
|
|
|
|
|
# text except for the last 5.
|
2004-11-20 11:28:37 +00:00
|
|
|
else if ( strlen( $arr[$i] ) > 5 )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
2004-11-20 11:28:37 +00:00
|
|
|
$arr[$i-1] .= str_repeat( "'", strlen( $arr[$i] ) - 5 );
|
2004-08-06 20:47:21 +00:00
|
|
|
$arr[$i] = "'''''";
|
|
|
|
|
}
|
2004-08-06 21:13:30 +00:00
|
|
|
# Count the number of occurrences of bold and italics mark-ups.
|
2004-08-07 12:37:20 +00:00
|
|
|
# We are not counting sequences of five apostrophes.
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( strlen( $arr[$i] ) == 2 ) $numitalics++; else
|
|
|
|
|
if ( strlen( $arr[$i] ) == 3 ) $numbold++; else
|
|
|
|
|
if ( strlen( $arr[$i] ) == 5 ) { $numitalics++; $numbold++; }
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
2004-08-06 20:47:21 +00:00
|
|
|
$i++;
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-08-06 20:47:21 +00:00
|
|
|
# If there is an odd number of both bold and italics, it is likely
|
|
|
|
|
# that one of the bold ones was meant to be an apostrophe followed
|
|
|
|
|
# by italics. Which one we cannot know for certain, but it is more
|
|
|
|
|
# likely to be one that has a single-letter word before it.
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
|
|
|
|
$i = 0;
|
|
|
|
|
$firstsingleletterword = -1;
|
|
|
|
|
$firstmultiletterword = -1;
|
|
|
|
|
$firstspace = -1;
|
2004-11-20 11:28:37 +00:00
|
|
|
foreach ( $arr as $r )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( ( $i % 2 == 1 ) and ( strlen( $r ) == 3 ) )
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
|
|
|
|
$x1 = substr ($arr[$i-1], -1);
|
|
|
|
|
$x2 = substr ($arr[$i-1], -2, 1);
|
2004-08-22 17:24:50 +00:00
|
|
|
if ($x1 == ' ') {
|
2004-08-06 20:47:21 +00:00
|
|
|
if ($firstspace == -1) $firstspace = $i;
|
2004-08-22 17:24:50 +00:00
|
|
|
} else if ($x2 == ' ') {
|
2004-08-06 20:47:21 +00:00
|
|
|
if ($firstsingleletterword == -1) $firstsingleletterword = $i;
|
|
|
|
|
} else {
|
|
|
|
|
if ($firstmultiletterword == -1) $firstmultiletterword = $i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-08-06 20:47:21 +00:00
|
|
|
# If there is a single-letter word, use it!
|
|
|
|
|
if ($firstsingleletterword > -1)
|
|
|
|
|
{
|
|
|
|
|
$arr [ $firstsingleletterword ] = "''";
|
|
|
|
|
$arr [ $firstsingleletterword-1 ] .= "'";
|
|
|
|
|
}
|
|
|
|
|
# If not, but there's a multi-letter word, use that one.
|
|
|
|
|
else if ($firstmultiletterword > -1)
|
|
|
|
|
{
|
|
|
|
|
$arr [ $firstmultiletterword ] = "''";
|
|
|
|
|
$arr [ $firstmultiletterword-1 ] .= "'";
|
|
|
|
|
}
|
|
|
|
|
# ... otherwise use the first one that has neither.
|
2004-08-14 08:32:57 +00:00
|
|
|
# (notice that it is possible for all three to be -1 if, for example,
|
|
|
|
|
# there is only one pentuple-apostrophe in the line)
|
|
|
|
|
else if ($firstspace > -1)
|
2004-08-06 20:47:21 +00:00
|
|
|
{
|
|
|
|
|
$arr [ $firstspace ] = "''";
|
|
|
|
|
$arr [ $firstspace-1 ] .= "'";
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-07 12:35:59 +00:00
|
|
|
|
2004-08-06 20:47:21 +00:00
|
|
|
# Now let's actually convert our apostrophic mush to HTML!
|
|
|
|
|
$output = '';
|
|
|
|
|
$buffer = '';
|
|
|
|
|
$state = '';
|
|
|
|
|
$i = 0;
|
|
|
|
|
foreach ($arr as $r)
|
|
|
|
|
{
|
|
|
|
|
if (($i % 2) == 0)
|
|
|
|
|
{
|
|
|
|
|
if ($state == 'both')
|
|
|
|
|
$buffer .= $r;
|
|
|
|
|
else
|
|
|
|
|
$output .= $r;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (strlen ($r) == 2)
|
|
|
|
|
{
|
2004-09-11 08:40:26 +00:00
|
|
|
if ($state == 'i')
|
|
|
|
|
{ $output .= '</i>'; $state = ''; }
|
|
|
|
|
else if ($state == 'bi')
|
|
|
|
|
{ $output .= '</i>'; $state = 'b'; }
|
|
|
|
|
else if ($state == 'ib')
|
|
|
|
|
{ $output .= '</b></i><b>'; $state = 'b'; }
|
2004-08-06 20:47:21 +00:00
|
|
|
else if ($state == 'both')
|
2004-09-11 08:40:26 +00:00
|
|
|
{ $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
|
|
|
|
|
else # $state can be 'b' or ''
|
|
|
|
|
{ $output .= '<i>'; $state .= 'i'; }
|
2004-08-06 20:47:21 +00:00
|
|
|
}
|
|
|
|
|
else if (strlen ($r) == 3)
|
|
|
|
|
{
|
2004-09-11 08:40:26 +00:00
|
|
|
if ($state == 'b')
|
|
|
|
|
{ $output .= '</b>'; $state = ''; }
|
|
|
|
|
else if ($state == 'bi')
|
|
|
|
|
{ $output .= '</i></b><i>'; $state = 'i'; }
|
|
|
|
|
else if ($state == 'ib')
|
|
|
|
|
{ $output .= '</b>'; $state = 'i'; }
|
2004-08-06 20:47:21 +00:00
|
|
|
else if ($state == 'both')
|
2004-09-11 08:40:26 +00:00
|
|
|
{ $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
|
|
|
|
|
else # $state can be 'i' or ''
|
|
|
|
|
{ $output .= '<b>'; $state .= 'b'; }
|
2004-08-06 20:47:21 +00:00
|
|
|
}
|
|
|
|
|
else if (strlen ($r) == 5)
|
|
|
|
|
{
|
2004-09-11 08:40:26 +00:00
|
|
|
if ($state == 'b')
|
|
|
|
|
{ $output .= '</b><i>'; $state = 'i'; }
|
|
|
|
|
else if ($state == 'i')
|
|
|
|
|
{ $output .= '</i><b>'; $state = 'b'; }
|
|
|
|
|
else if ($state == 'bi')
|
|
|
|
|
{ $output .= '</i></b>'; $state = ''; }
|
|
|
|
|
else if ($state == 'ib')
|
|
|
|
|
{ $output .= '</b></i>'; $state = ''; }
|
2004-08-06 20:47:21 +00:00
|
|
|
else if ($state == 'both')
|
2004-09-11 08:40:26 +00:00
|
|
|
{ $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
|
2004-08-06 20:47:21 +00:00
|
|
|
else # ($state == '')
|
|
|
|
|
{ $buffer = ''; $state = 'both'; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$i++;
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
2004-08-07 12:37:20 +00:00
|
|
|
# Now close all remaining tags. Notice that the order is important.
|
2004-09-11 08:40:26 +00:00
|
|
|
if ($state == 'b' || $state == 'ib')
|
|
|
|
|
$output .= '</b>';
|
|
|
|
|
if ($state == 'i' || $state == 'bi' || $state == 'ib')
|
|
|
|
|
$output .= '</i>';
|
|
|
|
|
if ($state == 'bi')
|
|
|
|
|
$output .= '</b>';
|
2004-08-07 12:37:20 +00:00
|
|
|
if ($state == 'both')
|
2004-09-11 08:40:26 +00:00
|
|
|
$output .= '<b><i>'.$buffer.'</i></b>';
|
2004-08-06 20:47:21 +00:00
|
|
|
return $output;
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replace external links
|
|
|
|
|
*
|
2004-10-25 05:24:23 +00:00
|
|
|
* Note: this is all very hackish and the order of execution matters a lot.
|
|
|
|
|
* Make sure to run maintenance/parserTests.php if you change this code.
|
2004-09-21 05:49:12 +00:00
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function replaceExternalLinks( $text ) {
|
2005-04-27 20:19:35 +00:00
|
|
|
global $wgContLang;
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::replaceExternalLinks';
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-08-07 08:54:52 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
2004-12-09 05:51:20 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
$bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
|
|
|
|
|
|
|
|
|
$s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
|
|
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
while ( $i<count( $bits ) ) {
|
|
|
|
|
$url = $bits[$i++];
|
|
|
|
|
$protocol = $bits[$i++];
|
|
|
|
|
$text = $bits[$i++];
|
|
|
|
|
$trail = $bits[$i++];
|
2004-08-14 22:38:46 +00:00
|
|
|
|
2004-10-11 16:57:49 +00:00
|
|
|
# The characters '<' and '>' (which were escaped by
|
|
|
|
|
# removeHTMLtags()) should not be included in
|
|
|
|
|
# URLs, per RFC 2396.
|
|
|
|
|
if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
|
2004-10-11 19:15:24 +00:00
|
|
|
$text = substr($url, $m2[0][1]) . ' ' . $text;
|
2004-10-11 16:57:49 +00:00
|
|
|
$url = substr($url, 0, $m2[0][1]);
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
# If the link text is an image URL, replace it with an <img> tag
|
|
|
|
|
# This happened by accident in the original parser, but some people used it extensively
|
2005-04-27 07:48:14 +00:00
|
|
|
$img = $this->maybeMakeExternalImage( $text );
|
2004-08-07 18:24:12 +00:00
|
|
|
if ( $img !== false ) {
|
|
|
|
|
$text = $img;
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
$dtrail = '';
|
2004-08-07 08:54:52 +00:00
|
|
|
|
2005-01-15 23:56:26 +00:00
|
|
|
# Set linktype for CSS - if URL==text, link is essentially free
|
|
|
|
|
$linktype = ($text == $url) ? 'free' : 'text';
|
|
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
# No link text, e.g. [http://domain.tld/some.link]
|
|
|
|
|
if ( $text == '' ) {
|
|
|
|
|
# Autonumber if allowed
|
2004-08-14 22:38:46 +00:00
|
|
|
if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
$text = '[' . ++$this->mAutonumber . ']';
|
2005-01-15 23:56:26 +00:00
|
|
|
$linktype = 'autonumber';
|
2004-08-14 22:38:46 +00:00
|
|
|
} else {
|
2004-08-07 18:24:12 +00:00
|
|
|
# Otherwise just use the URL
|
2004-08-14 22:38:46 +00:00
|
|
|
$text = htmlspecialchars( $url );
|
2005-01-15 23:56:26 +00:00
|
|
|
$linktype = 'free';
|
2004-08-07 18:24:12 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# Have link text, e.g. [http://domain.tld/some.link text]s
|
|
|
|
|
# Check for trail
|
2005-04-27 07:48:14 +00:00
|
|
|
list( $dtrail, $trail ) = Linker::splitTrail( $trail );
|
2004-08-07 18:24:12 +00:00
|
|
|
}
|
2004-08-14 22:38:46 +00:00
|
|
|
|
2005-04-26 20:50:16 +00:00
|
|
|
$text = $wgContLang->markNoConversion($text);
|
|
|
|
|
|
2005-01-30 04:11:22 +00:00
|
|
|
# Replace & from obsolete syntax with &.
|
|
|
|
|
# All HTML entities will be escaped by makeExternalLink()
|
2005-04-27 07:48:14 +00:00
|
|
|
# or maybeMakeExternalImage()
|
2005-01-30 04:11:22 +00:00
|
|
|
$url = str_replace( '&', '&', $url );
|
|
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
# Process the trail (i.e. everything after this link up until start of the next link),
|
|
|
|
|
# replacing any non-bracketed links
|
|
|
|
|
$trail = $this->replaceFreeExternalLinks( $trail );
|
|
|
|
|
|
2005-04-26 20:50:16 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
# Use the encoded URL
|
|
|
|
|
# This means that users can paste URLs directly into the text
|
|
|
|
|
# Funny characters like ö aren't valid in URLs anyway
|
|
|
|
|
# This was changed in August 2004
|
2005-01-15 23:56:26 +00:00
|
|
|
$s .= $sk->makeExternalLink( $url, $text, false, $linktype ) . $dtrail . $trail;
|
2004-08-07 18:24:12 +00:00
|
|
|
}
|
2004-08-07 08:54:52 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replace anything that looks like a URL with a link
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-08-07 18:24:12 +00:00
|
|
|
function replaceFreeExternalLinks( $text ) {
|
2005-04-26 20:50:16 +00:00
|
|
|
global $wgContLang;
|
2004-11-26 12:10:24 +00:00
|
|
|
$fname = 'Parser::replaceFreeExternalLinks';
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2005-01-08 03:03:10 +00:00
|
|
|
$bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
2004-08-07 18:24:12 +00:00
|
|
|
$s = array_shift( $bits );
|
|
|
|
|
$i = 0;
|
2004-08-07 08:54:52 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
while ( $i < count( $bits ) ){
|
|
|
|
|
$protocol = $bits[$i++];
|
|
|
|
|
$remainder = $bits[$i++];
|
2004-08-07 08:54:52 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
if ( preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
|
|
|
|
|
# Found some characters after the protocol that look promising
|
|
|
|
|
$url = $protocol . $m[1];
|
2004-02-26 13:37:26 +00:00
|
|
|
$trail = $m[2];
|
2004-08-14 22:38:46 +00:00
|
|
|
|
2004-10-11 16:57:49 +00:00
|
|
|
# The characters '<' and '>' (which were escaped by
|
|
|
|
|
# removeHTMLtags()) should not be included in
|
|
|
|
|
# URLs, per RFC 2396.
|
|
|
|
|
if (preg_match('/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE)) {
|
|
|
|
|
$trail = substr($url, $m2[0][1]) . $trail;
|
|
|
|
|
$url = substr($url, 0, $m2[0][1]);
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 18:45:30 +00:00
|
|
|
# Move trailing punctuation to $trail
|
|
|
|
|
$sep = ',;\.:!?';
|
|
|
|
|
# If there is no left bracket, then consider right brackets fair game too
|
|
|
|
|
if ( strpos( $url, '(' ) === false ) {
|
|
|
|
|
$sep .= ')';
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
$numSepChars = strspn( strrev( $url ), $sep );
|
|
|
|
|
if ( $numSepChars ) {
|
|
|
|
|
$trail = substr( $url, -$numSepChars ) . $trail;
|
|
|
|
|
$url = substr( $url, 0, -$numSepChars );
|
2004-07-13 00:01:33 +00:00
|
|
|
}
|
2004-07-12 22:57:13 +00:00
|
|
|
|
2004-10-11 16:57:49 +00:00
|
|
|
# Replace & from obsolete syntax with &.
|
|
|
|
|
# All HTML entities will be escaped by makeExternalLink()
|
2005-04-27 07:48:14 +00:00
|
|
|
# or maybeMakeExternalImage()
|
2004-10-11 16:57:49 +00:00
|
|
|
$url = str_replace( '&', '&', $url );
|
2004-08-10 11:24:24 +00:00
|
|
|
|
2004-08-07 18:24:12 +00:00
|
|
|
# Is this an external image?
|
2005-04-27 07:48:14 +00:00
|
|
|
$text = $this->maybeMakeExternalImage( $url );
|
2004-08-07 18:24:12 +00:00
|
|
|
if ( $text === false ) {
|
|
|
|
|
# Not an image, make a link
|
2005-04-26 20:50:16 +00:00
|
|
|
$text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free' );
|
2004-08-07 18:24:12 +00:00
|
|
|
}
|
|
|
|
|
$s .= $text . $trail;
|
2004-03-21 11:28:44 +00:00
|
|
|
} else {
|
2004-08-07 18:24:12 +00:00
|
|
|
$s .= $protocol . $remainder;
|
2004-03-21 11:28:44 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-11-26 12:10:24 +00:00
|
|
|
wfProfileOut();
|
2004-02-26 13:37:26 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
2004-08-14 22:38:46 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* make an image if it's allowed
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2005-04-27 07:48:14 +00:00
|
|
|
function maybeMakeExternalImage( $url ) {
|
2004-08-07 18:24:12 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
|
|
|
|
$text = false;
|
|
|
|
|
if ( $this->mOptions->getAllowExternalImages() ) {
|
|
|
|
|
if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
|
|
|
|
|
# Image found
|
2005-04-27 07:48:14 +00:00
|
|
|
$text = $sk->makeExternalImage( htmlspecialchars( $url ) );
|
2004-08-07 18:24:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Process [[ ]] wikilinks
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function replaceInternalLinks( $s ) {
|
2005-04-21 06:30:48 +00:00
|
|
|
global $wgContLang, $wgLinkCache;
|
2004-06-08 18:11:28 +00:00
|
|
|
static $fname = 'Parser::replaceInternalLinks' ;
|
2004-10-23 13:00:33 +00:00
|
|
|
|
2004-02-28 23:38:08 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
wfProfileIn( $fname.'-setup' );
|
2004-02-28 23:38:08 +00:00
|
|
|
static $tc = FALSE;
|
2004-05-25 14:26:14 +00:00
|
|
|
# the % is needed to support urlencoded titles as well
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
|
2004-10-19 07:30:56 +00:00
|
|
|
|
2004-02-29 08:43:29 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-10-05 03:55:41 +00:00
|
|
|
#split the entire text string on occurences of [[
|
2004-06-08 18:11:28 +00:00
|
|
|
$a = explode( '[[', ' ' . $s );
|
2004-10-05 03:55:41 +00:00
|
|
|
#get the first element (all text up to first [[), and remove the space we added
|
2004-05-26 16:29:04 +00:00
|
|
|
$s = array_shift( $a );
|
|
|
|
|
$s = substr( $s, 1 );
|
|
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
# Match a link having the form [[namespace:link|alternate]]trail
|
2004-02-28 23:38:08 +00:00
|
|
|
static $e1 = FALSE;
|
2005-01-13 09:13:22 +00:00
|
|
|
if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD"; }
|
2004-10-05 03:55:41 +00:00
|
|
|
# Match cases where there is no "]]", which might still be images
|
|
|
|
|
static $e1_img = FALSE;
|
|
|
|
|
if ( !$e1_img ) { $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; }
|
2004-02-26 13:37:26 +00:00
|
|
|
# Match the end of a line for a word that's not followed by whitespace,
|
|
|
|
|
# e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
|
2004-06-02 22:39:06 +00:00
|
|
|
static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-24 13:14:52 +00:00
|
|
|
$useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2005-02-21 11:28:07 +00:00
|
|
|
if( is_null( $this->mTitle ) ) {
|
|
|
|
|
wfDebugDieBacktrace( 'nooo' );
|
|
|
|
|
}
|
|
|
|
|
$nottalk = !$this->mTitle->isTalkPage();
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-06-02 22:54:01 +00:00
|
|
|
if ( $useLinkPrefixExtension ) {
|
|
|
|
|
if ( preg_match( $e2, $s, $m ) ) {
|
|
|
|
|
$first_prefix = $m[2];
|
|
|
|
|
$s = $m[1];
|
|
|
|
|
} else {
|
|
|
|
|
$first_prefix = false;
|
|
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
} else {
|
2004-06-02 22:54:01 +00:00
|
|
|
$prefix = '';
|
2004-05-26 16:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-23 07:41:07 +00:00
|
|
|
$selflink = $this->mTitle->getPrefixedText();
|
2004-06-08 18:11:28 +00:00
|
|
|
wfProfileOut( $fname.'-setup' );
|
2004-05-26 16:29:04 +00:00
|
|
|
|
2004-10-21 02:47:51 +00:00
|
|
|
$checkVariantLink = sizeof($wgContLang->getVariants())>1;
|
2004-11-28 03:29:50 +00:00
|
|
|
$useSubpages = $this->areSubpagesAllowed();
|
|
|
|
|
|
2004-10-05 03:55:41 +00:00
|
|
|
# Loop for each link
|
|
|
|
|
for ($k = 0; isset( $a[$k] ); $k++) {
|
|
|
|
|
$line = $a[$k];
|
2004-06-02 22:54:01 +00:00
|
|
|
if ( $useLinkPrefixExtension ) {
|
2004-11-22 10:05:11 +00:00
|
|
|
wfProfileIn( $fname.'-prefixhandling' );
|
2004-06-02 22:54:01 +00:00
|
|
|
if ( preg_match( $e2, $s, $m ) ) {
|
|
|
|
|
$prefix = $m[2];
|
|
|
|
|
$s = $m[1];
|
|
|
|
|
} else {
|
|
|
|
|
$prefix='';
|
|
|
|
|
}
|
|
|
|
|
# first link
|
|
|
|
|
if($first_prefix) {
|
|
|
|
|
$prefix = $first_prefix;
|
|
|
|
|
$first_prefix = false;
|
|
|
|
|
}
|
2004-11-22 10:05:11 +00:00
|
|
|
wfProfileOut( $fname.'-prefixhandling' );
|
2004-06-02 22:39:06 +00:00
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-10-05 03:55:41 +00:00
|
|
|
$might_be_img = false;
|
|
|
|
|
|
2004-05-26 16:29:04 +00:00
|
|
|
if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
|
|
|
|
|
$text = $m[2];
|
2005-04-12 06:07:23 +00:00
|
|
|
# If we get a ] at the beginning of $m[3] that means we have a link that's something like:
|
|
|
|
|
# [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
|
|
|
|
|
# the real problem is with the $e1 regex
|
|
|
|
|
# See bug 1300.
|
2005-04-19 17:19:52 +00:00
|
|
|
if (preg_match( "/^\](.*)/s", $m[3], $n ) ) {
|
2005-04-12 06:07:23 +00:00
|
|
|
$text .= ']'; # so that replaceExternalLinks($text) works later
|
|
|
|
|
$m[3] = $n[1];
|
|
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
# fix up urlencoded title texts
|
2004-06-08 18:11:28 +00:00
|
|
|
if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
|
2004-05-26 16:29:04 +00:00
|
|
|
$trail = $m[3];
|
2004-10-05 03:55:41 +00:00
|
|
|
} elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
|
|
|
|
|
$might_be_img = true;
|
|
|
|
|
$text = $m[2];
|
|
|
|
|
if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
|
|
|
|
|
$trail = "";
|
2004-10-05 00:21:52 +00:00
|
|
|
} else { # Invalid form; output directly
|
|
|
|
|
$s .= $prefix . '[[' . $line ;
|
|
|
|
|
continue;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
|
2004-09-24 18:29:01 +00:00
|
|
|
# Don't allow internal links to pages containing
|
|
|
|
|
# PROTO: where PROTO is a valid URL protocol; these
|
|
|
|
|
# should be external links.
|
2005-01-08 03:03:10 +00:00
|
|
|
if (preg_match('/^(\b(?:'.URL_PROTOCOLS.'):)/', $m[1])) {
|
2004-09-24 18:29:01 +00:00
|
|
|
$s .= $prefix . '[[' . $line ;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-25 20:35:38 +00:00
|
|
|
# Make subpage if necessary
|
2004-11-28 03:29:50 +00:00
|
|
|
if( $useSubpages ) {
|
|
|
|
|
$link = $this->maybeDoSubpageLink( $m[1], $text );
|
|
|
|
|
} else {
|
|
|
|
|
$link = $m[1];
|
|
|
|
|
}
|
2004-09-25 20:35:38 +00:00
|
|
|
|
|
|
|
|
$noforce = (substr($m[1], 0, 1) != ':');
|
|
|
|
|
if (!$noforce) {
|
|
|
|
|
# Strip off leading ':'
|
|
|
|
|
$link = substr($link, 1);
|
|
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
|
2004-11-23 07:41:07 +00:00
|
|
|
$nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
|
2004-05-26 16:29:04 +00:00
|
|
|
if( !$nt ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$s .= $prefix . '[[' . $line;
|
2004-05-26 16:29:04 +00:00
|
|
|
continue;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-10-04 03:47:39 +00:00
|
|
|
|
2004-10-19 18:02:44 +00:00
|
|
|
#check other language variants of the link
|
|
|
|
|
#if the article does not exist
|
2004-11-22 10:05:11 +00:00
|
|
|
if( $checkVariantLink
|
|
|
|
|
&& $nt->getArticleID() == 0 ) {
|
2004-10-21 02:47:51 +00:00
|
|
|
$wgContLang->findVariantLink($link, $nt);
|
2004-10-04 03:47:39 +00:00
|
|
|
}
|
2004-10-21 02:47:51 +00:00
|
|
|
|
2004-05-26 16:29:04 +00:00
|
|
|
$ns = $nt->getNamespace();
|
|
|
|
|
$iw = $nt->getInterWiki();
|
2004-08-16 20:01:21 +00:00
|
|
|
|
2004-10-05 03:55:41 +00:00
|
|
|
if ($might_be_img) { # if this is actually an invalid link
|
|
|
|
|
if ($ns == NS_IMAGE && $noforce) { #but might be an image
|
|
|
|
|
$found = false;
|
|
|
|
|
while (isset ($a[$k+1]) ) {
|
|
|
|
|
#look at the next 'line' to see if we can close it there
|
|
|
|
|
$next_line = array_shift(array_splice( $a, $k + 1, 1) );
|
|
|
|
|
if( preg_match("/^(.*?]].*?)]](.*)$/sD", $next_line, $m) ) {
|
|
|
|
|
# the first ]] closes the inner link, the second the image
|
|
|
|
|
$found = true;
|
|
|
|
|
$text .= '[[' . $m[1];
|
|
|
|
|
$trail = $m[2];
|
|
|
|
|
break;
|
|
|
|
|
} elseif( preg_match("/^.*?]].*$/sD", $next_line, $m) ) {
|
|
|
|
|
#if there's exactly one ]] that's fine, we'll keep looking
|
|
|
|
|
$text .= '[[' . $m[0];
|
|
|
|
|
} else {
|
|
|
|
|
#if $next_line is invalid too, we need look no further
|
|
|
|
|
$text .= '[[' . $next_line;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !$found ) {
|
|
|
|
|
# we couldn't find the end of this imageLink, so output it raw
|
2004-10-06 19:22:42 +00:00
|
|
|
#but don't ignore what might be perfectly normal links in the text we've examined
|
|
|
|
|
$text = $this->replaceInternalLinks($text);
|
2004-10-05 03:55:41 +00:00
|
|
|
$s .= $prefix . '[[' . $link . '|' . $text;
|
|
|
|
|
# note: no $trail, because without an end, there *is* no trail
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} else { #it's not an image, so output it raw
|
|
|
|
|
$s .= $prefix . '[[' . $link . '|' . $text;
|
|
|
|
|
# note: no $trail, because without an end, there *is* no trail
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wasblank = ( '' == $text );
|
|
|
|
|
if( $wasblank ) $text = $link;
|
|
|
|
|
|
|
|
|
|
|
2004-08-16 20:01:21 +00:00
|
|
|
# Link not escaped by : , create the various objects
|
2004-05-26 16:29:04 +00:00
|
|
|
if( $noforce ) {
|
2004-08-16 20:01:21 +00:00
|
|
|
|
|
|
|
|
# Interwikis
|
2004-09-24 13:14:52 +00:00
|
|
|
if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgContLang->getLanguageName( $iw ) ) {
|
2004-07-29 04:48:42 +00:00
|
|
|
array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
|
2005-04-13 10:14:01 +00:00
|
|
|
$s = rtrim($s . "\n");
|
|
|
|
|
$s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
|
2004-05-26 16:29:04 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
|
2004-08-07 03:50:46 +00:00
|
|
|
if ( $ns == NS_IMAGE ) {
|
2004-11-23 07:41:07 +00:00
|
|
|
wfProfileIn( "$fname-image" );
|
2005-04-12 04:03:21 +00:00
|
|
|
if ( !wfIsBadImage( $nt->getDBkey() ) ) {
|
|
|
|
|
# recursively parse links inside the image caption
|
|
|
|
|
# actually, this will parse them in any other parameters, too,
|
|
|
|
|
# but it might be hard to fix that, and it doesn't matter ATM
|
|
|
|
|
$text = $this->replaceExternalLinks($text);
|
|
|
|
|
$text = $this->replaceInternalLinks($text);
|
|
|
|
|
|
|
|
|
|
# cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
|
2005-04-27 07:48:14 +00:00
|
|
|
$s .= $prefix . str_replace('http://', 'http-noparse://', $this->makeImage( $nt, $text ) ) . $trail;
|
2005-04-12 04:03:21 +00:00
|
|
|
$wgLinkCache->addImageLinkObj( $nt );
|
|
|
|
|
|
|
|
|
|
wfProfileOut( "$fname-image" );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-11-23 07:41:07 +00:00
|
|
|
wfProfileOut( "$fname-image" );
|
2005-04-12 04:03:21 +00:00
|
|
|
|
2004-05-27 01:10:01 +00:00
|
|
|
}
|
2004-08-16 20:01:21 +00:00
|
|
|
|
2004-08-07 03:50:46 +00:00
|
|
|
if ( $ns == NS_CATEGORY ) {
|
2004-11-23 07:41:07 +00:00
|
|
|
wfProfileIn( "$fname-category" );
|
2005-05-01 01:11:15 +00:00
|
|
|
$t = $wgContLang->convert($nt->getText());
|
2005-04-13 08:26:08 +00:00
|
|
|
$s = rtrim($s . "\n"); # bug 87
|
2004-05-26 16:29:04 +00:00
|
|
|
|
|
|
|
|
$wgLinkCache->suspend(); # Don't save in links/brokenlinks
|
2004-10-05 03:55:41 +00:00
|
|
|
$t = $sk->makeLinkObj( $nt, $t, '', '' , $prefix );
|
2004-05-26 16:29:04 +00:00
|
|
|
$wgLinkCache->resume();
|
|
|
|
|
|
2004-09-07 22:08:01 +00:00
|
|
|
if ( $wasblank ) {
|
|
|
|
|
if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
|
|
|
|
|
$sortkey = $this->mTitle->getText();
|
|
|
|
|
} else {
|
|
|
|
|
$sortkey = $this->mTitle->getPrefixedText();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$sortkey = $text;
|
|
|
|
|
}
|
2005-04-28 03:33:54 +00:00
|
|
|
$sortkey = $wgContLang->convertCategoryKey( $sortkey );
|
2004-05-26 16:29:04 +00:00
|
|
|
$wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
|
2004-11-25 22:02:30 +00:00
|
|
|
$this->mOutput->addCategoryLink( $t );
|
2005-04-09 00:31:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Strip the whitespace Category links produce, see bug 87
|
|
|
|
|
* @todo We might want to use trim($tmp, "\n") here.
|
|
|
|
|
*/
|
2005-04-13 08:26:08 +00:00
|
|
|
$s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
|
2004-11-23 07:41:07 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( "$fname-category" );
|
2004-05-26 16:29:04 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-10-08 04:27:07 +00:00
|
|
|
|
2004-11-23 07:41:07 +00:00
|
|
|
if( ( $nt->getPrefixedText() === $selflink ) &&
|
2004-10-13 04:30:19 +00:00
|
|
|
( $nt->getFragment() === '' ) ) {
|
2004-05-26 16:29:04 +00:00
|
|
|
# Self-links are handled specially; generally de-link and change to bold.
|
2004-06-08 18:11:28 +00:00
|
|
|
$s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
|
2004-05-26 16:29:04 +00:00
|
|
|
continue;
|
2004-04-20 21:08:24 +00:00
|
|
|
}
|
2004-03-20 12:10:25 +00:00
|
|
|
|
2004-10-05 03:55:41 +00:00
|
|
|
# Special and Media are pseudo-namespaces; no pages actually exist in them
|
2004-08-07 03:50:46 +00:00
|
|
|
if( $ns == NS_MEDIA ) {
|
2004-10-25 05:24:23 +00:00
|
|
|
$s .= $prefix . $sk->makeMediaLinkObj( $nt, $text, true ) . $trail;
|
2004-05-26 16:29:04 +00:00
|
|
|
$wgLinkCache->addImageLinkObj( $nt );
|
2004-06-05 02:22:16 +00:00
|
|
|
continue;
|
2004-08-07 03:50:46 +00:00
|
|
|
} elseif( $ns == NS_SPECIAL ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
|
2004-05-26 16:29:04 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2005-04-27 07:48:14 +00:00
|
|
|
if ( $nt->isAlwaysKnown() ) {
|
|
|
|
|
$s .= $sk->makeKnownLinkObj( $nt, $text, '', $trail, $prefix );
|
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* Add a link placeholder
|
|
|
|
|
* Later, this will be replaced by a real link, after the existence or
|
|
|
|
|
* non-existence of all the links is known
|
|
|
|
|
*/
|
|
|
|
|
$s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
|
|
|
|
|
}
|
2004-02-28 23:38:08 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 07:48:14 +00:00
|
|
|
/**
|
|
|
|
|
* Make a link placeholder. The text returned can be later resolved to a real link with
|
|
|
|
|
* replaceLinkHolders(). This is done for two reasons: firstly to avoid further
|
|
|
|
|
* parsing of interwiki links, and secondly to allow all extistence checks and
|
|
|
|
|
* article length checks (for stub links) to be bundled into a single query.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
function makeLinkHolder( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
|
|
|
|
|
if ( ! is_object($nt) ) {
|
|
|
|
|
# Fail gracefully
|
|
|
|
|
$retVal = "<!-- ERROR -->{$prefix}{$text}{$trail}";
|
|
|
|
|
} else {
|
|
|
|
|
# Separate the link trail from the rest of the link
|
|
|
|
|
list( $inside, $trail ) = Linker::splitTrail( $trail );
|
|
|
|
|
|
|
|
|
|
if ( $nt->isExternal() ) {
|
|
|
|
|
$iwRecord = array( $nt->getPrefixedDBkey(), $prefix.$text.$inside );
|
|
|
|
|
$nr = array_push($this->mInterwikiLinkHolders, $iwRecord);
|
|
|
|
|
$retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
|
|
|
|
|
} else {
|
|
|
|
|
$nr = array_push( $this->mLinkHolders['namespaces'], $nt->getNamespace() );
|
|
|
|
|
$this->mLinkHolders['dbkeys'][] = $nt->getDBkey();
|
|
|
|
|
$this->mLinkHolders['queries'][] = $query;
|
|
|
|
|
$this->mLinkHolders['texts'][] = $prefix.$text.$inside;
|
|
|
|
|
$this->mLinkHolders['titles'][] =& $nt;
|
|
|
|
|
|
|
|
|
|
$retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-28 03:29:50 +00:00
|
|
|
/**
|
|
|
|
|
* Return true if subpage links should be expanded on this page.
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function areSubpagesAllowed() {
|
|
|
|
|
# Some namespaces don't allow subpages
|
|
|
|
|
global $wgNamespacesWithSubpages;
|
|
|
|
|
return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-25 20:13:14 +00:00
|
|
|
/**
|
|
|
|
|
* Handle link to subpage if necessary
|
2004-10-15 17:39:10 +00:00
|
|
|
* @param string $target the source of the link
|
|
|
|
|
* @param string &$text the link text, modified as necessary
|
2004-09-25 20:13:14 +00:00
|
|
|
* @return string the full name of the link
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-09-25 20:35:38 +00:00
|
|
|
function maybeDoSubpageLink($target, &$text) {
|
2004-09-25 20:13:14 +00:00
|
|
|
# Valid link forms:
|
|
|
|
|
# Foobar -- normal
|
|
|
|
|
# :Foobar -- override special treatment of prefix (images, language links)
|
|
|
|
|
# /Foobar -- convert to CurrentPage/Foobar
|
|
|
|
|
# /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
|
2004-12-07 01:23:32 +00:00
|
|
|
# ../ -- convert to CurrentPage, from CurrentPage/CurrentSubPage
|
|
|
|
|
# ../Foobar -- convert to CurrentPage/Foobar, from CurrentPage/CurrentSubPage
|
2004-09-25 20:13:14 +00:00
|
|
|
|
2004-09-27 04:44:00 +00:00
|
|
|
$fname = 'Parser::maybeDoSubpageLink';
|
|
|
|
|
wfProfileIn( $fname );
|
2004-12-07 01:23:32 +00:00
|
|
|
$ret = $target; # default return value is no change
|
|
|
|
|
|
|
|
|
|
# Some namespaces don't allow subpages,
|
|
|
|
|
# so only perform processing if subpages are allowed
|
|
|
|
|
if( $this->areSubpagesAllowed() ) {
|
|
|
|
|
# Look at the first character
|
|
|
|
|
if( $target != '' && $target{0} == '/' ) {
|
|
|
|
|
# / at end means we don't want the slash to be shown
|
|
|
|
|
if( substr( $target, -1, 1 ) == '/' ) {
|
|
|
|
|
$target = substr( $target, 1, -1 );
|
|
|
|
|
$noslash = $target;
|
|
|
|
|
} else {
|
|
|
|
|
$noslash = substr( $target, 1 );
|
|
|
|
|
}
|
2004-09-25 20:13:14 +00:00
|
|
|
|
|
|
|
|
$ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
|
|
|
|
|
if( '' === $text ) {
|
|
|
|
|
$text = $target;
|
|
|
|
|
} # this might be changed for ugliness reasons
|
|
|
|
|
} else {
|
2004-12-07 01:23:32 +00:00
|
|
|
# check for .. subpage backlinks
|
|
|
|
|
$dotdotcount = 0;
|
|
|
|
|
$nodotdot = $target;
|
|
|
|
|
while( strncmp( $nodotdot, "../", 3 ) == 0 ) {
|
|
|
|
|
++$dotdotcount;
|
|
|
|
|
$nodotdot = substr( $nodotdot, 3 );
|
|
|
|
|
}
|
|
|
|
|
if($dotdotcount > 0) {
|
|
|
|
|
$exploded = explode( '/', $this->mTitle->GetPrefixedText() );
|
|
|
|
|
if( count( $exploded ) > $dotdotcount ) { # not allowed to go below top level page
|
|
|
|
|
$ret = implode( '/', array_slice( $exploded, 0, -$dotdotcount ) );
|
|
|
|
|
# / at the end means don't show full path
|
|
|
|
|
if( substr( $nodotdot, -1, 1 ) == '/' ) {
|
|
|
|
|
$nodotdot = substr( $nodotdot, 0, -1 );
|
|
|
|
|
if( '' === $text ) {
|
|
|
|
|
$text = $nodotdot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$nodotdot = trim( $nodotdot );
|
|
|
|
|
if( $nodotdot != '' ) {
|
|
|
|
|
$ret .= '/' . $nodotdot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-09-25 20:13:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-27 04:44:00 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-09-25 20:13:14 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**#@+
|
|
|
|
|
* Used by doBlockLevels()
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
/* private */ function closeParagraph() {
|
2004-06-08 18:11:28 +00:00
|
|
|
$result = '';
|
2004-04-04 01:45:02 +00:00
|
|
|
if ( '' != $this->mLastSection ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$result = '</' . $this->mLastSection . ">\n";
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-15 14:25:34 +00:00
|
|
|
$this->mInPre = false;
|
2004-06-08 18:11:28 +00:00
|
|
|
$this->mLastSection = '';
|
2004-04-09 13:40:50 +00:00
|
|
|
return $result;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
# getCommon() returns the length of the longest common substring
|
|
|
|
|
# of both arguments, starting at the beginning of both.
|
|
|
|
|
#
|
2004-06-12 06:15:09 +00:00
|
|
|
/* private */ function getCommon( $st1, $st2 ) {
|
2004-02-26 13:37:26 +00:00
|
|
|
$fl = strlen( $st1 );
|
|
|
|
|
$shorter = strlen( $st2 );
|
|
|
|
|
if ( $fl < $shorter ) { $shorter = $fl; }
|
|
|
|
|
|
|
|
|
|
for ( $i = 0; $i < $shorter; ++$i ) {
|
|
|
|
|
if ( $st1{$i} != $st2{$i} ) { break; }
|
|
|
|
|
}
|
|
|
|
|
return $i;
|
|
|
|
|
}
|
|
|
|
|
# These next three functions open, continue, and close the list
|
|
|
|
|
# element appropriate to the prefix character passed into them.
|
|
|
|
|
#
|
2004-08-16 15:23:46 +00:00
|
|
|
/* private */ function openList( $char ) {
|
2004-02-26 13:37:26 +00:00
|
|
|
$result = $this->closeParagraph();
|
|
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '*' == $char ) { $result .= '<ul><li>'; }
|
|
|
|
|
else if ( '#' == $char ) { $result .= '<ol><li>'; }
|
|
|
|
|
else if ( ':' == $char ) { $result .= '<dl><dd>'; }
|
|
|
|
|
else if ( ';' == $char ) {
|
|
|
|
|
$result .= '<dl><dt>';
|
2004-02-26 13:37:26 +00:00
|
|
|
$this->mDTopen = true;
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
else { $result = '<!-- ERR 1 -->'; }
|
2004-02-26 13:37:26 +00:00
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-12 06:15:09 +00:00
|
|
|
/* private */ function nextItem( $char ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
|
|
|
|
|
else if ( ':' == $char || ';' == $char ) {
|
2004-08-16 15:29:17 +00:00
|
|
|
$close = '</dd>';
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( $this->mDTopen ) { $close = '</dt>'; }
|
|
|
|
|
if ( ';' == $char ) {
|
2004-02-26 13:37:26 +00:00
|
|
|
$this->mDTopen = true;
|
2004-06-08 18:11:28 +00:00
|
|
|
return $close . '<dt>';
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
|
|
|
|
$this->mDTopen = false;
|
2004-06-08 18:11:28 +00:00
|
|
|
return $close . '<dd>';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
return '<!-- ERR 2 -->';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-08-16 20:01:21 +00:00
|
|
|
/* private */ function closeList( $char ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '*' == $char ) { $text = '</li></ul>'; }
|
|
|
|
|
else if ( '#' == $char ) { $text = '</li></ol>'; }
|
|
|
|
|
else if ( ':' == $char ) {
|
2004-02-26 13:37:26 +00:00
|
|
|
if ( $this->mDTopen ) {
|
|
|
|
|
$this->mDTopen = false;
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = '</dt></dl>';
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = '</dd></dl>';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
else { return '<!-- ERR 3 -->'; }
|
2004-02-26 13:37:26 +00:00
|
|
|
return $text."\n";
|
|
|
|
|
}
|
2004-09-21 05:49:12 +00:00
|
|
|
/**#@-*/
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Make lists from lines starting with ':', '*', '#', etc.
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
* @return string the lists rendered as HTML
|
|
|
|
|
*/
|
|
|
|
|
function doBlockLevels( $text, $linestart ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::doBlockLevels';
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
# Parsing through the text line by line. The main thing
|
|
|
|
|
# happening here is handling of block-level elements p, pre,
|
|
|
|
|
# and making lists from lines starting with * # : etc.
|
|
|
|
|
#
|
2004-04-29 06:16:21 +00:00
|
|
|
$textLines = explode( "\n", $text );
|
2004-04-11 16:46:06 +00:00
|
|
|
|
2005-04-21 06:30:48 +00:00
|
|
|
$lastPrefix = $output = '';
|
2004-04-11 16:46:06 +00:00
|
|
|
$this->mDTopen = $inBlockElem = false;
|
2004-04-29 06:16:21 +00:00
|
|
|
$prefixLength = 0;
|
|
|
|
|
$paragraphStack = false;
|
|
|
|
|
|
|
|
|
|
if ( !$linestart ) {
|
|
|
|
|
$output .= array_shift( $textLines );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $textLines as $oLine ) {
|
|
|
|
|
$lastPrefixLength = strlen( $lastPrefix );
|
2004-08-16 15:29:17 +00:00
|
|
|
$preCloseMatch = preg_match('/<\\/pre/i', $oLine );
|
|
|
|
|
$preOpenMatch = preg_match('/<pre/i', $oLine );
|
2004-07-14 18:46:02 +00:00
|
|
|
if ( !$this->mInPre ) {
|
2004-04-29 06:16:21 +00:00
|
|
|
# Multiple prefixes may abut each other for nested lists.
|
2004-06-08 18:11:28 +00:00
|
|
|
$prefixLength = strspn( $oLine, '*#:;' );
|
2004-04-29 06:16:21 +00:00
|
|
|
$pref = substr( $oLine, 0, $prefixLength );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-04-29 06:16:21 +00:00
|
|
|
# eh?
|
2004-06-08 18:11:28 +00:00
|
|
|
$pref2 = str_replace( ';', ':', $pref );
|
2004-04-29 06:16:21 +00:00
|
|
|
$t = substr( $oLine, $prefixLength );
|
2004-07-14 18:46:02 +00:00
|
|
|
$this->mInPre = !empty($preOpenMatch);
|
2004-04-16 11:21:51 +00:00
|
|
|
} else {
|
2004-04-29 06:16:21 +00:00
|
|
|
# Don't interpret any other prefixes in preformatted text
|
|
|
|
|
$prefixLength = 0;
|
2004-04-16 11:21:51 +00:00
|
|
|
$pref = $pref2 = '';
|
2004-04-29 06:16:21 +00:00
|
|
|
$t = $oLine;
|
2004-04-16 11:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-29 06:16:21 +00:00
|
|
|
# List generation
|
|
|
|
|
if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
|
|
|
|
|
# Same as the last item, so no need to deal with nesting or opening stuff
|
|
|
|
|
$output .= $this->nextItem( substr( $pref, -1 ) );
|
|
|
|
|
$paragraphStack = false;
|
2004-04-16 11:21:51 +00:00
|
|
|
|
2004-08-16 15:29:17 +00:00
|
|
|
if ( substr( $pref, -1 ) == ';') {
|
2004-04-29 06:16:21 +00:00
|
|
|
# The one nasty exception: definition lists work like this:
|
|
|
|
|
# ; title : definition text
|
|
|
|
|
# So we check for : in the remainder text to split up the
|
|
|
|
|
# title and definition, without b0rking links.
|
2005-04-21 06:30:48 +00:00
|
|
|
$term = $t2 = '';
|
2004-09-27 21:01:39 +00:00
|
|
|
if ($this->findColonNoLinks($t, $term, $t2) !== false) {
|
|
|
|
|
$t = $t2;
|
2004-06-08 18:11:28 +00:00
|
|
|
$output .= $term . $this->nextItem( ':' );
|
2004-04-16 11:21:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
} elseif( $prefixLength || $lastPrefixLength ) {
|
|
|
|
|
# Either open or close a level...
|
|
|
|
|
$commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
|
|
|
|
|
$paragraphStack = false;
|
|
|
|
|
|
|
|
|
|
while( $commonPrefixLength < $lastPrefixLength ) {
|
|
|
|
|
$output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
|
|
|
|
|
--$lastPrefixLength;
|
2004-04-16 11:21:51 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
|
|
|
|
|
$output .= $this->nextItem( $pref{$commonPrefixLength-1} );
|
2004-04-16 11:21:51 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
while ( $prefixLength > $commonPrefixLength ) {
|
|
|
|
|
$char = substr( $pref, $commonPrefixLength, 1 );
|
|
|
|
|
$output .= $this->openList( $char );
|
2004-04-16 11:21:51 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( ';' == $char ) {
|
2004-04-29 06:16:21 +00:00
|
|
|
# FIXME: This is dupe of code above
|
2004-09-27 21:01:39 +00:00
|
|
|
if ($this->findColonNoLinks($t, $term, $t2) !== false) {
|
|
|
|
|
$t = $t2;
|
2004-08-22 17:24:50 +00:00
|
|
|
$output .= $term . $this->nextItem( ':' );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
++$commonPrefixLength;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
$lastPrefix = $pref2;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
if( 0 == $prefixLength ) {
|
2004-11-26 10:49:46 +00:00
|
|
|
wfProfileIn( "$fname-paragraph" );
|
2004-04-29 06:16:21 +00:00
|
|
|
# No prefix (not in list)--go to paragraph mode
|
2004-04-09 07:31:35 +00:00
|
|
|
$uniq_prefix = UNIQ_PREFIX;
|
2004-04-09 13:40:50 +00:00
|
|
|
// XXX: use a stack for nestable elements like span, table and div
|
2004-11-26 10:49:46 +00:00
|
|
|
$openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
|
2004-04-12 23:59:37 +00:00
|
|
|
$closematch = preg_match(
|
2004-06-08 18:11:28 +00:00
|
|
|
'/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
|
2004-11-26 10:49:46 +00:00
|
|
|
'<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/iS', $t );
|
2004-04-09 13:40:50 +00:00
|
|
|
if ( $openmatch or $closematch ) {
|
2004-04-29 06:16:21 +00:00
|
|
|
$paragraphStack = false;
|
|
|
|
|
$output .= $this->closeParagraph();
|
2004-04-15 14:25:34 +00:00
|
|
|
if($preOpenMatch and !$preCloseMatch) {
|
2004-07-12 19:49:20 +00:00
|
|
|
$this->mInPre = true;
|
2004-04-15 14:25:34 +00:00
|
|
|
}
|
2004-08-16 15:23:46 +00:00
|
|
|
if ( $closematch ) {
|
2004-04-09 13:40:50 +00:00
|
|
|
$inBlockElem = false;
|
2004-04-09 14:55:12 +00:00
|
|
|
} else {
|
|
|
|
|
$inBlockElem = true;
|
2004-04-09 13:40:50 +00:00
|
|
|
}
|
2004-06-02 12:29:15 +00:00
|
|
|
} else if ( !$inBlockElem && !$this->mInPre ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
|
2004-04-10 15:11:14 +00:00
|
|
|
// pre
|
2004-04-10 03:09:52 +00:00
|
|
|
if ($this->mLastSection != 'pre') {
|
2004-04-29 06:16:21 +00:00
|
|
|
$paragraphStack = false;
|
|
|
|
|
$output .= $this->closeParagraph().'<pre>';
|
2004-04-10 15:11:14 +00:00
|
|
|
$this->mLastSection = 'pre';
|
2004-04-10 03:09:52 +00:00
|
|
|
}
|
2004-09-12 13:07:52 +00:00
|
|
|
$t = substr( $t, 1 );
|
2004-04-12 23:59:37 +00:00
|
|
|
} else {
|
2004-04-10 15:11:14 +00:00
|
|
|
// paragraph
|
|
|
|
|
if ( '' == trim($t) ) {
|
2004-04-29 06:16:21 +00:00
|
|
|
if ( $paragraphStack ) {
|
2004-05-31 00:58:57 +00:00
|
|
|
$output .= $paragraphStack.'<br />';
|
2004-04-29 06:16:21 +00:00
|
|
|
$paragraphStack = false;
|
2004-04-10 15:11:14 +00:00
|
|
|
$this->mLastSection = 'p';
|
|
|
|
|
} else {
|
|
|
|
|
if ($this->mLastSection != 'p' ) {
|
2004-04-29 06:16:21 +00:00
|
|
|
$output .= $this->closeParagraph();
|
2004-04-10 15:11:14 +00:00
|
|
|
$this->mLastSection = '';
|
2004-06-08 18:11:28 +00:00
|
|
|
$paragraphStack = '<p>';
|
2004-04-10 15:11:14 +00:00
|
|
|
} else {
|
2004-04-29 06:16:21 +00:00
|
|
|
$paragraphStack = '</p><p>';
|
2004-04-10 15:11:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2004-04-29 06:16:21 +00:00
|
|
|
if ( $paragraphStack ) {
|
|
|
|
|
$output .= $paragraphStack;
|
|
|
|
|
$paragraphStack = false;
|
2004-04-10 15:11:14 +00:00
|
|
|
$this->mLastSection = 'p';
|
|
|
|
|
} else if ($this->mLastSection != 'p') {
|
2004-04-29 06:16:21 +00:00
|
|
|
$output .= $this->closeParagraph().'<p>';
|
2004-04-10 15:11:14 +00:00
|
|
|
$this->mLastSection = 'p';
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-09 13:40:50 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
}
|
2004-11-26 10:49:46 +00:00
|
|
|
wfProfileOut( "$fname-paragraph" );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
if ($paragraphStack === false) {
|
|
|
|
|
$output .= $t."\n";
|
2004-04-10 15:11:14 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-29 06:16:21 +00:00
|
|
|
while ( $prefixLength ) {
|
|
|
|
|
$output .= $this->closeList( $pref2{$prefixLength-1} );
|
|
|
|
|
--$prefixLength;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '' != $this->mLastSection ) {
|
|
|
|
|
$output .= '</' . $this->mLastSection . '>';
|
|
|
|
|
$this->mLastSection = '';
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-04-29 06:16:21 +00:00
|
|
|
return $output;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-27 21:01:39 +00:00
|
|
|
/**
|
|
|
|
|
* Split up a string on ':', ignoring any occurences inside
|
|
|
|
|
* <a>..</a> or <span>...</span>
|
2004-10-15 17:39:10 +00:00
|
|
|
* @param string $str the string to split
|
|
|
|
|
* @param string &$before set to everything before the ':'
|
|
|
|
|
* @param string &$after set to everything after the ':'
|
2004-09-27 21:01:39 +00:00
|
|
|
* return string the position of the ':', or false if none found
|
|
|
|
|
*/
|
|
|
|
|
function findColonNoLinks($str, &$before, &$after) {
|
|
|
|
|
# I wonder if we should make this count all tags, not just <a>
|
|
|
|
|
# and <span>. That would prevent us from matching a ':' that
|
|
|
|
|
# comes in the middle of italics other such formatting....
|
|
|
|
|
# -- Wil
|
|
|
|
|
$fname = 'Parser::findColonNoLinks';
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
$pos = 0;
|
|
|
|
|
do {
|
|
|
|
|
$colon = strpos($str, ':', $pos);
|
|
|
|
|
|
|
|
|
|
if ($colon !== false) {
|
|
|
|
|
$before = substr($str, 0, $colon);
|
|
|
|
|
$after = substr($str, $colon + 1);
|
|
|
|
|
|
|
|
|
|
# Skip any ':' within <a> or <span> pairs
|
|
|
|
|
$a = substr_count($before, '<a');
|
|
|
|
|
$s = substr_count($before, '<span');
|
|
|
|
|
$ca = substr_count($before, '</a>');
|
|
|
|
|
$cs = substr_count($before, '</span>');
|
|
|
|
|
|
|
|
|
|
if ($a <= $ca and $s <= $cs) {
|
|
|
|
|
# Tags are balanced before ':'; ok
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$pos = $colon + 1;
|
|
|
|
|
}
|
|
|
|
|
} while ($colon !== false);
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $colon;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Return value of a magic variable (like PAGENAME)
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-03-20 15:03:26 +00:00
|
|
|
function getVariableValue( $index ) {
|
2005-03-26 18:24:32 +00:00
|
|
|
global $wgContLang, $wgSitename, $wgServer, $wgArticle;
|
2004-11-21 14:07:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Some of these require message or data lookups and can be
|
|
|
|
|
* expensive to check many times.
|
|
|
|
|
*/
|
|
|
|
|
static $varCache = array();
|
|
|
|
|
if( isset( $varCache[$index] ) ) return $varCache[$index];
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
switch ( $index ) {
|
|
|
|
|
case MAG_CURRENTMONTH:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_CURRENTMONTHNAME:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->getMonthName( date('n') );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_CURRENTMONTHNAMEGEN:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->getMonthNameGen( date('n') );
|
2005-03-24 03:00:06 +00:00
|
|
|
case MAG_CURRENTMONTHABBREV:
|
|
|
|
|
return $varCache[$index] = $wgContLang->getMonthAbbreviation( date('n') );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_CURRENTDAY:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->formatNum( date('j') );
|
2004-04-24 20:19:05 +00:00
|
|
|
case MAG_PAGENAME:
|
|
|
|
|
return $this->mTitle->getText();
|
2004-08-12 13:32:04 +00:00
|
|
|
case MAG_PAGENAMEE:
|
|
|
|
|
return $this->mTitle->getPartialURL();
|
2005-03-26 18:24:32 +00:00
|
|
|
case MAG_REVISIONID:
|
|
|
|
|
return $wgArticle->getRevIdFetched();
|
2004-04-24 20:19:05 +00:00
|
|
|
case MAG_NAMESPACE:
|
2004-04-26 10:20:54 +00:00
|
|
|
# return Namespace::getCanonicalName($this->mTitle->getNamespace());
|
2004-09-24 13:14:52 +00:00
|
|
|
return $wgContLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
|
2004-04-24 21:20:32 +00:00
|
|
|
case MAG_CURRENTDAYNAME:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->getWeekdayName( date('w')+1 );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_CURRENTYEAR:
|
2005-04-13 08:26:08 +00:00
|
|
|
return $varCache[$index] = $wgContLang->formatNum( date( 'Y' ), true );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_CURRENTTIME:
|
2004-11-21 14:07:24 +00:00
|
|
|
return $varCache[$index] = $wgContLang->time( wfTimestampNow(), false );
|
2005-01-11 01:30:13 +00:00
|
|
|
case MAG_CURRENTWEEK:
|
|
|
|
|
return $varCache[$index] = $wgContLang->formatNum( date('W') );
|
|
|
|
|
case MAG_CURRENTDOW:
|
|
|
|
|
return $varCache[$index] = $wgContLang->formatNum( date('w') );
|
2004-03-20 15:03:26 +00:00
|
|
|
case MAG_NUMBEROFARTICLES:
|
2005-04-09 01:35:42 +00:00
|
|
|
return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
|
2004-04-05 10:38:40 +00:00
|
|
|
case MAG_SITENAME:
|
|
|
|
|
return $wgSitename;
|
|
|
|
|
case MAG_SERVER:
|
|
|
|
|
return $wgServer;
|
2004-03-20 15:03:26 +00:00
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* initialise the magic variables (like CURRENTMONTHNAME)
|
|
|
|
|
*
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function initialiseVariables() {
|
2004-09-27 04:44:00 +00:00
|
|
|
$fname = 'Parser::initialiseVariables';
|
2004-09-26 17:59:08 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-03-20 15:03:26 +00:00
|
|
|
global $wgVariableIDs;
|
|
|
|
|
$this->mVariables = array();
|
|
|
|
|
foreach ( $wgVariableIDs as $id ) {
|
|
|
|
|
$mw =& MagicWord::get( $id );
|
2005-01-18 11:52:06 +00:00
|
|
|
$mw->addToArray( $this->mVariables, $id );
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-09-26 17:59:08 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Replace magic variables, templates, and template arguments
|
|
|
|
|
* with the appropriate text. Templates are substituted recursively,
|
|
|
|
|
* taking care to avoid infinite loops.
|
|
|
|
|
*
|
|
|
|
|
* Note that the substitution depends on value of $mOutputType:
|
|
|
|
|
* OT_WIKI: only {{subst:}} templates
|
|
|
|
|
* OT_MSG: only magic variables
|
|
|
|
|
* OT_HTML: all templates and magic variables
|
|
|
|
|
*
|
|
|
|
|
* @param string $tex The text to transform
|
|
|
|
|
* @param array $args Key-value pairs representing template parameters to substitute
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function replaceVariables( $text, $args = array() ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
|
2004-08-16 15:23:46 +00:00
|
|
|
# Prevent too big inclusions
|
2004-11-21 14:07:24 +00:00
|
|
|
if( strlen( $text ) > MAX_INCLUDE_SIZE ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-07-24 00:37:24 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::replaceVariables';
|
2004-02-26 13:37:26 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
$titleChars = Title::legalChars();
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
# This function is called recursively. To keep track of arguments we need a stack:
|
|
|
|
|
array_push( $this->mArgStack, $args );
|
2004-04-09 15:29:33 +00:00
|
|
|
|
2004-09-21 23:30:46 +00:00
|
|
|
# Variable substitution
|
2004-10-23 07:10:37 +00:00
|
|
|
$text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
|
2004-09-21 23:30:46 +00:00
|
|
|
|
2004-09-22 00:15:54 +00:00
|
|
|
if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
|
2004-05-29 05:19:27 +00:00
|
|
|
# Argument substitution
|
2004-10-23 07:10:37 +00:00
|
|
|
$text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
|
2004-05-23 03:39:24 +00:00
|
|
|
}
|
2004-05-29 05:19:27 +00:00
|
|
|
# Template substitution
|
2004-09-25 05:16:38 +00:00
|
|
|
$regex = '/(\\n|{)?{{(['.$titleChars.']*)(\\|.*?|)}}/s';
|
2004-10-23 07:10:37 +00:00
|
|
|
$text = preg_replace_callback( $regex, array( &$this, 'braceSubstitution' ), $text );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
array_pop( $this->mArgStack );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-05-26 12:30:36 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-04-11 16:46:06 +00:00
|
|
|
return $text;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-09-21 23:30:46 +00:00
|
|
|
/**
|
|
|
|
|
* Replace magic variables
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function variableSubstitution( $matches ) {
|
2004-11-28 04:05:05 +00:00
|
|
|
$fname = 'parser::variableSubstitution';
|
2005-01-18 11:52:06 +00:00
|
|
|
$varname = $matches[1];
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-09-21 23:30:46 +00:00
|
|
|
if ( !$this->mVariables ) {
|
|
|
|
|
$this->initialiseVariables();
|
|
|
|
|
}
|
|
|
|
|
$skip = false;
|
|
|
|
|
if ( $this->mOutputType == OT_WIKI ) {
|
|
|
|
|
# Do only magic variables prefixed by SUBST
|
|
|
|
|
$mwSubst =& MagicWord::get( MAG_SUBST );
|
2005-01-18 11:52:06 +00:00
|
|
|
if (!$mwSubst->matchStartAndRemove( $varname ))
|
2004-09-21 23:30:46 +00:00
|
|
|
$skip = true;
|
|
|
|
|
# Note that if we don't substitute the variable below,
|
|
|
|
|
# we don't remove the {{subst:}} magic word, in case
|
|
|
|
|
# it is a template rather than a magic variable.
|
|
|
|
|
}
|
2005-01-18 11:52:06 +00:00
|
|
|
if ( !$skip && array_key_exists( $varname, $this->mVariables ) ) {
|
|
|
|
|
$id = $this->mVariables[$varname];
|
|
|
|
|
$text = $this->getVariableValue( $id );
|
2004-09-21 23:30:46 +00:00
|
|
|
$this->mOutput->mContainsOldMagic = true;
|
|
|
|
|
} else {
|
|
|
|
|
$text = $matches[0];
|
|
|
|
|
}
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-09-21 23:30:46 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-31 14:13:07 +00:00
|
|
|
# Split template arguments
|
|
|
|
|
function getTemplateArgs( $argsString ) {
|
|
|
|
|
if ( $argsString === '' ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$args = explode( '|', substr( $argsString, 1 ) );
|
|
|
|
|
|
|
|
|
|
# If any of the arguments contains a '[[' but no ']]', it needs to be
|
|
|
|
|
# merged with the next arg because the '|' character between belongs
|
|
|
|
|
# to the link syntax and not the template parameter syntax.
|
|
|
|
|
$argc = count($args);
|
2005-04-09 07:52:04 +00:00
|
|
|
|
2004-07-31 14:13:07 +00:00
|
|
|
for ( $i = 0; $i < $argc-1; $i++ ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
|
|
|
|
|
$args[$i] .= '|'.$args[$i+1];
|
2004-07-31 14:13:07 +00:00
|
|
|
array_splice($args, $i+1, 1);
|
|
|
|
|
$i--;
|
|
|
|
|
$argc--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Return the text of a template, after recursively
|
|
|
|
|
* replacing any variables or templates within the template.
|
|
|
|
|
*
|
|
|
|
|
* @param array $matches The parts of the template
|
|
|
|
|
* $matches[1]: the title, i.e. the part before the |
|
|
|
|
|
* $matches[2]: the parameters (including a leading |), if any
|
|
|
|
|
* @return string the text of the template
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function braceSubstitution( $matches ) {
|
2004-09-24 13:14:52 +00:00
|
|
|
global $wgLinkCache, $wgContLang;
|
2004-06-08 18:11:28 +00:00
|
|
|
$fname = 'Parser::braceSubstitution';
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
$found = false;
|
|
|
|
|
$nowiki = false;
|
2004-05-23 03:39:24 +00:00
|
|
|
$noparse = false;
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
$title = NULL;
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-09-25 05:16:38 +00:00
|
|
|
# Need to know if the template comes at the start of a line,
|
|
|
|
|
# to treat the beginning of the template like the beginning
|
|
|
|
|
# of a line for tables and block-level elements.
|
|
|
|
|
$linestart = $matches[1];
|
|
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
# $part1 is the bit before the first |, and must contain only title characters
|
|
|
|
|
# $args is a list of arguments, starting from index 0, not including $part1
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-09-25 05:16:38 +00:00
|
|
|
$part1 = $matches[2];
|
|
|
|
|
# If the third subpattern matched anything, it will start with |
|
2004-07-31 14:13:07 +00:00
|
|
|
|
2004-09-25 05:16:38 +00:00
|
|
|
$args = $this->getTemplateArgs($matches[3]);
|
2004-04-11 16:46:06 +00:00
|
|
|
$argc = count( $args );
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-09-25 05:16:38 +00:00
|
|
|
# Don't parse {{{}}} because that's only for template arguments
|
|
|
|
|
if ( $linestart === '{' ) {
|
2004-05-23 03:39:24 +00:00
|
|
|
$text = $matches[0];
|
|
|
|
|
$found = true;
|
|
|
|
|
$noparse = true;
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
|
|
|
|
|
# SUBST
|
2004-05-23 03:39:24 +00:00
|
|
|
if ( !$found ) {
|
|
|
|
|
$mwSubst =& MagicWord::get( MAG_SUBST );
|
2004-09-22 00:15:54 +00:00
|
|
|
if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
|
|
|
|
|
# One of two possibilities is true:
|
|
|
|
|
# 1) Found SUBST but not in the PST phase
|
|
|
|
|
# 2) Didn't find SUBST and in the PST phase
|
|
|
|
|
# In either case, return without further processing
|
2004-03-20 15:03:26 +00:00
|
|
|
$text = $matches[0];
|
|
|
|
|
$found = true;
|
2004-09-22 00:15:54 +00:00
|
|
|
$noparse = true;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
# MSG, MSGNW and INT
|
2004-03-20 15:03:26 +00:00
|
|
|
if ( !$found ) {
|
|
|
|
|
# Check for MSGNW:
|
|
|
|
|
$mwMsgnw =& MagicWord::get( MAG_MSGNW );
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
$nowiki = true;
|
|
|
|
|
} else {
|
|
|
|
|
# Remove obsolete MSG:
|
|
|
|
|
$mwMsg =& MagicWord::get( MAG_MSG );
|
2004-04-11 16:46:06 +00:00
|
|
|
$mwMsg->matchStartAndRemove( $part1 );
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Check if it is an internal message
|
|
|
|
|
$mwInt =& MagicWord::get( MAG_INT );
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $mwInt->matchStartAndRemove( $part1 ) ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . wfMsgReal( $part1, $args, true );
|
2004-04-11 16:46:06 +00:00
|
|
|
$found = true;
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
# NS
|
|
|
|
|
if ( !$found ) {
|
|
|
|
|
# Check for NS: (namespace expansion)
|
|
|
|
|
$mwNs = MagicWord::get( MAG_NS );
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $mwNs->matchStartAndRemove( $part1 ) ) {
|
|
|
|
|
if ( intval( $part1 ) ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
|
2004-04-05 10:38:40 +00:00
|
|
|
$found = true;
|
|
|
|
|
} else {
|
2004-04-11 16:46:06 +00:00
|
|
|
$index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
|
2004-04-05 10:38:40 +00:00
|
|
|
if ( !is_null( $index ) ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $wgContLang->getNsText( $index );
|
2004-04-05 10:38:40 +00:00
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
# LOCALURL and LOCALURLE
|
|
|
|
|
if ( !$found ) {
|
|
|
|
|
$mwLocal = MagicWord::get( MAG_LOCALURL );
|
|
|
|
|
$mwLocalE = MagicWord::get( MAG_LOCALURLE );
|
|
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
|
2004-04-05 10:38:40 +00:00
|
|
|
$func = 'getLocalURL';
|
2004-04-11 16:46:06 +00:00
|
|
|
} elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
|
2004-04-05 10:38:40 +00:00
|
|
|
$func = 'escapeLocalURL';
|
|
|
|
|
} else {
|
|
|
|
|
$func = '';
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
if ( $func !== '' ) {
|
2004-04-11 16:46:06 +00:00
|
|
|
$title = Title::newFromText( $part1 );
|
|
|
|
|
if ( !is_null( $title ) ) {
|
|
|
|
|
if ( $argc > 0 ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $title->$func( $args[0] );
|
2004-04-11 16:46:06 +00:00
|
|
|
} else {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $title->$func();
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
2004-04-11 16:46:06 +00:00
|
|
|
$found = true;
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
2004-04-11 16:46:06 +00:00
|
|
|
}
|
2004-04-05 10:38:40 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-08-27 14:55:41 +00:00
|
|
|
# GRAMMAR
|
|
|
|
|
if ( !$found && $argc == 1 ) {
|
|
|
|
|
$mwGrammar =& MagicWord::get( MAG_GRAMMAR );
|
|
|
|
|
if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $wgContLang->convertGrammar( $args[0], $part1 );
|
2004-08-27 14:55:41 +00:00
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-16 15:23:46 +00:00
|
|
|
# Template table test
|
2004-07-24 00:37:24 +00:00
|
|
|
|
|
|
|
|
# Did we encounter this template already? If yes, it is in the cache
|
|
|
|
|
# and we need to check for loops.
|
2004-09-24 02:38:20 +00:00
|
|
|
if ( !$found && isset( $this->mTemplates[$part1] ) ) {
|
2004-09-24 18:29:01 +00:00
|
|
|
$found = true;
|
|
|
|
|
|
2004-07-24 00:37:24 +00:00
|
|
|
# Infinite loop test
|
|
|
|
|
if ( isset( $this->mTemplatePath[$part1] ) ) {
|
|
|
|
|
$noparse = true;
|
|
|
|
|
$found = true;
|
2005-02-11 09:02:15 +00:00
|
|
|
$text = $linestart .
|
|
|
|
|
"\{\{$part1}}" .
|
|
|
|
|
'<!-- WARNING: template loop detected -->';
|
|
|
|
|
wfDebug( "$fname: template loop broken at '$part1'\n" );
|
|
|
|
|
} else {
|
|
|
|
|
# set $text to cached message.
|
|
|
|
|
$text = $linestart . $this->mTemplates[$part1];
|
2004-08-07 12:35:59 +00:00
|
|
|
}
|
2004-04-11 16:46:06 +00:00
|
|
|
}
|
2004-07-24 00:37:24 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# Load from database
|
2004-09-25 16:06:10 +00:00
|
|
|
$itcamefromthedatabase = false;
|
2005-02-11 09:02:15 +00:00
|
|
|
$lastPathLevel = $this->mTemplatePath;
|
2004-03-20 15:03:26 +00:00
|
|
|
if ( !$found ) {
|
2004-09-25 20:13:14 +00:00
|
|
|
$ns = NS_TEMPLATE;
|
2004-09-25 20:35:38 +00:00
|
|
|
$part1 = $this->maybeDoSubpageLink( $part1, $subpage='' );
|
2004-09-25 20:13:14 +00:00
|
|
|
if ($subpage !== '') {
|
|
|
|
|
$ns = $this->mTitle->getNamespace();
|
|
|
|
|
}
|
|
|
|
|
$title = Title::newFromText( $part1, $ns );
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( !is_null( $title ) && !$title->isExternal() ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
# Check for excessive inclusion
|
|
|
|
|
$dbk = $title->getPrefixedDBkey();
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $this->incrementIncludeCount( $dbk ) ) {
|
2004-07-24 00:37:24 +00:00
|
|
|
# This should never be reached.
|
2004-03-23 10:16:32 +00:00
|
|
|
$article = new Article( $title );
|
|
|
|
|
$articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
|
|
|
|
|
if ( $articleContent !== false ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
$found = true;
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . $articleContent;
|
2004-09-25 16:06:10 +00:00
|
|
|
$itcamefromthedatabase = true;
|
2004-04-12 23:59:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-03-20 15:03:26 +00:00
|
|
|
# If the title is valid but undisplayable, make a link to it
|
|
|
|
|
if ( $this->mOutputType == OT_HTML && !$found ) {
|
2004-09-25 05:16:38 +00:00
|
|
|
$text = $linestart . '[['.$title->getPrefixedText().']]';
|
2004-03-20 15:03:26 +00:00
|
|
|
$found = true;
|
|
|
|
|
}
|
2004-07-24 00:37:24 +00:00
|
|
|
|
2004-08-16 15:23:46 +00:00
|
|
|
# Template cache array insertion
|
2005-02-05 07:14:25 +00:00
|
|
|
if( $found ) {
|
|
|
|
|
$this->mTemplates[$part1] = $text;
|
|
|
|
|
}
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
# Recursive parsing, escaping and link table handling
|
|
|
|
|
# Only for HTML output
|
|
|
|
|
if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
|
|
|
|
|
$text = wfEscapeWikiText( $text );
|
2004-09-22 00:15:54 +00:00
|
|
|
} elseif ( ($this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI) && $found && !$noparse) {
|
2004-04-11 16:46:06 +00:00
|
|
|
# Clean up argument array
|
|
|
|
|
$assocArgs = array();
|
|
|
|
|
$index = 1;
|
|
|
|
|
foreach( $args as $arg ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$eqpos = strpos( $arg, '=' );
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( $eqpos === false ) {
|
|
|
|
|
$assocArgs[$index++] = $arg;
|
|
|
|
|
} else {
|
|
|
|
|
$name = trim( substr( $arg, 0, $eqpos ) );
|
|
|
|
|
$value = trim( substr( $arg, $eqpos+1 ) );
|
|
|
|
|
if ( $value === false ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$value = '';
|
2004-04-11 16:46:06 +00:00
|
|
|
}
|
|
|
|
|
if ( $name !== false ) {
|
|
|
|
|
$assocArgs[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-16 15:23:46 +00:00
|
|
|
# Add a new element to the templace recursion path
|
2004-07-24 00:37:24 +00:00
|
|
|
$this->mTemplatePath[$part1] = 1;
|
|
|
|
|
|
2005-04-20 22:55:49 +00:00
|
|
|
if( $this->mOutputType == OT_HTML ) {
|
|
|
|
|
$text = $this->strip( $text, $this->mStripState );
|
|
|
|
|
$text = Sanitizer::removeHTMLtags( $text );
|
|
|
|
|
}
|
2004-09-17 06:15:12 +00:00
|
|
|
$text = $this->replaceVariables( $text, $assocArgs );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-11 16:46:06 +00:00
|
|
|
# Resume the link cache and register the inclusion as a link
|
2004-09-22 17:46:13 +00:00
|
|
|
if ( $this->mOutputType == OT_HTML && !is_null( $title ) ) {
|
2004-04-11 16:46:06 +00:00
|
|
|
$wgLinkCache->addLinkObj( $title );
|
|
|
|
|
}
|
2004-08-27 14:55:41 +00:00
|
|
|
|
2004-09-25 16:06:10 +00:00
|
|
|
# If the template begins with a table or block-level
|
|
|
|
|
# element, it should be treated as beginning a new line.
|
|
|
|
|
if ($linestart !== '\n' && preg_match('/^({\\||:|;|#|\*)/', $text)) {
|
|
|
|
|
$text = "\n" . $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-02-11 09:02:15 +00:00
|
|
|
# Prune lower levels off the recursion check path
|
|
|
|
|
$this->mTemplatePath = $lastPathLevel;
|
|
|
|
|
|
2004-09-25 16:06:10 +00:00
|
|
|
if ( !$found ) {
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-09-25 16:06:10 +00:00
|
|
|
return $matches[0];
|
|
|
|
|
} else {
|
2004-09-21 04:33:51 +00:00
|
|
|
# replace ==section headers==
|
|
|
|
|
# XXX this needs to go away once we have a better parser.
|
2004-09-25 16:06:10 +00:00
|
|
|
if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
|
2004-09-21 11:54:45 +00:00
|
|
|
if( !is_null( $title ) )
|
|
|
|
|
$encodedname = base64_encode($title->getPrefixedDBkey());
|
|
|
|
|
else
|
|
|
|
|
$encodedname = base64_encode("");
|
2004-09-25 05:16:38 +00:00
|
|
|
$m = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
|
2004-09-21 11:54:45 +00:00
|
|
|
PREG_SPLIT_DELIM_CAPTURE);
|
|
|
|
|
$text = '';
|
|
|
|
|
$nsec = 0;
|
2004-09-25 05:16:38 +00:00
|
|
|
for( $i = 0; $i < count($m); $i += 2 ) {
|
|
|
|
|
$text .= $m[$i];
|
|
|
|
|
if (!isset($m[$i + 1]) || $m[$i + 1] == "") continue;
|
|
|
|
|
$hl = $m[$i + 1];
|
2004-09-21 18:20:56 +00:00
|
|
|
if( strstr($hl, "<!--MWTEMPLATESECTION") ) {
|
2004-09-21 11:54:45 +00:00
|
|
|
$text .= $hl;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
|
2004-09-21 18:20:56 +00:00
|
|
|
$text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
|
|
|
|
|
. $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
|
2004-09-21 11:54:45 +00:00
|
|
|
|
|
|
|
|
$nsec++;
|
2004-09-21 06:54:18 +00:00
|
|
|
}
|
2004-09-21 04:33:51 +00:00
|
|
|
}
|
2004-09-25 05:16:38 +00:00
|
|
|
}
|
2005-02-11 09:02:15 +00:00
|
|
|
# Prune lower levels off the recursion check path
|
|
|
|
|
$this->mTemplatePath = $lastPathLevel;
|
2004-11-21 14:07:24 +00:00
|
|
|
|
2004-09-25 05:16:38 +00:00
|
|
|
if ( !$found ) {
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-09-25 05:16:38 +00:00
|
|
|
return $matches[0];
|
|
|
|
|
} else {
|
2004-11-28 04:05:05 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-05-02 23:18:25 +00:00
|
|
|
return $text;
|
2004-03-20 15:03:26 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Triple brace replacement -- used for template arguments
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function argSubstitution( $matches ) {
|
2004-09-18 21:02:27 +00:00
|
|
|
$arg = trim( $matches[1] );
|
2004-05-23 03:39:24 +00:00
|
|
|
$text = $matches[0];
|
|
|
|
|
$inputArgs = end( $this->mArgStack );
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( $arg, $inputArgs ) ) {
|
2004-09-21 23:56:25 +00:00
|
|
|
$text = $inputArgs[$arg];
|
2004-05-23 03:39:24 +00:00
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-05-23 03:39:24 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the function is allowed to include this entity
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function incrementIncludeCount( $dbk ) {
|
2004-04-11 16:46:06 +00:00
|
|
|
if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
|
|
|
|
|
$this->mIncludeCount[$dbk] = 0;
|
|
|
|
|
}
|
|
|
|
|
if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* This function accomplishes several tasks:
|
|
|
|
|
* 1) Auto-number headings if that option is enabled
|
|
|
|
|
* 2) Add an [edit] link to sections for logged in users who have enabled the option
|
|
|
|
|
* 3) Add a Table of contents on the top for users who have enabled the option
|
|
|
|
|
* 4) Auto-anchor headings
|
|
|
|
|
*
|
|
|
|
|
* It loops through all headlines, collects the necessary data, then splits up the
|
|
|
|
|
* string and re-inserts the newly formatted headlines.
|
2005-01-31 22:59:55 +00:00
|
|
|
*
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param boolean $isMain
|
2004-09-21 05:49:12 +00:00
|
|
|
* @access private
|
|
|
|
|
*/
|
2005-01-31 22:59:55 +00:00
|
|
|
function formatHeadings( $text, $isMain=true ) {
|
2005-01-22 09:34:56 +00:00
|
|
|
global $wgInputEncoding, $wgMaxTocLevel, $wgContLang, $wgLinkHolders, $wgInterwikiLinkHolders;
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
$doNumberHeadings = $this->mOptions->getNumberHeadings();
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = true;
|
2004-06-29 23:59:30 +00:00
|
|
|
$forceTocHere = false;
|
2004-03-21 11:28:44 +00:00
|
|
|
if( !$this->mTitle->userCanEdit() ) {
|
|
|
|
|
$showEditLink = 0;
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
2004-03-21 11:28:44 +00:00
|
|
|
$showEditLink = $this->mOptions->getEditSection();
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Inhibit editsection links if requested in the page
|
|
|
|
|
$esw =& MagicWord::get( MAG_NOEDITSECTION );
|
2004-03-21 11:28:44 +00:00
|
|
|
if( $esw->matchAndRemove( $text ) ) {
|
|
|
|
|
$showEditLink = 0;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
# if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
|
2004-02-26 13:37:26 +00:00
|
|
|
# do not add TOC
|
|
|
|
|
$mw =& MagicWord::get( MAG_NOTOC );
|
2004-03-21 11:28:44 +00:00
|
|
|
if( $mw->matchAndRemove( $text ) ) {
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = false;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-04-03 04:38:09 +00:00
|
|
|
# Get all headlines for numbering them and adding funky stuff like [edit]
|
|
|
|
|
# links - this is for later, but we need the number of headlines right now
|
2005-01-06 00:13:49 +00:00
|
|
|
$numMatches = preg_match_all( '/<H([1-6])(.*?'.'>)(.*?)<\/H[1-6] *>/i', $text, $matches );
|
2004-04-03 04:38:09 +00:00
|
|
|
|
|
|
|
|
# if there are fewer than 4 headlines in the article, do not show TOC
|
|
|
|
|
if( $numMatches < 4 ) {
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = false;
|
2004-04-03 04:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-29 23:59:30 +00:00
|
|
|
# if the string __TOC__ (not case-sensitive) occurs in the HTML,
|
|
|
|
|
# override above conditions and always show TOC at that place
|
2005-01-15 23:21:52 +00:00
|
|
|
|
2004-06-29 23:59:30 +00:00
|
|
|
$mw =& MagicWord::get( MAG_TOC );
|
2005-01-15 23:21:52 +00:00
|
|
|
if($mw->match( $text ) ) {
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = true;
|
2004-06-29 23:59:30 +00:00
|
|
|
$forceTocHere = true;
|
|
|
|
|
} else {
|
|
|
|
|
# if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
|
|
|
|
|
# override above conditions and always show TOC above first header
|
|
|
|
|
$mw =& MagicWord::get( MAG_FORCETOC );
|
|
|
|
|
if ($mw->matchAndRemove( $text ) ) {
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = true;
|
2004-06-29 23:59:30 +00:00
|
|
|
}
|
2004-04-03 04:38:09 +00:00
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2005-01-15 23:21:52 +00:00
|
|
|
# Never ever show TOC if no headers
|
|
|
|
|
if( $numMatches < 1 ) {
|
2005-03-25 09:35:59 +00:00
|
|
|
$doShowToc = false;
|
2005-01-15 23:21:52 +00:00
|
|
|
}
|
2004-04-03 04:38:09 +00:00
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
# We need this to perform operations on the HTML
|
2004-02-29 08:43:29 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
2004-02-26 13:37:26 +00:00
|
|
|
|
|
|
|
|
# headline counter
|
2004-03-21 11:28:44 +00:00
|
|
|
$headlineCount = 0;
|
2004-09-21 04:33:51 +00:00
|
|
|
$sectionCount = 0; # headlineCount excluding template sections
|
2004-02-26 13:37:26 +00:00
|
|
|
|
|
|
|
|
# Ugh .. the TOC should have neat indentation levels which can be
|
|
|
|
|
# passed to the skin functions. These are determined here
|
2004-06-08 18:11:28 +00:00
|
|
|
$toc = '';
|
|
|
|
|
$full = '';
|
2004-03-08 02:50:04 +00:00
|
|
|
$head = array();
|
2004-03-21 11:28:44 +00:00
|
|
|
$sublevelCount = array();
|
2005-01-15 23:21:52 +00:00
|
|
|
$levelCount = array();
|
|
|
|
|
$toclevel = 0;
|
2004-03-29 14:48:07 +00:00
|
|
|
$level = 0;
|
|
|
|
|
$prevlevel = 0;
|
2005-01-15 23:21:52 +00:00
|
|
|
$toclevel = 0;
|
|
|
|
|
$prevtoclevel = 0;
|
|
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
foreach( $matches[3] as $headline ) {
|
2004-09-21 04:33:51 +00:00
|
|
|
$istemplate = 0;
|
2005-01-15 23:21:52 +00:00
|
|
|
$templatetitle = '';
|
2004-09-21 08:31:25 +00:00
|
|
|
$templatesection = 0;
|
2005-01-15 23:21:52 +00:00
|
|
|
$numbering = '';
|
2004-09-21 08:31:25 +00:00
|
|
|
|
2004-09-21 18:20:56 +00:00
|
|
|
if (preg_match("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", $headline, $mat)) {
|
2004-09-21 04:33:51 +00:00
|
|
|
$istemplate = 1;
|
2004-09-21 08:31:25 +00:00
|
|
|
$templatetitle = base64_decode($mat[1]);
|
|
|
|
|
$templatesection = 1 + (int)base64_decode($mat[2]);
|
2004-09-21 18:20:56 +00:00
|
|
|
$headline = preg_replace("/<!--MWTEMPLATESECTION=([^&]+)&([^_]+)-->/", "", $headline);
|
2004-09-21 04:33:51 +00:00
|
|
|
}
|
2004-09-21 08:31:25 +00:00
|
|
|
|
2005-01-15 23:21:52 +00:00
|
|
|
if( $toclevel ) {
|
2004-03-21 11:28:44 +00:00
|
|
|
$prevlevel = $level;
|
2005-01-15 23:21:52 +00:00
|
|
|
$prevtoclevel = $toclevel;
|
2004-03-21 11:28:44 +00:00
|
|
|
}
|
|
|
|
|
$level = $matches[1][$headlineCount];
|
2005-01-15 23:21:52 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
if( $doNumberHeadings || $doShowToc ) {
|
2005-01-15 23:21:52 +00:00
|
|
|
|
|
|
|
|
if ( $level > $prevlevel ) {
|
|
|
|
|
# Increase TOC level
|
|
|
|
|
$toclevel++;
|
|
|
|
|
$sublevelCount[$toclevel] = 0;
|
|
|
|
|
$toc .= $sk->tocIndent();
|
|
|
|
|
}
|
|
|
|
|
elseif ( $level < $prevlevel && $toclevel > 1 ) {
|
|
|
|
|
# Decrease TOC level, find level to jump to
|
|
|
|
|
|
|
|
|
|
if ( $toclevel == 2 && $level <= $levelCount[1] ) {
|
|
|
|
|
# Can only go down to level 1
|
|
|
|
|
$toclevel = 1;
|
|
|
|
|
} else {
|
|
|
|
|
for ($i = $toclevel; $i > 0; $i--) {
|
|
|
|
|
if ( $levelCount[$i] == $level ) {
|
|
|
|
|
# Found last matching level
|
|
|
|
|
$toclevel = $i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
elseif ( $levelCount[$i] < $level ) {
|
|
|
|
|
# Found first matching level below current level
|
|
|
|
|
$toclevel = $i + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
# No change in level, end TOC line
|
|
|
|
|
$toc .= $sk->tocLineEnd();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$levelCount[$toclevel] = $level;
|
|
|
|
|
|
|
|
|
|
# count number of headlines for each level
|
|
|
|
|
@$sublevelCount[$toclevel]++;
|
2004-03-29 14:48:07 +00:00
|
|
|
$dot = 0;
|
2005-01-15 23:21:52 +00:00
|
|
|
for( $i = 1; $i <= $toclevel; $i++ ) {
|
2004-03-29 14:48:07 +00:00
|
|
|
if( !empty( $sublevelCount[$i] ) ) {
|
2004-03-21 11:28:44 +00:00
|
|
|
if( $dot ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$numbering .= '.';
|
2004-03-21 11:28:44 +00:00
|
|
|
}
|
2004-09-24 13:14:52 +00:00
|
|
|
$numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
|
2004-04-12 23:59:37 +00:00
|
|
|
$dot = 1;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
# The canonized header is a version of the header text safe to use for links
|
|
|
|
|
# Avoid insertion of weird stuff like <math> by expanding the relevant sections
|
2004-04-11 16:46:06 +00:00
|
|
|
$canonized_headline = $this->unstrip( $headline, $this->mStripState );
|
2005-01-22 09:13:02 +00:00
|
|
|
$canonized_headline = $this->unstripNoWiki( $canonized_headline, $this->mStripState );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-08-26 18:48:13 +00:00
|
|
|
# Remove link placeholders by the link text.
|
2004-09-20 23:02:08 +00:00
|
|
|
# <!--LINK number-->
|
2004-08-26 18:48:13 +00:00
|
|
|
# turns into
|
|
|
|
|
# link text with suffix
|
2004-09-20 23:02:08 +00:00
|
|
|
$canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
|
2005-04-27 07:48:14 +00:00
|
|
|
"\$this->mLinkHolders['texts'][\$1]",
|
2004-09-20 23:02:08 +00:00
|
|
|
$canonized_headline );
|
2005-01-22 09:34:56 +00:00
|
|
|
$canonized_headline = preg_replace( '/<!--IWLINK ([0-9]*)-->/e',
|
2005-04-27 07:48:14 +00:00
|
|
|
"\$this->mInterwikiLinkHolders[\$1][1]",
|
2005-01-22 09:34:56 +00:00
|
|
|
$canonized_headline );
|
2004-09-20 23:02:08 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
# strip out HTML
|
2004-06-08 18:11:28 +00:00
|
|
|
$canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
|
2004-04-12 23:59:37 +00:00
|
|
|
$tocline = trim( $canonized_headline );
|
2004-06-05 10:34:32 +00:00
|
|
|
$canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
|
|
|
|
|
$replacearray = array(
|
|
|
|
|
'%3A' => ':',
|
|
|
|
|
'%' => '.'
|
|
|
|
|
);
|
|
|
|
|
$canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
|
2005-04-21 06:30:48 +00:00
|
|
|
$refers[$headlineCount] = $canonized_headline;
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
# count how many in assoc. array so we can track dupes in anchors
|
2004-03-29 14:48:07 +00:00
|
|
|
@$refers[$canonized_headline]++;
|
2004-03-21 11:28:44 +00:00
|
|
|
$refcount[$headlineCount]=$refers[$canonized_headline];
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2005-01-15 23:21:52 +00:00
|
|
|
# Don't number the heading if it is the only one (looks silly)
|
|
|
|
|
if( $doNumberHeadings && count( $matches[3] ) > 1) {
|
|
|
|
|
# the two are different if the line contains a link
|
|
|
|
|
$headline=$numbering . ' ' . $headline;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
# Create the anchor for linking from the TOC to the section
|
|
|
|
|
$anchor = $canonized_headline;
|
|
|
|
|
if($refcount[$headlineCount] > 1 ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$anchor .= '_' . $refcount[$headlineCount];
|
2004-03-21 11:28:44 +00:00
|
|
|
}
|
2004-07-19 05:15:50 +00:00
|
|
|
if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
|
2005-01-15 23:21:52 +00:00
|
|
|
$toc .= $sk->tocLine($anchor, $tocline, $numbering, $toclevel);
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-09-21 11:54:45 +00:00
|
|
|
if( $showEditLink && ( !$istemplate || $templatetitle !== "" ) ) {
|
2004-03-29 14:48:07 +00:00
|
|
|
if ( empty( $head[$headlineCount] ) ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$head[$headlineCount] = '';
|
2004-03-29 14:48:07 +00:00
|
|
|
}
|
2004-09-21 08:31:25 +00:00
|
|
|
if( $istemplate )
|
|
|
|
|
$head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
|
|
|
|
|
else
|
2004-10-15 17:39:10 +00:00
|
|
|
$head[$headlineCount] .= $sk->editSectionLink($this->mTitle, $sectionCount+1);
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-04 01:45:02 +00:00
|
|
|
# give headline the correct <h#> tag
|
2004-08-22 17:24:50 +00:00
|
|
|
@$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
$headlineCount++;
|
2004-09-21 04:33:51 +00:00
|
|
|
if( !$istemplate )
|
|
|
|
|
$sectionCount++;
|
2004-04-12 23:59:37 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
if( $doShowToc ) {
|
2005-01-15 23:21:52 +00:00
|
|
|
$toc .= $sk->tocUnindent( $toclevel - 1 );
|
|
|
|
|
$toc = $sk->tocList( $toc );
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
# split up and insert constructed headlines
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
$blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
|
2004-03-21 11:28:44 +00:00
|
|
|
$i = 0;
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-03-21 11:28:44 +00:00
|
|
|
foreach( $blocks as $block ) {
|
2004-03-29 14:48:07 +00:00
|
|
|
if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
|
2004-08-16 15:23:46 +00:00
|
|
|
# This is the [edit] link that appears for the top block of text when
|
2004-02-26 13:37:26 +00:00
|
|
|
# section editing is enabled
|
2004-04-11 16:46:06 +00:00
|
|
|
|
|
|
|
|
# Disabled because it broke block formatting
|
|
|
|
|
# For example, a bullet point in the top line
|
|
|
|
|
# $full .= $sk->editSectionLink(0);
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
2004-03-21 11:28:44 +00:00
|
|
|
$full .= $block;
|
2004-06-29 23:59:30 +00:00
|
|
|
if( $doShowToc && !$i && $isMain && !$forceTocHere) {
|
2004-04-07 16:12:18 +00:00
|
|
|
# Top anchor now in skin
|
|
|
|
|
$full = $full.$toc;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-08 02:50:04 +00:00
|
|
|
if( !empty( $head[$i] ) ) {
|
|
|
|
|
$full .= $head[$i];
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
$i++;
|
|
|
|
|
}
|
2004-06-29 23:59:30 +00:00
|
|
|
if($forceTocHere) {
|
|
|
|
|
$mw =& MagicWord::get( MAG_TOC );
|
|
|
|
|
return $mw->replace( $toc, $full );
|
|
|
|
|
} else {
|
|
|
|
|
return $full;
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Return an HTML link for the "ISBN 123456" text
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function magicISBN( $text ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
$fname = 'Parser::magicISBN';
|
|
|
|
|
wfProfileIn( $fname );
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$a = split( 'ISBN ', ' '.$text );
|
2004-07-10 03:09:26 +00:00
|
|
|
if ( count ( $a ) < 2 ) {
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
$text = substr( array_shift( $a ), 1);
|
2005-04-10 20:22:11 +00:00
|
|
|
$valid = '0123456789-Xx';
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-05-26 16:29:04 +00:00
|
|
|
foreach ( $a as $x ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$isbn = $blank = '' ;
|
|
|
|
|
while ( ' ' == $x{0} ) {
|
|
|
|
|
$blank .= ' ';
|
2004-02-26 13:37:26 +00:00
|
|
|
$x = substr( $x, 1 );
|
|
|
|
|
}
|
2004-09-09 23:24:36 +00:00
|
|
|
if ( $x == '' ) { # blank isbn
|
|
|
|
|
$text .= "ISBN $blank";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-03-06 20:04:25 +00:00
|
|
|
while ( strstr( $valid, $x{0} ) != false ) {
|
2004-02-26 13:37:26 +00:00
|
|
|
$isbn .= $x{0};
|
|
|
|
|
$x = substr( $x, 1 );
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
$num = str_replace( '-', '', $isbn );
|
|
|
|
|
$num = str_replace( ' ', '', $num );
|
2005-04-10 20:22:11 +00:00
|
|
|
$num = str_replace( 'x', 'X', $num );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '' == $num ) {
|
2004-05-26 16:29:04 +00:00
|
|
|
$text .= "ISBN $blank$x";
|
2004-02-26 13:37:26 +00:00
|
|
|
} else {
|
2004-06-08 18:11:28 +00:00
|
|
|
$titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
|
|
|
|
|
$text .= '<a href="' .
|
2004-08-22 17:24:50 +00:00
|
|
|
$titleObj->escapeLocalUrl( 'isbn='.$num ) .
|
2004-03-06 20:04:25 +00:00
|
|
|
"\" class=\"internal\">ISBN $isbn</a>";
|
2004-02-26 13:37:26 +00:00
|
|
|
$text .= $x;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-02-26 13:37:26 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2004-07-12 19:49:20 +00:00
|
|
|
|
2004-09-20 21:41:26 +00:00
|
|
|
/**
|
|
|
|
|
* Return an HTML link for the "RFC 1234" text
|
2005-04-07 14:24:42 +00:00
|
|
|
*
|
2004-09-20 21:41:26 +00:00
|
|
|
* @access private
|
2005-04-07 14:24:42 +00:00
|
|
|
* @param string $text Text to be processed
|
|
|
|
|
* @param string $keyword Magic keyword to use (default RFC)
|
|
|
|
|
* @param string $urlmsg Interface message to use (default rfcurl)
|
|
|
|
|
* @return string
|
2004-09-20 21:41:26 +00:00
|
|
|
*/
|
2004-11-20 11:28:37 +00:00
|
|
|
function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl' ) {
|
2004-09-20 21:41:26 +00:00
|
|
|
|
|
|
|
|
$valid = '0123456789';
|
|
|
|
|
$internal = false;
|
2004-03-06 21:30:42 +00:00
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
$a = split( $keyword, ' '.$text );
|
|
|
|
|
if ( count ( $a ) < 2 ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-05-26 16:29:04 +00:00
|
|
|
$text = substr( array_shift( $a ), 1);
|
2004-09-20 21:41:26 +00:00
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
/* Check if keyword is preceed by [[.
|
2004-09-20 21:41:26 +00:00
|
|
|
* This test is made here cause of the array_shift above
|
|
|
|
|
* that prevent the test to be done in the foreach.
|
|
|
|
|
*/
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( substr( $text, -2 ) == '[[' ) {
|
|
|
|
|
$internal = true;
|
|
|
|
|
}
|
2004-03-06 21:30:42 +00:00
|
|
|
|
2004-05-26 16:29:04 +00:00
|
|
|
foreach ( $a as $x ) {
|
2004-09-20 21:41:26 +00:00
|
|
|
/* token might be empty if we have RFC RFC 1234 */
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( $x=='' ) {
|
|
|
|
|
$text.=$keyword;
|
2004-09-20 21:41:26 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
$id = $blank = '' ;
|
2004-09-20 21:41:26 +00:00
|
|
|
|
|
|
|
|
/** remove and save whitespaces in $blank */
|
|
|
|
|
while ( $x{0} == ' ' ) {
|
2004-06-08 18:11:28 +00:00
|
|
|
$blank .= ' ';
|
2004-03-06 21:30:42 +00:00
|
|
|
$x = substr( $x, 1 );
|
|
|
|
|
}
|
2004-09-20 21:41:26 +00:00
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
/** remove and save the rfc number in $id */
|
2004-03-06 21:30:42 +00:00
|
|
|
while ( strstr( $valid, $x{0} ) != false ) {
|
2004-11-20 11:28:37 +00:00
|
|
|
$id .= $x{0};
|
2004-03-06 21:30:42 +00:00
|
|
|
$x = substr( $x, 1 );
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( $id == '' ) {
|
2004-09-20 21:41:26 +00:00
|
|
|
/* call back stripped spaces*/
|
2004-11-20 11:28:37 +00:00
|
|
|
$text .= $keyword.$blank.$x;
|
|
|
|
|
} elseif( $internal ) {
|
2004-09-20 21:41:26 +00:00
|
|
|
/* normal link */
|
2004-11-20 11:28:37 +00:00
|
|
|
$text .= $keyword.$id.$x;
|
2004-03-06 21:30:42 +00:00
|
|
|
} else {
|
2004-09-20 21:41:26 +00:00
|
|
|
/* build the external link*/
|
2005-04-07 14:24:42 +00:00
|
|
|
$url = wfMsg( $urlmsg, $id);
|
2004-03-06 21:30:42 +00:00
|
|
|
$sk =& $this->mOptions->getSkin();
|
2004-11-20 11:28:37 +00:00
|
|
|
$la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
|
|
|
|
|
$text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
|
2004-03-06 21:30:42 +00:00
|
|
|
}
|
2004-09-20 21:41:26 +00:00
|
|
|
|
|
|
|
|
/* Check if the next RFC keyword is preceed by [[ */
|
2004-11-20 11:28:37 +00:00
|
|
|
$internal = ( substr($x,-2) == '[[' );
|
2004-03-06 21:30:42 +00:00
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Transform wiki markup when saving a page by doing \r\n -> \n
|
|
|
|
|
* conversion, substitting signatures, {{subst:}} templates, etc.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text to transform
|
|
|
|
|
* @param Title &$title the Title object for the current article
|
|
|
|
|
* @param User &$user the User object describing the current user
|
|
|
|
|
* @param ParserOptions $options parsing options
|
|
|
|
|
* @param bool $clearState whether to clear the parser state first
|
|
|
|
|
* @return string the altered wiki markup
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
|
2004-03-06 01:49:16 +00:00
|
|
|
$this->mOptions = $options;
|
2004-03-27 22:47:25 +00:00
|
|
|
$this->mTitle =& $title;
|
2004-03-20 15:03:26 +00:00
|
|
|
$this->mOutputType = OT_WIKI;
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
if ( $clearState ) {
|
2004-03-08 09:09:35 +00:00
|
|
|
$this->clearState();
|
2004-03-06 01:49:16 +00:00
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
$stripState = false;
|
2004-04-09 16:22:12 +00:00
|
|
|
$pairs = array(
|
|
|
|
|
"\r\n" => "\n",
|
2005-04-21 06:30:48 +00:00
|
|
|
);
|
2004-11-20 11:28:37 +00:00
|
|
|
$text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
$text = $this->strip( $text, $stripState, false );
|
|
|
|
|
$text = $this->pstPass2( $text, $user );
|
|
|
|
|
$text = $this->unstrip( $text, $stripState );
|
2004-06-02 12:29:15 +00:00
|
|
|
$text = $this->unstripNoWiki( $text, $stripState );
|
2004-03-06 01:49:16 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Pre-save transform helper function
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function pstPass2( $text, &$user ) {
|
2005-04-21 06:30:48 +00:00
|
|
|
global $wgContLang, $wgLocaltimezone;
|
2004-03-20 15:03:26 +00:00
|
|
|
|
|
|
|
|
# Variable replacement
|
|
|
|
|
# Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
|
|
|
|
|
$text = $this->replaceVariables( $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
|
|
|
|
|
# Signatures
|
|
|
|
|
#
|
|
|
|
|
$n = $user->getName();
|
2004-06-08 18:11:28 +00:00
|
|
|
$k = $user->getOption( 'nickname' );
|
|
|
|
|
if ( '' == $k ) { $k = $n; }
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( isset( $wgLocaltimezone ) ) {
|
|
|
|
|
$oldtz = getenv( 'TZ' );
|
|
|
|
|
putenv( 'TZ='.$wgLocaltimezone );
|
2004-03-06 01:49:16 +00:00
|
|
|
}
|
2005-04-07 18:14:31 +00:00
|
|
|
|
|
|
|
|
/* Note: This is the timestamp saved as hardcoded wikitext to
|
|
|
|
|
* the database, we use $wgContLang here in order to give
|
|
|
|
|
* everyone the same signiture and use the default one rather
|
|
|
|
|
* than the one selected in each users preferences.
|
|
|
|
|
*/
|
|
|
|
|
$d = $wgContLang->timeanddate( wfTimestampNow(), false, false) .
|
2004-06-08 18:11:28 +00:00
|
|
|
' (' . date( 'T' ) . ')';
|
2004-11-20 11:28:37 +00:00
|
|
|
if ( isset( $wgLocaltimezone ) ) {
|
2005-04-21 06:30:48 +00:00
|
|
|
putenv( 'TZ='.$oldtz );
|
2004-11-20 11:28:37 +00:00
|
|
|
}
|
2004-03-06 01:49:16 +00:00
|
|
|
|
2004-12-23 08:40:51 +00:00
|
|
|
if( $user->getOption( 'fancysig' ) ) {
|
|
|
|
|
$sigText = $k;
|
|
|
|
|
} else {
|
|
|
|
|
$sigText = '[[' . $wgContLang->getNsText( NS_USER ) . ":$n|$k]]";
|
|
|
|
|
}
|
2004-12-23 07:47:17 +00:00
|
|
|
$text = preg_replace( '/~~~~~/', $d, $text );
|
2004-12-23 08:40:51 +00:00
|
|
|
$text = preg_replace( '/~~~~/', "$sigText $d", $text );
|
|
|
|
|
$text = preg_replace( '/~~~/', $sigText, $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
|
|
|
|
|
# Context links: [[|name]] and [[name (context)|]]
|
|
|
|
|
#
|
|
|
|
|
$tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
|
|
|
|
|
$np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
|
|
|
|
|
$namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
|
|
|
|
|
$conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
|
|
|
|
|
|
|
|
|
|
$p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
|
|
|
|
|
$p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
|
2004-08-28 16:42:57 +00:00
|
|
|
$p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
|
|
|
|
|
$p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
|
2004-08-16 20:01:21 +00:00
|
|
|
$context = '';
|
2004-03-06 01:49:16 +00:00
|
|
|
$t = $this->mTitle->getText();
|
|
|
|
|
if ( preg_match( $conpat, $t, $m ) ) {
|
|
|
|
|
$context = $m[2];
|
|
|
|
|
}
|
2004-06-08 18:11:28 +00:00
|
|
|
$text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
|
|
|
|
|
$text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
|
|
|
|
|
$text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
|
2004-06-08 18:11:28 +00:00
|
|
|
if ( '' == $context ) {
|
|
|
|
|
$text = preg_replace( $p2, '[[\\1]]', $text );
|
2004-03-06 01:49:16 +00:00
|
|
|
} else {
|
|
|
|
|
$text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
|
|
|
|
|
}
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
# Trim trailing whitespace
|
2004-04-12 23:59:37 +00:00
|
|
|
# MAG_END (__END__) tag allows for trailing
|
2004-03-06 01:49:16 +00:00
|
|
|
# whitespace to be deliberately included
|
|
|
|
|
$text = rtrim( $text );
|
|
|
|
|
$mw =& MagicWord::get( MAG_END );
|
|
|
|
|
$mw->matchAndRemove( $text );
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Set up some variables which are usually set up in parse()
|
|
|
|
|
* so that an external function can call some class members with confidence
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
|
2004-03-27 22:47:25 +00:00
|
|
|
$this->mTitle =& $title;
|
|
|
|
|
$this->mOptions = $options;
|
|
|
|
|
$this->mOutputType = $outputType;
|
|
|
|
|
if ( $clearState ) {
|
|
|
|
|
$this->clearState();
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-05 10:38:40 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Transform a MediaWiki message by replacing magic variables.
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text to transform
|
|
|
|
|
* @param ParserOptions $options options
|
|
|
|
|
* @return string the text with variables substituted
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
2004-04-05 10:38:40 +00:00
|
|
|
function transformMsg( $text, $options ) {
|
|
|
|
|
global $wgTitle;
|
|
|
|
|
static $executing = false;
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
# Guard against infinite recursion
|
|
|
|
|
if ( $executing ) {
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
$executing = true;
|
|
|
|
|
|
|
|
|
|
$this->mTitle = $wgTitle;
|
|
|
|
|
$this->mOptions = $options;
|
|
|
|
|
$this->mOutputType = OT_MSG;
|
|
|
|
|
$this->clearState();
|
|
|
|
|
$text = $this->replaceVariables( $text );
|
2004-04-12 23:59:37 +00:00
|
|
|
|
2004-04-05 10:38:40 +00:00
|
|
|
$executing = false;
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2004-06-09 12:15:42 +00:00
|
|
|
|
2004-09-21 05:49:12 +00:00
|
|
|
/**
|
|
|
|
|
* Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
|
|
|
|
|
* Callback will be called with the text within
|
|
|
|
|
* Transform and return the text within
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
2004-06-12 06:15:09 +00:00
|
|
|
function setHook( $tag, $callback ) {
|
|
|
|
|
$oldVal = @$this->mTagHooks[$tag];
|
|
|
|
|
$this->mTagHooks[$tag] = $callback;
|
2004-06-09 12:15:42 +00:00
|
|
|
return $oldVal;
|
|
|
|
|
}
|
2004-10-15 17:39:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace <!--LINK--> link placeholders with actual links, in the buffer
|
|
|
|
|
* Placeholders created in Skin::makeLinkObj()
|
|
|
|
|
* Returns an array of links found, indexed by PDBK:
|
|
|
|
|
* 0 - broken
|
|
|
|
|
* 1 - normal link
|
|
|
|
|
* 2 - stub
|
|
|
|
|
* $options is a bit field, RLH_FOR_UPDATE to select for update
|
|
|
|
|
*/
|
|
|
|
|
function replaceLinkHolders( &$text, $options = 0 ) {
|
2005-04-27 07:48:14 +00:00
|
|
|
global $wgUser, $wgLinkCache;
|
|
|
|
|
global $wgOutputReplace;
|
2004-10-15 17:39:10 +00:00
|
|
|
|
|
|
|
|
$fname = 'Parser::replaceLinkHolders';
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
$pdbks = array();
|
|
|
|
|
$colours = array();
|
2005-04-27 07:48:14 +00:00
|
|
|
$sk = $this->mOptions->getSkin();
|
2004-10-15 17:39:10 +00:00
|
|
|
|
2005-04-27 07:48:14 +00:00
|
|
|
if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
|
2004-10-15 17:39:10 +00:00
|
|
|
wfProfileIn( $fname.'-check' );
|
|
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2004-12-19 08:00:50 +00:00
|
|
|
$page = $dbr->tableName( 'page' );
|
2004-10-15 17:39:10 +00:00
|
|
|
$threshold = $wgUser->getOption('stubthreshold');
|
|
|
|
|
|
|
|
|
|
# Sort by namespace
|
2005-04-27 07:48:14 +00:00
|
|
|
asort( $this->mLinkHolders['namespaces'] );
|
2004-10-15 17:39:10 +00:00
|
|
|
|
|
|
|
|
# Generate query
|
|
|
|
|
$query = false;
|
2005-04-27 07:48:14 +00:00
|
|
|
foreach ( $this->mLinkHolders['namespaces'] as $key => $val ) {
|
2004-10-15 17:39:10 +00:00
|
|
|
# Make title object
|
2005-04-27 07:48:14 +00:00
|
|
|
$title = $this->mLinkHolders['titles'][$key];
|
2004-10-15 17:39:10 +00:00
|
|
|
|
|
|
|
|
# Skip invalid entries.
|
|
|
|
|
# Result will be ugly, but prevents crash.
|
|
|
|
|
if ( is_null( $title ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$pdbk = $pdbks[$key] = $title->getPrefixedDBkey();
|
|
|
|
|
|
|
|
|
|
# Check if it's in the link cache already
|
|
|
|
|
if ( $wgLinkCache->getGoodLinkID( $pdbk ) ) {
|
|
|
|
|
$colours[$pdbk] = 1;
|
|
|
|
|
} elseif ( $wgLinkCache->isBadLink( $pdbk ) ) {
|
|
|
|
|
$colours[$pdbk] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
# Not in the link cache, add it to the query
|
|
|
|
|
if ( !isset( $current ) ) {
|
|
|
|
|
$current = $val;
|
2004-12-19 08:00:50 +00:00
|
|
|
$query = "SELECT page_id, page_namespace, page_title";
|
2004-10-15 17:39:10 +00:00
|
|
|
if ( $threshold > 0 ) {
|
2005-03-12 11:51:02 +00:00
|
|
|
$query .= ', page_len, page_is_redirect';
|
2004-12-19 08:00:50 +00:00
|
|
|
}
|
2005-03-12 11:51:02 +00:00
|
|
|
$query .= " FROM $page WHERE (page_namespace=$val AND page_title IN(";
|
2004-10-15 17:39:10 +00:00
|
|
|
} elseif ( $current != $val ) {
|
|
|
|
|
$current = $val;
|
2004-12-19 08:00:50 +00:00
|
|
|
$query .= ")) OR (page_namespace=$val AND page_title IN(";
|
2004-10-15 17:39:10 +00:00
|
|
|
} else {
|
|
|
|
|
$query .= ', ';
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 07:48:14 +00:00
|
|
|
$query .= $dbr->addQuotes( $this->mLinkHolders['dbkeys'][$key] );
|
2004-10-15 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $query ) {
|
|
|
|
|
$query .= '))';
|
|
|
|
|
if ( $options & RLH_FOR_UPDATE ) {
|
|
|
|
|
$query .= ' FOR UPDATE';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$res = $dbr->query( $query, $fname );
|
|
|
|
|
|
|
|
|
|
# Fetch data and form into an associative array
|
|
|
|
|
# non-existent = broken
|
|
|
|
|
# 1 = known
|
|
|
|
|
# 2 = stub
|
|
|
|
|
while ( $s = $dbr->fetchObject($res) ) {
|
2004-12-19 08:00:50 +00:00
|
|
|
$title = Title::makeTitle( $s->page_namespace, $s->page_title );
|
2004-10-15 17:39:10 +00:00
|
|
|
$pdbk = $title->getPrefixedDBkey();
|
2004-12-19 08:00:50 +00:00
|
|
|
$wgLinkCache->addGoodLink( $s->page_id, $pdbk );
|
2004-10-15 17:39:10 +00:00
|
|
|
|
|
|
|
|
if ( $threshold > 0 ) {
|
2004-12-19 08:00:50 +00:00
|
|
|
$size = $s->page_len;
|
2005-02-21 06:07:52 +00:00
|
|
|
if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) {
|
2004-10-15 17:39:10 +00:00
|
|
|
$colours[$pdbk] = 1;
|
|
|
|
|
} else {
|
|
|
|
|
$colours[$pdbk] = 2;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$colours[$pdbk] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wfProfileOut( $fname.'-check' );
|
|
|
|
|
|
|
|
|
|
# Construct search and replace arrays
|
|
|
|
|
wfProfileIn( $fname.'-construct' );
|
2005-04-27 07:48:14 +00:00
|
|
|
$wgOutputReplace = array();
|
|
|
|
|
foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
|
2004-10-15 17:39:10 +00:00
|
|
|
$pdbk = $pdbks[$key];
|
2005-04-27 07:48:14 +00:00
|
|
|
$searchkey = "<!--LINK $key-->";
|
|
|
|
|
$title = $this->mLinkHolders['titles'][$key];
|
2004-10-15 17:39:10 +00:00
|
|
|
if ( empty( $colours[$pdbk] ) ) {
|
|
|
|
|
$wgLinkCache->addBadLink( $pdbk );
|
|
|
|
|
$colours[$pdbk] = 0;
|
2005-04-27 07:48:14 +00:00
|
|
|
$wgOutputReplace[$searchkey] = $sk->makeBrokenLinkObj( $title,
|
|
|
|
|
$this->mLinkHolders['texts'][$key],
|
|
|
|
|
$this->mLinkHolders['queries'][$key] );
|
2004-10-15 17:39:10 +00:00
|
|
|
} elseif ( $colours[$pdbk] == 1 ) {
|
2005-04-27 07:48:14 +00:00
|
|
|
$wgOutputReplace[$searchkey] = $sk->makeKnownLinkObj( $title,
|
|
|
|
|
$this->mLinkHolders['texts'][$key],
|
|
|
|
|
$this->mLinkHolders['queries'][$key] );
|
2004-10-15 17:39:10 +00:00
|
|
|
} elseif ( $colours[$pdbk] == 2 ) {
|
2005-04-27 07:48:14 +00:00
|
|
|
$wgOutputReplace[$searchkey] = $sk->makeStubLinkObj( $title,
|
|
|
|
|
$this->mLinkHolders['texts'][$key],
|
|
|
|
|
$this->mLinkHolders['queries'][$key] );
|
2004-10-15 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wfProfileOut( $fname.'-construct' );
|
|
|
|
|
|
|
|
|
|
# Do the thing
|
|
|
|
|
wfProfileIn( $fname.'-replace' );
|
|
|
|
|
|
|
|
|
|
$text = preg_replace_callback(
|
|
|
|
|
'/(<!--LINK .*?-->)/',
|
2005-04-27 07:48:14 +00:00
|
|
|
"wfOutputReplaceMatches",
|
2004-10-15 17:39:10 +00:00
|
|
|
$text);
|
2005-04-27 07:48:14 +00:00
|
|
|
|
2004-10-15 17:39:10 +00:00
|
|
|
wfProfileOut( $fname.'-replace' );
|
2004-11-13 14:24:42 +00:00
|
|
|
}
|
2004-10-15 17:39:10 +00:00
|
|
|
|
2005-04-27 07:48:14 +00:00
|
|
|
# Now process interwiki link holders
|
|
|
|
|
# This is quite a bit simpler than internal links
|
|
|
|
|
if ( !empty( $this->mInterwikiLinkHolders ) ) {
|
2004-10-15 17:39:10 +00:00
|
|
|
wfProfileIn( $fname.'-interwiki' );
|
2005-04-27 07:48:14 +00:00
|
|
|
# Make interwiki link HTML
|
|
|
|
|
$wgOutputReplace = array();
|
|
|
|
|
foreach( $this->mInterwikiLinkHolders as $i => $lh ) {
|
|
|
|
|
$s = $sk->makeLink( $lh[0], $lh[1] );
|
|
|
|
|
$wgOutputReplace[] = $s;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-15 17:39:10 +00:00
|
|
|
$text = preg_replace_callback(
|
|
|
|
|
'/<!--IWLINK (.*?)-->/',
|
2005-04-27 07:48:14 +00:00
|
|
|
"wfOutputReplaceMatches",
|
2004-11-20 11:28:37 +00:00
|
|
|
$text );
|
2004-10-15 17:39:10 +00:00
|
|
|
wfProfileOut( $fname.'-interwiki' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $colours;
|
|
|
|
|
}
|
2004-11-20 11:28:37 +00:00
|
|
|
|
2004-11-13 12:04:31 +00:00
|
|
|
/**
|
|
|
|
|
* Renders an image gallery from a text with one line per image.
|
|
|
|
|
* text labels may be given by using |-style alternative text. E.g.
|
|
|
|
|
* Image:one.jpg|The number "1"
|
|
|
|
|
* Image:tree.jpg|A tree
|
|
|
|
|
* given as text will return the HTML of a gallery with two images,
|
|
|
|
|
* labeled 'The number "1"' and
|
|
|
|
|
* 'A tree'.
|
2005-04-27 07:48:14 +00:00
|
|
|
*
|
|
|
|
|
* @static
|
2004-11-13 12:04:31 +00:00
|
|
|
*/
|
|
|
|
|
function renderImageGallery( $text ) {
|
2005-03-26 18:55:10 +00:00
|
|
|
# Setup the parser
|
|
|
|
|
global $wgUser, $wgParser, $wgTitle;
|
|
|
|
|
$parserOptions = ParserOptions::newFromUser( $wgUser );
|
|
|
|
|
|
2004-11-13 12:04:31 +00:00
|
|
|
global $wgLinkCache;
|
|
|
|
|
$ig = new ImageGallery();
|
|
|
|
|
$ig->setShowBytes( false );
|
|
|
|
|
$ig->setShowFilename( false );
|
|
|
|
|
$lines = explode( "\n", $text );
|
2004-11-20 11:28:37 +00:00
|
|
|
|
2004-11-13 12:04:31 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2004-11-20 11:28:37 +00:00
|
|
|
# match lines like these:
|
|
|
|
|
# Image:someimage.jpg|This is some image
|
2004-11-13 12:04:31 +00:00
|
|
|
preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
|
|
|
|
|
# Skip empty lines
|
|
|
|
|
if ( count( $matches ) == 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$nt = Title::newFromURL( $matches[1] );
|
2004-12-15 09:07:21 +00:00
|
|
|
if( is_null( $nt ) ) {
|
|
|
|
|
# Bogus title. Ignore these so we don't bomb out later.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-11-13 12:04:31 +00:00
|
|
|
if ( isset( $matches[3] ) ) {
|
|
|
|
|
$label = $matches[3];
|
|
|
|
|
} else {
|
|
|
|
|
$label = '';
|
|
|
|
|
}
|
2004-12-09 02:13:00 +00:00
|
|
|
|
2005-03-26 18:55:10 +00:00
|
|
|
$html = $wgParser->parse( $label , $wgTitle, $parserOptions );
|
|
|
|
|
$html = $html->mText;
|
2004-12-09 02:13:00 +00:00
|
|
|
|
2005-04-10 18:29:30 +00:00
|
|
|
$ig->add( new Image( $nt ), $html );
|
2004-11-13 12:04:31 +00:00
|
|
|
$wgLinkCache->addImageLinkObj( $nt );
|
|
|
|
|
}
|
|
|
|
|
return $ig->toHTML();
|
|
|
|
|
}
|
2005-04-27 07:48:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse image options text and use it to make an image
|
|
|
|
|
*/
|
|
|
|
|
function makeImage( &$nt, $options ) {
|
|
|
|
|
global $wgContLang, $wgUseImageResize;
|
|
|
|
|
global $wgUser, $wgThumbLimits;
|
|
|
|
|
|
|
|
|
|
$align = '';
|
|
|
|
|
|
|
|
|
|
# Check if the options text is of the form "options|alt text"
|
|
|
|
|
# Options are:
|
|
|
|
|
# * thumbnail make a thumbnail with enlarge-icon and caption, alignment depends on lang
|
|
|
|
|
# * left no resizing, just left align. label is used for alt= only
|
|
|
|
|
# * right same, but right aligned
|
|
|
|
|
# * none same, but not aligned
|
|
|
|
|
# * ___px scale to ___ pixels width, no aligning. e.g. use in taxobox
|
|
|
|
|
# * center center the image
|
|
|
|
|
# * framed Keep original image size, no magnify-button.
|
|
|
|
|
|
|
|
|
|
$part = explode( '|', $options);
|
|
|
|
|
|
|
|
|
|
$mwThumb =& MagicWord::get( MAG_IMG_THUMBNAIL );
|
|
|
|
|
$mwLeft =& MagicWord::get( MAG_IMG_LEFT );
|
|
|
|
|
$mwRight =& MagicWord::get( MAG_IMG_RIGHT );
|
|
|
|
|
$mwNone =& MagicWord::get( MAG_IMG_NONE );
|
|
|
|
|
$mwWidth =& MagicWord::get( MAG_IMG_WIDTH );
|
|
|
|
|
$mwCenter =& MagicWord::get( MAG_IMG_CENTER );
|
|
|
|
|
$mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
|
|
|
|
|
$caption = '';
|
|
|
|
|
|
|
|
|
|
$width = $height = $framed = $thumb = false;
|
|
|
|
|
$manual_thumb = "" ;
|
|
|
|
|
|
|
|
|
|
foreach( $part as $key => $val ) {
|
|
|
|
|
$val_parts = explode ( "=" , $val , 2 ) ;
|
|
|
|
|
$left_part = array_shift ( $val_parts ) ;
|
|
|
|
|
if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
$thumb=true;
|
|
|
|
|
} elseif ( $wgUseImageResize && count ( $val_parts ) == 1 && ! is_null( $mwThumb->matchVariableStartToEnd($left_part) ) ) {
|
|
|
|
|
# use manually specified thumbnail
|
|
|
|
|
$thumb=true;
|
|
|
|
|
$manual_thumb = array_shift ( $val_parts ) ;
|
|
|
|
|
} elseif ( ! is_null( $mwRight->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
# remember to set an alignment, don't render immediately
|
|
|
|
|
$align = 'right';
|
|
|
|
|
} elseif ( ! is_null( $mwLeft->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
# remember to set an alignment, don't render immediately
|
|
|
|
|
$align = 'left';
|
|
|
|
|
} elseif ( ! is_null( $mwCenter->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
# remember to set an alignment, don't render immediately
|
|
|
|
|
$align = 'center';
|
|
|
|
|
} elseif ( ! is_null( $mwNone->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
# remember to set an alignment, don't render immediately
|
|
|
|
|
$align = 'none';
|
|
|
|
|
} elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
# $match is the image width in pixels
|
|
|
|
|
if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
|
|
|
|
|
$width = intval( $m[1] );
|
|
|
|
|
$height = intval( $m[2] );
|
|
|
|
|
} else {
|
|
|
|
|
$width = intval($match);
|
|
|
|
|
}
|
|
|
|
|
} elseif ( ! is_null( $mwFramed->matchVariableStartToEnd($val) ) ) {
|
|
|
|
|
$framed=true;
|
|
|
|
|
} else {
|
|
|
|
|
$caption = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# Strip bad stuff out of the alt text
|
|
|
|
|
$alt = $caption;
|
|
|
|
|
$this->replaceLinkHolders( $alt );
|
|
|
|
|
$alt = Sanitizer::stripAllTags( $alt );
|
|
|
|
|
|
|
|
|
|
# Linker does the rest
|
|
|
|
|
$sk =& $this->mOptions->getSkin();
|
|
|
|
|
return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $width, $height, $framed, $thumb, $manual_thumb );
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-02-26 13:37:26 +00:00
|
|
|
class ParserOutput
|
|
|
|
|
{
|
|
|
|
|
var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
|
2004-05-30 07:31:26 +00:00
|
|
|
var $mCacheTime; # Used in ParserCache
|
2004-11-25 22:02:30 +00:00
|
|
|
var $mVersion; # Compatibility check
|
2005-04-07 23:04:08 +00:00
|
|
|
var $mTitleText; # title text of the chosen language variant
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
|
2005-04-07 23:04:08 +00:00
|
|
|
$containsOldMagic = false, $titletext = '' )
|
2004-02-26 13:37:26 +00:00
|
|
|
{
|
|
|
|
|
$this->mText = $text;
|
|
|
|
|
$this->mLanguageLinks = $languageLinks;
|
|
|
|
|
$this->mCategoryLinks = $categoryLinks;
|
|
|
|
|
$this->mContainsOldMagic = $containsOldMagic;
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->mCacheTime = '';
|
2004-11-25 22:02:30 +00:00
|
|
|
$this->mVersion = MW_PARSER_VERSION;
|
2005-04-07 23:04:08 +00:00
|
|
|
$this->mTitleText = $titletext;
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-11-20 11:28:37 +00:00
|
|
|
function getText() { return $this->mText; }
|
|
|
|
|
function getLanguageLinks() { return $this->mLanguageLinks; }
|
2004-11-25 22:02:30 +00:00
|
|
|
function getCategoryLinks() { return array_keys( $this->mCategoryLinks ); }
|
2004-11-20 11:28:37 +00:00
|
|
|
function getCacheTime() { return $this->mCacheTime; }
|
2005-04-07 23:04:08 +00:00
|
|
|
function getTitleText() { return $this->mTitleText; }
|
2004-11-20 11:28:37 +00:00
|
|
|
function containsOldMagic() { return $this->mContainsOldMagic; }
|
|
|
|
|
function setText( $text ) { return wfSetVar( $this->mText, $text ); }
|
|
|
|
|
function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
|
|
|
|
|
function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
|
2004-02-26 13:37:26 +00:00
|
|
|
function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
|
2004-11-20 11:28:37 +00:00
|
|
|
function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
|
2005-04-07 23:04:08 +00:00
|
|
|
function setTitleText( $t ) { return wfSetVar ($this->mTitleText, $t); }
|
|
|
|
|
|
2004-11-25 22:02:30 +00:00
|
|
|
function addCategoryLink( $c ) { $this->mCategoryLinks[$c] = 1; }
|
2004-03-20 15:03:26 +00:00
|
|
|
|
|
|
|
|
function merge( $other ) {
|
|
|
|
|
$this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
|
|
|
|
|
$this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
|
|
|
|
|
$this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
|
|
|
|
|
}
|
|
|
|
|
|
2004-11-25 22:02:30 +00:00
|
|
|
/**
|
|
|
|
|
* Return true if this cached output object predates the global or
|
|
|
|
|
* per-article cache invalidation timestamps, or if it comes from
|
|
|
|
|
* an incompatible older version.
|
|
|
|
|
*
|
|
|
|
|
* @param string $touched the affected article's last touched timestamp
|
|
|
|
|
* @return bool
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
function expired( $touched ) {
|
|
|
|
|
global $wgCacheEpoch;
|
|
|
|
|
return $this->getCacheTime() <= $touched ||
|
|
|
|
|
$this->getCacheTime() <= $wgCacheEpoch ||
|
|
|
|
|
!isset( $this->mVersion ) ||
|
|
|
|
|
version_compare( $this->mVersion, MW_PARSER_VERSION, "lt" );
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Set options of the Parser
|
|
|
|
|
* @todo document
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-02-29 08:43:29 +00:00
|
|
|
class ParserOptions
|
|
|
|
|
{
|
|
|
|
|
# All variables are private
|
|
|
|
|
var $mUseTeX; # Use texvc to expand <math> tags
|
2005-04-20 15:42:08 +00:00
|
|
|
var $mUseDynamicDates; # Use DateFormatter to format dates
|
2004-02-29 08:43:29 +00:00
|
|
|
var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
|
|
|
|
|
var $mAllowExternalImages; # Allow external images inline
|
|
|
|
|
var $mSkin; # Reference to the preferred skin
|
|
|
|
|
var $mDateFormat; # Date format index
|
|
|
|
|
var $mEditSection; # Create "edit section" links
|
|
|
|
|
var $mNumberHeadings; # Automatically number headings
|
|
|
|
|
|
2004-06-12 06:15:09 +00:00
|
|
|
function getUseTeX() { return $this->mUseTeX; }
|
|
|
|
|
function getUseDynamicDates() { return $this->mUseDynamicDates; }
|
|
|
|
|
function getInterwikiMagic() { return $this->mInterwikiMagic; }
|
|
|
|
|
function getAllowExternalImages() { return $this->mAllowExternalImages; }
|
|
|
|
|
function getSkin() { return $this->mSkin; }
|
|
|
|
|
function getDateFormat() { return $this->mDateFormat; }
|
|
|
|
|
function getEditSection() { return $this->mEditSection; }
|
|
|
|
|
function getNumberHeadings() { return $this->mNumberHeadings; }
|
|
|
|
|
|
|
|
|
|
function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
|
|
|
|
|
function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
|
|
|
|
|
function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
|
|
|
|
|
function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
|
|
|
|
|
function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
|
|
|
|
|
function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
|
|
|
|
|
function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
|
|
|
|
|
|
2004-08-16 15:23:46 +00:00
|
|
|
function setSkin( &$x ) { $this->mSkin =& $x; }
|
2004-06-12 06:15:09 +00:00
|
|
|
|
2005-01-31 22:59:55 +00:00
|
|
|
/**
|
|
|
|
|
* Get parser options
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function newFromUser( &$user ) {
|
2004-02-29 08:43:29 +00:00
|
|
|
$popts = new ParserOptions;
|
2004-04-17 11:20:15 +00:00
|
|
|
$popts->initialiseFromUser( $user );
|
2004-02-29 08:43:29 +00:00
|
|
|
return $popts;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-31 22:59:55 +00:00
|
|
|
/** Get user options */
|
2004-06-12 06:15:09 +00:00
|
|
|
function initialiseFromUser( &$userInput ) {
|
2004-08-27 06:15:13 +00:00
|
|
|
global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
|
2004-08-22 17:24:50 +00:00
|
|
|
$fname = 'ParserOptions::initialiseFromUser';
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileIn( $fname );
|
2004-02-29 08:43:29 +00:00
|
|
|
if ( !$userInput ) {
|
|
|
|
|
$user = new User;
|
2004-04-05 10:38:40 +00:00
|
|
|
$user->setLoaded( true );
|
2004-02-29 08:43:29 +00:00
|
|
|
} else {
|
|
|
|
|
$user =& $userInput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mUseTeX = $wgUseTeX;
|
|
|
|
|
$this->mUseDynamicDates = $wgUseDynamicDates;
|
|
|
|
|
$this->mInterwikiMagic = $wgInterwikiMagic;
|
|
|
|
|
$this->mAllowExternalImages = $wgAllowExternalImages;
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( $fname.'-skin' );
|
2004-02-29 08:43:29 +00:00
|
|
|
$this->mSkin =& $user->getSkin();
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( $fname.'-skin' );
|
2004-06-08 18:11:28 +00:00
|
|
|
$this->mDateFormat = $user->getOption( 'date' );
|
|
|
|
|
$this->mEditSection = $user->getOption( 'editsection' );
|
|
|
|
|
$this->mNumberHeadings = $user->getOption( 'numberheadings' );
|
2004-08-19 08:44:13 +00:00
|
|
|
wfProfileOut( $fname );
|
2004-02-29 08:43:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-02-26 13:37:26 +00:00
|
|
|
|
2004-10-15 17:39:10 +00:00
|
|
|
/**
|
|
|
|
|
* Callback function used by Parser::replaceLinkHolders()
|
|
|
|
|
* to substitute link placeholders.
|
|
|
|
|
*/
|
2005-04-27 07:48:14 +00:00
|
|
|
function &wfOutputReplaceMatches( $matches ) {
|
|
|
|
|
global $wgOutputReplace;
|
|
|
|
|
return $wgOutputReplace[$matches[1]];
|
2004-10-15 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Return the total number of articles
|
|
|
|
|
*/
|
2004-08-16 15:23:46 +00:00
|
|
|
function wfNumberOfArticles() {
|
2004-08-13 15:55:59 +00:00
|
|
|
global $wgNumberOfArticles;
|
|
|
|
|
|
|
|
|
|
wfLoadSiteStats();
|
|
|
|
|
return $wgNumberOfArticles;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Get various statistics from the database
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function wfLoadSiteStats() {
|
2004-08-13 15:55:59 +00:00
|
|
|
global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
|
|
|
|
|
$fname = 'wfLoadSiteStats';
|
|
|
|
|
|
|
|
|
|
if ( -1 != $wgNumberOfArticles ) return;
|
|
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
2004-10-24 07:10:33 +00:00
|
|
|
$s = $dbr->selectRow( 'site_stats',
|
2004-08-13 15:55:59 +00:00
|
|
|
array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
|
|
|
|
|
array( 'ss_row_id' => 1 ), $fname
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $s === false ) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
$wgTotalViews = $s->ss_total_views;
|
|
|
|
|
$wgTotalEdits = $s->ss_total_edits;
|
|
|
|
|
$wgNumberOfArticles = $s->ss_good_articles;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-31 22:59:55 +00:00
|
|
|
/**
|
|
|
|
|
* Escape html tags
|
|
|
|
|
* Basicly replacing " > and < with HTML entities ( ", >, <)
|
|
|
|
|
*
|
|
|
|
|
* @param string $in Text that might contain HTML tags
|
|
|
|
|
* @return string Escaped string
|
|
|
|
|
*/
|
2004-08-13 15:55:59 +00:00
|
|
|
function wfEscapeHTMLTagsOnly( $in ) {
|
|
|
|
|
return str_replace(
|
|
|
|
|
array( '"', '>', '<' ),
|
|
|
|
|
array( '"', '>', '<' ),
|
|
|
|
|
$in );
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-26 13:37:26 +00:00
|
|
|
?>
|