2011-01-01 03:39:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-31 20:35:35 +00:00
|
|
|
* Although marked as a stub, can work independently.
|
|
|
|
|
*
|
2011-01-01 03:39:37 +00:00
|
|
|
* @group Database
|
2011-02-26 21:49:42 +00:00
|
|
|
* @group Parser
|
2011-05-31 20:35:35 +00:00
|
|
|
* @group Stub
|
2013-10-22 23:50:38 +00:00
|
|
|
*
|
|
|
|
|
* @todo covers tags
|
2011-01-01 03:39:37 +00:00
|
|
|
*/
|
|
|
|
|
class NewParserTest extends MediaWikiTestCase {
|
2013-02-15 10:24:31 +00:00
|
|
|
static protected $articles = array(); // Array of test articles defined by the tests
|
2012-10-08 10:56:20 +00:00
|
|
|
/* The data provider is run on a different instance than the test, so it must be static
|
2011-02-26 21:49:42 +00:00
|
|
|
* When running tests from several files, all tests will see all articles.
|
|
|
|
|
*/
|
2012-01-28 01:20:42 +00:00
|
|
|
static protected $backendToUse;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
public $keepUploads = false;
|
|
|
|
|
public $runDisabled = false;
|
2013-02-23 04:26:08 +00:00
|
|
|
public $runParsoid = false;
|
2011-01-01 03:39:37 +00:00
|
|
|
public $regex = '';
|
|
|
|
|
public $showProgress = true;
|
2011-01-01 22:16:54 +00:00
|
|
|
public $savedWeirdGlobals = array();
|
2011-01-01 03:39:37 +00:00
|
|
|
public $savedGlobals = array();
|
|
|
|
|
public $hooks = array();
|
|
|
|
|
public $functionHooks = array();
|
2014-06-20 15:36:08 +00:00
|
|
|
public $transparentHooks = array();
|
2011-01-10 18:43:59 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
//Fuzz test
|
|
|
|
|
public $maxFuzzTestLength = 300;
|
|
|
|
|
public $fuzzSeed = 0;
|
|
|
|
|
public $memoryLimit = 50;
|
2011-01-10 18:43:59 +00:00
|
|
|
|
2013-12-18 17:56:15 +00:00
|
|
|
/**
|
|
|
|
|
* @var DjVuSupport
|
|
|
|
|
*/
|
|
|
|
|
private $djVuSupport;
|
|
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
protected $file = false;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2013-05-20 13:21:02 +00:00
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
|
// Inject ParserTest well-known interwikis
|
|
|
|
|
ParserTest::setupInterwikis();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2013-10-02 19:28:37 +00:00
|
|
|
global $wgNamespaceAliases, $wgContLang;
|
2011-02-26 21:49:42 +00:00
|
|
|
global $wgHooks, $IP;
|
2012-11-05 10:10:42 +00:00
|
|
|
|
2012-10-22 23:53:51 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
//Setup CLI arguments
|
2014-05-05 16:10:07 +00:00
|
|
|
if ( $this->getCliArg( 'regex' ) ) {
|
|
|
|
|
$this->regex = $this->getCliArg( 'regex' );
|
2011-01-01 05:53:04 +00:00
|
|
|
} else {
|
|
|
|
|
# Matches anything
|
|
|
|
|
$this->regex = '';
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 05:53:04 +00:00
|
|
|
$this->keepUploads = $this->getCliArg( 'keep-uploads' );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals = array();
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2013-02-18 17:58:35 +00:00
|
|
|
$tmpGlobals['wgLanguageCode'] = 'en';
|
|
|
|
|
$tmpGlobals['wgContLang'] = Language::factory( 'en' );
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgSitename'] = 'MediaWiki';
|
|
|
|
|
$tmpGlobals['wgServer'] = 'http://example.org';
|
2014-05-06 09:31:24 +00:00
|
|
|
$tmpGlobals['wgServerName'] = 'example.org';
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals['wgScript'] = '/index.php';
|
|
|
|
|
$tmpGlobals['wgScriptPath'] = '/';
|
|
|
|
|
$tmpGlobals['wgArticlePath'] = '/wiki/$1';
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgActionPaths'] = array();
|
|
|
|
|
$tmpGlobals['wgVariantArticlePath'] = false;
|
|
|
|
|
$tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals['wgStylePath'] = '/skins';
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgEnableUploads'] = true;
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals['wgThumbnailScriptPath'] = false;
|
|
|
|
|
$tmpGlobals['wgLocalFileRepo'] = array(
|
2013-02-15 10:24:31 +00:00
|
|
|
'class' => 'LocalRepo',
|
|
|
|
|
'name' => 'local',
|
|
|
|
|
'url' => 'http://example.com/images',
|
|
|
|
|
'hashLevels' => 2,
|
2011-01-01 03:39:37 +00:00
|
|
|
'transformVia404' => false,
|
2013-02-15 10:24:31 +00:00
|
|
|
'backend' => 'local-backend'
|
2011-01-01 03:39:37 +00:00
|
|
|
);
|
2011-12-20 03:52:06 +00:00
|
|
|
$tmpGlobals['wgForeignFileRepos'] = array();
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgDefaultExternalStore'] = array();
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals['wgEnableParserCache'] = false;
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgCapitalLinks'] = true;
|
|
|
|
|
$tmpGlobals['wgNoFollowLinks'] = true;
|
|
|
|
|
$tmpGlobals['wgNoFollowDomainExceptions'] = array();
|
|
|
|
|
$tmpGlobals['wgExternalLinkTarget'] = false;
|
|
|
|
|
$tmpGlobals['wgThumbnailScriptPath'] = false;
|
|
|
|
|
$tmpGlobals['wgUseImageResize'] = true;
|
|
|
|
|
$tmpGlobals['wgAllowExternalImages'] = true;
|
|
|
|
|
$tmpGlobals['wgRawHtml'] = false;
|
|
|
|
|
$tmpGlobals['wgUseTidy'] = false;
|
|
|
|
|
$tmpGlobals['wgAlwaysUseTidy'] = false;
|
|
|
|
|
$tmpGlobals['wgWellFormedXml'] = true;
|
|
|
|
|
$tmpGlobals['wgAllowMicrodataAttributes'] = true;
|
|
|
|
|
$tmpGlobals['wgExperimentalHtmlIds'] = false;
|
|
|
|
|
$tmpGlobals['wgAdaptiveMessageCache'] = true;
|
|
|
|
|
$tmpGlobals['wgUseDatabaseMessages'] = true;
|
|
|
|
|
$tmpGlobals['wgLocaltimezone'] = 'UTC';
|
2011-01-01 22:16:54 +00:00
|
|
|
$tmpGlobals['wgDeferredUpdateList'] = array();
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpGlobals['wgGroupPermissions'] = array(
|
|
|
|
|
'*' => array(
|
|
|
|
|
'createaccount' => true,
|
|
|
|
|
'read' => true,
|
|
|
|
|
'edit' => true,
|
|
|
|
|
'createpage' => true,
|
|
|
|
|
'createtalk' => true,
|
|
|
|
|
) );
|
|
|
|
|
$tmpGlobals['wgNamespaceProtection'] = array( NS_MEDIAWIKI => 'editinterface' );
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2013-07-16 18:06:04 +00:00
|
|
|
$tmpGlobals['wgParser'] = new StubObject(
|
|
|
|
|
'wgParser', $GLOBALS['wgParserConf']['class'],
|
|
|
|
|
array( $GLOBALS['wgParserConf'] ) );
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2013-05-24 12:56:06 +00:00
|
|
|
$tmpGlobals['wgFileExtensions'][] = 'svg';
|
|
|
|
|
$tmpGlobals['wgSVGConverter'] = 'rsvg';
|
2013-07-16 18:06:04 +00:00
|
|
|
$tmpGlobals['wgSVGConverters']['rsvg'] =
|
|
|
|
|
'$path/rsvg-convert -w $width -h $height $input -o $output';
|
2013-05-24 12:56:06 +00:00
|
|
|
|
2011-01-01 22:16:54 +00:00
|
|
|
if ( $GLOBALS['wgStyleDirectory'] === false ) {
|
|
|
|
|
$tmpGlobals['wgStyleDirectory'] = "$IP/skins";
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2013-04-28 21:13:49 +00:00
|
|
|
# Replace all media handlers with a mock. We do not need to generate
|
|
|
|
|
# actual thumbnails to do parser testing, we only care about receiving
|
|
|
|
|
# a ThumbnailImage properly initialized.
|
|
|
|
|
global $wgMediaHandlers;
|
2013-05-17 14:47:00 +00:00
|
|
|
foreach ( $wgMediaHandlers as $type => $handler ) {
|
2013-04-28 21:13:49 +00:00
|
|
|
$tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
|
|
|
|
|
}
|
2013-05-24 12:56:06 +00:00
|
|
|
// Vector images have to be handled slightly differently
|
|
|
|
|
$tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
|
2013-04-28 21:13:49 +00:00
|
|
|
|
2013-12-18 17:56:15 +00:00
|
|
|
// DjVu images have to be handled slightly differently
|
|
|
|
|
$tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
|
|
|
|
|
|
2013-04-20 20:15:20 +00:00
|
|
|
$tmpHooks = $wgHooks;
|
|
|
|
|
$tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
|
|
|
|
|
$tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
|
|
|
|
|
$tmpGlobals['wgHooks'] = $tmpHooks;
|
2013-10-02 19:28:37 +00:00
|
|
|
# add a namespace shadowing a interwiki link, to test
|
|
|
|
|
# proper precedence when resolving links. (bug 51680)
|
|
|
|
|
$tmpGlobals['wgExtraNamespaces'] = array( 100 => 'MemoryAlpha' );
|
2011-01-01 22:16:54 +00:00
|
|
|
|
2014-07-21 02:35:50 +00:00
|
|
|
$tmpGlobals['wgLocalInterwikis'] = array( 'local', 'mi' );
|
2014-06-26 21:50:46 +00:00
|
|
|
# "extra language links"
|
|
|
|
|
# see https://gerrit.wikimedia.org/r/111390
|
|
|
|
|
$tmpGlobals['wgExtraInterlanguageLinkPrefixes'] = array( 'mul' );
|
|
|
|
|
|
2013-12-18 17:56:15 +00:00
|
|
|
//DjVu support
|
|
|
|
|
$this->djVuSupport = new DjVuSupport();
|
|
|
|
|
|
2013-04-20 20:15:20 +00:00
|
|
|
$this->setMwGlobals( $tmpGlobals );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 22:16:54 +00:00
|
|
|
$this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
|
|
|
|
|
$this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 22:16:54 +00:00
|
|
|
$wgNamespaceAliases['Image'] = NS_FILE;
|
|
|
|
|
$wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
|
2013-10-02 19:28:37 +00:00
|
|
|
|
|
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function tearDown() {
|
2013-10-02 19:28:37 +00:00
|
|
|
global $wgNamespaceAliases, $wgContLang;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 22:16:54 +00:00
|
|
|
$wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
|
|
|
|
|
$wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
// Restore backends
|
|
|
|
|
RepoGroup::destroySingleton();
|
2012-01-07 01:33:23 +00:00
|
|
|
FileBackendGroup::destroySingleton();
|
2012-10-22 23:53:51 +00:00
|
|
|
|
2013-05-04 12:21:39 +00:00
|
|
|
// Remove temporary pages from the link cache
|
|
|
|
|
LinkCache::singleton()->clear();
|
|
|
|
|
|
|
|
|
|
// Restore message cache (temporary pages and $wgUseDatabaseMessages)
|
|
|
|
|
MessageCache::destroyInstance();
|
|
|
|
|
|
2012-10-22 23:53:51 +00:00
|
|
|
parent::tearDown();
|
2013-10-02 19:28:37 +00:00
|
|
|
|
|
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
2011-01-01 20:57:13 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2013-08-22 18:38:50 +00:00
|
|
|
public static function tearDownAfterClass() {
|
|
|
|
|
ParserTest::tearDownInterwikis();
|
|
|
|
|
parent::tearDownAfterClass();
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
function addDBData() {
|
2012-01-25 23:11:53 +00:00
|
|
|
$this->tablesUsed[] = 'site_stats';
|
|
|
|
|
# disabled for performance
|
|
|
|
|
#$this->tablesUsed[] = 'image';
|
|
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
# Update certain things in site_stats
|
2011-07-25 22:01:19 +00:00
|
|
|
$this->db->insert( 'site_stats',
|
2011-05-25 00:49:51 +00:00
|
|
|
array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
|
2012-01-25 23:11:53 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2011-01-01 20:57:13 +00:00
|
|
|
|
|
|
|
|
$user = User::newFromId( 0 );
|
2011-02-26 21:49:42 +00:00
|
|
|
LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
# Upload DB table entries for files.
|
|
|
|
|
# We will upload the actual files later. Note that if anything causes LocalFile::load()
|
|
|
|
|
# to be triggered before then, it will break via maybeUpgrade() setting the fileExists
|
|
|
|
|
# member to false and storing it in cache.
|
2013-02-27 00:12:12 +00:00
|
|
|
# note that the size/width/height/bits/etc of the file
|
|
|
|
|
# are actually set by inspecting the file itself; the arguments
|
|
|
|
|
# to recordUpload2 have no effect. That said, we try to make things
|
|
|
|
|
# match up so it is less confusing to readers of the code & tests.
|
2011-01-01 20:57:13 +00:00
|
|
|
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
|
2012-01-25 23:11:53 +00:00
|
|
|
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
|
|
|
|
|
$image->recordUpload2(
|
|
|
|
|
'', // archive name
|
2012-08-16 08:00:45 +00:00
|
|
|
'Upload of some lame file',
|
2012-01-25 23:11:53 +00:00
|
|
|
'Some lame file',
|
|
|
|
|
array(
|
2013-02-27 00:12:12 +00:00
|
|
|
'size' => 7881,
|
2013-02-15 10:24:31 +00:00
|
|
|
'width' => 1941,
|
|
|
|
|
'height' => 220,
|
2013-02-27 00:12:12 +00:00
|
|
|
'bits' => 8,
|
2013-02-15 10:24:31 +00:00
|
|
|
'media_type' => MEDIATYPE_BITMAP,
|
|
|
|
|
'mime' => 'image/jpeg',
|
|
|
|
|
'metadata' => serialize( array() ),
|
2013-02-27 00:12:12 +00:00
|
|
|
'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
|
2013-02-15 10:24:31 +00:00
|
|
|
'fileExists' => true ),
|
2012-01-25 23:11:53 +00:00
|
|
|
$this->db->timestamp( '20010115123500' ), $user
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-01-01 20:57:13 +00:00
|
|
|
|
2013-02-27 00:12:12 +00:00
|
|
|
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
|
|
|
|
|
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
|
|
|
|
|
$image->recordUpload2(
|
|
|
|
|
'', // archive name
|
|
|
|
|
'Upload of some lame thumbnail',
|
|
|
|
|
'Some lame thumbnail',
|
|
|
|
|
array(
|
|
|
|
|
'size' => 22589,
|
|
|
|
|
'width' => 135,
|
|
|
|
|
'height' => 135,
|
|
|
|
|
'bits' => 8,
|
|
|
|
|
'media_type' => MEDIATYPE_BITMAP,
|
|
|
|
|
'mime' => 'image/png',
|
|
|
|
|
'metadata' => serialize( array() ),
|
|
|
|
|
'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
|
|
|
|
|
'fileExists' => true ),
|
|
|
|
|
$this->db->timestamp( '20130225203040' ), $user
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
# This image will be blacklisted in [[MediaWiki:Bad image list]]
|
|
|
|
|
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
|
2012-01-25 23:11:53 +00:00
|
|
|
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
|
|
|
|
|
$image->recordUpload2(
|
|
|
|
|
'', // archive name
|
2012-08-16 08:00:45 +00:00
|
|
|
'zomgnotcensored',
|
|
|
|
|
'Borderline image',
|
2012-01-25 23:11:53 +00:00
|
|
|
array(
|
2013-02-15 10:24:31 +00:00
|
|
|
'size' => 12345,
|
|
|
|
|
'width' => 320,
|
|
|
|
|
'height' => 240,
|
|
|
|
|
'bits' => 24,
|
|
|
|
|
'media_type' => MEDIATYPE_BITMAP,
|
|
|
|
|
'mime' => 'image/jpeg',
|
|
|
|
|
'metadata' => serialize( array() ),
|
2013-02-27 00:12:12 +00:00
|
|
|
'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
|
2013-02-15 10:24:31 +00:00
|
|
|
'fileExists' => true ),
|
2012-01-25 23:11:53 +00:00
|
|
|
$this->db->timestamp( '20010115123500' ), $user
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-05-24 12:56:06 +00:00
|
|
|
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
|
|
|
|
|
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
|
|
|
|
|
$image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', array(
|
|
|
|
|
'size' => 12345,
|
2014-03-18 17:00:11 +00:00
|
|
|
'width' => 240,
|
|
|
|
|
'height' => 180,
|
2014-06-03 19:44:53 +00:00
|
|
|
'bits' => 0,
|
2013-05-24 12:56:06 +00:00
|
|
|
'media_type' => MEDIATYPE_DRAWING,
|
|
|
|
|
'mime' => 'image/svg+xml',
|
|
|
|
|
'metadata' => serialize( array() ),
|
|
|
|
|
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
|
|
|
|
|
'fileExists' => true
|
|
|
|
|
), $this->db->timestamp( '20010115123500' ), $user );
|
|
|
|
|
}
|
2013-12-18 17:56:15 +00:00
|
|
|
|
|
|
|
|
# A DjVu file
|
|
|
|
|
$image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
|
|
|
|
|
if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
|
|
|
|
|
$image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', array(
|
|
|
|
|
'size' => 3249,
|
|
|
|
|
'width' => 2480,
|
|
|
|
|
'height' => 3508,
|
2014-06-03 19:44:53 +00:00
|
|
|
'bits' => 0,
|
2013-12-18 17:56:15 +00:00
|
|
|
'media_type' => MEDIATYPE_BITMAP,
|
|
|
|
|
'mime' => 'image/vnd.djvu',
|
|
|
|
|
'metadata' => '<?xml version="1.0" ?>
|
|
|
|
|
<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
|
|
|
|
|
<DjVuXML>
|
|
|
|
|
<HEAD></HEAD>
|
|
|
|
|
<BODY><OBJECT height="3508" width="2480">
|
|
|
|
|
<PARAM name="DPI" value="300" />
|
|
|
|
|
<PARAM name="GAMMA" value="2.2" />
|
|
|
|
|
</OBJECT>
|
|
|
|
|
<OBJECT height="3508" width="2480">
|
|
|
|
|
<PARAM name="DPI" value="300" />
|
|
|
|
|
<PARAM name="GAMMA" value="2.2" />
|
|
|
|
|
</OBJECT>
|
|
|
|
|
<OBJECT height="3508" width="2480">
|
|
|
|
|
<PARAM name="DPI" value="300" />
|
|
|
|
|
<PARAM name="GAMMA" value="2.2" />
|
|
|
|
|
</OBJECT>
|
|
|
|
|
<OBJECT height="3508" width="2480">
|
|
|
|
|
<PARAM name="DPI" value="300" />
|
|
|
|
|
<PARAM name="GAMMA" value="2.2" />
|
|
|
|
|
</OBJECT>
|
|
|
|
|
<OBJECT height="3508" width="2480">
|
|
|
|
|
<PARAM name="DPI" value="300" />
|
|
|
|
|
<PARAM name="GAMMA" value="2.2" />
|
|
|
|
|
</OBJECT>
|
|
|
|
|
</BODY>
|
|
|
|
|
</DjVuXML>',
|
|
|
|
|
'sha1' => wfBaseConvert( '', 16, 36, 31 ),
|
|
|
|
|
'fileExists' => true
|
|
|
|
|
), $this->db->timestamp( '20140115123600' ), $user );
|
|
|
|
|
}
|
2011-01-01 20:57:13 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
//ParserTest setup/teardown functions
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
/**
|
|
|
|
|
* Set up the global variables for a consistent environment for each test.
|
|
|
|
|
* Ideally this should replace the global configuration entirely.
|
|
|
|
|
*/
|
2013-02-18 17:58:35 +00:00
|
|
|
protected function setupGlobals( $opts = array(), $config = '' ) {
|
2012-01-28 01:20:42 +00:00
|
|
|
global $wgFileBackends;
|
2011-01-01 03:39:37 +00:00
|
|
|
# Find out values for some special options.
|
|
|
|
|
$lang =
|
|
|
|
|
self::getOptionValue( 'language', $opts, 'en' );
|
|
|
|
|
$variant =
|
|
|
|
|
self::getOptionValue( 'variant', $opts, false );
|
|
|
|
|
$maxtoclevel =
|
|
|
|
|
self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
|
|
|
|
|
$linkHolderBatchSize =
|
|
|
|
|
self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
|
|
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
$uploadDir = $this->getUploadDir();
|
2014-05-05 16:10:07 +00:00
|
|
|
if ( $this->getCliArg( 'use-filebackend' ) ) {
|
2012-01-28 01:20:42 +00:00
|
|
|
if ( self::$backendToUse ) {
|
|
|
|
|
$backend = self::$backendToUse;
|
|
|
|
|
} else {
|
2014-05-05 16:10:07 +00:00
|
|
|
$name = $this->getCliArg( 'use-filebackend' );
|
2012-01-28 01:20:42 +00:00
|
|
|
$useConfig = array();
|
|
|
|
|
foreach ( $wgFileBackends as $conf ) {
|
|
|
|
|
if ( $conf['name'] == $name ) {
|
|
|
|
|
$useConfig = $conf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$useConfig['name'] = 'local-backend'; // swap name
|
2013-12-03 20:55:44 +00:00
|
|
|
unset( $useConfig['lockManager'] );
|
|
|
|
|
unset( $useConfig['fileJournal'] );
|
2013-12-19 01:41:02 +00:00
|
|
|
$class = $useConfig['class'];
|
2012-01-28 01:20:42 +00:00
|
|
|
self::$backendToUse = new $class( $useConfig );
|
|
|
|
|
$backend = self::$backendToUse;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2013-04-28 21:13:49 +00:00
|
|
|
# Replace with a mock. We do not care about generating real
|
|
|
|
|
# files on the filesystem, just need to expose the file
|
|
|
|
|
# informations.
|
|
|
|
|
$backend = new MockFileBackend( array(
|
2013-02-15 10:24:31 +00:00
|
|
|
'name' => 'local-backend',
|
2013-12-19 01:41:02 +00:00
|
|
|
'wikiId' => wfWikiId()
|
2012-01-28 01:20:42 +00:00
|
|
|
) );
|
|
|
|
|
}
|
2012-01-25 23:11:53 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
$settings = array(
|
|
|
|
|
'wgLocalFileRepo' => array(
|
2013-02-15 10:24:31 +00:00
|
|
|
'class' => 'LocalRepo',
|
|
|
|
|
'name' => 'local',
|
|
|
|
|
'url' => 'http://example.com/images',
|
|
|
|
|
'hashLevels' => 2,
|
2011-01-01 03:39:37 +00:00
|
|
|
'transformVia404' => false,
|
2013-02-15 10:24:31 +00:00
|
|
|
'backend' => $backend
|
2011-01-01 03:39:37 +00:00
|
|
|
),
|
|
|
|
|
'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
|
|
|
|
|
'wgLanguageCode' => $lang,
|
|
|
|
|
'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
|
2013-07-10 22:10:14 +00:00
|
|
|
'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
|
2013-02-18 17:58:35 +00:00
|
|
|
'wgNamespacesWithSubpages' => array( NS_MAIN => isset( $opts['subpage'] ) ),
|
2013-07-10 22:10:14 +00:00
|
|
|
'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
|
2014-03-19 18:51:21 +00:00
|
|
|
'wgThumbLimits' => array( self::getOptionValue( 'thumbsize', $opts, 180 ) ),
|
2011-01-01 03:39:37 +00:00
|
|
|
'wgMaxTocLevel' => $maxtoclevel,
|
2013-06-21 15:39:29 +00:00
|
|
|
'wgUseTeX' => isset( $opts['math'] ) || isset( $opts['texvc'] ),
|
2012-01-25 23:11:53 +00:00
|
|
|
'wgMathDirectory' => $uploadDir . '/math',
|
2011-01-01 03:39:37 +00:00
|
|
|
'wgDefaultLanguageVariant' => $variant,
|
|
|
|
|
'wgLinkHolderBatchSize' => $linkHolderBatchSize,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $config ) {
|
|
|
|
|
$configLines = explode( "\n", $config );
|
|
|
|
|
|
|
|
|
|
foreach ( $configLines as $line ) {
|
|
|
|
|
list( $var, $value ) = explode( '=', $line, 2 );
|
|
|
|
|
|
|
|
|
|
$settings[$var] = eval( "return $value;" ); //???
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->savedGlobals = array();
|
|
|
|
|
|
2012-08-13 22:18:50 +00:00
|
|
|
/** @since 1.20 */
|
|
|
|
|
wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
|
|
|
|
|
|
2013-04-20 20:15:20 +00:00
|
|
|
$langObj = Language::factory( $lang );
|
|
|
|
|
$settings['wgContLang'] = $langObj;
|
|
|
|
|
$settings['wgLang'] = $langObj;
|
|
|
|
|
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
$settings['wgOut'] = $context->getOutput();
|
|
|
|
|
$settings['wgUser'] = $context->getUser();
|
|
|
|
|
$settings['wgRequest'] = $context->getRequest();
|
|
|
|
|
|
2014-03-19 18:51:21 +00:00
|
|
|
// We (re)set $wgThumbLimits to a single-element array above.
|
|
|
|
|
$context->getUser()->setOption( 'thumbsize', 0 );
|
|
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
foreach ( $settings as $var => $val ) {
|
|
|
|
|
if ( array_key_exists( $var, $GLOBALS ) ) {
|
|
|
|
|
$this->savedGlobals[$var] = $GLOBALS[$var];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$GLOBALS[$var] = $val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MagicWord::clearCache();
|
2013-05-04 12:21:39 +00:00
|
|
|
|
|
|
|
|
# The entries saved into RepoGroup cache with previous globals will be wrong.
|
2011-12-20 03:52:06 +00:00
|
|
|
RepoGroup::destroySingleton();
|
2012-01-07 01:33:23 +00:00
|
|
|
FileBackendGroup::destroySingleton();
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
# Create dummy files in storage
|
|
|
|
|
$this->setupUploads();
|
|
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
# Publish the articles after we have the final language set
|
|
|
|
|
$this->publishTestArticles();
|
|
|
|
|
|
2012-08-16 08:00:45 +00:00
|
|
|
MessageCache::destroyInstance();
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-11-10 12:49:10 +00:00
|
|
|
return $context;
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
/**
|
2012-01-25 23:11:53 +00:00
|
|
|
* Get an FS upload directory (only applies to FSFileBackend)
|
2011-01-01 20:57:13 +00:00
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @return string The directory
|
2011-01-01 20:57:13 +00:00
|
|
|
*/
|
2012-01-25 23:11:53 +00:00
|
|
|
protected function getUploadDir() {
|
2011-01-01 20:57:13 +00:00
|
|
|
if ( $this->keepUploads ) {
|
|
|
|
|
$dir = wfTempDir() . '/mwParser-images';
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
return $dir;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
|
|
|
|
|
}
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
// wfDebug( "Creating upload directory $dir\n" );
|
|
|
|
|
if ( file_exists( $dir ) ) {
|
|
|
|
|
wfDebug( "Already exists!\n" );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
return $dir;
|
|
|
|
|
}
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
return $dir;
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
/**
|
|
|
|
|
* Create a dummy uploads directory which will contain a couple
|
|
|
|
|
* of files in order to pass existence tests.
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @return string The directory
|
2012-01-25 23:11:53 +00:00
|
|
|
*/
|
|
|
|
|
protected function setupUploads() {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$base = $this->getBaseDir();
|
|
|
|
|
$backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
|
|
|
|
|
$backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
|
|
|
|
|
$backend->store( array(
|
2014-07-24 17:03:17 +00:00
|
|
|
'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
|
|
|
|
|
'dst' => "$base/local-public/3/3a/Foobar.jpg"
|
2012-01-25 23:11:53 +00:00
|
|
|
) );
|
2013-02-27 00:12:12 +00:00
|
|
|
$backend->prepare( array( 'dir' => "$base/local-public/e/ea" ) );
|
|
|
|
|
$backend->store( array(
|
2014-07-24 17:03:17 +00:00
|
|
|
'src' => "$IP/tests/phpunit/data/parser/wiki.png",
|
|
|
|
|
'dst' => "$base/local-public/e/ea/Thumb.png"
|
2013-02-27 00:12:12 +00:00
|
|
|
) );
|
2012-01-25 23:11:53 +00:00
|
|
|
$backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
|
|
|
|
|
$backend->store( array(
|
2014-07-24 17:03:17 +00:00
|
|
|
'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
|
|
|
|
|
'dst' => "$base/local-public/0/09/Bad.jpg"
|
2012-01-25 23:11:53 +00:00
|
|
|
) );
|
2013-12-18 17:56:15 +00:00
|
|
|
$backend->prepare( array( 'dir' => "$base/local-public/5/5f" ) );
|
|
|
|
|
$backend->store( array(
|
2014-07-24 17:03:17 +00:00
|
|
|
'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
|
|
|
|
|
'dst' => "$base/local-public/5/5f/LoremIpsum.djvu"
|
2013-12-18 17:56:15 +00:00
|
|
|
) );
|
2013-05-24 12:56:06 +00:00
|
|
|
|
|
|
|
|
// No helpful SVG file to copy, so make one ourselves
|
2013-12-19 01:41:02 +00:00
|
|
|
$data = '<?xml version="1.0" encoding="utf-8"?>' .
|
2014-03-18 17:00:11 +00:00
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg"' .
|
|
|
|
|
' version="1.1" width="240" height="180"/>';
|
2013-05-24 12:56:06 +00:00
|
|
|
|
|
|
|
|
$backend->prepare( array( 'dir' => "$base/local-public/f/ff" ) );
|
2013-12-19 01:41:02 +00:00
|
|
|
$backend->quickCreate( array(
|
|
|
|
|
'content' => $data, 'dst' => "$base/local-public/f/ff/Foobar.svg"
|
2013-05-24 12:56:06 +00:00
|
|
|
) );
|
2012-01-25 23:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-16 21:07:37 +00:00
|
|
|
/**
|
|
|
|
|
* Restore default values and perform any necessary clean-up
|
|
|
|
|
* after each test runs.
|
|
|
|
|
*/
|
|
|
|
|
protected function teardownGlobals() {
|
2012-01-25 23:11:53 +00:00
|
|
|
$this->teardownUploads();
|
|
|
|
|
|
2011-06-16 21:07:37 +00:00
|
|
|
foreach ( $this->savedGlobals as $var => $val ) {
|
|
|
|
|
$GLOBALS[$var] = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the dummy uploads directory
|
|
|
|
|
*/
|
2012-01-25 23:11:53 +00:00
|
|
|
private function teardownUploads() {
|
2011-06-16 21:07:37 +00:00
|
|
|
if ( $this->keepUploads ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 21:13:49 +00:00
|
|
|
$backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
|
2013-05-17 14:47:00 +00:00
|
|
|
if ( $backend instanceof MockFileBackend ) {
|
2013-04-28 21:13:49 +00:00
|
|
|
# In memory backend, so dont bother cleaning them up.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
$base = $this->getBaseDir();
|
2011-06-16 21:07:37 +00:00
|
|
|
// delete the files first, then the dirs.
|
|
|
|
|
self::deleteFiles(
|
2013-02-15 10:24:31 +00:00
|
|
|
array(
|
2012-01-25 23:11:53 +00:00
|
|
|
"$base/local-public/3/3a/Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
|
2012-01-25 23:11:53 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
|
|
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
|
2012-11-16 22:28:39 +00:00
|
|
|
"$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
|
2011-06-16 21:07:37 +00:00
|
|
|
|
2013-02-27 00:12:12 +00:00
|
|
|
"$base/local-public/e/ea/Thumb.png",
|
|
|
|
|
|
2012-01-27 22:57:14 +00:00
|
|
|
"$base/local-public/0/09/Bad.jpg",
|
2011-06-16 21:07:37 +00:00
|
|
|
|
2013-12-18 17:56:15 +00:00
|
|
|
"$base/local-public/5/5f/LoremIpsum.djvu",
|
|
|
|
|
"$base/local-thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg",
|
|
|
|
|
"$base/local-thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg",
|
|
|
|
|
"$base/local-thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg",
|
|
|
|
|
|
2013-05-24 12:56:06 +00:00
|
|
|
"$base/local-public/f/ff/Foobar.svg",
|
2014-03-25 17:13:50 +00:00
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
|
|
|
|
|
"$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
|
2013-05-24 12:56:06 +00:00
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
"$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
|
2011-06-16 21:07:37 +00:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the specified files, if they exist.
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param array $files Full paths to files to delete.
|
2011-06-16 21:07:37 +00:00
|
|
|
*/
|
|
|
|
|
private static function deleteFiles( $files ) {
|
2012-01-25 23:11:53 +00:00
|
|
|
$backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
|
|
|
|
|
foreach ( $files as $file ) {
|
|
|
|
|
$backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
|
|
|
|
|
}
|
2011-06-16 21:07:37 +00:00
|
|
|
foreach ( $files as $file ) {
|
2012-01-25 23:11:53 +00:00
|
|
|
$tmp = $file;
|
|
|
|
|
while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
|
2012-01-27 22:57:14 +00:00
|
|
|
if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-06-16 21:07:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 23:11:53 +00:00
|
|
|
protected function getBaseDir() {
|
|
|
|
|
return 'mwstore://local-backend';
|
2011-06-16 21:07:37 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
public function parserTestProvider() {
|
|
|
|
|
if ( $this->file === false ) {
|
|
|
|
|
global $wgParserTestFiles;
|
|
|
|
|
$this->file = $wgParserTestFiles[0];
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-02-26 23:45:35 +00:00
|
|
|
return new TestFileIterator( $this->file, $this );
|
2011-02-26 21:49:42 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
/**
|
|
|
|
|
* Set the file from whose tests will be run by this instance
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $filename
|
2011-02-26 21:49:42 +00:00
|
|
|
*/
|
|
|
|
|
public function setParserTestFile( $filename ) {
|
|
|
|
|
$this->file = $filename;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2012-04-20 17:34:15 +00:00
|
|
|
/**
|
|
|
|
|
* @group medium
|
|
|
|
|
* @dataProvider parserTestProvider
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $desc
|
|
|
|
|
* @param string $input
|
|
|
|
|
* @param string $result
|
|
|
|
|
* @param array $opts
|
|
|
|
|
* @param array $config
|
2012-04-20 17:34:15 +00:00
|
|
|
*/
|
2011-02-26 21:49:42 +00:00
|
|
|
public function testParserTest( $desc, $input, $result, $opts, $config ) {
|
2012-01-25 23:11:53 +00:00
|
|
|
if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
|
|
|
|
|
$this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
|
|
|
|
|
//$this->markTestSkipped( 'Filtered out by the user' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-03-18 21:45:32 +00:00
|
|
|
|
2012-10-12 13:39:01 +00:00
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
|
|
|
|
// parser tests frequently assume that the main namespace contains wikitext.
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo When setting up pages, force the content model. Only skip if
|
2012-10-12 13:39:01 +00:00
|
|
|
// $wgtContentModelUseDB is false.
|
|
|
|
|
$this->markTestSkipped( "Main namespace does not support wikitext,"
|
2013-02-15 10:24:31 +00:00
|
|
|
. "skipping parser test: $desc" );
|
2012-10-12 13:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-18 21:45:32 +00:00
|
|
|
wfDebug( "Running parser test: $desc\n" );
|
|
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
$opts = $this->parseOptions( $opts );
|
2011-11-10 12:49:10 +00:00
|
|
|
$context = $this->setupGlobals( $opts, $config );
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2011-11-10 12:49:10 +00:00
|
|
|
$user = $context->getUser();
|
|
|
|
|
$options = ParserOptions::newFromContext( $context );
|
2011-02-26 21:49:42 +00:00
|
|
|
|
|
|
|
|
if ( isset( $opts['title'] ) ) {
|
|
|
|
|
$titleText = $opts['title'];
|
2013-02-15 10:24:31 +00:00
|
|
|
} else {
|
2011-02-26 21:49:42 +00:00
|
|
|
$titleText = 'Parser test';
|
2011-01-01 05:53:04 +00:00
|
|
|
}
|
2011-02-26 21:49:42 +00:00
|
|
|
|
|
|
|
|
$local = isset( $opts['local'] );
|
|
|
|
|
$preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
|
|
|
|
|
$parser = $this->getParser( $preprocessor );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
$title = Title::newFromText( $titleText );
|
|
|
|
|
|
2013-06-21 15:39:29 +00:00
|
|
|
# Parser test requiring math. Make sure texvc is executable
|
|
|
|
|
# or just skip such tests.
|
|
|
|
|
if ( isset( $opts['math'] ) || isset( $opts['texvc'] ) ) {
|
|
|
|
|
global $wgTexvc;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $wgTexvc ) ) {
|
|
|
|
|
$this->markTestSkipped( "SKIPPED: \$wgTexvc is not set" );
|
|
|
|
|
} elseif ( !is_executable( $wgTexvc ) ) {
|
|
|
|
|
$this->markTestSkipped( "SKIPPED: texvc binary does not exist"
|
|
|
|
|
. " or is not executable.\n"
|
|
|
|
|
. "Current configuration is:\n\$wgTexvc = '$wgTexvc'" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-18 17:56:15 +00:00
|
|
|
if ( isset( $opts['djvu'] ) ) {
|
|
|
|
|
if ( !$this->djVuSupport->isEnabled() ) {
|
|
|
|
|
$this->markTestSkipped( "SKIPPED: djvu binaries do not exist or are not executable.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-21 15:39:29 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
if ( isset( $opts['pst'] ) ) {
|
|
|
|
|
$out = $parser->preSaveTransform( $input, $title, $user, $options );
|
|
|
|
|
} elseif ( isset( $opts['msg'] ) ) {
|
2011-06-29 10:58:54 +00:00
|
|
|
$out = $parser->transformMsg( $input, $options, $title );
|
2011-02-26 21:49:42 +00:00
|
|
|
} elseif ( isset( $opts['section'] ) ) {
|
|
|
|
|
$section = $opts['section'];
|
|
|
|
|
$out = $parser->getSection( $input, $section );
|
|
|
|
|
} elseif ( isset( $opts['replace'] ) ) {
|
|
|
|
|
$section = $opts['replace'][0];
|
|
|
|
|
$replace = $opts['replace'][1];
|
|
|
|
|
$out = $parser->replaceSection( $input, $section, $replace );
|
|
|
|
|
} elseif ( isset( $opts['comment'] ) ) {
|
2011-08-05 15:17:19 +00:00
|
|
|
$out = Linker::formatComment( $input, $title, $local );
|
2011-02-26 21:49:42 +00:00
|
|
|
} elseif ( isset( $opts['preload'] ) ) {
|
2013-03-22 16:44:34 +00:00
|
|
|
$out = $parser->getPreloadText( $input, $title, $options );
|
2011-02-26 21:49:42 +00:00
|
|
|
} else {
|
|
|
|
|
$output = $parser->parse( $input, $title, $options, true, true, 1337 );
|
2013-08-22 22:22:03 +00:00
|
|
|
$output->setTOCEnabled( !isset( $opts['notoc'] ) );
|
2011-02-26 21:49:42 +00:00
|
|
|
$out = $output->getText();
|
|
|
|
|
|
|
|
|
|
if ( isset( $opts['showtitle'] ) ) {
|
|
|
|
|
if ( $output->getTitleText() ) {
|
|
|
|
|
$title = $output->getTitleText();
|
2011-01-01 20:57:13 +00:00
|
|
|
}
|
2011-02-26 21:49:42 +00:00
|
|
|
|
|
|
|
|
$out = "$title\n$out";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $opts['ill'] ) ) {
|
|
|
|
|
$out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
|
|
|
|
|
} elseif ( isset( $opts['cat'] ) ) {
|
2011-11-10 12:49:10 +00:00
|
|
|
$outputPage = $context->getOutput();
|
|
|
|
|
$outputPage->addCategoryLinks( $output->getCategories() );
|
|
|
|
|
$cats = $outputPage->getCategoryLinks();
|
2011-02-26 21:49:42 +00:00
|
|
|
|
|
|
|
|
if ( isset( $cats['normal'] ) ) {
|
|
|
|
|
$out = $this->tidy( implode( ' ', $cats['normal'] ) );
|
|
|
|
|
} else {
|
|
|
|
|
$out = '';
|
2011-01-01 17:05:08 +00:00
|
|
|
}
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-02-26 21:49:42 +00:00
|
|
|
$parser->mPreprocessor = null;
|
|
|
|
|
|
|
|
|
|
$result = $this->tidy( $result );
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-02-26 21:49:42 +00:00
|
|
|
|
|
|
|
|
$this->teardownGlobals();
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
$this->assertEquals( $result, $out, $desc );
|
2011-01-01 18:10:28 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
/**
|
|
|
|
|
* Run a fuzz test series
|
|
|
|
|
* Draw input from a set of test files
|
2011-08-09 16:13:56 +00:00
|
|
|
*
|
2012-07-10 15:16:46 +00:00
|
|
|
* @todo fixme Needs some work to not eat memory until the world explodes
|
2011-08-09 16:13:56 +00:00
|
|
|
*
|
2012-01-10 20:50:34 +00:00
|
|
|
* @group ParserFuzz
|
2011-01-01 18:10:28 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testFuzzTests() {
|
2011-01-01 18:10:28 +00:00
|
|
|
global $wgParserTestFiles;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
$files = $wgParserTestFiles;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2014-05-05 16:10:07 +00:00
|
|
|
if ( $this->getCliArg( 'file' ) ) {
|
|
|
|
|
$files = array( $this->getCliArg( 'file' ) );
|
2011-01-01 18:10:28 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
$dict = $this->getFuzzInput( $files );
|
|
|
|
|
$dictSize = strlen( $dict );
|
|
|
|
|
$logMaxLength = log( $this->maxFuzzTestLength );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-07-21 22:04:55 +00:00
|
|
|
ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
|
2011-01-01 18:10:28 +00:00
|
|
|
|
|
|
|
|
$user = new User;
|
|
|
|
|
$opts = ParserOptions::newFromUser( $user );
|
|
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Parser_test' );
|
|
|
|
|
|
|
|
|
|
$id = 1;
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
while ( true ) {
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
// Generate test input
|
|
|
|
|
mt_srand( ++$this->fuzzSeed );
|
|
|
|
|
$totalLength = mt_rand( 1, $this->maxFuzzTestLength );
|
|
|
|
|
$input = '';
|
|
|
|
|
|
|
|
|
|
while ( strlen( $input ) < $totalLength ) {
|
|
|
|
|
$logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
|
|
|
|
|
$hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
|
|
|
|
|
$offset = mt_rand( 0, $dictSize - $hairLength );
|
|
|
|
|
$input .= substr( $dict, $offset, $hairLength );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setupGlobals();
|
|
|
|
|
$parser = $this->getParser();
|
|
|
|
|
|
|
|
|
|
// Run the test
|
|
|
|
|
try {
|
|
|
|
|
$parser->parse( $input, $title, $opts );
|
|
|
|
|
$this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
|
|
|
|
|
} catch ( Exception $exception ) {
|
2011-02-09 17:21:39 +00:00
|
|
|
$input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2013-07-16 18:06:04 +00:00
|
|
|
$this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\n" .
|
|
|
|
|
"Input: $input_dump\n\nError: {$exception->getMessage()}\n\n" .
|
|
|
|
|
"Backtrace: {$exception->getTraceAsString()}" );
|
2011-01-01 18:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->teardownGlobals();
|
|
|
|
|
$parser->__destruct();
|
|
|
|
|
|
|
|
|
|
if ( $id % 100 == 0 ) {
|
|
|
|
|
$usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
|
|
|
|
|
//echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
|
|
|
|
|
if ( $usage > 90 ) {
|
|
|
|
|
$ret = "Out of memory:\n";
|
|
|
|
|
$memStats = $this->getMemoryBreakdown();
|
|
|
|
|
|
|
|
|
|
foreach ( $memStats as $name => $usage ) {
|
|
|
|
|
$ret .= "$name: $usage\n";
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
throw new MWException( $ret );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
$id++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-10 18:41:31 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
//Various getter functions
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
/**
|
|
|
|
|
* Get an input dictionary from a set of parser test files
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param array $filenames
|
2011-01-01 18:10:28 +00:00
|
|
|
*/
|
|
|
|
|
function getFuzzInput( $filenames ) {
|
|
|
|
|
$dict = '';
|
|
|
|
|
|
|
|
|
|
foreach ( $filenames as $filename ) {
|
|
|
|
|
$contents = file_get_contents( $filename );
|
|
|
|
|
preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
|
|
|
|
|
|
|
|
|
|
foreach ( $matches[1] as $match ) {
|
|
|
|
|
$dict .= $match . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $dict;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 18:10:28 +00:00
|
|
|
/**
|
|
|
|
|
* Get a memory usage breakdown
|
|
|
|
|
*/
|
|
|
|
|
function getMemoryBreakdown() {
|
|
|
|
|
$memStats = array();
|
|
|
|
|
|
|
|
|
|
foreach ( $GLOBALS as $name => $value ) {
|
|
|
|
|
$memStats['$' . $name] = strlen( serialize( $value ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$classes = get_declared_classes();
|
|
|
|
|
|
|
|
|
|
foreach ( $classes as $class ) {
|
|
|
|
|
$rc = new ReflectionClass( $class );
|
|
|
|
|
$props = $rc->getStaticProperties();
|
|
|
|
|
$memStats[$class] = strlen( serialize( $props ) );
|
|
|
|
|
$methods = $rc->getMethods();
|
|
|
|
|
|
|
|
|
|
foreach ( $methods as $method ) {
|
|
|
|
|
$memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$functions = get_defined_functions();
|
|
|
|
|
|
|
|
|
|
foreach ( $functions['user'] as $function ) {
|
|
|
|
|
$rf = new ReflectionFunction( $function );
|
|
|
|
|
$memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asort( $memStats );
|
|
|
|
|
|
|
|
|
|
return $memStats;
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
/**
|
|
|
|
|
* Get a Parser object
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param Preprocessor $preprocessor
|
2011-01-01 03:39:37 +00:00
|
|
|
*/
|
|
|
|
|
function getParser( $preprocessor = null ) {
|
2011-03-06 23:28:32 +00:00
|
|
|
global $wgParserConf;
|
2011-01-01 03:39:37 +00:00
|
|
|
|
|
|
|
|
$class = $wgParserConf['class'];
|
|
|
|
|
$parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
|
|
|
|
|
|
|
|
|
|
wfRunHooks( 'ParserTestParser', array( &$parser ) );
|
|
|
|
|
|
|
|
|
|
return $parser;
|
|
|
|
|
}
|
2011-01-10 18:43:59 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
//Various action functions
|
2011-01-01 03:39:37 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
public function addArticle( $name, $text, $line ) {
|
2012-01-07 12:19:10 +00:00
|
|
|
self::$articles[$name] = array( $text, $line );
|
2011-07-25 22:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
public function publishTestArticles() {
|
|
|
|
|
if ( empty( self::$articles ) ) {
|
|
|
|
|
return;
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-07 12:19:10 +00:00
|
|
|
foreach ( self::$articles as $name => $info ) {
|
|
|
|
|
list( $text, $line ) = $info;
|
2012-01-09 22:33:00 +00:00
|
|
|
ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
/**
|
|
|
|
|
* Steal a callback function from the primary parser, save it for
|
|
|
|
|
* application to our scary parser. If the hook is not installed,
|
|
|
|
|
* abort processing of this file.
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return bool True if tag hook is present
|
2011-01-01 03:39:37 +00:00
|
|
|
*/
|
|
|
|
|
public function requireHook( $name ) {
|
|
|
|
|
global $wgParser;
|
2013-02-15 10:24:31 +00:00
|
|
|
$wgParser->firstCallInit(); // make sure hooks are loaded.
|
2011-02-26 21:49:42 +00:00
|
|
|
return isset( $wgParser->mTagHooks[$name] );
|
2011-01-01 03:39:37 +00:00
|
|
|
}
|
2011-01-10 18:43:59 +00:00
|
|
|
|
2011-02-26 21:49:42 +00:00
|
|
|
public function requireFunctionHook( $name ) {
|
|
|
|
|
global $wgParser;
|
2013-02-15 10:24:31 +00:00
|
|
|
$wgParser->firstCallInit(); // make sure hooks are loaded.
|
2011-02-26 21:49:42 +00:00
|
|
|
return isset( $wgParser->mFunctionHooks[$name] );
|
|
|
|
|
}
|
2013-02-15 10:24:31 +00:00
|
|
|
|
2014-06-20 15:36:08 +00:00
|
|
|
public function requireTransparentHook( $name ) {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
$wgParser->firstCallInit(); // make sure hooks are loaded.
|
|
|
|
|
return isset( $wgParser->mTransparentTagHooks[$name] );
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
//Various "cleanup" functions
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
2011-01-01 20:57:13 +00:00
|
|
|
* Run the "tidy" command on text if the $wgUseTidy
|
|
|
|
|
* global is true
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $text The text to tidy
|
|
|
|
|
* @return string
|
2011-01-01 20:57:13 +00:00
|
|
|
*/
|
|
|
|
|
protected function tidy( $text ) {
|
|
|
|
|
global $wgUseTidy;
|
|
|
|
|
|
|
|
|
|
if ( $wgUseTidy ) {
|
|
|
|
|
$text = MWTidy::tidy( $text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
/**
|
|
|
|
|
* Remove last character if it is a newline
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $s
|
2011-01-01 20:57:13 +00:00
|
|
|
*/
|
|
|
|
|
public function removeEndingNewline( $s ) {
|
|
|
|
|
if ( substr( $s, -1 ) === "\n" ) {
|
|
|
|
|
return substr( $s, 0, -1 );
|
2013-02-15 10:24:31 +00:00
|
|
|
} else {
|
2011-01-01 20:57:13 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-10 18:43:59 +00:00
|
|
|
|
2011-01-01 20:57:13 +00:00
|
|
|
//Test options parser functions
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
protected function parseOptions( $instring ) {
|
|
|
|
|
$opts = array();
|
|
|
|
|
// foo
|
|
|
|
|
// foo=bar
|
|
|
|
|
// foo="bar baz"
|
|
|
|
|
// foo=[[bar baz]]
|
|
|
|
|
// foo=bar,"baz quux"
|
|
|
|
|
$regex = '/\b
|
|
|
|
|
([\w-]+) # Key
|
|
|
|
|
\b
|
|
|
|
|
(?:\s*
|
|
|
|
|
= # First sub-value
|
|
|
|
|
\s*
|
|
|
|
|
(
|
|
|
|
|
"
|
|
|
|
|
[^"]* # Quoted val
|
|
|
|
|
"
|
|
|
|
|
|
|
|
|
|
|
\[\[
|
|
|
|
|
[^]]* # Link target
|
|
|
|
|
\]\]
|
|
|
|
|
|
|
|
|
|
|
[\w-]+ # Plain word
|
|
|
|
|
)
|
|
|
|
|
(?:\s*
|
|
|
|
|
, # Sub-vals 1..N
|
|
|
|
|
\s*
|
|
|
|
|
(
|
|
|
|
|
"[^"]*" # Quoted val
|
|
|
|
|
|
|
|
|
|
|
\[\[[^]]*\]\] # Link target
|
|
|
|
|
|
|
|
|
|
|
[\w-]+ # Plain word
|
|
|
|
|
)
|
|
|
|
|
)*
|
|
|
|
|
)?
|
|
|
|
|
/x';
|
|
|
|
|
|
|
|
|
|
if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
|
|
|
|
|
foreach ( $matches as $bits ) {
|
|
|
|
|
array_shift( $bits );
|
|
|
|
|
$key = strtolower( array_shift( $bits ) );
|
|
|
|
|
if ( count( $bits ) == 0 ) {
|
|
|
|
|
$opts[$key] = true;
|
|
|
|
|
} elseif ( count( $bits ) == 1 ) {
|
|
|
|
|
$opts[$key] = $this->cleanupOption( array_shift( $bits ) );
|
|
|
|
|
} else {
|
|
|
|
|
// Array!
|
|
|
|
|
$opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
return $opts;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
protected function cleanupOption( $opt ) {
|
|
|
|
|
if ( substr( $opt, 0, 1 ) == '"' ) {
|
|
|
|
|
return substr( $opt, 1, -1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( substr( $opt, 0, 2 ) == '[[' ) {
|
|
|
|
|
return substr( $opt, 2, -2 );
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
return $opt;
|
|
|
|
|
}
|
2011-07-25 22:01:19 +00:00
|
|
|
|
2011-01-01 03:39:37 +00:00
|
|
|
/**
|
|
|
|
|
* Use a regex to find out the value of an option
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $key Name of option val to retrieve
|
|
|
|
|
* @param array $opts Options array to look in
|
|
|
|
|
* @param mixed $default Default value returned if not found
|
2011-01-01 03:39:37 +00:00
|
|
|
*/
|
|
|
|
|
protected static function getOptionValue( $key, $opts, $default ) {
|
|
|
|
|
$key = strtolower( $key );
|
|
|
|
|
|
|
|
|
|
if ( isset( $opts[$key] ) ) {
|
|
|
|
|
return $opts[$key];
|
|
|
|
|
} else {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|