ThumbnailRenderJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Job for asynchronous rendering of thumbnails.
  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 JobQueue
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. /**
  25. * Job for asynchronous rendering of thumbnails.
  26. *
  27. * @ingroup JobQueue
  28. */
  29. class ThumbnailRenderJob extends Job {
  30. public function __construct( Title $title, array $params ) {
  31. parent::__construct( 'ThumbnailRender', $title, $params );
  32. }
  33. public function run() {
  34. global $wgUploadThumbnailRenderMethod;
  35. $transformParams = $this->params['transformParams'];
  36. $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
  37. ->newFile( $this->title );
  38. $file->load( File::READ_LATEST );
  39. if ( $file && $file->exists() ) {
  40. if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
  41. $thumb = $file->transform( $transformParams, File::RENDER_NOW );
  42. if ( !$thumb || $thumb->isError() ) {
  43. if ( $thumb instanceof MediaTransformError ) {
  44. $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated:' .
  45. $thumb->toText() );
  46. } else {
  47. $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
  48. }
  49. return false;
  50. }
  51. return true;
  52. } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
  53. return $this->hitThumbUrl( $file, $transformParams );
  54. } else {
  55. $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
  56. $wgUploadThumbnailRenderMethod );
  57. return false;
  58. }
  59. } else {
  60. $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
  61. return false;
  62. }
  63. }
  64. /**
  65. * @param LocalFile $file
  66. * @param array $transformParams
  67. * @return bool Success status (error will be set via setLastError() when false)
  68. */
  69. protected function hitThumbUrl( LocalFile $file, $transformParams ) {
  70. global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
  71. $handler = $file->getHandler();
  72. if ( !$handler ) {
  73. $this->setLastError( __METHOD__ . ': could not get handler' );
  74. return false;
  75. } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
  76. $this->setLastError( __METHOD__ . ': failed to normalize' );
  77. return false;
  78. }
  79. $thumbName = $file->thumbName( $transformParams );
  80. $thumbUrl = $file->getThumbUrl( $thumbName );
  81. if ( $thumbUrl === null ) {
  82. $this->setLastError( __METHOD__ . ': could not get thumb URL' );
  83. return false;
  84. }
  85. if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
  86. $parsedUrl = wfParseUrl( $thumbUrl );
  87. if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
  88. $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
  89. return false;
  90. }
  91. $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
  92. }
  93. wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
  94. // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
  95. // Looking at the HTTP status of requests that take less than 1s is a sanity check.
  96. $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
  97. $thumbUrl,
  98. [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
  99. __METHOD__
  100. );
  101. if ( $wgUploadThumbnailRenderHttpCustomHost ) {
  102. $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
  103. }
  104. $status = $request->execute();
  105. $statusCode = $request->getStatus();
  106. wfDebug( __METHOD__ . ": received status {$statusCode}\n" );
  107. // 400 happens when requesting a size greater or equal than the original
  108. // TODO use proper error signaling. 400 could mean a number of other things.
  109. if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
  110. return true;
  111. } elseif ( $statusCode ) {
  112. $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
  113. } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
  114. // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
  115. // minutes for the slower thumbnails to complete.
  116. return true;
  117. } else {
  118. $this->setLastError( __METHOD__ . ': HTTP request failure: '
  119. . Status::wrap( $status )->getWikiText( null, null, 'en' ) );
  120. }
  121. return false;
  122. }
  123. /**
  124. * Whether to retry the job.
  125. * @return bool
  126. */
  127. public function allowRetries() {
  128. // ThumbnailRenderJob is a warmup for the thumbnails cache,
  129. // so loosing it is not a problem. Most times the job fails
  130. // for non-renderable or missing images which will not be fixed
  131. // by a retry, but will create additional load on the renderer.
  132. return false;
  133. }
  134. }