|
| 1 | +<?php |
| 2 | + |
| 3 | +# Copyright (c) 2019 David Hopkins, FBR Ltd |
| 4 | +# Copyright (c) 2015 John Bailey |
| 5 | +# Copyright (c) 2012 John Reese |
| 6 | +# Licensed under the MIT license |
| 7 | + |
| 8 | +if ( false === include_once( config_get( 'plugin_path' ) . 'SourceSVN/SourceSVN.php' ) ) { |
| 9 | + return; |
| 10 | +} |
| 11 | + |
| 12 | +class SourceVisualSVNServerPlugin extends SourceSVNPlugin { |
| 13 | + |
| 14 | + const PLUGIN_VERSION = '2.0.2'; |
| 15 | + const FRAMEWORK_VERSION_REQUIRED = '2.0.0'; |
| 16 | + const SOURCESVN_VERSION_REQUIRED = '2.0.0'; |
| 17 | + |
| 18 | + public function register() { |
| 19 | + $this->name = plugin_lang_get( 'title' ); |
| 20 | + $this->description = plugin_lang_get( 'description' ); |
| 21 | + |
| 22 | + $this->version = self::PLUGIN_VERSION; |
| 23 | + $this->requires = array( |
| 24 | + 'MantisCore' => self::MANTIS_VERSION, |
| 25 | + 'Source' => self::FRAMEWORK_VERSION_REQUIRED, |
| 26 | + 'SourceSVN' => self::SOURCESVN_VERSION_REQUIRED, |
| 27 | + ); |
| 28 | + |
| 29 | + $this->author = 'David Hopkins'; |
| 30 | + $this->contact = 'david.hopkins@fbr.com.au'; |
| 31 | + $this->url = 'https://github.com/mantisbt-plugins/source-integration'; |
| 32 | + } |
| 33 | + |
| 34 | + public $type = 'vsvns'; |
| 35 | + |
| 36 | + public function show_type() { |
| 37 | + return plugin_lang_get( 'vsvns' ); |
| 38 | + } |
| 39 | + |
| 40 | + public function get_visualsvnserver_url_prefix( $p_repo ) { |
| 41 | + return isset( $p_repo->info['visualsvnserver_url_prefix'] ) |
| 42 | + ? $p_repo->info['visualsvnserver_url_prefix'] |
| 43 | + : 'svn'; # Match VisualSVN Server default configuration |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Builds the VisualSVNServer URL base string |
| 48 | + * @param object $p_repo repository |
| 49 | + * @return string VisualSVNServer URL |
| 50 | + */ |
| 51 | + protected function url_base( $p_repo ) { |
| 52 | + # VisualSVN Server web interface is always on the same host name |
| 53 | + # as the HTTP(S)-served repositories, accessed via the /!/#reponame path |
| 54 | + $t_repo_url = parse_url( $p_repo->url ); |
| 55 | + $t_repo_path = $t_repo_url['path']; |
| 56 | + |
| 57 | + $t_url_prefix = $this->get_visualsvnserver_url_prefix( $p_repo ); |
| 58 | + |
| 59 | + # Strip repo prefix (typically '/svn/') from path |
| 60 | + $t_prefix = empty( $t_url_prefix ) ? '/' : '/' . urlencode( $t_url_prefix ) . '/'; |
| 61 | + if( substr( $t_repo_path, 0, strlen( $t_prefix ) ) == $t_prefix ) { |
| 62 | + $t_repo_path = substr( $t_repo_path, strlen( $t_prefix ) ); |
| 63 | + } |
| 64 | + |
| 65 | + # Only include port in final URL if it was present originally |
| 66 | + $t_port = isset( $t_repo_url['port'] ) ? ':' . $t_repo_url['port'] : ''; |
| 67 | + |
| 68 | + $t_url = $t_repo_url['scheme'] . '://' . $t_repo_url['host'] . $t_port . '/!/#' . $t_repo_path; |
| 69 | + return $t_url; |
| 70 | + } |
| 71 | + |
| 72 | + public function url_repo( $p_repo, $p_changeset = null ) { |
| 73 | + $t_url = $this->url_base( $p_repo ); |
| 74 | + |
| 75 | + if( !is_null( $p_changeset ) ) { |
| 76 | + $t_revision = $p_changeset->revision; |
| 77 | + $t_url .= '/view/r' . urlencode( $t_revision ) . '/'; |
| 78 | + } |
| 79 | + |
| 80 | + return $t_url; |
| 81 | + } |
| 82 | + |
| 83 | + public function url_changeset( $p_repo, $p_changeset ) { |
| 84 | + $t_repo_url = $this->url_base( $p_repo ); |
| 85 | + $t_revision = $p_changeset->revision; |
| 86 | + |
| 87 | + $t_url = $t_repo_url . '/commit/r' . urlencode( $t_revision ) . '/'; |
| 88 | + return $t_url; |
| 89 | + } |
| 90 | + |
| 91 | + public function url_file( $p_repo, $p_changeset, $p_file ) { |
| 92 | + $t_repo_url = $this->url_base( $p_repo ); |
| 93 | + |
| 94 | + # if the file has been removed, it doesn't exist in current revision |
| 95 | + # so we generate a link to (current revision - 1) |
| 96 | + $t_revision = ( $p_file->action == 'rm' ) |
| 97 | + ? $p_changeset->revision - 1 |
| 98 | + : $p_changeset->revision; |
| 99 | + |
| 100 | + $t_url = $t_repo_url . '/view/r' . urlencode( $t_revision ) . $p_file->filename; |
| 101 | + |
| 102 | + return $t_url; |
| 103 | + } |
| 104 | + |
| 105 | + public function url_diff( $p_repo, $p_changeset, $p_file ) { |
| 106 | + if( $p_file->action == 'rm' || $p_file->action == 'add' ) { |
| 107 | + # Return default no-link for add/remove change diffs |
| 108 | + return parent::url_diff( $p_repo, $p_changeset, $p_file ); |
| 109 | + } |
| 110 | + |
| 111 | + # The web interface for VisualSVN Server displays file diffs as inline content |
| 112 | + # when viewing a particular commit. |
| 113 | + # It doesn't have a specific page for single-file diffs, |
| 114 | + # at least as of v3.9.5, 2019-04-29 |
| 115 | + $t_url = $this->url_changeset( $p_repo, $p_changeset ); |
| 116 | + return $t_url; |
| 117 | + } |
| 118 | + |
| 119 | + public function update_repo_form( $p_repo ) { |
| 120 | + $t_url_prefix = $this->get_visualsvnserver_url_prefix( $p_repo ); |
| 121 | +?> |
| 122 | +<tr> |
| 123 | + <td class="category"><?php echo plugin_lang_get( 'visualsvnserver_url_prefix' ) ?></td> |
| 124 | + <td> |
| 125 | + <input type="text" name="visualsvnserver_url_prefix" maxlength="250" size="40" |
| 126 | + value="<?php echo string_attribute( $t_url_prefix ) ?>" |
| 127 | + /> |
| 128 | + </td> |
| 129 | +</tr> |
| 130 | +<?php |
| 131 | + return parent::update_repo_form( $p_repo ); |
| 132 | + } |
| 133 | + |
| 134 | + public function update_repo( $p_repo ) { |
| 135 | + $p_repo->info['visualsvnserver_url_prefix'] = gpc_get_string( 'visualsvnserver_url_prefix' ); |
| 136 | + |
| 137 | + return parent::update_repo( $p_repo ); |
| 138 | + } |
| 139 | +} |
0 commit comments