Completed
Push — master ( a6b4cf...c49ffc )
by
unknown
03:27
created

WikiExtension::wikiHistoryLink()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 8
nop 5
1
<?php
2
/**
3
 * This file contains only the WikiExtension class.
4
 */
5
6
namespace AppBundle\Twig;
7
8
use Twig_SimpleFunction;
9
10
/**
11
 * Twig extension filters and functions for MediaWiki project links.
12
 */
13
class WikiExtension extends Extension
14
{
15
16
    /**
17
     * Get the name of this extension.
18
     * @return string
19
     */
20
    public function getName()
21
    {
22
        return 'wiki_extension';
23
    }
24
25
    /**
26
     * Get a i18n message.
27
     * @param string $message
28
     * @param array $vars
29
     * @return mixed|null|string
30
     */
31
    public function intuitionMessage($message = "", $vars = [])
32
    {
33
        return $this->getIntuition()->msg($message, [ "domain" => "xtools", "variables" => $vars ]);
34
    }
35
36
    /*********************************** FUNCTIONS ***********************************/
37
38
    /**
39
     * Get all functions provided by this extension.
40
     * @return array
41
     */
42
    public function getFunctions()
43
    {
44
        $options = [ 'is_safe' => [ 'html']];
0 ignored issues
show
Unused Code introduced by
$options is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
        return [];
46
    }
47
48
    /*********************************** FILTERS ***********************************/
49
50
    /**
51
     * Get all functions provided by this extension.
52
     * @return array
53
     */
54
    public function getFilters()
55
    {
56
        return [
57
            new \Twig_SimpleFilter('diff_format', [ $this, 'diffFormat' ], [ 'is_safe' => [ 'html' ] ]),
58
            new \Twig_SimpleFilter('wikify_comment', [ $this, 'wikifyComment' ], [ 'is_safe' => [ 'html' ] ]),
59
        ];
60
    }
61
62
    /**
63
     * Format a given number as a diff, colouring it green if it's postive, red if negative, gary if zero
64
     * @param  number $size Diff size
65
     * @return string       Markup with formatted number
66
     */
67
    public function diffFormat($size)
68
    {
69
        if ($size < 0) {
70
            $class = 'diff-neg';
71
        } elseif ($size > 0) {
72
            $class = 'diff-pos';
73
        } else {
74
            $class = 'diff-zero';
75
        }
76
77
        $size = number_format($size);
78
79
        return "<span class='$class'>$size</span>";
80
    }
81
82
    /**
83
     * Basic wikification of an edit summary (links, italicize section names)
84
     * @param  string $wikitext   Wikitext from edit summary
85
     * @param  string $title      Title of page
86
     * @param  string $projectUrl Project domain and protocol such as https://en.wikipedia.org
87
     * @return string             HTML markup
88
     */
89
    public function wikifyComment($wikitext, $title, $projectUrl)
90
    {
91
        $sectionMatch = null;
92
        $isSection = preg_match_all("/^\/\* (.*?) \*\//", $wikitext, $sectionMatch);
93
94
        if ($isSection) {
95
            $sectionTitle = $sectionMatch[1][0];
96
            $sectionTitleLink = str_replace(' ', '_', $sectionTitle);
97
            $sectionWikitext = "<a target='_blank' href='$projectUrl/wiki/$title#$sectionTitleLink'>&rarr;</a>" .
98
                "<em class='text-muted'>$sectionTitle:</em> ";
99
            $wikitext = str_replace($sectionMatch[0][0], $sectionWikitext, $wikitext);
100
        }
101
102
        $linkMatch = null;
103
104
        while (preg_match_all("/\[\[(.*?)\]\]/", $wikitext, $linkMatch)) {
105
            $wikiLinkParts = explode('|', $linkMatch[1][0]);
106
            $wikiLinkPath = $wikiLinkParts[0];
107
            $wikiLinkText = isset($wikiLinkParts[1]) ? $wikiLinkParts[1] : $wikiLinkPath;
108
            $link = "<a target='_blank' href='$projectUrl/wiki/$wikiLinkPath'>$wikiLinkText</a>";
109
            $wikitext = str_replace($linkMatch[0][0], $link, $wikitext);
110
        }
111
112
        return $wikitext;
113
    }
114
}
115