MediaWiki  1.34.0
UriLibrary.php
Go to the documentation of this file.
1 <?php
2 
4  public function register() {
5  $lib = [
6  'anchorEncode' => [ $this, 'anchorEncode' ],
7  'localUrl' => [ $this, 'localUrl' ],
8  'fullUrl' => [ $this, 'fullUrl' ],
9  'canonicalUrl' => [ $this, 'canonicalUrl' ],
10  ];
11 
12  return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, [
13  'defaultUrl' => $this->getTitle()->getFullUrl(),
14  ] );
15  }
16 
23  public function anchorEncode( $s ) {
25  $this->getParser(), $s
26  ) ];
27  }
28 
29  private function getUrl( $func, $page, $query ) {
30  $title = Title::newFromText( $page );
31  if ( !$title ) {
32  $title = Title::newFromURL( urldecode( $page ) );
33  }
34  if ( $title ) {
35  # Convert NS_MEDIA -> NS_FILE
36  if ( $title->getNamespace() == NS_MEDIA ) {
37  $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
38  }
39  if ( $query !== null ) {
40  $text = $title->$func( $query );
41  } else {
42  $text = $title->$func();
43  }
44  return [ $text ];
45  } else {
46  return [ null ];
47  }
48  }
49 
57  public function localUrl( $page, $query ) {
58  return $this->getUrl( 'getLocalURL', $page, $query );
59  }
60 
68  public function fullUrl( $page, $query ) {
69  return $this->getUrl( 'getFullURL', $page, $query );
70  }
71 
79  public function canonicalUrl( $page, $query ) {
80  return $this->getUrl( 'getCanonicalURL', $page, $query );
81  }
82 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
Scribunto_LuaUriLibrary
Definition: UriLibrary.php:3
Scribunto_LuaUriLibrary\fullUrl
fullUrl( $page, $query)
Handler for fullUrl.
Definition: UriLibrary.php:68
Scribunto_LuaLibraryBase\getTitle
getTitle()
Get the title.
Definition: LibraryBase.php:83
NS_FILE
const NS_FILE
Definition: Defines.php:66
$s
$s
Definition: mergeMessageFileList.php:185
Scribunto_LuaLibraryBase\getEngine
getEngine()
Get the engine.
Definition: LibraryBase.php:56
Scribunto_LuaLibraryBase\getParser
getParser()
Get the parser.
Definition: LibraryBase.php:74
Scribunto_LuaUriLibrary\anchorEncode
anchorEncode( $s)
Handler for anchorEncode.
Definition: UriLibrary.php:23
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
Scribunto_LuaUriLibrary\localUrl
localUrl( $page, $query)
Handler for localUrl.
Definition: UriLibrary.php:57
Scribunto_LuaLibraryBase
This class provides some basic services that Lua libraries will probably need.
Definition: LibraryBase.php:27
Scribunto_LuaUriLibrary\canonicalUrl
canonicalUrl( $page, $query)
Handler for canonicalUrl.
Definition: UriLibrary.php:79
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:48
Title\newFromURL
static newFromURL( $url)
THIS IS NOT THE FUNCTION YOU WANT.
Definition: Title.php:404
CoreParserFunctions\anchorencode
static anchorencode( $parser, $text)
Definition: CoreParserFunctions.php:953
Scribunto_LuaUriLibrary\getUrl
getUrl( $func, $page, $query)
Definition: UriLibrary.php:29