8 class SpecialPageExecutor
{
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
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(
24 WebRequest
$request = null,
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();
43 $response->header( 'Status: ' . $code . ' ' . HttpStatus
::getMessage( $code ) );
47 return [ $html, $response ];
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,
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 );
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() );
95 * @param SpecialPage $page
96 * @param string $subPage
101 private function getHTMLFromSpecialPage( SpecialPage
$page, $subPage ) {
105 $page->execute( $subPage );
107 $output = $page->getOutput();
109 if ( $output->getRedirect() !== '' ) {
111 $html = ob_get_contents();
112 } elseif ( $output->isDisabled() ) {
113 $html = ob_get_contents();
115 $html = $output->getHTML();
117 } catch ( Exception
$ex ) {
120 // Re-throw exception after "finally" handling because PHP 5.3 doesn't have "finally".