Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ApiMain often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApiMain, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ApiMain extends ApiBase { |
||
| 42 | /** |
||
| 43 | * When no format parameter is given, this format will be used |
||
| 44 | */ |
||
| 45 | const API_DEFAULT_FORMAT = 'jsonfm'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of available modules: action name => module class |
||
| 49 | */ |
||
| 50 | private static $Modules = [ |
||
| 51 | 'login' => 'ApiLogin', |
||
| 52 | 'clientlogin' => 'ApiClientLogin', |
||
| 53 | 'logout' => 'ApiLogout', |
||
| 54 | 'createaccount' => 'ApiAMCreateAccount', |
||
| 55 | 'linkaccount' => 'ApiLinkAccount', |
||
| 56 | 'unlinkaccount' => 'ApiRemoveAuthenticationData', |
||
| 57 | 'changeauthenticationdata' => 'ApiChangeAuthenticationData', |
||
| 58 | 'removeauthenticationdata' => 'ApiRemoveAuthenticationData', |
||
| 59 | 'resetpassword' => 'ApiResetPassword', |
||
| 60 | 'query' => 'ApiQuery', |
||
| 61 | 'expandtemplates' => 'ApiExpandTemplates', |
||
| 62 | 'parse' => 'ApiParse', |
||
| 63 | 'stashedit' => 'ApiStashEdit', |
||
| 64 | 'opensearch' => 'ApiOpenSearch', |
||
| 65 | 'feedcontributions' => 'ApiFeedContributions', |
||
| 66 | 'feedrecentchanges' => 'ApiFeedRecentChanges', |
||
| 67 | 'feedwatchlist' => 'ApiFeedWatchlist', |
||
| 68 | 'help' => 'ApiHelp', |
||
| 69 | 'paraminfo' => 'ApiParamInfo', |
||
| 70 | 'rsd' => 'ApiRsd', |
||
| 71 | 'compare' => 'ApiComparePages', |
||
| 72 | 'tokens' => 'ApiTokens', |
||
| 73 | 'checktoken' => 'ApiCheckToken', |
||
| 74 | |||
| 75 | // Write modules |
||
| 76 | 'purge' => 'ApiPurge', |
||
| 77 | 'setnotificationtimestamp' => 'ApiSetNotificationTimestamp', |
||
| 78 | 'rollback' => 'ApiRollback', |
||
| 79 | 'delete' => 'ApiDelete', |
||
| 80 | 'undelete' => 'ApiUndelete', |
||
| 81 | 'protect' => 'ApiProtect', |
||
| 82 | 'block' => 'ApiBlock', |
||
| 83 | 'unblock' => 'ApiUnblock', |
||
| 84 | 'move' => 'ApiMove', |
||
| 85 | 'edit' => 'ApiEditPage', |
||
| 86 | 'upload' => 'ApiUpload', |
||
| 87 | 'filerevert' => 'ApiFileRevert', |
||
| 88 | 'emailuser' => 'ApiEmailUser', |
||
| 89 | 'watch' => 'ApiWatch', |
||
| 90 | 'patrol' => 'ApiPatrol', |
||
| 91 | 'import' => 'ApiImport', |
||
| 92 | 'clearhasmsg' => 'ApiClearHasMsg', |
||
| 93 | 'userrights' => 'ApiUserrights', |
||
| 94 | 'options' => 'ApiOptions', |
||
| 95 | 'imagerotate' => 'ApiImageRotate', |
||
| 96 | 'revisiondelete' => 'ApiRevisionDelete', |
||
| 97 | 'managetags' => 'ApiManageTags', |
||
| 98 | 'tag' => 'ApiTag', |
||
| 99 | 'mergehistory' => 'ApiMergeHistory', |
||
| 100 | ]; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * List of available formats: format name => format class |
||
| 104 | */ |
||
| 105 | private static $Formats = [ |
||
| 106 | 'json' => 'ApiFormatJson', |
||
| 107 | 'jsonfm' => 'ApiFormatJson', |
||
| 108 | 'php' => 'ApiFormatPhp', |
||
| 109 | 'phpfm' => 'ApiFormatPhp', |
||
| 110 | 'xml' => 'ApiFormatXml', |
||
| 111 | 'xmlfm' => 'ApiFormatXml', |
||
| 112 | 'rawfm' => 'ApiFormatJson', |
||
| 113 | 'none' => 'ApiFormatNone', |
||
| 114 | ]; |
||
| 115 | |||
| 116 | // @codingStandardsIgnoreStart String contenation on "msg" not allowed to break long line |
||
| 117 | /** |
||
| 118 | * List of user roles that are specifically relevant to the API. |
||
| 119 | * array( 'right' => array ( 'msg' => 'Some message with a $1', |
||
| 120 | * 'params' => array ( $someVarToSubst ) ), |
||
| 121 | * ); |
||
| 122 | */ |
||
| 123 | private static $mRights = [ |
||
| 124 | 'writeapi' => [ |
||
| 125 | 'msg' => 'right-writeapi', |
||
| 126 | 'params' => [] |
||
| 127 | ], |
||
| 128 | 'apihighlimits' => [ |
||
| 129 | 'msg' => 'api-help-right-apihighlimits', |
||
| 130 | 'params' => [ ApiBase::LIMIT_SML2, ApiBase::LIMIT_BIG2 ] |
||
| 131 | ] |
||
| 132 | ]; |
||
| 133 | // @codingStandardsIgnoreEnd |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var ApiFormatBase |
||
| 137 | */ |
||
| 138 | private $mPrinter; |
||
| 139 | |||
| 140 | private $mModuleMgr, $mResult, $mErrorFormatter, $mContinuationManager; |
||
|
|
|||
| 141 | private $mAction; |
||
| 142 | private $mEnableWrite; |
||
| 143 | private $mInternalMode, $mSquidMaxage; |
||
| 144 | /** @var ApiBase */ |
||
| 145 | private $mModule; |
||
| 146 | |||
| 147 | private $mCacheMode = 'private'; |
||
| 148 | private $mCacheControl = []; |
||
| 149 | private $mParamsUsed = []; |
||
| 150 | |||
| 151 | /** @var bool|null Cached return value from self::lacksSameOriginSecurity() */ |
||
| 152 | private $lacksSameOriginSecurity = null; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Constructs an instance of ApiMain that utilizes the module and format specified by $request. |
||
| 156 | * |
||
| 157 | * @param IContextSource|WebRequest $context If this is an instance of |
||
| 158 | * FauxRequest, errors are thrown and no printing occurs |
||
| 159 | * @param bool $enableWrite Should be set to true if the api may modify data |
||
| 160 | */ |
||
| 161 | public function __construct( $context = null, $enableWrite = false ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return true if the API was started by other PHP code using FauxRequest |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isInternalMode() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the ApiResult object associated with current request |
||
| 244 | * |
||
| 245 | * @return ApiResult |
||
| 246 | */ |
||
| 247 | public function getResult() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the security flag for the current request |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function lacksSameOriginSecurity() { |
||
| 256 | if ( $this->lacksSameOriginSecurity !== null ) { |
||
| 257 | return $this->lacksSameOriginSecurity; |
||
| 258 | } |
||
| 259 | |||
| 260 | $request = $this->getRequest(); |
||
| 261 | |||
| 262 | // JSONP mode |
||
| 263 | if ( $request->getVal( 'callback' ) !== null ) { |
||
| 264 | $this->lacksSameOriginSecurity = true; |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 268 | // Header to be used from XMLHTTPRequest when the request might |
||
| 269 | // otherwise be used for XSS. |
||
| 270 | if ( $request->getHeader( 'Treat-as-Untrusted' ) !== false ) { |
||
| 271 | $this->lacksSameOriginSecurity = true; |
||
| 272 | return true; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Allow extensions to override. |
||
| 276 | $this->lacksSameOriginSecurity = !Hooks::run( 'RequestHasSameOriginSecurity', [ $request ] ); |
||
| 277 | return $this->lacksSameOriginSecurity; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the ApiErrorFormatter object associated with current request |
||
| 282 | * @return ApiErrorFormatter |
||
| 283 | */ |
||
| 284 | public function getErrorFormatter() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the continuation manager |
||
| 290 | * @return ApiContinuationManager|null |
||
| 291 | */ |
||
| 292 | public function getContinuationManager() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the continuation manager |
||
| 298 | * @param ApiContinuationManager|null |
||
| 299 | */ |
||
| 300 | public function setContinuationManager( $manager ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get the API module object. Only works after executeAction() |
||
| 319 | * |
||
| 320 | * @return ApiBase |
||
| 321 | */ |
||
| 322 | public function getModule() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get the result formatter object. Only works after setupExecuteAction() |
||
| 328 | * |
||
| 329 | * @return ApiFormatBase |
||
| 330 | */ |
||
| 331 | public function getPrinter() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set how long the response should be cached. |
||
| 337 | * |
||
| 338 | * @param int $maxage |
||
| 339 | */ |
||
| 340 | public function setCacheMaxAge( $maxage ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the type of caching headers which will be sent. |
||
| 349 | * |
||
| 350 | * @param string $mode One of: |
||
| 351 | * - 'public': Cache this object in public caches, if the maxage or smaxage |
||
| 352 | * parameter is set, or if setCacheMaxAge() was called. If a maximum age is |
||
| 353 | * not provided by any of these means, the object will be private. |
||
| 354 | * - 'private': Cache this object only in private client-side caches. |
||
| 355 | * - 'anon-public-user-private': Make this object cacheable for logged-out |
||
| 356 | * users, but private for logged-in users. IMPORTANT: If this is set, it must be |
||
| 357 | * set consistently for a given URL, it cannot be set differently depending on |
||
| 358 | * things like the contents of the database, or whether the user is logged in. |
||
| 359 | * |
||
| 360 | * If the wiki does not allow anonymous users to read it, the mode set here |
||
| 361 | * will be ignored, and private caching headers will always be sent. In other words, |
||
| 362 | * the "public" mode is equivalent to saying that the data sent is as public as a page |
||
| 363 | * view. |
||
| 364 | * |
||
| 365 | * For user-dependent data, the private mode should generally be used. The |
||
| 366 | * anon-public-user-private mode should only be used where there is a particularly |
||
| 367 | * good performance reason for caching the anonymous response, but where the |
||
| 368 | * response to logged-in users may differ, or may contain private data. |
||
| 369 | * |
||
| 370 | * If this function is never called, then the default will be the private mode. |
||
| 371 | */ |
||
| 372 | public function setCacheMode( $mode ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set directives (key/value pairs) for the Cache-Control header. |
||
| 405 | * Boolean values will be formatted as such, by including or omitting |
||
| 406 | * without an equals sign. |
||
| 407 | * |
||
| 408 | * Cache control values set here will only be used if the cache mode is not |
||
| 409 | * private, see setCacheMode(). |
||
| 410 | * |
||
| 411 | * @param array $directives |
||
| 412 | */ |
||
| 413 | public function setCacheControl( $directives ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Create an instance of an output formatter by its name |
||
| 419 | * |
||
| 420 | * @param string $format |
||
| 421 | * |
||
| 422 | * @return ApiFormatBase |
||
| 423 | */ |
||
| 424 | public function createPrinterByName( $format ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Execute api request. Any errors will be handled if the API was called by the remote client. |
||
| 435 | */ |
||
| 436 | public function execute() { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Execute an action, and in case of an error, erase whatever partial results |
||
| 446 | * have been accumulated, and replace it with an error message and a help screen. |
||
| 447 | */ |
||
| 448 | protected function executeActionWithErrorHandling() { |
||
| 449 | // Verify the CORS header before executing the action |
||
| 450 | if ( !$this->handleCORS() ) { |
||
| 451 | // handleCORS() has sent a 403, abort |
||
| 452 | return; |
||
| 453 | } |
||
| 454 | |||
| 455 | // Exit here if the request method was OPTIONS |
||
| 456 | // (assume there will be a followup GET or POST) |
||
| 457 | if ( $this->getRequest()->getMethod() === 'OPTIONS' ) { |
||
| 458 | return; |
||
| 459 | } |
||
| 460 | |||
| 461 | // In case an error occurs during data output, |
||
| 462 | // clear the output buffer and print just the error information |
||
| 463 | $obLevel = ob_get_level(); |
||
| 464 | ob_start(); |
||
| 465 | |||
| 466 | $t = microtime( true ); |
||
| 467 | $isError = false; |
||
| 468 | try { |
||
| 469 | $this->executeAction(); |
||
| 470 | $runTime = microtime( true ) - $t; |
||
| 471 | $this->logRequest( $runTime ); |
||
| 472 | if ( $this->mModule->isWriteMode() && $this->getRequest()->wasPosted() ) { |
||
| 473 | $this->getStats()->timing( |
||
| 474 | 'api.' . $this->mModule->getModuleName() . '.executeTiming', 1000 * $runTime |
||
| 475 | ); |
||
| 476 | } |
||
| 477 | } catch ( Exception $e ) { |
||
| 478 | $this->handleException( $e ); |
||
| 479 | $this->logRequest( microtime( true ) - $t, $e ); |
||
| 480 | $isError = true; |
||
| 481 | } |
||
| 482 | |||
| 483 | // Commit DBs and send any related cookies and headers |
||
| 484 | MediaWiki::preOutputCommit( $this->getContext() ); |
||
| 485 | |||
| 486 | // Send cache headers after any code which might generate an error, to |
||
| 487 | // avoid sending public cache headers for errors. |
||
| 488 | $this->sendCacheHeaders( $isError ); |
||
| 489 | |||
| 490 | // Executing the action might have already messed with the output |
||
| 491 | // buffers. |
||
| 492 | while ( ob_get_level() > $obLevel ) { |
||
| 493 | ob_end_flush(); |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Handle an exception as an API response |
||
| 499 | * |
||
| 500 | * @since 1.23 |
||
| 501 | * @param Exception $e |
||
| 502 | */ |
||
| 503 | protected function handleException( Exception $e ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Handle an exception from the ApiBeforeMain hook. |
||
| 564 | * |
||
| 565 | * This tries to print the exception as an API response, to be more |
||
| 566 | * friendly to clients. If it fails, it will rethrow the exception. |
||
| 567 | * |
||
| 568 | * @since 1.23 |
||
| 569 | * @param Exception $e |
||
| 570 | * @throws Exception |
||
| 571 | */ |
||
| 572 | public static function handleApiBeforeMainException( Exception $e ) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Check the &origin= query parameter against the Origin: HTTP header and respond appropriately. |
||
| 592 | * |
||
| 593 | * If no origin parameter is present, nothing happens. |
||
| 594 | * If an origin parameter is present but doesn't match the Origin header, a 403 status code |
||
| 595 | * is set and false is returned. |
||
| 596 | * If the parameter and the header do match, the header is checked against $wgCrossSiteAJAXdomains |
||
| 597 | * and $wgCrossSiteAJAXdomainExceptions, and if the origin qualifies, the appropriate CORS |
||
| 598 | * headers are set. |
||
| 599 | * http://www.w3.org/TR/cors/#resource-requests |
||
| 600 | * http://www.w3.org/TR/cors/#resource-preflight-requests |
||
| 601 | * |
||
| 602 | * @return bool False if the caller should abort (403 case), true otherwise (all other cases) |
||
| 603 | */ |
||
| 604 | protected function handleCORS() { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Attempt to match an Origin header against a set of rules and a set of exceptions |
||
| 680 | * @param string $value Origin header |
||
| 681 | * @param array $rules Set of wildcard rules |
||
| 682 | * @param array $exceptions Set of wildcard rules |
||
| 683 | * @return bool True if $value matches a rule in $rules and doesn't match |
||
| 684 | * any rules in $exceptions, false otherwise |
||
| 685 | */ |
||
| 686 | protected static function matchOrigin( $value, $rules, $exceptions ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Attempt to validate the value of Access-Control-Request-Headers against a list |
||
| 705 | * of headers that we allow the follow up request to send. |
||
| 706 | * |
||
| 707 | * @param string $requestedHeaders Comma seperated list of HTTP headers |
||
| 708 | * @return bool True if all requested headers are in the list of allowed headers |
||
| 709 | */ |
||
| 710 | protected static function matchRequestedHeaders( $requestedHeaders ) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Helper function to convert wildcard string into a regex |
||
| 740 | * '*' => '.*?' |
||
| 741 | * '?' => '.' |
||
| 742 | * |
||
| 743 | * @param string $wildcard String with wildcards |
||
| 744 | * @return string Regular expression |
||
| 745 | */ |
||
| 746 | protected static function wildcardToRegex( $wildcard ) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Send caching headers |
||
| 759 | * @param bool $isError Whether an error response is being output |
||
| 760 | * @since 1.26 added $isError parameter |
||
| 761 | */ |
||
| 762 | protected function sendCacheHeaders( $isError ) { |
||
| 763 | $response = $this->getRequest()->response(); |
||
| 764 | $out = $this->getOutput(); |
||
| 765 | |||
| 766 | $out->addVaryHeader( 'Treat-as-Untrusted' ); |
||
| 767 | |||
| 768 | $config = $this->getConfig(); |
||
| 769 | |||
| 770 | if ( $config->get( 'VaryOnXFP' ) ) { |
||
| 771 | $out->addVaryHeader( 'X-Forwarded-Proto' ); |
||
| 772 | } |
||
| 773 | |||
| 774 | if ( !$isError && $this->mModule && |
||
| 775 | ( $this->getRequest()->getMethod() === 'GET' || $this->getRequest()->getMethod() === 'HEAD' ) |
||
| 776 | ) { |
||
| 777 | $etag = $this->mModule->getConditionalRequestData( 'etag' ); |
||
| 778 | if ( $etag !== null ) { |
||
| 779 | $response->header( "ETag: $etag" ); |
||
| 780 | } |
||
| 781 | $lastMod = $this->mModule->getConditionalRequestData( 'last-modified' ); |
||
| 782 | if ( $lastMod !== null ) { |
||
| 783 | $response->header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $lastMod ) ); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | |||
| 787 | // The logic should be: |
||
| 788 | // $this->mCacheControl['max-age'] is set? |
||
| 789 | // Use it, the module knows better than our guess. |
||
| 790 | // !$this->mModule || $this->mModule->isWriteMode(), and mCacheMode is private? |
||
| 791 | // Use 0 because we can guess caching is probably the wrong thing to do. |
||
| 792 | // Use $this->getParameter( 'maxage' ), which already defaults to 0. |
||
| 793 | $maxage = 0; |
||
| 794 | if ( isset( $this->mCacheControl['max-age'] ) ) { |
||
| 795 | $maxage = $this->mCacheControl['max-age']; |
||
| 796 | } elseif ( ( $this->mModule && !$this->mModule->isWriteMode() ) || |
||
| 797 | $this->mCacheMode !== 'private' |
||
| 798 | ) { |
||
| 799 | $maxage = $this->getParameter( 'maxage' ); |
||
| 800 | } |
||
| 801 | $privateCache = 'private, must-revalidate, max-age=' . $maxage; |
||
| 802 | |||
| 803 | if ( $this->mCacheMode == 'private' ) { |
||
| 804 | $response->header( "Cache-Control: $privateCache" ); |
||
| 805 | return; |
||
| 806 | } |
||
| 807 | |||
| 808 | $useKeyHeader = $config->get( 'UseKeyHeader' ); |
||
| 809 | if ( $this->mCacheMode == 'anon-public-user-private' ) { |
||
| 810 | $out->addVaryHeader( 'Cookie' ); |
||
| 811 | $response->header( $out->getVaryHeader() ); |
||
| 812 | if ( $useKeyHeader ) { |
||
| 813 | $response->header( $out->getKeyHeader() ); |
||
| 814 | if ( $out->haveCacheVaryCookies() ) { |
||
| 815 | // Logged in, mark this request private |
||
| 816 | $response->header( "Cache-Control: $privateCache" ); |
||
| 817 | return; |
||
| 818 | } |
||
| 819 | // Logged out, send normal public headers below |
||
| 820 | } elseif ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) { |
||
| 821 | // Logged in or otherwise has session (e.g. anonymous users who have edited) |
||
| 822 | // Mark request private |
||
| 823 | $response->header( "Cache-Control: $privateCache" ); |
||
| 824 | |||
| 825 | return; |
||
| 826 | } // else no Key and anonymous, send public headers below |
||
| 827 | } |
||
| 828 | |||
| 829 | // Send public headers |
||
| 830 | $response->header( $out->getVaryHeader() ); |
||
| 831 | if ( $useKeyHeader ) { |
||
| 832 | $response->header( $out->getKeyHeader() ); |
||
| 833 | } |
||
| 834 | |||
| 835 | // If nobody called setCacheMaxAge(), use the (s)maxage parameters |
||
| 836 | if ( !isset( $this->mCacheControl['s-maxage'] ) ) { |
||
| 837 | $this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' ); |
||
| 838 | } |
||
| 839 | if ( !isset( $this->mCacheControl['max-age'] ) ) { |
||
| 840 | $this->mCacheControl['max-age'] = $this->getParameter( 'maxage' ); |
||
| 841 | } |
||
| 842 | |||
| 843 | if ( !$this->mCacheControl['s-maxage'] && !$this->mCacheControl['max-age'] ) { |
||
| 844 | // Public cache not requested |
||
| 845 | // Sending a Vary header in this case is harmless, and protects us |
||
| 846 | // against conditional calls of setCacheMaxAge(). |
||
| 847 | $response->header( "Cache-Control: $privateCache" ); |
||
| 848 | |||
| 849 | return; |
||
| 850 | } |
||
| 851 | |||
| 852 | $this->mCacheControl['public'] = true; |
||
| 853 | |||
| 854 | // Send an Expires header |
||
| 855 | $maxAge = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] ); |
||
| 856 | $expiryUnixTime = ( $maxAge == 0 ? 1 : time() + $maxAge ); |
||
| 857 | $response->header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) ); |
||
| 858 | |||
| 859 | // Construct the Cache-Control header |
||
| 860 | $ccHeader = ''; |
||
| 861 | $separator = ''; |
||
| 862 | foreach ( $this->mCacheControl as $name => $value ) { |
||
| 863 | if ( is_bool( $value ) ) { |
||
| 864 | if ( $value ) { |
||
| 865 | $ccHeader .= $separator . $name; |
||
| 866 | $separator = ', '; |
||
| 867 | } |
||
| 868 | } else { |
||
| 869 | $ccHeader .= $separator . "$name=$value"; |
||
| 870 | $separator = ', '; |
||
| 871 | } |
||
| 872 | } |
||
| 873 | |||
| 874 | $response->header( "Cache-Control: $ccHeader" ); |
||
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Create the printer for error output |
||
| 879 | */ |
||
| 880 | private function createErrorPrinter() { |
||
| 895 | |||
| 896 | /** |
||
| 897 | * Create an error message for the given exception. |
||
| 898 | * |
||
| 899 | * If the exception is a UsageException then |
||
| 900 | * UsageException::getMessageArray() will be called to create the message. |
||
| 901 | * |
||
| 902 | * @param Exception $e |
||
| 903 | * @return array ['code' => 'some string', 'info' => 'some other string'] |
||
| 904 | * @since 1.27 |
||
| 905 | */ |
||
| 906 | protected function errorMessageFromException( $e ) { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Replace the result data with the information about an exception. |
||
| 929 | * Returns the error code |
||
| 930 | * @param Exception $e |
||
| 931 | * @return string |
||
| 932 | */ |
||
| 933 | protected function substituteResultWithError( $e ) { |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Set up for the execution. |
||
| 977 | * @return array |
||
| 978 | */ |
||
| 979 | protected function setupExecuteAction() { |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Set up the module for response |
||
| 1012 | * @return ApiBase The module that will handle this action |
||
| 1013 | * @throws MWException |
||
| 1014 | * @throws UsageException |
||
| 1015 | */ |
||
| 1016 | protected function setupModule() { |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Check the max lag if necessary |
||
| 1065 | * @param ApiBase $module Api module being used |
||
| 1066 | * @param array $params Array an array containing the request parameters. |
||
| 1067 | * @return bool True on success, false should exit immediately |
||
| 1068 | */ |
||
| 1069 | protected function checkMaxLag( $module, $params ) { |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * Check selected RFC 7232 precondition headers |
||
| 1092 | * |
||
| 1093 | * RFC 7232 envisions a particular model where you send your request to "a |
||
| 1094 | * resource", and for write requests that you can read "the resource" by |
||
| 1095 | * changing the method to GET. When the API receives a GET request, it |
||
| 1096 | * works out even though "the resource" from RFC 7232's perspective might |
||
| 1097 | * be many resources from MediaWiki's perspective. But it totally fails for |
||
| 1098 | * a POST, since what HTTP sees as "the resource" is probably just |
||
| 1099 | * "/api.php" with all the interesting bits in the body. |
||
| 1100 | * |
||
| 1101 | * Therefore, we only support RFC 7232 precondition headers for GET (and |
||
| 1102 | * HEAD). That means we don't need to bother with If-Match and |
||
| 1103 | * If-Unmodified-Since since they only apply to modification requests. |
||
| 1104 | * |
||
| 1105 | * And since we don't support Range, If-Range is ignored too. |
||
| 1106 | * |
||
| 1107 | * @since 1.26 |
||
| 1108 | * @param ApiBase $module Api module being used |
||
| 1109 | * @return bool True on success, false should exit immediately |
||
| 1110 | */ |
||
| 1111 | protected function checkConditionalRequestHeaders( $module ) { |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Check for sufficient permissions to execute |
||
| 1207 | * @param ApiBase $module An Api module |
||
| 1208 | */ |
||
| 1209 | protected function checkExecutePermissions( $module ) { |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Check if the DB is read-only for this user |
||
| 1241 | * @param ApiBase $module An Api module |
||
| 1242 | */ |
||
| 1243 | protected function checkReadOnly( $module ) { |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * Check whether we are readonly for bots |
||
| 1258 | */ |
||
| 1259 | private function checkBotReadOnly() { |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * Check asserts of the user's rights |
||
| 1294 | * @param array $params |
||
| 1295 | */ |
||
| 1296 | protected function checkAsserts( $params ) { |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Check POST for external response and setup result printer |
||
| 1316 | * @param ApiBase $module An Api module |
||
| 1317 | * @param array $params An array with the request parameters |
||
| 1318 | */ |
||
| 1319 | protected function setupExternalResponse( $module, $params ) { |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Execute the actual module, without any error handling |
||
| 1346 | */ |
||
| 1347 | protected function executeAction() { |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Set database connection, query, and write expectations given this module request |
||
| 1389 | * @param ApiBase $module |
||
| 1390 | */ |
||
| 1391 | protected function setRequestExpectations( ApiBase $module ) { |
||
| 1392 | $limits = $this->getConfig()->get( 'TrxProfilerLimits' ); |
||
| 1393 | $trxProfiler = Profiler::instance()->getTransactionProfiler(); |
||
| 1394 | if ( $this->getRequest()->hasSafeMethod() ) { |
||
| 1395 | $trxProfiler->setExpectations( $limits['GET'], __METHOD__ ); |
||
| 1396 | } elseif ( $this->getRequest()->wasPosted() && !$module->isWriteMode() ) { |
||
| 1397 | $trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ ); |
||
| 1398 | $this->getRequest()->markAsSafeRequest(); |
||
| 1399 | } else { |
||
| 1400 | $trxProfiler->setExpectations( $limits['POST'], __METHOD__ ); |
||
| 1401 | } |
||
| 1402 | } |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Log the preceding request |
||
| 1406 | * @param float $time Time in seconds |
||
| 1407 | * @param Exception $e Exception caught while processing the request |
||
| 1408 | */ |
||
| 1409 | protected function logRequest( $time, $e = null ) { |
||
| 1453 | |||
| 1454 | /** |
||
| 1455 | * Encode a value in a format suitable for a space-separated log line. |
||
| 1456 | * @param string $s |
||
| 1457 | * @return string |
||
| 1458 | */ |
||
| 1459 | protected function encodeRequestLogValue( $s ) { |
||
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Get the request parameters used in the course of the preceding execute() request |
||
| 1474 | * @return array |
||
| 1475 | */ |
||
| 1476 | protected function getParamsUsed() { |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Mark parameters as used |
||
| 1482 | * @param string|string[] $params |
||
| 1483 | */ |
||
| 1484 | public function markParamsUsed( $params ) { |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Get a request value, and register the fact that it was used, for logging. |
||
| 1490 | * @param string $name |
||
| 1491 | * @param mixed $default |
||
| 1492 | * @return mixed |
||
| 1493 | */ |
||
| 1494 | public function getVal( $name, $default = null ) { |
||
| 1510 | |||
| 1511 | /** |
||
| 1512 | * Get a boolean request value, and register the fact that the parameter |
||
| 1513 | * was used, for logging. |
||
| 1514 | * @param string $name |
||
| 1515 | * @return bool |
||
| 1516 | */ |
||
| 1517 | public function getCheck( $name ) { |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * Get a request upload, and register the fact that it was used, for logging. |
||
| 1523 | * |
||
| 1524 | * @since 1.21 |
||
| 1525 | * @param string $name Parameter name |
||
| 1526 | * @return WebRequestUpload |
||
| 1527 | */ |
||
| 1528 | public function getUpload( $name ) { |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Report unused parameters, so the client gets a hint in case it gave us parameters we don't know, |
||
| 1536 | * for example in case of spelling mistakes or a missing 'g' prefix for generators. |
||
| 1537 | */ |
||
| 1538 | protected function reportUnusedParams() { |
||
| 1558 | |||
| 1559 | /** |
||
| 1560 | * Print results using the current printer |
||
| 1561 | * |
||
| 1562 | * @param bool $isError |
||
| 1563 | */ |
||
| 1564 | protected function printResult( $isError ) { |
||
| 1574 | |||
| 1575 | /** |
||
| 1576 | * @return bool |
||
| 1577 | */ |
||
| 1578 | public function isReadMode() { |
||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * See ApiBase for description. |
||
| 1584 | * |
||
| 1585 | * @return array |
||
| 1586 | */ |
||
| 1587 | public function getAllowedParams() { |
||
| 1620 | |||
| 1621 | /** @see ApiBase::getExamplesMessages() */ |
||
| 1622 | protected function getExamplesMessages() { |
||
| 1630 | |||
| 1631 | public function modifyHelp( array &$help, array $options, array &$tocData ) { |
||
| 1728 | |||
| 1729 | private $mCanApiHighLimits = null; |
||
| 1730 | |||
| 1731 | /** |
||
| 1732 | * Check whether the current user is allowed to use high limits |
||
| 1733 | * @return bool |
||
| 1734 | */ |
||
| 1735 | public function canApiHighLimits() { |
||
| 1742 | |||
| 1743 | /** |
||
| 1744 | * Overrides to return this instance's module manager. |
||
| 1745 | * @return ApiModuleManager |
||
| 1746 | */ |
||
| 1747 | public function getModuleManager() { |
||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * Fetches the user agent used for this request |
||
| 1753 | * |
||
| 1754 | * The value will be the combination of the 'Api-User-Agent' header (if |
||
| 1755 | * any) and the standard User-Agent header (if any). |
||
| 1756 | * |
||
| 1757 | * @return string |
||
| 1758 | */ |
||
| 1759 | public function getUserAgent() { |
||
| 1765 | |||
| 1766 | /************************************************************************//** |
||
| 1767 | * @name Deprecated |
||
| 1768 | * @{ |
||
| 1769 | */ |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Sets whether the pretty-printer should format *bold* and $italics$ |
||
| 1773 | * |
||
| 1774 | * @deprecated since 1.25 |
||
| 1775 | * @param bool $help |
||
| 1776 | */ |
||
| 1777 | public function setHelp( $help = true ) { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Override the parent to generate help messages for all available modules. |
||
| 1784 | * |
||
| 1785 | * @deprecated since 1.25 |
||
| 1786 | * @return string |
||
| 1787 | */ |
||
| 1788 | public function makeHelpMsg() { |
||
| 1804 | |||
| 1805 | /** |
||
| 1806 | * @deprecated since 1.25 |
||
| 1807 | * @return mixed|string |
||
| 1808 | */ |
||
| 1809 | public function reallyMakeHelpMsg() { |
||
| 1858 | |||
| 1859 | /** |
||
| 1860 | * @deprecated since 1.25 |
||
| 1861 | * @param ApiBase $module |
||
| 1862 | * @param string $paramName What type of request is this? e.g. action, |
||
| 1863 | * query, list, prop, meta, format |
||
| 1864 | * @return string |
||
| 1865 | */ |
||
| 1866 | public static function makeHelpMsgHeader( $module, $paramName ) { |
||
| 1875 | |||
| 1876 | /**@}*/ |
||
| 1877 | |||
| 1878 | } |
||
| 1879 | |||
| 1940 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.