PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/TimedMediaHandler/TimedMediaHandler.hooks.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 243 lines | 148 code | 32 blank | 63 comment | 19 complexity | 0222d4a9c6d05edf607d56547666a40e MD5 | raw file
  1. <?php
  2. /**
  3. * Hooks for TimedMediaHandler extension
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. class TimedMediaHandlerHooks {
  9. // Register TimedMediaHandler Hooks
  10. static function register(){
  11. global $wgParserOutputHooks, $wgHooks, $wgJobClasses, $wgJobTypesExcludedFromDefaultQueue,
  12. $wgMediaHandlers, $wgResourceModules, $wgExcludeFromThumbnailPurge, $wgExtraNamespaces,
  13. $wgTmhFileExtensions, $wgParserOutputHooks, $wgOut, $wgAPIPropModules, $wgTimedTextNS;
  14. // Register the Timed Media Handler javascript resources ( MwEmbed modules )
  15. MwEmbedResourceManager::register( 'extensions/TimedMediaHandler/MwEmbedModules/EmbedPlayer' );
  16. MwEmbedResourceManager::register( 'extensions/TimedMediaHandler/MwEmbedModules/TimedText' );
  17. // Set the default webPath for this embed player extension
  18. global $wgExtensionAssetsPath, $wgMwEmbedModuleConfig, $timedMediaDir;
  19. $wgMwEmbedModuleConfig['EmbedPlayer.WebPath'] = $wgExtensionAssetsPath .
  20. '/' . basename ( $timedMediaDir ) . '/MwEmbedModules/EmbedPlayer';
  21. // Setup media Handlers:
  22. $wgMediaHandlers['application/ogg'] = 'OggHandler';
  23. $wgMediaHandlers['video/webm'] = 'WebMHandler';
  24. // Add transcode job class:
  25. $wgJobClasses+= array(
  26. 'webVideoTranscode' => 'WebVideoTranscodeJob'
  27. );
  28. // Transcode jobs must be explicitly requested from the job queue:
  29. $wgJobTypesExcludedFromDefaultQueue[] = 'webVideoTranscode';
  30. $baseExtensionResource = array(
  31. 'localBasePath' => dirname( __FILE__ ),
  32. 'remoteExtPath' => 'TimedMediaHandler',
  33. );
  34. // Add the PopUpMediaTransform module ( specific to timedMedia handler ( no support in mwEmbed modules )
  35. $wgResourceModules+= array(
  36. 'mw.PopUpMediaTransform' => array_merge( $baseExtensionResource, array(
  37. 'scripts' => 'resources/mw.PopUpThumbVideo.js',
  38. 'styles' => 'resources/PopUpThumbVideo.css',
  39. 'dependencies' => array( 'jquery.ui.dialog' ),
  40. ) ),
  41. 'embedPlayerIframeStyle'=> array_merge( $baseExtensionResource, array(
  42. 'styles' => 'resources/embedPlayerIframe.css',
  43. ) ),
  44. 'ext.tmh.transcodetable' => array_merge($baseExtensionResource, array(
  45. 'scripts' => 'resources/ext.tmh.transcodetable.js',
  46. 'styles' => 'resources/transcodeTable.css',
  47. 'messages'=> array(
  48. 'mwe-ok',
  49. 'mwe-cancel',
  50. 'timedmedia-reset-error',
  51. 'timedmedia-reset',
  52. 'timedmedia-reset-confirm'
  53. )
  54. ) )
  55. );
  56. // Setup a hook for iframe embed handling:
  57. $wgHooks['ArticleFromTitle'][] = 'TimedMediaIframeOutput::iframeHook';
  58. // When an upload completes ( check clear any existing transcodes )
  59. $wgHooks['UploadComplete'][] = 'TimedMediaHandlerHooks::checkUploadComplete';
  60. // When an image page is moved:
  61. $wgHooks['TitleMoveComplete'][] = 'TimedMediaHandlerHooks::checkTitleMoveComplete';
  62. // When image page is deleted so that we remove transcode settings / files.
  63. $wgHooks['ArticleDeleteComplete'][] = 'TimedMediaHandlerHooks::checkArticleDeleteComplete';
  64. // Add parser hook
  65. $wgParserOutputHooks['TimedMediaHandler'] = array( 'TimedMediaHandler', 'outputHook' );
  66. // We should probably move this script output to a parser function but not working correctly in
  67. // dynamic contexts ( for example in special upload, when there is an "existing file" warning. )
  68. $wgHooks['BeforePageDisplay'][] = 'TimedMediaHandlerHooks::pageOutputHook';
  69. // Exclude transcoded assets from normal thumbnail purging
  70. // ( a maintenance script could handle transcode asset purging)
  71. if ( isset( $wgExcludeFromThumbnailPurge ) ) {
  72. $wgExcludeFromThumbnailPurge = array_merge( $wgExcludeFromThumbnailPurge, $wgTmhFileExtensions );
  73. // Also add the .log file ( used in two pass encoding )
  74. // ( probably should move in-progress encodes out of web accessible directory )
  75. $wgExcludeFromThumbnailPurge[] = 'log';
  76. }
  77. $wgHooks['LoadExtensionSchemaUpdates'][] = 'TimedMediaHandlerHooks::loadExtensionSchemaUpdates';
  78. // Add unit tests
  79. $wgHooks['UnitTestsList'][] = 'TimedMediaHandlerHooks::registerUnitTests';
  80. /**
  81. * Add support for the "TimedText" NameSpace
  82. */
  83. define( "NS_TIMEDTEXT", $wgTimedTextNS );
  84. define( "NS_TIMEDTEXT_TALK", $wgTimedTextNS +1 );
  85. $wgExtraNamespaces[NS_TIMEDTEXT] = "TimedText";
  86. $wgExtraNamespaces[NS_TIMEDTEXT_TALK] = "TimedText_talk";
  87. // Check for timed text page:
  88. $wgHooks[ 'ArticleFromTitle' ][] = 'TimedMediaHandlerHooks::checkForTimedTextPage';
  89. // Add transcode status to video asset pages:
  90. $wgHooks[ 'ImagePageAfterImageLinks' ][] = 'TimedMediaHandlerHooks::checkForTranscodeStatus';
  91. // for MediaWiki 1.17 compatibility
  92. TranscodeStatusTable::getLinker();
  93. return true;
  94. }
  95. public static function checkForTimedTextPage( &$title, &$article ){
  96. if( $title->getNamespace() == NS_TIMEDTEXT ) {
  97. $article = new TimedTextPage( $title );
  98. }
  99. return true;
  100. }
  101. /**
  102. * Wraps the isTranscodableFile function
  103. * @param $title Title
  104. */
  105. public static function isTranscodableTitle( $title ){
  106. if( $title->getNamespace() != NS_FILE ){
  107. return false;
  108. }
  109. $file = wfFindFile( $title );
  110. return self::isTranscodableFile( $file );
  111. }
  112. /**
  113. * Utility function to check if a given file can be "transcoded"
  114. * @param $file File object
  115. */
  116. public static function isTranscodableFile( & $file ){
  117. global $wgEnableTranscode;
  118. // don't show the transcode table if transcode is disabled
  119. if( $wgEnableTranscode === false ){
  120. return false;
  121. }
  122. // Can't find file
  123. if( !$file ){
  124. return false;
  125. }
  126. // We can only transcode local files
  127. if( !$file->isLocal() ){
  128. return false;
  129. }
  130. $mediaType = $file->getHandler()->getMetadataType( $image = '' );
  131. // If ogg or webm format and not audio we can "transcode" this file
  132. if( ( $mediaType == 'webm' || $mediaType == 'ogg' ) && ! $file->getHandler()->isAudio( $file ) ){
  133. return true;
  134. }
  135. return false;
  136. }
  137. public static function checkForTranscodeStatus( $article, &$html ){
  138. // load the file:
  139. $file = wfFindFile( $article->getTitle() );
  140. if( self::isTranscodableFile( $file ) ){
  141. $html = TranscodeStatusTable::getHTML( $file );
  142. }
  143. return true;
  144. }
  145. public static function checkUploadComplete( &$image ){
  146. $title = $image->getTitle();
  147. // Check that the file is a transcodable asset:
  148. if( self::isTranscodableTitle( $title ) ){
  149. // Remove all the transcode files and db states for this asset ( will be re-added the first time the asset is displayed )
  150. WebVideoTranscode::removeTranscodes( $title );
  151. }
  152. return true;
  153. }
  154. /**
  155. * Handle moved titles
  156. *
  157. * For now we just remove all the derivatives for the oldTitle. In the future we could
  158. * look at moving the files, but right now thumbs are not moved, so I don't want to be
  159. * inconsistent.
  160. */
  161. public static function checkTitleMoveComplete( &$title, &$newTitle, &$user, $oldid, $newid ){
  162. if( self::isTranscodableTitle( $title ) ){
  163. // Remove all the transcode files and db states for this asset
  164. // ( will be re-added the first time the asset is displayed with its new title )
  165. WebVideoTranscode::removeTranscodes( $title );
  166. }
  167. return true;
  168. }
  169. public static function checkArticleDeleteComplete( &$article, &$user, $reason, $id ){
  170. // Check if the article is a file and remove transcode files:
  171. if( $article->getTitle()->getNamespace() == NS_FILE ) {
  172. $file = wfFindFile( $article->getTitle() );
  173. if( self::isTranscodableFile( $file ) ){
  174. WebVideoTranscode::removeTranscodes( $file );
  175. }
  176. }
  177. return true;
  178. }
  179. /**
  180. * Adds the transcode sql
  181. */
  182. public static function loadExtensionSchemaUpdates( ){
  183. global $wgExtNewTables;
  184. $wgExtNewTables[] = array(
  185. 'transcode',
  186. dirname( __FILE__ ) . '/WebVideoTranscode/transcodeTable.sql' );
  187. return true;
  188. }
  189. /**
  190. * Hook to add list of PHPUnit test cases.
  191. * @param $files Array of files
  192. */
  193. public static function registerUnitTests( array &$files ) {
  194. $testDir = dirname( __FILE__ ) . '/tests/phpunit/';
  195. $testFiles = array(
  196. 'TestTimeParsing.php',
  197. 'TestApiUploadVideo.php',
  198. 'TestVideoThumbnail.php',
  199. 'TestVideoTranscode.php'
  200. );
  201. foreach( $testFiles as $fileName ){
  202. $files[] = $testDir . $fileName;
  203. }
  204. return true;
  205. }
  206. static function pageOutputHook( &$out, &$sk ){
  207. $out->addModules( 'mw.PopUpMediaTransform' );
  208. $out->addModuleStyles( 'mw.PopUpMediaTransform' );
  209. return true;
  210. }
  211. }