http://www.mediawiki.org/wiki/Special:Code/MediaWiki/82864

Revision: 82864
Author:   dale
Date:     2011-02-26 20:17:45 +0000 (Sat, 26 Feb 2011)
Log Message:
-----------
improved remote repo timed text support

Modified Paths:
--------------
    trunk/extensions/TimedMediaHandler/TimedMediaHandler.hooks.php
    trunk/extensions/TimedMediaHandler/TimedMediaHandler.php
    trunk/extensions/TimedMediaHandler/TimedMediaTransformOutput.php

Added Paths:
-----------
    trunk/extensions/TimedMediaHandler/handlers/TextHandler/
    trunk/extensions/TimedMediaHandler/handlers/TextHandler/TextHandler.php

Modified: trunk/extensions/TimedMediaHandler/TimedMediaHandler.hooks.php
===================================================================
--- trunk/extensions/TimedMediaHandler/TimedMediaHandler.hooks.php      
2011-02-26 18:49:07 UTC (rev 82863)
+++ trunk/extensions/TimedMediaHandler/TimedMediaHandler.hooks.php      
2011-02-26 20:17:45 UTC (rev 82864)
@@ -67,7 +67,7 @@
                global $wgExtraNamespaces;
                $timedTextNS = null;
        
-               // Make sure $wgExtraNamespaces in an array (set to NULL by 
default) :
+               // Make sure $wgExtraNamespaces in an array ( set to NULL by 
default ) :
                if ( !is_array( $wgExtraNamespaces ) ) {
                        $wgExtraNamespaces = array();
                }
@@ -82,9 +82,16 @@
                        }
                }
                
-               // If not found add Add a custom timedText NS
+               // @@TODO maybe we should fire a warning here? 
+               // Custom namespae management in mediawiki sucks :( 
+               //
+               // Since other extension use hacks like this as well.. it 
difficult to guarantee consistency 
+               // of the timed text namespace if LocalSettings.php 
$wgExtraNamespaces is modified or another
+               // extension that includes namespaces is added. 
+               // ( obviously its best if set in LocalSetting.php )
                if( !$timedTextNS ){
-                       $timedTextNS = ( $maxNS + 1 );
+                       // Make sure that timedText is on an "even" page 
namespace: 
+                       $timedTextNS = ( ($maxNS + 1)&1 )? $maxNS + 1 : $maxNS 
+ 2;
                        $wgExtraNamespaces[     $timedTextNS ] = 'TimedText';
                        $wgExtraNamespaces[ $timedTextNS +1 ] =  
'TimedText_talk';
                }       

Modified: trunk/extensions/TimedMediaHandler/TimedMediaHandler.php
===================================================================
--- trunk/extensions/TimedMediaHandler/TimedMediaHandler.php    2011-02-26 
18:49:07 UTC (rev 82863)
+++ trunk/extensions/TimedMediaHandler/TimedMediaHandler.php    2011-02-26 
20:17:45 UTC (rev 82864)
@@ -34,6 +34,10 @@
 $wgAutoloadClasses['WebMHandler'] = 
"$timedMediaDir/handlers/WebMHandler/WebMHandler.php";
 $wgAutoloadClasses['getID3' ] = 
"$timedMediaDir/handlers/WebMHandler/getid3/getid3.php"; 
 
+// Text handler 
+$wgAutoloadClasses['TextHandler'] = 
"$timedMediaDir/handlers/TextHandler/TextHandler.php";
+
+// Transcode support
 $wgAutoloadClasses['WebVideoTranscode'] = 
"$timedMediaDir/WebVideoTranscode/WebVideoTranscode.php";
 $wgAutoloadClasses['WebVideoTranscodeJob'] = 
"$timedMediaDir/WebVideoTranscode/WebVideoTranscodeJob.php";
 
@@ -45,16 +49,6 @@
 $wgExtensionMessagesFiles['TimedMediaHandler'] = 
"$timedMediaDir/TimedMediaHandler.i18n.php";
 $wgExtensionMessagesFiles['TimedMediaHandlerMagic'] = 
