SpecialRunJobs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Implements Special:RunJobs
  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 SpecialPage
  22. */
  23. use MediaWiki\Logger\LoggerFactory;
  24. /**
  25. * Special page designed for running background tasks (internal use only)
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialRunJobs extends UnlistedSpecialPage {
  30. public function __construct() {
  31. parent::__construct( 'RunJobs' );
  32. }
  33. public function doesWrites() {
  34. return true;
  35. }
  36. public function execute( $par = '' ) {
  37. $this->getOutput()->disable();
  38. if ( wfReadOnly() ) {
  39. wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
  40. return;
  41. }
  42. // Validate request method
  43. if ( !$this->getRequest()->wasPosted() ) {
  44. wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
  45. return;
  46. }
  47. // Validate request parameters
  48. $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false,
  49. 'async' => true, 'stats' => false ];
  50. $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
  51. $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
  52. $missing = array_diff_key( $required, $params );
  53. if ( count( $missing ) ) {
  54. wfHttpError( 400, 'Bad Request',
  55. 'Missing parameters: ' . implode( ', ', array_keys( $missing ) )
  56. );
  57. return;
  58. }
  59. // Validate request signature
  60. $squery = $params;
  61. unset( $squery['signature'] );
  62. $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
  63. $providedSignature = $params['signature'];
  64. $verified = is_string( $providedSignature )
  65. && hash_equals( $correctSignature, $providedSignature );
  66. if ( !$verified || $params['sigexpiry'] < time() ) {
  67. wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' );
  68. return;
  69. }
  70. // Apply any default parameter values
  71. $params += $optional;
  72. if ( $params['async'] ) {
  73. // HTTP 202 Accepted
  74. HttpStatus::header( 202 );
  75. // Clients are meant to disconnect without waiting for the full response.
  76. // Let the page output happen before the jobs start, so that clients know it's
  77. // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
  78. // or similar to make sure we stay alive to run the deferred update.
  79. DeferredUpdates::addUpdate(
  80. new TransactionRoundDefiningUpdate(
  81. function () use ( $params ) {
  82. $this->doRun( $params );
  83. },
  84. __METHOD__
  85. ),
  86. DeferredUpdates::POSTSEND
  87. );
  88. } else {
  89. $stats = $this->doRun( $params );
  90. if ( $params['stats'] ) {
  91. $this->getRequest()->response()->header( 'Content-Type: application/json' );
  92. print FormatJson::encode( $stats );
  93. } else {
  94. print "Done\n";
  95. }
  96. }
  97. }
  98. protected function doRun( array $params ) {
  99. $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
  100. return $runner->run( [
  101. 'type' => $params['type'],
  102. 'maxJobs' => $params['maxjobs'] ?: 1,
  103. 'maxTime' => $params['maxtime'] ?: 30
  104. ] );
  105. }
  106. /**
  107. * @param array $query
  108. * @param string $secretKey
  109. * @return string
  110. */
  111. public static function getQuerySignature( array $query, $secretKey ) {
  112. ksort( $query ); // stable order
  113. return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
  114. }
  115. }