MediaWiki REL1_32
LinkerTest.php
Go to the documentation of this file.
1<?php
2
3
8
13 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
14 $this->setMwGlobals( [
15 'wgArticlePath' => '/wiki/$1',
16 ] );
17
18 $this->assertEquals(
19 $expected,
20 Linker::userLink( $userId, $userName, $altUserName ),
21 $msg
22 );
23 }
24
25 public static function provideCasesForUserLink() {
26 # Format:
27 # - expected
28 # - userid
29 # - username
30 # - optional altUserName
31 # - optional message
32 return [
33
34 # ## ANONYMOUS USER ########################################
35 [
36 '<a href="/wiki/Special:Contributions/JohnDoe" '
37 . 'class="mw-userlink mw-anonuserlink" '
38 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
39 0, 'JohnDoe', false,
40 ],
41 [
42 '<a href="/wiki/Special:Contributions/::1" '
43 . 'class="mw-userlink mw-anonuserlink" '
44 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
45 0, '::1', false,
46 'Anonymous with pretty IPv6'
47 ],
48 [
49 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
50 . 'class="mw-userlink mw-anonuserlink" '
51 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
52 0, '0:0:0:0:0:0:0:1', false,
53 'Anonymous with almost pretty IPv6'
54 ],
55 [
56 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
57 . 'class="mw-userlink mw-anonuserlink" '
58 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
59 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
60 'Anonymous with full IPv6'
61 ],
62 [
63 '<a href="/wiki/Special:Contributions/::1" '
64 . 'class="mw-userlink mw-anonuserlink" '
65 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
66 0, '::1', 'AlternativeUsername',
67 'Anonymous with pretty IPv6 and an alternative username'
68 ],
69
70 # IPV4
71 [
72 '<a href="/wiki/Special:Contributions/127.0.0.1" '
73 . 'class="mw-userlink mw-anonuserlink" '
74 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
75 0, '127.0.0.1', false,
76 'Anonymous with IPv4'
77 ],
78 [
79 '<a href="/wiki/Special:Contributions/127.0.0.1" '
80 . 'class="mw-userlink mw-anonuserlink" '
81 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
82 0, '127.0.0.1', 'AlternativeUsername',
83 'Anonymous with IPv4 and an alternative username'
84 ],
85
86 # ## Regular user ##########################################
87 # TODO!
88 ];
89 }
90
97 public function testFormatComment(
98 $expected, $comment, $title = false, $local = false, $wikiId = null
99 ) {
100 $conf = new SiteConfiguration();
101 $conf->settings = [
102 'wgServer' => [
103 'enwiki' => '//en.example.org',
104 'dewiki' => '//de.example.org',
105 ],
106 'wgArticlePath' => [
107 'enwiki' => '/w/$1',
108 'dewiki' => '/w/$1',
109 ],
110 ];
111 $conf->suffixes = [ 'wiki' ];
112
113 $this->setMwGlobals( [
114 'wgScript' => '/wiki/index.php',
115 'wgArticlePath' => '/wiki/$1',
116 'wgCapitalLinks' => true,
117 'wgConf' => $conf,
118 ] );
119
120 if ( $title === false ) {
121 // We need a page title that exists
122 $title = Title::newFromText( 'Special:BlankPage' );
123 }
124
125 $this->assertEquals(
126 $expected,
127 Linker::formatComment( $comment, $title, $local, $wikiId )
128 );
129 }
130
132 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
133
134 // phpcs:disable Generic.Files.LineLength
135 return [
136 // Linker::formatComment
137 [
138 'a&lt;script&gt;b',
139 'a<script>b',
140 ],
141 [
142 'a—b',
143 'a&mdash;b',
144 ],
145 [
146 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
147 "'''not bolded'''",
148 ],
149 [
150 "try &lt;script&gt;evil&lt;/scipt&gt; things",
151 "try <script>evil</scipt> things",
152 ],
153 // Linker::formatAutocomments
154 [
155 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
156 "/* autocomment */",
157 ],
158 [
159 '<a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment"><a href="/wiki/index.php?title=Linkie%3F&amp;action=edit&amp;redlink=1" class="new" title="Linkie? (page does not exist)">linkie?</a></span></span>',
160 "/* [[linkie?]] */",
161 ],
162 [
163 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
164 "/* autocomment */ post",
165 ],
166 [
167 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
168 "pre /* autocomment */",
169 ],
170 [
171 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
172 "pre /* autocomment */ post",
173 ],
174 [
175 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> multiple? <a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment2: </span> </span></span>',
176 "/* autocomment */ multiple? /* autocomment2 */ ",
177 ],
178 [
179 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
180 "/* autocomment containing /* */ T70361"
181 ],
182 [
183 '<a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing &quot;quotes&quot;</span></span>',
184 "/* autocomment containing \"quotes\" */"
185 ],
186 [
187 '<a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing &lt;script&gt;tags&lt;/script&gt;</span></span>',
188 "/* autocomment containing <script>tags</script> */"
189 ],
190 [
191 '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
192 "/* autocomment */",
193 false, true
194 ],
195 [
196 '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
197 "/* autocomment */",
198 null
199 ],
200 [
201 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
202 "/* autocomment */",
203 false, false
204 ],
205 [
206 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
207 "/* autocomment */",
208 false, false, $wikiId
209 ],
210 // Linker::formatLinksInComment
211 [
212 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
213 "abc [[link]] def",
214 ],
215 [
216 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">text</a> def',
217 "abc [[link|text]] def",
218 ],
219 [
220 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
221 "abc [[Special:BlankPage|]] def",
222 ],
223 [
224 'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&amp;action=edit&amp;redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
225 "abc [[%C4%85%C5%9B%C5%BC]] def",
226 ],
227 [
228 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
229 "abc [[#section]] def",
230 ],
231 [
232 'abc <a href="/wiki/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
233 "abc [[/subpage]] def",
234 ],
235 [
236 'abc <a href="/wiki/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" class="new" title="&quot;evil!&quot; (page does not exist)">&quot;evil!&quot;</a> def',
237 "abc [[\"evil!\"]] def",
238 ],
239 [
240 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
241 "abc [[<script>very evil</script>]] def",
242 ],
243 [
244 'abc [[|]] def',
245 "abc [[|]] def",
246 ],
247 [
248 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
249 "abc [[link]] def",
250 false, false
251 ],
252 [
253 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
254 "abc [[link]] def",
255 false, false, $wikiId
256 ],
257 ];
258 // phpcs:enable
259 }
260
265 public function testFormatLinksInComment( $expected, $input, $wiki ) {
266 $conf = new SiteConfiguration();
267 $conf->settings = [
268 'wgServer' => [
269 'enwiki' => '//en.example.org'
270 ],
271 'wgArticlePath' => [
272 'enwiki' => '/w/$1',
273 ],
274 ];
275 $conf->suffixes = [ 'wiki' ];
276 $this->setMwGlobals( [
277 'wgScript' => '/wiki/index.php',
278 'wgArticlePath' => '/wiki/$1',
279 'wgCapitalLinks' => true,
280 'wgConf' => $conf,
281 ] );
282
283 $this->assertEquals(
284 $expected,
285 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
286 );
287 }
288
289 public static function provideCasesForFormatLinksInComment() {
290 // phpcs:disable Generic.Files.LineLength
291 return [
292 [
293 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
294 'foo bar [[Special:BlankPage]]',
295 null,
296 ],
297 [
298 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
299 '[[ :Special:BlankPage]]',
300 null,
301 ],
302 [
303 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
304 "[[Foo'bar]]",
305 'enwiki',
306 ],
307 [
308 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
309 'foo bar [[Special:BlankPage]]',
310 'enwiki',
311 ],
312 [
313 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
314 'foo bar [[Image:Example]]',
315 'enwiki',
316 ],
317 ];
318 // phpcs:enable
319 }
320
321 public static function provideLinkBeginHook() {
322 // phpcs:disable Generic.Files.LineLength
323 return [
324 // Modify $html
325 [
326 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
327 $html = 'foobar';
328 },
329 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
330 ],
331 // Modify $attribs
332 [
333 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
334 $attribs['bar'] = 'baz';
335 },
336 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
337 ],
338 // Modify $query
339 [
340 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
341 $query['bar'] = 'baz';
342 },
343 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
344 ],
345 // Force HTTP $options
346 [
347 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
348 $options = [ 'http' ];
349 },
350 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
351 ],
352 // Force 'forcearticlepath' in $options
353 [
354 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
355 $options = [ 'forcearticlepath' ];
356 $query['foo'] = 'bar';
357 },
358 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
359 ],
360 // Abort early
361 [
362 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
363 $ret = 'foobar';
364 return false;
365 },
366 'foobar'
367 ],
368 ];
369 // phpcs:enable
370 }
371
376 public function testLinkBeginHook( $callback, $expected ) {
377 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
378 $this->setMwGlobals( [
379 'wgArticlePath' => '/wiki/$1',
380 'wgServer' => '//example.org',
381 'wgCanonicalServer' => 'http://example.org',
382 'wgScriptPath' => '/w',
383 'wgScript' => '/w/index.php',
384 ] );
385
386 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
387 $title = SpecialPage::getTitleFor( 'Blankpage' );
389 $this->assertEquals( $expected, $out );
390 }
391
392 public static function provideLinkEndHook() {
393 return [
394 // Override $html
395 [
396 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
397 $html = 'foobar';
398 },
399 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
400 ],
401 // Modify $attribs
402 [
403 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
404 $attribs['bar'] = 'baz';
405 },
406 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
407 ],
408 // Fully override return value and abort hook
409 [
410 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
411 $ret = 'blahblahblah';
412 return false;
413 },
414 'blahblahblah'
415 ],
416
417 ];
418 }
419
424 public function testLinkEndHook( $callback, $expected ) {
425 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
426 $this->setMwGlobals( [
427 'wgArticlePath' => '/wiki/$1',
428 ] );
429
430 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
431
432 $title = SpecialPage::getTitleFor( 'Blankpage' );
434 $this->assertEquals( $expected, $out );
435 }
436}
Database.
Definition LinkerTest.php:7
testUserLink( $expected, $userId, $userName, $altUserName=false, $msg='')
provideCasesForUserLink Linker::userLink
testLinkEndHook( $callback, $expected)
MediaWiki\Linker\LinkRenderer::buildAElement provideLinkEndHook.
provideCasesForFormatComment()
testLinkBeginHook( $callback, $expected)
MediaWiki\Linker\LinkRenderer::runLegacyBeginHook provideLinkBeginHook.
static provideCasesForFormatLinksInComment()
testFormatComment( $expected, $comment, $title=false, $local=false, $wikiId=null)
provideCasesForFormatComment Linker::formatComment Linker::formatAutocomments Linker::formatLinksInCo...
testFormatLinksInComment( $expected, $input, $wiki)
Linker::formatLinksInComment provideCasesForFormatLinksInComment.
static provideLinkEndHook()
static provideLinkBeginHook()
static provideCasesForUserLink()
static link( $target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition Linker.php:84
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:876
static formatLinksInComment( $comment, $title=null, $local=false, $wikiId=null)
Formats wiki links and media links in text; all other wiki formatting is ignored.
Definition Linker.php:1214
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1088
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
This is a class for holding configuration settings, particularly for multi-wiki sites.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2055
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2054
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:894
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2062
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2063
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
if(is_array($mode)) switch( $mode) $input