"$timedMediaDir/TimedMediaHandler.i18n.magic.php";
 
-
-/**
- * Setup a metadata cache :(
- * 
- * Its very costly to generate metadata! I am not sure why the file repos 
don't get
- * instantiated with a path, and then could lazy init things like other normal 
objects and 
- * have a local cache of their metadata! 
- */ 
-$wgMediaHandlerMetadataCache = array();
-
 // Register all Timed Media Handler hooks: 
 TimedMediaHandlerHooks::register();
 
@@ -110,9 +104,9 @@
  * 
  * -These transcodes are *in addition to* the source file. 
  * -Only derivatives with smaller width than the source asset size will be 
created
- * -At least one WebM and Ogg source will be created from the 
$wgEnabledTranscodeSet
+ * -Irregardless source size at least one WebM and Ogg source will be created 
from the $wgEnabledTranscodeSet 
  * -Derivative jobs are added to the mediaWiki JobQueue the first time the 
asset is displayed
- * -List Derivative from min to max
+ * -Derivative should be listed min to max
  */
 $wgEnabledTranscodeSet = array(
        // Cover accessibility for low bandwidth / low resources clients: 

Modified: trunk/extensions/TimedMediaHandler/TimedMediaTransformOutput.php
===================================================================
--- trunk/extensions/TimedMediaHandler/TimedMediaTransformOutput.php    
2011-02-26 18:49:07 UTC (rev 82863)
+++ trunk/extensions/TimedMediaHandler/TimedMediaTransformOutput.php    
2011-02-26 20:17:45 UTC (rev 82864)
@@ -20,6 +20,8 @@
                                $this->$key = false;
                        }
                }
+               // Init an associated textHandler
+               $this->textHandler = new TextHandler( $this->file );
        }
        
        function getPosterUrl(){
@@ -129,7 +131,7 @@
                        self::xmlTagSet( 'source', $mediaSources ) .
                        
                        // Timed text: 
-                       self::xmlTagSet( 'track', $this->getLocalTextSources() 
) .              
+                       self::xmlTagSet( 'track', 
$this->textHandler->getTracks() ) .           
                        
                        // Fallback text displayed for browsers without js and 
without video tag support: 
                        /// XXX note we may want to replace this with an image 
and download link play button
@@ -221,69 +223,5 @@
                }
                $this->hashTime = $hash;
                return $this->hashTime;
-       }
-       
-       function getLocalTextSources(){
-               global $wgServer, $wgScriptPath;
-               
-               // Don't do lookup if non-local path: 
-               // TODO integrate with repo api and do remote lookup
-               if( !$this->file->isLocal() ){
-                       return array();
-               }
-               
-               // Check local cache:           
-               if( $this->textTracks ){
-                       return $this->textTracks;
-               }
-               // Init $this->textTracks
-               $this->textTracks = array();
-               
-               $params = new FauxRequest( array (
-                       'action' => 'query',
-                       'list' => 'allpages',
-                       'apnamespace' => NS_TIMEDTEXT,
-                       'aplimit' => 200,
-                       'apprefix' => $this->file->getTitle()->getDBKey()
-               ));
-               $api = new ApiMain( $params );
-               $api->execute();
-               $data = $api->getResultData();                  
-               // Get the list of language Names
-               $langNames = Language::getLanguageNames();
-
-               if( $data['query'] && $data['query']['allpages'] ){
-                       foreach( $data['query']['allpages'] as $na => $page ){
-                               $subTitle = Title::newFromText( $page['title'] 
) ;
-                               $tileParts = explode( '.', $page['title'] );
-                               if( count( $tileParts) >= 3 ){
-                                       $subtitle_extension = array_pop( 
$tileParts );
-                                       $languageKey = array_pop( $tileParts );
-                               } else {
-                                       continue;
-                               }
-                               // If there is no valid language continue:
-                               if( !isset( $langNames[ $languageKey ] ) ){
-                                       continue;
-                               }
-                               $this->textTracks[] = array(            
-                                       'kind' => 'subtitles',
-                                       'data-mwtitle' => 
$subTitle->getNsText() . ':' . $subTitle->getDBkey(),
-                                       'data-mwprovider' => 'local',           
                
-                                       'type' => 'text/x-srt',
-                                       // TODO Should add a special entry 
point and output proper WebVTT format:
-                                       // 
http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
-                                       'src' => $subTitle->getFullURL( array( 
-                                               'action' => 'raw',
-                                               'ctype' => 'text/x-srt'
-                                       )),
-                                       'srclang' =>  $languageKey,
-                                       'label' => 
wfMsg('timedmedia-subtitle-language', 
-                                               $langNames[ $languageKey ], 
-                                               $languageKey )
-                               );
-                       }
-               }
-               return $this->textTracks;
-       }
+       }       
 }

