Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeCommentLinker
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 link
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 generalLink
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 messageBugLink
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 messageRevLink
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 makeExternalLink
n/a
0 / 0
n/a
0 / 0
0
 makeInternalLink
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3namespace MediaWiki\Extension\CodeReview\Backend;
4
5use SpecialPage;
6use Title;
7
8abstract class CodeCommentLinker {
9    /**
10     * @var CodeRepository
11     */
12    protected $mRepo;
13
14    /**
15     * @param CodeRepository $repo
16     */
17    public function __construct( $repo ) {
18        $this->mRepo = $repo;
19    }
20
21    /**
22     * @param string $text
23     * @return string
24     */
25    public function link( $text ) {
26        # Catch links like https://www.mediawiki.org/wiki/Special:Code/MediaWiki/44245#c829
27        # Ended by space or brackets (like those pesky <br /> tags)
28        $EXT_LINK_URL_CLASS = '[^][<>"\\x00-\\x20\\x7F\p{Zs}]';
29
30        $text = preg_replace_callback(
31            '/(^|[^\w[])(' . wfUrlProtocolsWithoutProtRel() . ')(' . $EXT_LINK_URL_CLASS . '+)/',
32            [ $this, 'generalLink' ], $text );
33        $text = preg_replace_callback( '/\br(\d+)\b/',
34            [ $this, 'messageRevLink' ], $text );
35        $text = preg_replace_callback( CodeRevision::BUG_REFERENCE,
36            [ $this, 'messageBugLink' ], $text );
37        return $text;
38    }
39
40    /**
41     * @param array $arr
42     * @return string
43     */
44    public function generalLink( $arr ) {
45        $url = $arr[2] . $arr[3];
46        // Re-add the surrounding space/punctuation
47        return $arr[1] . $this->makeExternalLink( $url, $url );
48    }
49
50    /**
51     * @param array $arr
52     * @return string
53     */
54    public function messageBugLink( $arr ) {
55        $text = $arr[0];
56        $bugNo = intval( $arr[1] );
57        $url = $this->mRepo->getBugPath( $bugNo );
58        if ( $url ) {
59            return $this->makeExternalLink( $url, $text );
60        } else {
61            return $text;
62        }
63    }
64
65    /**
66     * @param array $matches
67     * @return string
68     */
69    public function messageRevLink( $matches ) {
70        $text = $matches[0];
71        $rev = intval( $matches[1] );
72
73        $repo = $this->mRepo->getName();
74        $title = SpecialPage::getTitleFor( 'Code', "$repo/$rev" );
75
76        return $this->makeInternalLink( $title, $text );
77    }
78
79    /**
80     * @param string $url
81     * @param string $text
82     * @return string
83     */
84    abstract public function makeExternalLink( $url, $text );
85
86    /**
87     * @param Title $title
88     * @param string $text
89     * @return string
90     */
91    abstract public function makeInternalLink( $title, $text );
92}