testHelpers.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. <?php
  2. /**
  3. * Recording for passing/failing tests.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Testing
  22. */
  23. /**
  24. * Interface to record parser test results.
  25. *
  26. * The ITestRecorder is a very simple interface to record the result of
  27. * MediaWiki parser tests. One should call start() before running the
  28. * full parser tests and end() once all the tests have been finished.
  29. * After each test, you should use record() to keep track of your tests
  30. * results. Finally, report() is used to generate a summary of your
  31. * test run, one could dump it to the console for human consumption or
  32. * register the result in a database for tracking purposes.
  33. *
  34. * @since 1.22
  35. */
  36. interface ITestRecorder {
  37. /**
  38. * Called at beginning of the parser test run
  39. */
  40. public function start();
  41. /**
  42. * Called after each test
  43. * @param string $test
  44. * @param integer $subtest
  45. * @param bool $result
  46. */
  47. public function record( $test, $subtest, $result );
  48. /**
  49. * Called before finishing the test run
  50. */
  51. public function report();
  52. /**
  53. * Called at the end of the parser test run
  54. */
  55. public function end();
  56. }
  57. class TestRecorder implements ITestRecorder {
  58. public $parent;
  59. public $term;
  60. function __construct( $parent ) {
  61. $this->parent = $parent;
  62. $this->term = $parent->term;
  63. }
  64. function start() {
  65. $this->total = 0;
  66. $this->success = 0;
  67. }
  68. function record( $test, $subtest, $result ) {
  69. $this->total++;
  70. $this->success += ( $result ? 1 : 0 );
  71. }
  72. function end() {
  73. // dummy
  74. }
  75. function report() {
  76. if ( $this->total > 0 ) {
  77. $this->reportPercentage( $this->success, $this->total );
  78. } else {
  79. throw new MWException( "No tests found.\n" );
  80. }
  81. }
  82. function reportPercentage( $success, $total ) {
  83. $ratio = wfPercent( 100 * $success / $total );
  84. print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)... ";
  85. if ( $success == $total ) {
  86. print $this->term->color( 32 ) . "ALL TESTS PASSED!";
  87. } else {
  88. $failed = $total - $success;
  89. print $this->term->color( 31 ) . "$failed tests failed!";
  90. }
  91. print $this->term->reset() . "\n";
  92. return ( $success == $total );
  93. }
  94. }
  95. class DbTestPreviewer extends TestRecorder {
  96. protected $lb; // /< Database load balancer
  97. protected $db; // /< Database connection to the main DB
  98. protected $curRun; // /< run ID number for the current run
  99. protected $prevRun; // /< run ID number for the previous run, if any
  100. protected $results; // /< Result array
  101. /**
  102. * This should be called before the table prefix is changed
  103. * @param TestRecorder $parent
  104. */
  105. function __construct( $parent ) {
  106. parent::__construct( $parent );
  107. $this->lb = wfGetLBFactory()->newMainLB();
  108. // This connection will have the wiki's table prefix, not parsertest_
  109. $this->db = $this->lb->getConnection( DB_MASTER );
  110. }
  111. /**
  112. * Set up result recording; insert a record for the run with the date
  113. * and all that fun stuff
  114. */
  115. function start() {
  116. parent::start();
  117. if ( !$this->db->tableExists( 'testrun', __METHOD__ )
  118. || !$this->db->tableExists( 'testitem', __METHOD__ )
  119. ) {
  120. print "WARNING> `testrun` table not found in database.\n";
  121. $this->prevRun = false;
  122. } else {
  123. // We'll make comparisons against the previous run later...
  124. $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
  125. }
  126. $this->results = [];
  127. }
  128. function getName( $test, $subtest ) {
  129. if ( $subtest ) {
  130. return "$test subtest #$subtest";
  131. } else {
  132. return $test;
  133. }
  134. }
  135. function record( $test, $subtest, $result ) {
  136. parent::record( $test, $subtest, $result );
  137. $this->results[ $this->getName( $test, $subtest ) ] = $result;
  138. }
  139. function report() {
  140. if ( $this->prevRun ) {
  141. // f = fail, p = pass, n = nonexistent
  142. // codes show before then after
  143. $table = [
  144. 'fp' => 'previously failing test(s) now PASSING! :)',
  145. 'pn' => 'previously PASSING test(s) removed o_O',
  146. 'np' => 'new PASSING test(s) :)',
  147. 'pf' => 'previously passing test(s) now FAILING! :(',
  148. 'fn' => 'previously FAILING test(s) removed O_o',
  149. 'nf' => 'new FAILING test(s) :(',
  150. 'ff' => 'still FAILING test(s) :(',
  151. ];
  152. $prevResults = [];
  153. $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ],
  154. [ 'ti_run' => $this->prevRun ], __METHOD__ );
  155. foreach ( $res as $row ) {
  156. if ( !$this->parent->regex
  157. || preg_match( "/{$this->parent->regex}/i", $row->ti_name )
  158. ) {
  159. $prevResults[$row->ti_name] = $row->ti_success;
  160. }
  161. }
  162. $combined = array_keys( $this->results + $prevResults );
  163. # Determine breakdown by change type
  164. $breakdown = [];
  165. foreach ( $combined as $test ) {
  166. if ( !isset( $prevResults[$test] ) ) {
  167. $before = 'n';
  168. } elseif ( $prevResults[$test] == 1 ) {
  169. $before = 'p';
  170. } else /* if ( $prevResults[$test] == 0 )*/ {
  171. $before = 'f';
  172. }
  173. if ( !isset( $this->results[$test] ) ) {
  174. $after = 'n';
  175. } elseif ( $this->results[$test] == 1 ) {
  176. $after = 'p';
  177. } else /*if ( $this->results[$test] == 0 ) */ {
  178. $after = 'f';
  179. }
  180. $code = $before . $after;
  181. if ( isset( $table[$code] ) ) {
  182. $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
  183. }
  184. }
  185. # Write out results
  186. foreach ( $table as $code => $label ) {
  187. if ( !empty( $breakdown[$code] ) ) {
  188. $count = count( $breakdown[$code] );
  189. printf( "\n%4d %s\n", $count, $label );
  190. foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
  191. print " * $differing_test_name [$statusInfo]\n";
  192. }
  193. }
  194. }
  195. } else {
  196. print "No previous test runs to compare against.\n";
  197. }
  198. print "\n";
  199. parent::report();
  200. }
  201. /**
  202. * Returns a string giving information about when a test last had a status change.
  203. * Could help to track down when regressions were introduced, as distinct from tests
  204. * which have never passed (which are more change requests than regressions).
  205. * @param string $testname
  206. * @param string $after
  207. * @return string
  208. */
  209. private function getTestStatusInfo( $testname, $after ) {
  210. // If we're looking at a test that has just been removed, then say when it first appeared.
  211. if ( $after == 'n' ) {
  212. $changedRun = $this->db->selectField( 'testitem',
  213. 'MIN(ti_run)',
  214. [ 'ti_name' => $testname ],
  215. __METHOD__ );
  216. $appear = $this->db->selectRow( 'testrun',
  217. [ 'tr_date', 'tr_mw_version' ],
  218. [ 'tr_id' => $changedRun ],
  219. __METHOD__ );
  220. return "First recorded appearance: "
  221. . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
  222. . ", " . $appear->tr_mw_version;
  223. }
  224. // Otherwise, this test has previous recorded results.
  225. // See when this test last had a different result to what we're seeing now.
  226. $conds = [
  227. 'ti_name' => $testname,
  228. 'ti_success' => ( $after == 'f' ? "1" : "0" ) ];
  229. if ( $this->curRun ) {
  230. $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
  231. }
  232. $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
  233. // If no record of ever having had a different result.
  234. if ( is_null( $changedRun ) ) {
  235. if ( $after == "f" ) {
  236. return "Has never passed";
  237. } else {
  238. return "Has never failed";
  239. }
  240. }
  241. // Otherwise, we're looking at a test whose status has changed.
  242. // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
  243. // In this situation, give as much info as we can as to when it changed status.
  244. $pre = $this->db->selectRow( 'testrun',
  245. [ 'tr_date', 'tr_mw_version' ],
  246. [ 'tr_id' => $changedRun ],
  247. __METHOD__ );
  248. $post = $this->db->selectRow( 'testrun',
  249. [ 'tr_date', 'tr_mw_version' ],
  250. [ "tr_id > " . $this->db->addQuotes( $changedRun ) ],
  251. __METHOD__,
  252. [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
  253. );
  254. if ( $post ) {
  255. $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
  256. } else {
  257. $postDate = 'now';
  258. }
  259. return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
  260. . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
  261. . " and $postDate";
  262. }
  263. /**
  264. * Close the DB connection
  265. */
  266. function end() {
  267. $this->lb->closeAll();
  268. parent::end();
  269. }
  270. }
  271. class DbTestRecorder extends DbTestPreviewer {
  272. public $version;
  273. /**
  274. * Set up result recording; insert a record for the run with the date
  275. * and all that fun stuff
  276. */
  277. function start() {
  278. $this->db->begin( __METHOD__ );
  279. if ( !$this->db->tableExists( 'testrun' )
  280. || !$this->db->tableExists( 'testitem' )
  281. ) {
  282. print "WARNING> `testrun` table not found in database. Trying to create table.\n";
  283. $this->db->sourceFile( $this->db->patchPath( 'patch-testrun.sql' ) );
  284. echo "OK, resuming.\n";
  285. }
  286. parent::start();
  287. $this->db->insert( 'testrun',
  288. [
  289. 'tr_date' => $this->db->timestamp(),
  290. 'tr_mw_version' => $this->version,
  291. 'tr_php_version' => PHP_VERSION,
  292. 'tr_db_version' => $this->db->getServerVersion(),
  293. 'tr_uname' => php_uname()
  294. ],
  295. __METHOD__ );
  296. if ( $this->db->getType() === 'postgres' ) {
  297. $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' );
  298. } else {
  299. $this->curRun = $this->db->insertId();
  300. }
  301. }
  302. /**
  303. * Record an individual test item's success or failure to the db
  304. *
  305. * @param string $test
  306. * @param bool $result
  307. */
  308. function record( $test, $subtest, $result ) {
  309. parent::record( $test, $subtest, $result );
  310. $this->db->insert( 'testitem',
  311. [
  312. 'ti_run' => $this->curRun,
  313. 'ti_name' => $this->getName( $test, $subtest ),
  314. 'ti_success' => $result ? 1 : 0,
  315. ],
  316. __METHOD__ );
  317. }
  318. /**
  319. * Commit transaction and clean up for result recording
  320. */
  321. function end() {
  322. $this->db->commit( __METHOD__ );
  323. parent::end();
  324. }
  325. }
  326. class TestFileIterator implements Iterator {
  327. private $file;
  328. private $fh;
  329. /**
  330. * @var ParserTest|MediaWikiParserTest An instance of ParserTest (parserTests.php)
  331. * or MediaWikiParserTest (phpunit)
  332. */
  333. private $parserTest;
  334. private $index = 0;
  335. private $test;
  336. private $section = null;
  337. /** String|null: current test section being analyzed */
  338. private $sectionData = [];
  339. private $lineNum;
  340. private $eof;
  341. # Create a fake parser tests which never run anything unless
  342. # asked to do so. This will avoid running hooks for a disabled test
  343. private $delayedParserTest;
  344. private $nextSubTest = 0;
  345. function __construct( $file, $parserTest ) {
  346. $this->file = $file;
  347. $this->fh = fopen( $this->file, "rt" );
  348. if ( !$this->fh ) {
  349. throw new MWException( "Couldn't open file '$file'\n" );
  350. }
  351. $this->parserTest = $parserTest;
  352. $this->delayedParserTest = new DelayedParserTest();
  353. $this->lineNum = $this->index = 0;
  354. }
  355. function rewind() {
  356. if ( fseek( $this->fh, 0 ) ) {
  357. throw new MWException( "Couldn't fseek to the start of '$this->file'\n" );
  358. }
  359. $this->index = -1;
  360. $this->lineNum = 0;
  361. $this->eof = false;
  362. $this->next();
  363. return true;
  364. }
  365. function current() {
  366. return $this->test;
  367. }
  368. function key() {
  369. return $this->index;
  370. }
  371. function next() {
  372. if ( $this->readNextTest() ) {
  373. $this->index++;
  374. return true;
  375. } else {
  376. $this->eof = true;
  377. }
  378. }
  379. function valid() {
  380. return $this->eof != true;
  381. }
  382. function setupCurrentTest() {
  383. // "input" and "result" are old section names allowed
  384. // for backwards-compatibility.
  385. $input = $this->checkSection( [ 'wikitext', 'input' ], false );
  386. $result = $this->checkSection( [ 'html/php', 'html/*', 'html', 'result' ], false );
  387. // some tests have "with tidy" and "without tidy" variants
  388. $tidy = $this->checkSection( [ 'html/php+tidy', 'html+tidy' ], false );
  389. if ( $tidy != false ) {
  390. if ( $this->nextSubTest == 0 ) {
  391. if ( $result != false ) {
  392. $this->nextSubTest = 1; // rerun non-tidy variant later
  393. }
  394. $result = $tidy;
  395. } else {
  396. $this->nextSubTest = 0; // go on to next test after this
  397. $tidy = false;
  398. }
  399. }
  400. if ( !isset( $this->sectionData['options'] ) ) {
  401. $this->sectionData['options'] = '';
  402. }
  403. if ( !isset( $this->sectionData['config'] ) ) {
  404. $this->sectionData['config'] = '';
  405. }
  406. $isDisabled = preg_match( '/\\bdisabled\\b/i', $this->sectionData['options'] ) &&
  407. !$this->parserTest->runDisabled;
  408. $isParsoidOnly = preg_match( '/\\bparsoid\\b/i', $this->sectionData['options'] ) &&
  409. $result == 'html' &&
  410. !$this->parserTest->runParsoid;
  411. $isFiltered = !preg_match( "/" . $this->parserTest->regex . "/i", $this->sectionData['test'] );
  412. if ( $input == false || $result == false || $isDisabled || $isParsoidOnly || $isFiltered ) {
  413. # disabled test
  414. return false;
  415. }
  416. # We are really going to run the test, run pending hooks and hooks function
  417. wfDebug( __METHOD__ . " unleashing delayed test for: {$this->sectionData['test']}" );
  418. $hooksResult = $this->delayedParserTest->unleash( $this->parserTest );
  419. if ( !$hooksResult ) {
  420. # Some hook reported an issue. Abort.
  421. throw new MWException( "Problem running requested parser hook from the test file" );
  422. }
  423. $this->test = [
  424. 'test' => ParserTest::chomp( $this->sectionData['test'] ),
  425. 'subtest' => $this->nextSubTest,
  426. 'input' => ParserTest::chomp( $this->sectionData[$input] ),
  427. 'result' => ParserTest::chomp( $this->sectionData[$result] ),
  428. 'options' => ParserTest::chomp( $this->sectionData['options'] ),
  429. 'config' => ParserTest::chomp( $this->sectionData['config'] ),
  430. ];
  431. if ( $tidy != false ) {
  432. $this->test['options'] .= " tidy";
  433. }
  434. return true;
  435. }
  436. function readNextTest() {
  437. # Run additional subtests of previous test
  438. while ( $this->nextSubTest > 0 ) {
  439. if ( $this->setupCurrentTest() ) {
  440. return true;
  441. }
  442. }
  443. $this->clearSection();
  444. # Reset hooks for the delayed test object
  445. $this->delayedParserTest->reset();
  446. while ( false !== ( $line = fgets( $this->fh ) ) ) {
  447. $this->lineNum++;
  448. $matches = [];
  449. if ( preg_match( '/^!!\s*(\S+)/', $line, $matches ) ) {
  450. $this->section = strtolower( $matches[1] );
  451. if ( $this->section == 'endarticle' ) {
  452. $this->checkSection( 'text' );
  453. $this->checkSection( 'article' );
  454. $this->parserTest->addArticle(
  455. ParserTest::chomp( $this->sectionData['article'] ),
  456. $this->sectionData['text'], $this->lineNum );
  457. $this->clearSection();
  458. continue;
  459. }
  460. if ( $this->section == 'endhooks' ) {
  461. $this->checkSection( 'hooks' );
  462. foreach ( explode( "\n", $this->sectionData['hooks'] ) as $line ) {
  463. $line = trim( $line );
  464. if ( $line ) {
  465. $this->delayedParserTest->requireHook( $line );
  466. }
  467. }
  468. $this->clearSection();
  469. continue;
  470. }
  471. if ( $this->section == 'endfunctionhooks' ) {
  472. $this->checkSection( 'functionhooks' );
  473. foreach ( explode( "\n", $this->sectionData['functionhooks'] ) as $line ) {
  474. $line = trim( $line );
  475. if ( $line ) {
  476. $this->delayedParserTest->requireFunctionHook( $line );
  477. }
  478. }
  479. $this->clearSection();
  480. continue;
  481. }
  482. if ( $this->section == 'endtransparenthooks' ) {
  483. $this->checkSection( 'transparenthooks' );
  484. foreach ( explode( "\n", $this->sectionData['transparenthooks'] ) as $line ) {
  485. $line = trim( $line );
  486. if ( $line ) {
  487. $this->delayedParserTest->requireTransparentHook( $line );
  488. }
  489. }
  490. $this->clearSection();
  491. continue;
  492. }
  493. if ( $this->section == 'end' ) {
  494. $this->checkSection( 'test' );
  495. do {
  496. if ( $this->setupCurrentTest() ) {
  497. return true;
  498. }
  499. } while ( $this->nextSubTest > 0 );
  500. # go on to next test (since this was disabled)
  501. $this->clearSection();
  502. $this->delayedParserTest->reset();
  503. continue;
  504. }
  505. if ( isset( $this->sectionData[$this->section] ) ) {
  506. throw new MWException( "duplicate section '$this->section' "
  507. . "at line {$this->lineNum} of $this->file\n" );
  508. }
  509. $this->sectionData[$this->section] = '';
  510. continue;
  511. }
  512. if ( $this->section ) {
  513. $this->sectionData[$this->section] .= $line;
  514. }
  515. }
  516. return false;
  517. }
  518. /**
  519. * Clear section name and its data
  520. */
  521. private function clearSection() {
  522. $this->sectionData = [];
  523. $this->section = null;
  524. }
  525. /**
  526. * Verify the current section data has some value for the given token
  527. * name(s) (first parameter).
  528. * Throw an exception if it is not set, referencing current section
  529. * and adding the current file name and line number
  530. *
  531. * @param string|array $tokens Expected token(s) that should have been
  532. * mentioned before closing this section
  533. * @param bool $fatal True iff an exception should be thrown if
  534. * the section is not found.
  535. * @return bool|string
  536. * @throws MWException
  537. */
  538. private function checkSection( $tokens, $fatal = true ) {
  539. if ( is_null( $this->section ) ) {
  540. throw new MWException( __METHOD__ . " can not verify a null section!\n" );
  541. }
  542. if ( !is_array( $tokens ) ) {
  543. $tokens = [ $tokens ];
  544. }
  545. if ( count( $tokens ) == 0 ) {
  546. throw new MWException( __METHOD__ . " can not verify zero sections!\n" );
  547. }
  548. $data = $this->sectionData;
  549. $tokens = array_filter( $tokens, function ( $token ) use ( $data ) {
  550. return isset( $data[$token] );
  551. } );
  552. if ( count( $tokens ) == 0 ) {
  553. if ( !$fatal ) {
  554. return false;
  555. }
  556. throw new MWException( sprintf(
  557. "'%s' without '%s' at line %s of %s\n",
  558. $this->section,
  559. implode( ',', $tokens ),
  560. $this->lineNum,
  561. $this->file
  562. ) );
  563. }
  564. if ( count( $tokens ) > 1 ) {
  565. throw new MWException( sprintf(
  566. "'%s' with unexpected tokens '%s' at line %s of %s\n",
  567. $this->section,
  568. implode( ',', $tokens ),
  569. $this->lineNum,
  570. $this->file
  571. ) );
  572. }
  573. return array_values( $tokens )[0];
  574. }
  575. }
  576. /**
  577. * An iterator for use as a phpunit data provider. Provides the test arguments
  578. * in the order expected by NewParserTest::testParserTest().
  579. */
  580. class TestFileDataProvider extends TestFileIterator {
  581. function current() {
  582. $test = parent::current();
  583. if ( $test ) {
  584. return [
  585. $test['test'],
  586. $test['input'],
  587. $test['result'],
  588. $test['options'],
  589. $test['config'],
  590. ];
  591. } else {
  592. return $test;
  593. }
  594. }
  595. }
  596. /**
  597. * A class to delay execution of a parser test hooks.
  598. */
  599. class DelayedParserTest {
  600. /** Initialized on construction */
  601. private $hooks;
  602. private $fnHooks;
  603. private $transparentHooks;
  604. public function __construct() {
  605. $this->reset();
  606. }
  607. /**
  608. * Init/reset or forgot about the current delayed test.
  609. * Call to this will erase any hooks function that were pending.
  610. */
  611. public function reset() {
  612. $this->hooks = [];
  613. $this->fnHooks = [];
  614. $this->transparentHooks = [];
  615. }
  616. /**
  617. * Called whenever we actually want to run the hook.
  618. * Should be the case if we found the parserTest is not disabled
  619. * @param ParserTest|NewParserTest $parserTest
  620. * @return bool
  621. * @throws MWException
  622. */
  623. public function unleash( &$parserTest ) {
  624. if ( !( $parserTest instanceof ParserTest || $parserTest instanceof NewParserTest ) ) {
  625. throw new MWException( __METHOD__ . " must be passed an instance of ParserTest or "
  626. . "NewParserTest classes\n" );
  627. }
  628. # Trigger delayed hooks. Any failure will make us abort
  629. foreach ( $this->hooks as $hook ) {
  630. $ret = $parserTest->requireHook( $hook );
  631. if ( !$ret ) {
  632. return false;
  633. }
  634. }
  635. # Trigger delayed function hooks. Any failure will make us abort
  636. foreach ( $this->fnHooks as $fnHook ) {
  637. $ret = $parserTest->requireFunctionHook( $fnHook );
  638. if ( !$ret ) {
  639. return false;
  640. }
  641. }
  642. # Trigger delayed transparent hooks. Any failure will make us abort
  643. foreach ( $this->transparentHooks as $hook ) {
  644. $ret = $parserTest->requireTransparentHook( $hook );
  645. if ( !$ret ) {
  646. return false;
  647. }
  648. }
  649. # Delayed execution was successful.
  650. return true;
  651. }
  652. /**
  653. * Similar to ParserTest object but does not run anything
  654. * Use unleash() to really execute the hook
  655. * @param string $hook
  656. */
  657. public function requireHook( $hook ) {
  658. $this->hooks[] = $hook;
  659. }
  660. /**
  661. * Similar to ParserTest object but does not run anything
  662. * Use unleash() to really execute the hook function
  663. * @param string $fnHook
  664. */
  665. public function requireFunctionHook( $fnHook ) {
  666. $this->fnHooks[] = $fnHook;
  667. }
  668. /**
  669. * Similar to ParserTest object but does not run anything
  670. * Use unleash() to really execute the hook function
  671. * @param string $hook
  672. */
  673. public function requireTransparentHook( $hook ) {
  674. $this->transparentHooks[] = $hook;
  675. }
  676. }
  677. /**
  678. * Initialize and detect the DjVu files support
  679. */
  680. class DjVuSupport {
  681. /**
  682. * Initialises DjVu tools global with default values
  683. */
  684. public function __construct() {
  685. global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgFileExtensions, $wgDjvuTxt;
  686. $wgDjvuRenderer = $wgDjvuRenderer ? $wgDjvuRenderer : '/usr/bin/ddjvu';
  687. $wgDjvuDump = $wgDjvuDump ? $wgDjvuDump : '/usr/bin/djvudump';
  688. $wgDjvuToXML = $wgDjvuToXML ? $wgDjvuToXML : '/usr/bin/djvutoxml';
  689. $wgDjvuTxt = $wgDjvuTxt ? $wgDjvuTxt : '/usr/bin/djvutxt';
  690. if ( !in_array( 'djvu', $wgFileExtensions ) ) {
  691. $wgFileExtensions[] = 'djvu';
  692. }
  693. }
  694. /**
  695. * Returns true if the DjVu tools are usable
  696. *
  697. * @return bool
  698. */
  699. public function isEnabled() {
  700. global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML, $wgDjvuTxt;
  701. return is_executable( $wgDjvuRenderer )
  702. && is_executable( $wgDjvuDump )
  703. && is_executable( $wgDjvuToXML )
  704. && is_executable( $wgDjvuTxt );
  705. }
  706. }
  707. /**
  708. * Initialize and detect the tidy support
  709. */
  710. class TidySupport {
  711. private $internalTidy;
  712. private $externalTidy;
  713. /**
  714. * Determine if there is a usable tidy.
  715. */
  716. public function __construct() {
  717. global $wgTidyBin;
  718. $this->internalTidy = extension_loaded( 'tidy' ) &&
  719. class_exists( 'tidy' ) && !wfIsHHVM();
  720. $this->externalTidy = is_executable( $wgTidyBin ) ||
  721. Installer::locateExecutableInDefaultPaths( [ $wgTidyBin ] )
  722. !== false;
  723. }
  724. /**
  725. * Returns true if we should use internal tidy.
  726. *
  727. * @return bool
  728. */
  729. public function isInternal() {
  730. return $this->internalTidy;
  731. }
  732. /**
  733. * Returns true if tidy is usable
  734. *
  735. * @return bool
  736. */
  737. public function isEnabled() {
  738. return $this->internalTidy || $this->externalTidy;
  739. }
  740. }