Added: trunk/extensions/TimedMediaHandler/handlers/TextHandler/TextHandler.php
===================================================================
--- trunk/extensions/TimedMediaHandler/handlers/TextHandler/TextHandler.php     
                        (rev 0)
+++ trunk/extensions/TimedMediaHandler/handlers/TextHandler/TextHandler.php     
2011-02-26 20:17:45 UTC (rev 82864)
@@ -0,0 +1,167 @@
+<?php 
+/*
+ * Timed Text handling for mediaWiki
+ * 
+ * Timed text support is presently fairly limited. Unlike Ogg and WebM 
handlers, 
+ * timed text does not extend the TimedMediaHandler class. 
+ * 
+ * TODO On "new" timedtext laguage save purge all pages where file exists
+ */
+class TextHandler {
+       
+       var $remoteNs = null;//lazy init remote Namespace number
+       
+       function __construct( $file ){
+               $this->file = $file;
+       }
+       /**
+        * Get the timed text tracks elements as an associative array 
+        */
+       function getTracks(){                           
+               if( !$this->file->isLocal() ){
+                       return $this->getRemoteTextSources();
+               }else {
+                       return $this->getLocalTextSources();
+               }
+       }
+       function getTimedTextNamespace(){
+               if( $this->file->isLocal() ){
+                       return NS_TIMEDTEXT;
+               } else {
+                       if( $this->remoteNs !== null ){
+                               return $this->remoteNs;
+                       }
+                       // Get the namespace data from the image api repo: 
+                       // fetchImageQuery query caches results 
+                       $data = $this->file->repo->fetchImageQuery( array(
+                               'meta' =>'siteinfo',
+                               'siprop' => 'namespaces'
+                       ));
+                       
+                       if( isset( $data['query'] ) && isset( 
$data['query']['namespaces'] ) ){
+                               
+                               foreach( $data['query']['namespaces'] as $ns ){
+                                       if( $ns['*'] == 'TimedText' ){
+                                               $this->remoteNs = $ns['id'];
+                                               return $this->remoteNs;
+                                       }
+                               }
+                       } 
+                       
+                       // Look in the main namespace ?
+                       // ( probably should instead throw an error )
+                       return false;
+               }
+       }
+       function getTextPagesQuery(){
+               $ns = $this->getTimedTextNamespace();
+               if( $ns === false ){
+                       wfDebug("Repo: " . $this->file->repo->getName() . " 
does not have a TimedText namesapce \n");
+                       // No timed text namespace, don't try to look up timed 
text tracks
+                       return false;
+               }
+               return array( 
+                       'action' => 'query',
+                       'list' => 'allpages',
+                       'apnamespace' => $ns,
+                       'aplimit' => 300,
+                       'apprefix' => $this->file->getTitle()->getDBKey()
+               );
+       }
+       function getRemoteTextSources(){
+               global $wgMemc;
+               // Use descriptionCacheExpiry as our expire for timed text 
tracks info
+               if ( $this->file->repo->descriptionCacheExpiry > 0 ) {
+                       wfDebug("Attempting to get text tracks from cache...");
+                       $key = $this->file->repo->getLocalCacheKey( 
'RemoteTextTracks', 'url', $this->file->getName() );
+                       $obj = $wgMemc->get($key);
+                       if ($obj) {
+                               wfDebug("success!\n");
+                               return $obj;
+                       }
+                       wfDebug("miss\n");
+               }
+               wfDebug("Get text tracks from remote api \n");
+               $query = $this->getTextPagesQuery();
+               // Error in getting timed text namespace return empty array; 
+               if( $query === false ){
+                       return array();
+               }
+               $data = $this->file->repo->fetchImageQuery( $query );
+               if ( $data && $this->file->repo->descriptionCacheExpiry > 0 ) {
+                       $wgMemc->set( $key, $data, 
$this->file->repo->descriptionCacheExpiry );
+               }
+               return  $this->getTextTracksFromData( $data );
+       }
+       function getLocalTextSources(){
+               global $wgServer, $wgScriptPath;
+               
+               // Init $this->textTracks
+               $params = new FauxRequest( $this->getTextPagesQuery() );
+               $api = new ApiMain( $params );
+               $api->execute();
+               $data = $api->getResultData();                  
+               // Get the list of language Names
+               return $this->getTextTracksFromData( $data );
+       }
+       function getTextTracksFromData( $data ){
+               global $wgForeignFileRepos;
+               
+               $textTracks = array();
+               $providerName = $this->file->repo->getName();
+               // For a while commons repo in the manual was named "shared" if 
commons fix name
+               // ( we need commons to be named "commons" so that the 
javascript api provider names match up ) 
+               if( $providerName == 'shared' ){
+                       // We could alternatively check 
$this->file->repo->mApiBase 
+                       // ( but really we should add a getter ) 
+                       foreach( $wgForeignFileRepos as $repo ){
+                               if( $repo['name'] ==  
$this->file->repo->getName() 
+                                               &&
+                                       parse_url( $repo['apibase'] , 
PHP_URL_HOST ) == 'commons.wikimedia.org'
+                               ){
+                                       $providerName = 'commons';
+                               }
+                       }
+               }
+               // Provider name should be the same as the interwiki map
+               // @@todo more testing with this: 
+               $interWikiPrefix = ( $providerName == 'local' )? '' : 
$providerName . ':';
+               
+               $langNames = Language::getLanguageNames();
+
+               if( $data['query'] && $data['query']['allpages'] ){
+                       foreach( $data['query']['allpages'] as $na => $page ){
+                               $subTitle = Title::newFromText( 
$interWikiPrefix . $page['title'] ) ;
+                               $tileParts = explode( '.', $page['title'] );
+                               if( count( $tileParts) >= 3 ){
+                                       $subtitle_extension = array_pop( 
$tileParts );
+                                       $languageKey = array_pop( $tileParts );
+                               } else {
+                                       continue;
+                               }
+                               // If there is no valid language continue:
+                               if( !isset( $langNames[ $languageKey ] ) ){
+                                       continue;
+                               }
+                               $namespacePrefix = ( $subTitle->getNsText() )? 
$subTitle->getNsText() . ':' : '';
+                               $textTracks[] = array(          
+                                       'kind' => 'subtitles',
+                                       'data-mwtitle' => $namespacePrefix . 
$subTitle->getDBkey(),
+                                       'data-mwprovider' => $providerName,     
                        
+                                       'type' => 'text/x-srt',
+                                       // TODO Should add a special entry 
point and output proper WebVTT format:
+                                       // 
http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
+                                       'src' => $subTitle->getFullURL( array( 
+                                               'action' => 'raw',
+                                               'ctype' => 'text/x-srt'
+                                       )),
+                                       'srclang' =>  $languageKey,
+                                       'label' => 
wfMsg('timedmedia-subtitle-language', 
+                                               $langNames[ $languageKey ], 
+                                               $languageKey )
+                               );
+                       }
+               }
+               return $textTracks;
+       }
+}
\ No newline at end of file


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to