summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/UriLibrary.php
blob: 3492e7b9113fd2e5b906ee4b28654ab79b649179 (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
<?php

// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
class Scribunto_LuaUriLibrary extends Scribunto_LuaLibraryBase {
	function register() {
		$lib = [
			'anchorEncode' => [ $this, 'anchorEncode' ],
			'localUrl' => [ $this, 'localUrl' ],
			'fullUrl' => [ $this, 'fullUrl' ],
			'canonicalUrl' => [ $this, 'canonicalUrl' ],
		];

		return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, [
			'defaultUrl' => $this->getTitle()->getFullUrl(),
		] );
	}

	public function anchorEncode( $s ) {
		return [ CoreParserFunctions::anchorencode(
			$this->getParser(), $s
		) ];
	}

	private function getUrl( $func, $page, $query ) {
		$title = Title::newFromText( $page );
		if ( !$title ) {
			$title = Title::newFromURL( urldecode( $page ) );
		}
		if ( $title ) {
			# Convert NS_MEDIA -> NS_FILE
			if ( $title->getNamespace() == NS_MEDIA ) {
				$title = Title::makeTitle( NS_FILE, $title->getDBkey() );
			}
			if ( $query !== null ) {
				$text = $title->$func( $query );
			} else {
				$text = $title->$func();
			}
			return [ $text ];
		} else {
			return [ null ];
		}
	}

	public function localUrl( $page, $query ) {
		return $this->getUrl( 'getLocalURL', $page, $query );
	}

	public function fullUrl( $page, $query ) {
		return $this->getUrl( 'getFullURL', $page, $query );
	}

	public function canonicalUrl( $page, $query ) {
		return $this->getUrl( 'getCanonicalURL', $page, $query );
	}
}