Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / specials / SpecialPageExecutor.php
blob2f7b7678cad25dd46699aa39d0f0027bcb573059
1 <?php
3 /**
4 * @author Addshore
6 * @since 1.27
7 */
8 class SpecialPageExecutor {
10 /**
11 * @param SpecialPage $page The special page to execute
12 * @param string $subPage The subpage parameter to call the page with
13 * @param WebRequest|null $request Web request that may contain URL parameters, etc
14 * @param Language|string|null $language The language which should be used in the context
15 * @param User|null $user The user which should be used in the context of this special page
17 * @throws Exception
18 * @return array( string, WebResponse ) A two-elements array containing the HTML output
19 * generated by the special page as well as the response object.
21 public function executeSpecialPage(
22 SpecialPage $page,
23 $subPage = '',
24 WebRequest $request = null,
25 $language = null,
26 User $user = null
27 ) {
28 $context = $this->newContext( $request, $language, $user );
30 $output = new OutputPage( $context );
31 $context->setOutput( $output );
33 $page->setContext( $context );
34 $output->setTitle( $page->getPageTitle() );
36 $html = $this->getHTMLFromSpecialPage( $page, $subPage );
37 $response = $context->getRequest()->response();
39 if ( $response instanceof FauxResponse ) {
40 $code = $response->getStatusCode();
42 if ( $code > 0 ) {
43 $response->header( 'Status: ' . $code . ' ' . HttpStatus::getMessage( $code ) );
47 return [ $html, $response ];
50 /**
51 * @param WebRequest|null $request
52 * @param Language|string|null $language
53 * @param User|null $user
55 * @return DerivativeContext
57 private function newContext(
58 WebRequest $request = null,
59 $language = null,
60 User $user = null
61 ) {
62 $context = new DerivativeContext( RequestContext::getMain() );
64 $context->setRequest( $request ?: new FauxRequest() );
66 if ( $language !== null ) {
67 $context->setLanguage( $language );
70 if ( $user !== null ) {
71 $context->setUser( $user );
74 $this->setEditTokenFromUser( $context );
76 return $context;
79 /**
80 * If we are trying to edit and no token is set, supply one.
82 * @param DerivativeContext $context
84 private function setEditTokenFromUser( DerivativeContext $context ) {
85 $request = $context->getRequest();
87 // Edits via GET are a security issue and should not succeed. On the other hand, not all
88 // POST requests are edits, but should ignore unused parameters.
89 if ( !$request->getCheck( 'wpEditToken' ) && $request->wasPosted() ) {
90 $request->setVal( 'wpEditToken', $context->getUser()->getEditToken() );
94 /**
95 * @param SpecialPage $page
96 * @param string $subPage
98 * @throws Exception
99 * @return string HTML
101 private function getHTMLFromSpecialPage( SpecialPage $page, $subPage ) {
102 ob_start();
104 try {
105 $page->execute( $subPage );
107 $output = $page->getOutput();
109 if ( $output->getRedirect() !== '' ) {
110 $output->output();
111 $html = ob_get_contents();
112 } elseif ( $output->isDisabled() ) {
113 $html = ob_get_contents();
114 } else {
115 $html = $output->getHTML();
117 } catch ( Exception $ex ) {
118 ob_end_clean();
120 // Re-throw exception after "finally" handling because PHP 5.3 doesn't have "finally".
121 throw $ex;
124 ob_end_clean();
126 return $html;