summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/SpecialOrphanedTimedText.php
blob: 52e86bf4005d0488c530641e1380e07ce64b8764 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
 * Implements Special:OrphanedTimedText
 *
 * @author Brian Wolff
 * @file
 * @ingroup SpecialPage
 */

/**
 * Lists TimedText pages that don't have a corresponding video.
 *
 * @ingroup SpecialPage
 */
class SpecialOrphanedTimedText extends PageQueryPage {

	/** @var Array with keys being names of valid files */
	private $existingFiles;

	public function __construct( $name = 'OrphanedTimedText' ) {
		parent::__construct( $name );
	}

	/**
	 * This is alphabetical, so sort ascending.
	 */
	public function sortDescending() {
		return false;
	}

	/**
	 * Should this be cached?
	 *
	 * This query is actually almost cheap given the current
	 * number of things in TimedText namespace.
	 */
	public function isExpensive() {
		return true;
	}

	/**
	 * Main execution function
	 *
	 * @param $par String subpage
	 */
	public function execute( $par ) {
		global $wgEnableLocalTimedText;

		if ( !$wgEnableLocalTimedText ) {
			$this->setHeaders();
			$this->getOutput()->addWikiMsg( 'orphanedtimedtext-notimedtext' );
			return;
		} elseif ( !$this->canExecuteQuery() ) {
			$this->setHeaders();
			$this->outputHeader();
			$this->getOutput()->addWikiMsg( 'orphanedtimedtext-unsupported' );
			return;
		}
		return parent::execute( $par );
	}

	/**
	 * Can we cache the results of this query?
	 *
	 * Only if we support the query.
	 * @return bool
	 */
	public function isCacheable() {
		return $this->canExecute();
	}

	/**
	 * List in Special:SpecialPages?
	 *
	 * @return bool
	 */
	public function isListed() {
		return $this->canExecute();
	}

	/**
	 * Can we execute this special page?
	 *
	 * The query uses a mysql specific feature (substring_index), so disable on non mysql dbs.
	 *
	 * @return bool
	 */
	private function canExecuteQuery() {
		$dbr = wfGetDB( DB_SLAVE );
		return $dbr->getType() === 'mysql';
	}

	/**
	 * Can we execute this special page
	 *
	 * That is, db is mysql, and TimedText namespace enabled.
	 */
	private function canExecute() {
		global $wgEnableLocalTimedText;

		return $this->canExecuteQuery() && $wgEnableLocalTimedText;
	}

	/**
	 * Get query info
	 *
	 * The query here is meant to retrieve all pages in the TimedText namespace,
	 * such that if you strip the last two extensions (e.g. Foo.bar.baz.en.srt -> Foo.bar.baz)
	 * there is no corresponding img_name in image table. So if there is a page in TimedText
	 * namespace named TimedText:My.Dog.webm.ceb.srt, it will include it in the list provided
	 * that File:My.Dog.webm is not uploaded.
	 *
	 * TimedText does not support file redirects or foreign files, so we don't have
	 * to worry about those.
	 *
	 * Potentially this should maybe also include pages not ending in
	 * .<valid lang code>.srt . However, determining what a valid lang code
	 * is, is pretty hard (although perhaps it could check if its [a-z]{2,3}
	 * however then we've got things like roa-tara, cbk-zam, etc)
	 * and TimedText throws away the final .srt extension and will work with
	 * any extension, so things not ending in .srt arguably aren't oprhaned.
	 *
	 * @note This uses "substring_index" which is a mysql extension.
	 * @return Array Standard query info values.
	 */
	function getQueryInfo() {
		$tables = array( 'page', 'image' );
		$fields = array(
			'namespace' => 'page_namespace',
			'title' => 'page_title',
			'value' => 0,
		);
		$conds = array(
			'img_name' => null,
			'page_namespace' => NS_TIMEDTEXT,
		);

		// Now for the complicated bit
		// Note: This bit is mysql specific. Probably could do something
		// equivalent in postgress via split_part or regex substr,
		// but my sql-fu is not good enough to figure out how to do
		// this in standard sql, or in sqlite.
		$baseCond = 'substr( page_title, 1, length( page_title ) - '
			. "length( substring_index( page_title, '.' ,-2 ) ) - 1 )";
		$joinConds = array(
			'image' => array(
				'LEFT OUTER JOIN',
				 $baseCond . ' = img_name'
			)
		);
		return array(
			'tables' => $tables,
			'fields' => $fields,
			'conds' => $conds,
			'join_conds' => $joinConds
		);
	}

	public function getOrderFields() {
		return array( 'namespace', 'title' );
	}

	/**
	 * Is the TimedText page really orphaned?
	 *
	 * Given a title like "TimedText:Some bit here.webm.en.srt"
	 * check to see if "File:Some bit here.webm" really exists (locally).
	 * @return bool True if we should cross out the line.
	 */
	protected function existenceCheck( Title $title ) {
		$fileTitle = $this->getCorrespondingFile( $title );
		if ( !$fileTitle ) {
			return $title && !$title->isKnown();
		}
		return !$title->isKnown() ||
			( isset( $this->existingFiles[ $fileTitle->getDBKey() ] )
			&& $this->existingFiles[$fileTitle->getDBKey()]->getHandler()
			&& $this->existingFiles[$fileTitle->getDBKey()]->getHandler() instanceof TimedMediaHandler );
	}

	/**
	 * Given a TimedText title, get the File title
	 *
	 * @return Title|null Title in File namespace. null on error.
	 */
	private function getCorrespondingFile( Title $timedText ) {
		if ( !$timedText ) {
			return false;
		}
		$titleParts = explode( '.', $timedText->getDBkey() );
		array_pop( $titleParts );
		array_pop( $titleParts );
		$fileTitle = Title::makeTitleSafe( NS_FILE, implode( '.', $titleParts ) );
		return $fileTitle;
	}

	/**
	 * What group to include this page in on Special:SpecialPages
	 * @return String
	 */
	protected function getGroupName() {
		return 'media';
	}

	/**
	 * Preprocess result to do existence checks all at once.
	 *
	 * @param $db Database
	 * @param $res ResultWraper
	 */
	public function preprocessResults( $db, $res ) {
		parent::preprocessResults( $db, $res );

		if ( !$res->numRows() ) {
			return;
		}

		$filesToLookFor = array();
		foreach( $res as $row ) {
			$title = Title::makeTitle( $row->namespace, $row->title );
			$fileTitle = $this->getCorrespondingFile( $title );
			if ( !$fileTitle ) {
				continue;
			}
			$filesToLookFor[] = array( 'title' => $fileTitle, 'ignoreRedirect' => true );
		}
		$this->existingFiles = RepoGroup::singleton()->getLocalRepo()->findFiles( $filesToLookFor );
		$res->seek( 0 );
	}

	/**
	 * Format the result as a simple link to the page
	 *
	 * Based on parent class but with an existence check added.
	 *
	 * @param Skin $skin
	 * @param object $row Result row
	 * @return string
	 */
	public function formatResult( $skin, $row ) {
		global $wgContLang;

		$title = Title::makeTitleSafe( $row->namespace, $row->title );

		if ( $title instanceof Title ) {
			$text = $wgContLang->convert( $title->getPrefixedText() );
			$link = Linker::link( $title, htmlspecialchars( $text ) );
			if ( $this->existenceCheck( $title ) ) {
				// File got uploaded since this page was cached
				$link = '<del>' . $link . '</del>';
			}
			return $link;
		} else {
			return Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
				Linker::getInvalidTitleDescription( $this->getContext(), $row->namespace, $row->title ) );
		}
	}
}