summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/TimedTextPage.php
blob: 361c2eb990736f5302fcfbb110b1f2b0f06fd946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
 * TimedText page display the current video with subtitles to the right.
 *
 * Future features for this page"
 *  @todo add srt download links
 *  @todo parse and validate srt files
 *  @todo link-in or include the universal subtitles editor
 */
class TimedTextPage extends Article {
	// The width of the video plane:
	static private $videoWidth = 400;
	static private $knownTimedTextExtensions = array( 'srt', 'vtt' );

	public function view() {
		$request = $this->getContext()->getRequest();
		$out = $this->getContext()->getOutput();
		$user = $this->getContext()->getUser();

		$diff = $request->getVal( 'diff' );

		if ( $this->getTitle()->getNamespace() != NS_TIMEDTEXT || isset( $diff ) ) {
			parent::view();
			return;
		}
		$this->renderOutput( $out );
	}

	/**
	 * Render TimedText to given output
	 * @param $out OutputPage
	 */
	public function renderOutput( $out ){
		// parse page title:
		$titleParts = explode( '.', $this->getTitle()->getDBkey() );
		$timedTextExtension = array_pop( $titleParts );
		$languageKey = array_pop( $titleParts );

		$oldid = $this->getOldID();
		# Are we looking at an old revision
		if ( $oldid && $this->mRevision ) {
			$this->fetchContentObject();
			$out->setRevisionId( $this->getRevIdFetched() );
			$this->setOldSubtitle( $oldid );

			if ( !$this->showDeletedRevisionHeader() ) {
				wfDebug( __METHOD__ . ": cannot view deleted revision\n" );
				return;
			}
		}

		// Check for File name without text extension:
		// i.e TimedText:myfile.ogg
		$fileTitle = Title::newFromText( $this->getTitle()->getDBkey(), NS_FILE );
		$file = wfFindFile( $fileTitle );
		// Check for a valid srt page, present redirect form for the full title match:
		if( !in_array( $timedTextExtension, self::$knownTimedTextExtensions ) && $file && $file->exists() ){
			if( $file->isLocal() ){
				$this->doRedirectToPageForm( $fileTitle );
			} else {
				$this->doLinkToRemote( $file );
			}
			return;
		}

		// Check for File name with text extension ( from remaning parts of title )
		// i.e TimedText:myfile.ogg.en.srt

		$videoTitle = Title::newFromText( implode('.', $titleParts ), NS_FILE );

		// Check for remote file
		$basefile = wfFindFile( $videoTitle );
		if ( !$basefile ) {
			$out->addHTML( wfMessage( 'timedmedia-subtitle-no-video' )->escaped() );
			return;
		}

		if( !$basefile->isLocal() ){
			$this->doLinkToRemote( $basefile );
			return;
		}

		// Look up the language name:
		$language = $out->getLanguage()->getCode();
		$languages = Language::fetchLanguageNames( $language, 'all' );
		if( isset( $languages[ $languageKey ] ) ) {
			$languageName = $languages[ $languageKey ];
		} else {
			$languageName = $languageKey;
		}

		// Set title
		$message = $this->exists() ?
			'mwe-timedtext-language-subtitles-for-clip' :
			'mwe-timedtext-language-no-subtitles-for-clip';
		$out->setPageTitle( wfMessage($message, $languageName, $videoTitle ) );

		// Get the video with with a max of 600 pixel page
		$out->addHTML(
			xml::tags( 'table', array( 'style'=> 'border:none' ),
				xml::tags( 'tr', null,
					xml::tags( 'td', array( 'valign' => 'top',  'width' => self::$videoWidth ),
						$this->getVideoHTML( $videoTitle )
					) .
					xml::tags( 'td', array( 'valign' => 'top' ) , $this->getTimedTextHTML( $languageName ) )
				)
			)
		);
	}

	/**
	 * Timed text or file is hosted on remote repo, Add a short description and link to foring repo
	 * @param $file File the base file
	 */
	function doLinkToRemote( $file ){
		$output = $this->getContext()->getOutput();
		$output->setPageTitle( wfMessage( 'timedmedia-subtitle-remote',
			$file->getRepo()->getDisplayName() ) );
		$output->addHTML( wfMessage( 'timedmedia-subtitle-remote-link',
			$file->getDescriptionUrl(), $file->getRepo()->getDisplayName()  ) );
	}

	function doRedirectToPageForm(){
		$lang = $this->getContext()->getLanguage();
		$out = $this->getContext()->getOutput();

		// Set the page title:
		$out->setPageTitle( wfMessage( 'timedmedia-subtitle-new' ) );

		// Look up the language name:
		$language = $out->getLanguage()->getCode();
		$attrs = array( 'id' => 'timedmedia-tt-input' );
		$langSelect = Xml::languageSelector( $language, false, null, $attrs, null );

		$out->addHTML(
			Xml::tags('div', array( 'style' => 'text-align:center' ),
				Xml::tags( 'div', null,
					wfMessage( 'timedmedia-subtitle-new-desc', $lang->getCode() )->parse()
				) .
				$langSelect[1] .
				Xml::tags( 'button',
					array( 'id' => 'timedmedia-tt-go' ),
					wfMessage( 'timedmedia-subtitle-new-go' )->escaped()
				)
			)
		);
		$out->addModules( 'ext.tmh.TimedTextSelector' );
	}

	/**
	 * Gets the video HTML ( with the current language set as default )
	 * @param $videoTitle string
	 * @return String
	 */
	private function getVideoHTML( $videoTitle ){
		// Get the video embed:
		$file = wfFindFile( $videoTitle );
		if( !$file ){
			return wfMessage( 'timedmedia-subtitle-no-video' )->escaped();
		} else {
			$videoTransform= $file->transform(
				array(
					'width' => self::$videoWidth
				)
			);
			return $videoTransform->toHTML();
		}
	}

	/**
	 * Gets an HTML representation of the Timed Text
	 *
	 * @param $languageName string
	 * @return Message|string
	 */
	private function getTimedTextHTML( $languageName ){
		if( !$this->exists() ){
			return wfMessage( 'timedmedia-subtitle-no-subtitles',  $languageName );
		}
		return Xml::element(
			'pre',
			array( 'style' => 'margin-top: 0px;' ),
			$this->getContent(),
			false
		);
	}
}