summary refs log tree commit diff
path: root/MantisSmalltech.php
blob: 6207cb0f84306fd6ad6679be3db2a54e5d060acc (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

/**
 * Mantis plugin to make stylistic and navigation changes, as part of the
 * Smalltech project.
 */
class MantisSmalltechPlugin extends MantisPlugin  {
  /**
   * A method that populates the plugin information and minimum requirements.
   * @return void
   */
  function register() {
    $this->name = plugin_lang_get( 'title' );
    $this->description = plugin_lang_get( 'description' );
    $this->page = 'config';

    $this->version = '0.1';
    $this->requires = array(
      'MantisCore' => '2.3.0-dev',

      /* Plugin loading uses a topological sort. It's important that we know
       * what order we load in with respect to MantisCoreFormatting, because
       * that also affects the order our event bindings run in: The plugin
       * that loads last, gets the event last.
       *
       * We would really prefer to load before MantisCoreFormatting, so that
       * we can both consume and output Markdown, rather than HTML.
       * Unfortunatly, the only way that we as a plugin can control that is
       * by declaring a dependency, which makes us run afterwards. Otherwise,
       * it winds up being unpredictable and depends on things such as which
       * plugins are force-installed.
       *
       * By tweaking the full system configuration at the nix level, we'd have
       * the ability to run before, but it's best if MantisSmalltech doesn't
       * require nix.
       *
       * So, we run at the end, and deal with the limitations that come with
       * working in HTML.
       */
      'MantisCoreFormatting' => '2.3.0-dev',
    );

    $this->author = 'Irene Knapp';
    $this->contact = 'ireneista@internetsafetylabs.org';
    $this->url = 'https://irenes.space';

    /* The MantisCoreFormatting plugin already does bug links. We could
     * reimplement its logic, but it's simpler to just override its
     * configuration. We set it in memory, not in the database, so that
     * uninstalling MantisSmalltech will change it back.
     *
     * MantisCoreFormatting also has an "issue note" syntax, for links within
     * a single issue thread. We leave that one at its default.
     */
    config_set_global( 'bug_link_tag', 'b/', true );
  }

  /**
   * plugin hooks
   * @return array
   */
  function hooks() {
    $t_hooks = array(
      'EVENT_DISPLAY_BUG_ID' => 'display_bug_id',
      'EVENT_DISPLAY_FORMATTED' => 'display_formatted',
    );

    return $t_hooks;
  }

  /**
   * Default plugin configuration.
   * @return array
   */
  function config() {
    return array(
      'process_gerrit_links'          => ON
    );
  }

  function display_bug_id( $p_event_name, $p_string, $p_number ) {
    return sprintf( 'b/%u', $p_number );
  }

  function display_formatted( $p_event, $p_string, $p_multiline = true ):
      string
  {
    /* This is better documented in the Gerrit config. If you read that one
     * first, it's quite similar, except that Mantis leaves apostrophes alone.
     */
    $p_string = preg_replace(
      '/(?<=(?:^|(?<=\\s|>)))((?:[\\(\\[\\{\']|&quot;)*)'
      . 'cl\\/(\\d+)'
      . '(?=(?:[\\.,;?!\\)\\]\\}\']|&quot;)*(?:$|(?=\\s|<)))/',
      '\\1<a href="https://gerrit.internetsafetylabs.org/c/\\2">cl/\\2</a>',
      $p_string);

    return $p_string;
  }
}