2012-10-16 15:42:54 +00:00
|
|
|
<?php
|
2020-04-07 17:05:12 +00:00
|
|
|
/**
|
|
|
|
|
* ApiParse check functions
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2012-10-16 15:42:54 +00:00
|
|
|
|
2022-07-06 15:05:30 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2020-07-03 00:20:38 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2022-06-29 22:34:37 +00:00
|
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
2023-09-18 13:56:39 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2020-02-06 09:44:05 +00:00
|
|
|
|
2012-10-16 15:42:54 +00:00
|
|
|
/**
|
|
|
|
|
* @group API
|
|
|
|
|
* @group Database
|
2013-01-18 19:02:28 +00:00
|
|
|
* @group medium
|
2013-10-23 16:01:33 +00:00
|
|
|
*
|
|
|
|
|
* @covers ApiParse
|
2012-10-16 15:42:54 +00:00
|
|
|
*/
|
|
|
|
|
class ApiParseTest extends ApiTestCase {
|
2022-06-29 22:34:37 +00:00
|
|
|
use DummyServicesTrait;
|
2012-10-16 15:42:54 +00:00
|
|
|
|
2017-06-12 15:02:58 +00:00
|
|
|
protected static $pageId;
|
|
|
|
|
protected static $revIds = [];
|
|
|
|
|
|
|
|
|
|
public function addDBDataOnce() {
|
2023-06-23 18:12:09 +00:00
|
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()
|
|
|
|
|
->newFromLinkTarget( new TitleValue( NS_MAIN, __CLASS__ ) );
|
|
|
|
|
$status = $this->editPage( $page, 'Test for revdel' );
|
2022-11-14 20:17:19 +00:00
|
|
|
self::$pageId = $status->getNewRevision()->getPageId();
|
|
|
|
|
self::$revIds['revdel'] = $status->getNewRevision()->getId();
|
2017-06-12 15:02:58 +00:00
|
|
|
|
2023-06-23 18:12:09 +00:00
|
|
|
$status = $this->editPage( $page, 'Test for suppressed' );
|
2022-11-14 20:17:19 +00:00
|
|
|
self::$revIds['suppressed'] = $status->getNewRevision()->getId();
|
2018-03-27 17:12:48 +00:00
|
|
|
|
2023-06-23 18:12:09 +00:00
|
|
|
$status = $this->editPage( $page, 'Test for oldid' );
|
2022-11-14 20:17:19 +00:00
|
|
|
self::$revIds['oldid'] = $status->getNewRevision()->getId();
|
2017-06-12 15:02:58 +00:00
|
|
|
|
2023-06-23 18:12:09 +00:00
|
|
|
$status = $this->editPage( $page, 'Test for latest' );
|
2022-11-14 20:17:19 +00:00
|
|
|
self::$revIds['latest'] = $status->getNewRevision()->getId();
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->revisionDelete( self::$revIds['revdel'] );
|
|
|
|
|
$this->revisionDelete(
|
|
|
|
|
self::$revIds['suppressed'],
|
2020-07-03 00:20:38 +00:00
|
|
|
[
|
|
|
|
|
RevisionRecord::DELETED_TEXT => 1,
|
|
|
|
|
RevisionRecord::DELETED_RESTRICTED => 1
|
|
|
|
|
]
|
2017-06-12 15:02:58 +00:00
|
|
|
);
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert that the given result of calling $this->doApiRequest() with
|
|
|
|
|
* action=parse resulted in $html, accounting for the boilerplate that the
|
|
|
|
|
* parser adds around the parsed page. Also asserts that warnings match
|
|
|
|
|
* the provided $warning.
|
|
|
|
|
*
|
2021-01-14 08:20:36 +00:00
|
|
|
* @param string $expected Expected HTML
|
2018-03-27 17:12:48 +00:00
|
|
|
* @param array $res Returned from doApiRequest()
|
|
|
|
|
* @param string|null $warnings Exact value of expected warnings, null for
|
|
|
|
|
* no warnings
|
|
|
|
|
*/
|
|
|
|
|
protected function assertParsedTo( $expected, array $res, $warnings = null ) {
|
|
|
|
|
$this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertSame' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as above, but asserts that the HTML matches a regexp instead of a
|
|
|
|
|
* literal string match.
|
|
|
|
|
*
|
2021-01-14 08:20:36 +00:00
|
|
|
* @param string $expected Expected HTML
|
2018-03-27 17:12:48 +00:00
|
|
|
* @param array $res Returned from doApiRequest()
|
|
|
|
|
* @param string|null $warnings Exact value of expected warnings, null for
|
|
|
|
|
* no warnings
|
|
|
|
|
*/
|
|
|
|
|
protected function assertParsedToRegExp( $expected, array $res, $warnings = null ) {
|
2021-03-25 10:38:39 +00:00
|
|
|
$this->doAssertParsedTo( $expected, $res, $warnings, [ $this, 'assertMatchesRegularExpression' ] );
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function doAssertParsedTo( $expected, array $res, $warnings, callable $callback ) {
|
|
|
|
|
$html = $res[0]['parse']['text'];
|
|
|
|
|
|
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText
== Skin::wrapHTML ==
Skin::wrapHTML no longer has to perform any guessing of the
ParserOutput language. Nor does it have to special wiki pages vs
special pages in this regard. Yay, code removal.
== ImagePage ==
On URLs like /wiki/File:Example.jpg, the main output handler is
ImagePage::view. This calls the parent Article::view to handle most of
its output. Article::view obtains the ParserOptions, and then fetches
ParserOutput, and then adds `<div class=mw-parser-output>` and its
metadata to OutputPage.
Before this change, ImagePage::view was creating a wrapper based
on "predicting" what language the ParserOutput will contain. It
couldn't call the new OutputPage::getContentLanguage or some
equivalent as Article::view wouldn't have populated that yet.
This leaky abstraction is fixed by this change as now the `<div>`
from ParserOutput no longer comes with a "please wrap it properly"
contract that Article subclasses couldn't possibly implement correctly
(it coudln't wrap it after the fact because Article::view writes to
OutputPage directly).
RECENT (T310445):
A special case was recently added for file pages about translated SVGs.
For those, we decide which language to use for the "fullMedia" thumb
atop the page. This was recently changed as part of T310445 from a
hardcoded $wgLanguageCode (site content lang) to new problematic
Title::getPageViewLanguage, which tries to guestimate the page
language of the rendered ParserOutput and then gets the preferred
variant for the current user. The motivation for this was to support
language variants but used Title::getPageViewLanguage as a kitchen
sink to achieve that minor side-effect. The only part of this
now-deprecated method that we actually need is
LanguageConverter::getPreferredVariant().
Test plan: Covered by ImagePageTest.
== Skin mainpage-title ==
RECENT (T331095, T298715):
A special case was added to Skin::getTemplateData that powers the
mainpage-title interface message feature. This is empty by default,
but when created via MediaWiki:mainpage-title allows interface admins
to replace the H1 with a custom and localised page heading.
A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was
applied here to support language variants. Replace with the same
fix as for ImagePage. Revert back to Message::inContentLanguage()
but refactor to inLanguage() via MediaWikiServices::getContentLanguage
so that LanguageConverter::getPreferredVariant can be applied.
== EditPage ==
This was doing similar "predicting" of the ParserOutput language to
create an empty preview placeholder for use by preview.js. Now that
ApiParse (via ParserOutput::getText) returns a usable element without
any secret "you magically know the right class, lang, and dir" contract,
this placeholder is no longer needed.
Test Plan:
* EditPage: Default preview
1. index.php?title=Main_Page&action=edit
2. Show preview
3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
* EditPage: JS preview
1. Preferences > Editing > Show preview without reload
2. index.php?title=Main_Page&action=edit
3. Show preview
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
5. Type something and 'Show preview' again
6. Assert old element gone, new text is shown, and new element
attributes are the same as the above.
== McrUndoAction ==
Same as EditPage basically, but without the JS preview use case.
== DifferenceEngine ==
Test:
1. Open /w/index.php?title=Main_Page&diff=0
(this shows the latest diff, can do manually by viewing
/wiki/Main_Page, click "View history", click "Compare selected revisions")
2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
3. Open /w/index.php?title=Main_Page&diff=0&action=render
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
== Special:ExpandTemplates ==
Test:
1. /wiki/Special:ExpandTemplates
2. Write "Hello".
3. "OK"
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
Bug: T341244
Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c
Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1
Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105
Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
|
|
|
$expectedStart = '<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">';
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertSame( $expectedStart, substr( $html, 0, strlen( $expectedStart ) ) );
|
|
|
|
|
|
|
|
|
|
$html = substr( $html, strlen( $expectedStart ) );
|
|
|
|
|
|
2021-11-09 16:31:27 +00:00
|
|
|
$possibleParserCache = '/\n<!-- Saved in (?>parser cache|RevisionOutputCache) (?>.*?\n -->)\n/';
|
|
|
|
|
$html = preg_replace( $possibleParserCache, '', $html );
|
|
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
if ( $res[1]->getBool( 'disablelimitreport' ) ) {
|
|
|
|
|
$expectedEnd = "</div>";
|
|
|
|
|
$this->assertSame( $expectedEnd, substr( $html, -strlen( $expectedEnd ) ) );
|
|
|
|
|
|
2018-08-28 16:48:10 +00:00
|
|
|
$unexpectedEnd = '#<!-- \nNewPP limit report|' .
|
2023-03-24 03:21:20 +00:00
|
|
|
'<!--\nTransclusion expansion time report#';
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertDoesNotMatchRegularExpression( $unexpectedEnd, $html );
|
2018-08-28 16:48:10 +00:00
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
$html = substr( $html, 0, strlen( $html ) - strlen( $expectedEnd ) );
|
|
|
|
|
} else {
|
|
|
|
|
$expectedEnd = '#\n<!-- \nNewPP limit report\n(?>.+?\n-->)\n' .
|
|
|
|
|
'<!--\nTransclusion expansion time report \(%,ms,calls,template\)\n(?>.*?\n-->)\n' .
|
2021-11-09 16:31:27 +00:00
|
|
|
'</div>$#s';
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertMatchesRegularExpression( $expectedEnd, $html );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$html = preg_replace( $expectedEnd, '', $html );
|
2017-06-12 15:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 06:46:30 +00:00
|
|
|
$callback( $expected, $html );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
if ( $warnings === null ) {
|
|
|
|
|
$this->assertCount( 1, $res[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertCount( 2, $res[0] );
|
2018-09-21 14:57:18 +00:00
|
|
|
$this->assertSame( [ 'warnings' => $warnings ], $res[0]['warnings']['parse'] );
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up an interwiki entry for testing.
|
|
|
|
|
*/
|
|
|
|
|
protected function setupInterwiki() {
|
2023-09-25 18:49:16 +00:00
|
|
|
$this->getDb()->insert(
|
2018-03-27 17:12:48 +00:00
|
|
|
'interwiki',
|
|
|
|
|
[
|
|
|
|
|
'iw_prefix' => 'madeuplanguage',
|
|
|
|
|
'iw_url' => "https://example.com/wiki/$1",
|
|
|
|
|
'iw_api' => '',
|
|
|
|
|
'iw_wikiid' => '',
|
|
|
|
|
'iw_local' => false,
|
2017-06-12 15:02:58 +00:00
|
|
|
],
|
2018-03-27 17:12:48 +00:00
|
|
|
__METHOD__,
|
|
|
|
|
'IGNORE'
|
|
|
|
|
);
|
2017-06-12 15:02:58 +00:00
|
|
|
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::ExtraInterlanguageLinkPrefixes,
|
|
|
|
|
[ 'madeuplanguage' ]
|
|
|
|
|
);
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up a skin for testing.
|
|
|
|
|
*
|
2020-06-30 15:09:24 +00:00
|
|
|
* @todo Should this code be in MediaWikiIntegrationTestCase or something?
|
2018-03-27 17:12:48 +00:00
|
|
|
*/
|
|
|
|
|
protected function setupSkin() {
|
2022-06-29 22:34:37 +00:00
|
|
|
$factory = new SkinFactory( $this->getDummyObjectFactory(), [] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$factory->register( 'testing', 'Testing', function () {
|
|
|
|
|
$skin = $this->getMockBuilder( SkinFallback::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getDefaultModules' ] )
|
2018-03-27 17:12:48 +00:00
|
|
|
->getMock();
|
|
|
|
|
$skin->expects( $this->once() )->method( 'getDefaultModules' )
|
|
|
|
|
->willReturn( [
|
2020-11-22 23:07:27 +00:00
|
|
|
'styles' => [ 'core' => [ 'quux.styles' ] ],
|
2018-03-27 17:12:48 +00:00
|
|
|
'core' => [ 'foo', 'bar' ],
|
|
|
|
|
'content' => [ 'baz' ]
|
|
|
|
|
] );
|
|
|
|
|
return $skin;
|
|
|
|
|
} );
|
|
|
|
|
$this->setService( 'SkinFactory', $factory );
|
2012-10-16 15:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
2017-06-12 15:02:58 +00:00
|
|
|
public function testParseByName() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertParsedTo( "<p>Test for latest\n</p>", $res );
|
2017-06-12 15:02:58 +00:00
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
'disablelimitreport' => 1,
|
|
|
|
|
] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertParsedTo( "<p>Test for latest\n</p>", $res );
|
2017-06-12 15:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testParseById() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pageid' => self::$pageId,
|
|
|
|
|
] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertParsedTo( "<p>Test for latest\n</p>", $res );
|
2017-06-12 15:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testParseByOldId() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => self::$revIds['oldid'],
|
|
|
|
|
] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertParsedTo( "<p>Test for oldid\n</p>", $res );
|
2017-06-12 15:02:58 +00:00
|
|
|
$this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
public function testRevDel() {
|
2017-06-12 15:02:58 +00:00
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => self::$revIds['revdel'],
|
2018-03-27 17:12:48 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Test for revdel\n</p>", $res );
|
2017-06-12 15:02:58 +00:00
|
|
|
$this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
public function testRevDelNoPermission() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'permissiondenied' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => self::$revIds['revdel'],
|
2022-08-05 19:39:42 +00:00
|
|
|
], null, null, static::getTestUser()->getAuthority() );
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSuppressed() {
|
|
|
|
|
$this->setGroupPermissions( 'sysop', 'viewsuppressed', true );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => self::$revIds['suppressed']
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Test for suppressed\n</p>", $res );
|
|
|
|
|
$this->assertArrayHasKey( 'textsuppressed', $res[0]['parse'] );
|
|
|
|
|
$this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNonexistentPage() {
|
2017-06-12 15:02:58 +00:00
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => 'DoesNotExist',
|
|
|
|
|
] );
|
2012-10-16 15:42:54 +00:00
|
|
|
|
|
|
|
|
$this->fail( "API did not return an error when parsing a nonexistent page" );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $ex ) {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->assertApiErrorCode( 'missingtitle', $ex );
|
2012-10-16 15:42:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-25 01:07:59 +00:00
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
public function testTitleProvided() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Some interesting page',
|
|
|
|
|
'text' => '{{PAGENAME}} has attracted my attention',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some interesting page has attracted my attention\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSection() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( $name,
|
|
|
|
|
"Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
'section' => 1,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content 1\n</p>!', $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvalidSection() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidsection' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'section' => 'T-new',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSectionNoContent() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$status = $this->editPage( $name,
|
|
|
|
|
"Intro\n\n== Section 1 ==\n\nContent 1\n\n== Section 2 ==\n\nContent 2" );
|
|
|
|
|
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'missingcontent-pageid' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
2022-11-14 20:17:19 +00:00
|
|
|
$this->db->delete( 'revision', [ 'rev_id' => $status->getNewRevision()->getId() ] );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
2022-02-24 19:57:59 +00:00
|
|
|
// Ignore warning from WikiPage::getContentModel
|
|
|
|
|
@$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
'section' => 1,
|
|
|
|
|
] );
|
2018-03-27 17:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewSectionWithPage() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidparammix' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
'section' => 'new',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNonexistentOldId() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'nosuchrevid' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => pow( 2, 31 ) - 1,
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnfollowedRedirect() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( $name, "#REDIRECT [[$name 2]]" );
|
|
|
|
|
$this->editPage( "$name 2", "Some ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
// Can't use assertParsedTo because the parser output is different for
|
|
|
|
|
// redirects
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertMatchesRegularExpression( "/Redirect to:.*$name 2/", $res[0]['parse']['text'] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testFollowedRedirect() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( $name, "#REDIRECT [[$name 2]]" );
|
|
|
|
|
$this->editPage( "$name 2", "Some ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
'redirects' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testFollowedRedirectById() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
2020-06-10 00:37:04 +00:00
|
|
|
$id = $this->editPage( $name, "#REDIRECT [[$name 2]]" )
|
2022-11-14 20:17:19 +00:00
|
|
|
->getNewRevision()->getPageId();
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->editPage( "$name 2", "Some ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pageid' => $id,
|
|
|
|
|
'redirects' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 14:24:17 +00:00
|
|
|
public function testNonRedirectOk() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( $name, "Some ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
'redirects' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNonRedirectByIdOk() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
2022-11-14 20:17:19 +00:00
|
|
|
$id = $this->editPage( $name, "Some ''text''" )->getNewRevision()->getPageId();
|
2019-12-23 14:24:17 +00:00
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pageid' => $id,
|
|
|
|
|
'redirects' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
public function testInvalidTitle() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidtitle' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => '|',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleWithNonexistentRevId() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'nosuchrevid' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'revid' => pow( 2, 31 ) - 1,
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleWithNonMatchingRevId() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => $name,
|
|
|
|
|
'revid' => self::$revIds['latest'],
|
|
|
|
|
'text' => 'Some text',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some text\n</p>", $res,
|
|
|
|
|
'r' . self::$revIds['latest'] . " is not a revision of $name." );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRevId() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'revid' => self::$revIds['latest'],
|
|
|
|
|
'text' => 'My revid is {{REVISIONID}}!',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>My revid is " . self::$revIds['latest'] . "!\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTitleNoText() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Special:AllPages',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( '', $res,
|
|
|
|
|
'"title" used without "text", and parsed page properties were requested. ' .
|
|
|
|
|
'Did you mean to use "page" instead of "title"?' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRevidNoText() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'revid' => self::$revIds['latest'],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( '', $res,
|
|
|
|
|
'"revid" used without "text", and parsed page properties were requested. ' .
|
|
|
|
|
'Did you mean to use "oldid" instead of "revid"?' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTextNoContentModel() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => "Some ''text''",
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Some <i>text</i>\n</p>", $res,
|
|
|
|
|
'No "title" or "contentmodel" was given, assuming wikitext.' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSerializationError() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'parseerror' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->mergeMwGlobalArrayValue( 'wgContentHandlers',
|
|
|
|
|
[ 'testing-serialize-error' => 'DummySerializeErrorContentHandler' ] );
|
|
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => "Some ''text''",
|
|
|
|
|
'contentmodel' => 'testing-serialize-error',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNewSection() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'section' => 'new',
|
|
|
|
|
'sectiontitle' => 'Title',
|
|
|
|
|
'text' => 'Content',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedToRegExp( '!<h2>.*Title.*</h2>\n<p>Content\n</p>!', $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExistingSection() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'section' => 1,
|
|
|
|
|
'text' => "Intro\n\n== Section 1 ==\n\nContent\n\n== Section 2 ==\n\nMore content",
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedToRegExp( '!<h2>.*Section 1.*</h2>\n<p>Content\n</p>!', $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNoPst() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( "Template:$name", "Template ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => "{{subst:$name}}",
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>{{subst:$name}}\n</p>", $res );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPst() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( "Template:$name", "Template ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pst' => '',
|
|
|
|
|
'text' => "{{subst:$name}}",
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'prop' => 'text|wikitext',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedTo( "<p>Template <i>text</i>\n</p>", $res );
|
|
|
|
|
$this->assertSame( "{{subst:$name}}", $res[0]['parse']['wikitext'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOnlyPst() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( "Template:$name", "Template ''text''" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'onlypst' => '',
|
|
|
|
|
'text' => "{{subst:$name}}",
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'prop' => 'text|wikitext',
|
|
|
|
|
'summary' => 'Summary',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'parse' => [
|
|
|
|
|
'text' => "Template ''text''",
|
|
|
|
|
'wikitext' => "{{subst:$name}}",
|
|
|
|
|
'parsedsummary' => 'Summary',
|
|
|
|
|
] ],
|
|
|
|
|
$res[0]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 20:59:23 +00:00
|
|
|
/** @dataProvider providerTestParsoid */
|
|
|
|
|
public function testParsoid( $parsoid, $existing, $expected ) {
|
|
|
|
|
# For simplicity, ensure that [[Foo]] isn't a redlink.
|
|
|
|
|
$this->editPage( "Foo", __FUNCTION__ );
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
# check that we're using the contents of 'text' not the contents of
|
|
|
|
|
# [[<title>]] by using pre-existing title __CLASS__ sometimes
|
|
|
|
|
'title' => $existing ? __CLASS__ : 'Bar',
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => "[[Foo]]",
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'parsoid' => $parsoid ?: null,
|
|
|
|
|
'disablelimitreport' => true,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertParsedToRegexp( $expected, $res );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 22:32:30 +00:00
|
|
|
public static function providerTestParsoid() {
|
2023-03-21 20:59:23 +00:00
|
|
|
// Legacy parses, with and without pre-existing content.
|
|
|
|
|
$expected = '!^<p><a href="[^"]*" title="Foo">Foo</a>\n</p>$!';
|
|
|
|
|
yield [ false, false, $expected ];
|
|
|
|
|
yield [ false, true, $expected ];
|
|
|
|
|
// Parsoid parses, with and without pre-existing content.
|
2023-11-16 17:19:21 +00:00
|
|
|
$expected = '!^<section[^>]*><p[^>]*><a rel="mw:WikiLink" href="[^"]*Foo" title="Foo"[^>]*>Foo</a></p></section>!';
|
2023-03-21 20:59:23 +00:00
|
|
|
yield [ true, false, $expected ];
|
|
|
|
|
yield [ true, true, $expected ];
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 17:12:48 +00:00
|
|
|
public function testHeadHtml() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
'prop' => 'headhtml',
|
|
|
|
|
] );
|
|
|
|
|
|
2021-11-21 16:23:11 +00:00
|
|
|
// Just do a rough check
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertMatchesRegularExpression( '#<!DOCTYPE.*<html.*<head.*</head>.*<body#s',
|
2018-03-27 17:12:48 +00:00
|
|
|
$res[0]['parse']['headhtml'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCategoriesHtml() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( $name, "[[Category:$name]]" );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => $name,
|
|
|
|
|
'prop' => 'categorieshtml',
|
|
|
|
|
] );
|
|
|
|
|
|
2022-10-07 17:03:35 +00:00
|
|
|
$this->assertMatchesRegularExpression( "#Category.*Category:$name.*$name#",
|
2018-03-27 17:12:48 +00:00
|
|
|
$res[0]['parse']['categorieshtml'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEffectiveLangLinks() {
|
|
|
|
|
$hookRan = false;
|
|
|
|
|
$this->setTemporaryHook( 'LanguageLinks',
|
2021-02-07 13:10:36 +00:00
|
|
|
static function () use ( &$hookRan ) {
|
2018-03-27 17:12:48 +00:00
|
|
|
$hookRan = true;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => '[[zh:' . __CLASS__ . ']]',
|
|
|
|
|
'effectivelanglinks' => '',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $hookRan );
|
|
|
|
|
$this->assertSame( 'The parameter "effectivelanglinks" has been deprecated.',
|
|
|
|
|
$res[0]['warnings']['parse']['warnings'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $arr Extra params to add to API request
|
|
|
|
|
*/
|
|
|
|
|
private function doTestLangLinks( array $arr = [] ) {
|
|
|
|
|
$res = $this->doApiRequest( array_merge( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Omelette',
|
|
|
|
|
'text' => '[[madeuplanguage:Omelette]]',
|
|
|
|
|
'prop' => 'langlinks',
|
|
|
|
|
], $arr ) );
|
|
|
|
|
|
|
|
|
|
$langLinks = $res[0]['parse']['langlinks'];
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $langLinks );
|
|
|
|
|
$this->assertSame( 'madeuplanguage', $langLinks[0]['lang'] );
|
|
|
|
|
$this->assertSame( 'Omelette', $langLinks[0]['title'] );
|
|
|
|
|
$this->assertSame( 'https://example.com/wiki/Omelette', $langLinks[0]['url'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLangLinks() {
|
2019-04-10 15:03:54 +00:00
|
|
|
$this->setupInterwiki();
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->doTestLangLinks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLangLinksWithSkin() {
|
2019-04-10 15:03:54 +00:00
|
|
|
$this->setupInterwiki();
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->setupSkin();
|
|
|
|
|
$this->doTestLangLinks( [ 'useskin' => 'testing' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHeadItems() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => '',
|
|
|
|
|
'prop' => 'headitems',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( [], $res[0]['parse']['headitems'] );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'"prop=headitems" is deprecated since MediaWiki 1.28. ' .
|
|
|
|
|
'Use "prop=headhtml" when creating new HTML documents, ' .
|
|
|
|
|
'or "prop=modules|jsconfigvars" when updating a document client-side.',
|
|
|
|
|
$res[0]['warnings']['parse']['warnings']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHeadItemsWithSkin() {
|
|
|
|
|
$this->setupSkin();
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => '',
|
|
|
|
|
'prop' => 'headitems',
|
|
|
|
|
'useskin' => 'testing',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( [], $res[0]['parse']['headitems'] );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'"prop=headitems" is deprecated since MediaWiki 1.28. ' .
|
|
|
|
|
'Use "prop=headhtml" when creating new HTML documents, ' .
|
|
|
|
|
'or "prop=modules|jsconfigvars" when updating a document client-side.',
|
|
|
|
|
$res[0]['warnings']['parse']['warnings']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testModules() {
|
|
|
|
|
$this->setTemporaryHook( 'ParserAfterParse',
|
2021-02-07 13:10:36 +00:00
|
|
|
static function ( $parser ) {
|
2018-03-27 17:12:48 +00:00
|
|
|
$output = $parser->getOutput();
|
|
|
|
|
$output->addModules( [ 'foo', 'bar' ] );
|
|
|
|
|
$output->addModuleStyles( [ 'aaa', 'zzz' ] );
|
|
|
|
|
$output->addJsConfigVars( [ 'x' => 'y', 'z' => -3 ] );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => 'Content',
|
|
|
|
|
'prop' => 'modules|jsconfigvars|encodedjsconfigvars',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( [ 'foo', 'bar' ], $res[0]['parse']['modules'] );
|
2019-03-03 19:19:51 +00:00
|
|
|
$this->assertSame( [], $res[0]['parse']['modulescripts'] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertSame( [ 'aaa', 'zzz' ], $res[0]['parse']['modulestyles'] );
|
|
|
|
|
$this->assertSame( [ 'x' => 'y', 'z' => -3 ], $res[0]['parse']['jsconfigvars'] );
|
|
|
|
|
$this->assertSame( '{"x":"y","z":-3}', $res[0]['parse']['encodedjsconfigvars'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testModulesWithSkin() {
|
|
|
|
|
$this->setupSkin();
|
2017-10-25 01:07:59 +00:00
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pageid' => self::$pageId,
|
|
|
|
|
'useskin' => 'testing',
|
|
|
|
|
'prop' => 'modules',
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'foo', 'bar', 'baz' ],
|
|
|
|
|
$res[0]['parse']['modules'],
|
|
|
|
|
'resp.parse.modules'
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[],
|
|
|
|
|
$res[0]['parse']['modulescripts'],
|
|
|
|
|
'resp.parse.modulescripts'
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame(
|
2020-11-23 23:06:43 +00:00
|
|
|
[ 'quux.styles' ],
|
2017-10-25 01:07:59 +00:00
|
|
|
$res[0]['parse']['modulestyles'],
|
|
|
|
|
'resp.parse.modulestyles'
|
|
|
|
|
);
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'parse' =>
|
|
|
|
|
[ 'warnings' =>
|
|
|
|
|
'Property "modules" was set but not "jsconfigvars" or ' .
|
|
|
|
|
'"encodedjsconfigvars". Configuration variables are necessary for ' .
|
|
|
|
|
'proper module usage.'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
$res[0]['warnings']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIndicators() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' =>
|
|
|
|
|
'<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
|
|
|
|
|
'prop' => 'indicators',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
// It seems we return in markup order and not display order
|
|
|
|
|
[ 'b' => 'BBB!', 'a' => 'aaa' ],
|
|
|
|
|
$res[0]['parse']['indicators']
|
|
|
|
|
);
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIndicatorsWithSkin() {
|
|
|
|
|
$this->setupSkin();
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' =>
|
|
|
|
|
'<indicator name="b">BBB!</indicator>Some text<indicator name="a">aaa</indicator>',
|
|
|
|
|
'prop' => 'indicators',
|
|
|
|
|
'useskin' => 'testing',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
// Now we return in display order rather than markup order
|
2021-12-22 00:19:02 +00:00
|
|
|
[
|
|
|
|
|
'a' => '<div class="mw-parser-output">aaa</div>',
|
|
|
|
|
'b' => '<div class="mw-parser-output">BBB!</div>',
|
|
|
|
|
],
|
2018-03-27 17:12:48 +00:00
|
|
|
$res[0]['parse']['indicators']
|
|
|
|
|
);
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIwlinks() {
|
|
|
|
|
$this->setupInterwiki();
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Omelette',
|
|
|
|
|
'text' => '[[:madeuplanguage:Omelette]][[madeuplanguage:Spaghetti]]',
|
|
|
|
|
'prop' => 'iwlinks',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$iwlinks = $res[0]['parse']['iwlinks'];
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $iwlinks );
|
|
|
|
|
$this->assertSame( 'madeuplanguage', $iwlinks[0]['prefix'] );
|
|
|
|
|
$this->assertSame( 'https://example.com/wiki/Omelette', $iwlinks[0]['url'] );
|
|
|
|
|
$this->assertSame( 'madeuplanguage:Omelette', $iwlinks[0]['title'] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLimitReports() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'pageid' => self::$pageId,
|
|
|
|
|
'prop' => 'limitreportdata|limitreporthtml',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
// We don't bother testing the actual values here
|
2019-12-13 14:29:10 +00:00
|
|
|
$this->assertIsArray( $res[0]['parse']['limitreportdata'] );
|
|
|
|
|
$this->assertIsString( $res[0]['parse']['limitreporthtml'] );
|
2018-03-27 17:12:48 +00:00
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testParseTreeNonWikitext() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'notwikitext' );
|
2018-03-27 17:12:48 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => '',
|
|
|
|
|
'contentmodel' => 'json',
|
|
|
|
|
'prop' => 'parsetree',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testParseTree() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'text' => "Some ''text'' is {{nice|to have|i=think}}",
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'prop' => 'parsetree',
|
|
|
|
|
] );
|
|
|
|
|
|
Remove Preprocessor_DOM, deprecated in 1.34
Remove the deprecated Preprocessor_DOM class, which was hard-deprecated
in 1.34. This begins to simplify parser configuration and reduce redundant
code paths, but I've left two things for cleanup in a future patch:
1. The `preprocessorClass` configuration option to the parser, exposed
in `$wgParserConf`, ServiceWiring, ParserFactory, etc. There is no reason
for this to be exposed as configurable, but I've left this clean up to a
future patch.
2. The `$wgMaxGeneratedPPNodeCount` configuration, exposed also in
ParserOptions. Only Preprocessor_DOM calculated this count, and since
we are only using Preprocessor_Hash now, this configuration has no effect.
But since this value was exposed in ParserOptions and elsewhere, I've
deprecated where needed but left this clean up to a future patch.
Bug: T204945
Change-Id: I727f003f9a42d0c92bcbcce8a8289d5af6cd1298
2020-01-24 21:23:46 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'<root>Some \'\'text\'\' is <template><title>nice</title>' .
|
2018-03-27 17:12:48 +00:00
|
|
|
'<part><name index="1"/><value>to have</value></part>' .
|
Remove Preprocessor_DOM, deprecated in 1.34
Remove the deprecated Preprocessor_DOM class, which was hard-deprecated
in 1.34. This begins to simplify parser configuration and reduce redundant
code paths, but I've left two things for cleanup in a future patch:
1. The `preprocessorClass` configuration option to the parser, exposed
in `$wgParserConf`, ServiceWiring, ParserFactory, etc. There is no reason
for this to be exposed as configurable, but I've left this clean up to a
future patch.
2. The `$wgMaxGeneratedPPNodeCount` configuration, exposed also in
ParserOptions. Only Preprocessor_DOM calculated this count, and since
we are only using Preprocessor_Hash now, this configuration has no effect.
But since this value was exposed in ParserOptions and elsewhere, I've
deprecated where needed but left this clean up to a future patch.
Bug: T204945
Change-Id: I727f003f9a42d0c92bcbcce8a8289d5af6cd1298
2020-01-24 21:23:46 +00:00
|
|
|
'<part><name>i</name><equals>=</equals><value>think</value></part>' .
|
|
|
|
|
'</template></root>',
|
2018-03-27 17:12:48 +00:00
|
|
|
$res[0]['parse']['parsetree']
|
|
|
|
|
);
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testFormatCategories() {
|
|
|
|
|
$name = ucfirst( __FUNCTION__ );
|
|
|
|
|
|
|
|
|
|
$this->editPage( "Category:$name", 'Content' );
|
|
|
|
|
$this->editPage( 'Category:Hidden', '__HIDDENCAT__' );
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => "[[Category:$name]][[Category:Foo|Sort me]][[Category:Hidden]]",
|
|
|
|
|
'prop' => 'categories',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ [ 'sortkey' => '', 'category' => $name ],
|
|
|
|
|
[ 'sortkey' => 'Sort me', 'category' => 'Foo', 'missing' => true ],
|
|
|
|
|
[ 'sortkey' => '', 'category' => 'Hidden', 'hidden' => true ] ],
|
|
|
|
|
$res[0]['parse']['categories']
|
|
|
|
|
);
|
|
|
|
|
$this->assertArrayNotHasKey( 'warnings', $res[0] );
|
2017-10-25 01:07:59 +00:00
|
|
|
}
|
2020-04-07 17:05:12 +00:00
|
|
|
|
|
|
|
|
public function testConcurrentLimitPageParse() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::PoolCounterConf,
|
|
|
|
|
[
|
|
|
|
|
'ApiParser' => [
|
|
|
|
|
'class' => MockPoolCounterFailing::class,
|
|
|
|
|
]
|
2020-04-07 17:05:12 +00:00
|
|
|
]
|
2022-07-06 15:05:30 +00:00
|
|
|
);
|
2020-04-07 17:05:12 +00:00
|
|
|
|
2022-07-12 18:03:53 +00:00
|
|
|
try {
|
2020-04-07 17:05:12 +00:00
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
] );
|
|
|
|
|
$this->fail( "API did not return an error when concurrency exceeded" );
|
|
|
|
|
} catch ( ApiUsageException $ex ) {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->assertApiErrorCode( 'concurrency-limit', $ex );
|
2020-04-07 17:05:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConcurrentLimitContentParse() {
|
2022-07-06 15:05:30 +00:00
|
|
|
$this->overrideConfigValue(
|
|
|
|
|
MainConfigNames::PoolCounterConf,
|
|
|
|
|
[
|
|
|
|
|
'ApiParser' => [
|
|
|
|
|
'class' => MockPoolCounterFailing::class,
|
|
|
|
|
]
|
2020-04-07 17:05:12 +00:00
|
|
|
]
|
2022-07-06 15:05:30 +00:00
|
|
|
);
|
2020-04-07 17:05:12 +00:00
|
|
|
|
2022-07-12 18:03:53 +00:00
|
|
|
try {
|
2020-04-07 17:05:12 +00:00
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'oldid' => self::$revIds['revdel'],
|
|
|
|
|
] );
|
|
|
|
|
$this->fail( "API did not return an error when concurrency exceeded" );
|
|
|
|
|
} catch ( ApiUsageException $ex ) {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->assertApiErrorCode( 'concurrency-limit', $ex );
|
2020-04-07 17:05:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 18:47:20 +00:00
|
|
|
public function testDisplayTitle() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Art©',
|
|
|
|
|
'text' => '{{DISPLAYTITLE:art©}}foo',
|
|
|
|
|
'prop' => 'displaytitle',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'art&copy',
|
|
|
|
|
$res[0]['parse']['displaytitle']
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => 'Art©',
|
|
|
|
|
'text' => 'foo',
|
|
|
|
|
'prop' => 'displaytitle',
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
2022-08-09 02:52:53 +00:00
|
|
|
'<span class="mw-page-title-main">Art&copy</span>',
|
2021-09-28 18:47:20 +00:00
|
|
|
$res[0]['parse']['displaytitle']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 14:06:43 +00:00
|
|
|
public function testIncompatFormat() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'badformat-generic' );
|
2022-03-03 14:06:43 +00:00
|
|
|
|
|
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'prop' => 'categories',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'text' => '',
|
|
|
|
|
'contentformat' => 'application/json',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 15:22:01 +00:00
|
|
|
public function testIgnoreFormatUsingPage() {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'page' => __CLASS__,
|
|
|
|
|
'prop' => 'wikitext',
|
|
|
|
|
'contentformat' => 'text/plain',
|
|
|
|
|
] );
|
|
|
|
|
$this->assertArrayHasKey( 'wikitext', $res[0]['parse'] );
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 18:06:31 +00:00
|
|
|
public function testShouldCastNumericImageLinksToString(): void {
|
|
|
|
|
$res = $this->doApiRequest( [
|
|
|
|
|
'action' => 'parse',
|
|
|
|
|
'title' => __CLASS__,
|
|
|
|
|
'prop' => 'images',
|
|
|
|
|
'text' => '[[File:1]]',
|
|
|
|
|
] );
|
|
|
|
|
$this->assertSame( [ '1' ], $res[0]['parse']['images'] );
|
|
|
|
|
}
|
2012-10-16 15:42:54 +00:00
|
|
|
}
|