MediaWiki  1.23.1
GitInfo.php
Go to the documentation of this file.
1 <?php
26 class GitInfo {
27 
31  protected static $repo = null;
32 
36  protected $basedir;
37 
41  private static $viewers = false;
42 
46  public function __construct( $dir ) {
47  $this->basedir = $dir . DIRECTORY_SEPARATOR . '.git';
48  if ( is_readable( $this->basedir ) && !is_dir( $this->basedir ) ) {
49  $GITfile = file_get_contents( $this->basedir );
50  if ( strlen( $GITfile ) > 8 && substr( $GITfile, 0, 8 ) === 'gitdir: ' ) {
51  $path = rtrim( substr( $GITfile, 8 ), "\r\n" );
52  $isAbsolute = $path[0] === '/' || substr( $path, 1, 1 ) === ':';
53  $this->basedir = $isAbsolute ? $path : $dir . DIRECTORY_SEPARATOR . $path;
54  }
55  }
56  }
57 
62  public static function repo() {
63  global $IP;
64  if ( is_null( self::$repo ) ) {
65  self::$repo = new self( $IP );
66  }
67  return self::$repo;
68  }
69 
76  public static function isSHA1( $str ) {
77  return !!preg_match( '/^[0-9A-F]{40}$/i', $str );
78  }
79 
84  public function getHead() {
85  $headFile = "{$this->basedir}/HEAD";
86 
87  if ( !is_readable( $headFile ) ) {
88  return false;
89  }
90 
91  $head = file_get_contents( $headFile );
92 
93  if ( preg_match( "/ref: (.*)/", $head, $m ) ) {
94  return rtrim( $m[1] );
95  } else {
96  return rtrim( $head );
97  }
98  }
99 
104  public function getHeadSHA1() {
105  $head = $this->getHead();
106 
107  // If detached HEAD may be a SHA1
108  if ( self::isSHA1( $head ) ) {
109  return $head;
110  }
111 
112  // If not a SHA1 it may be a ref:
113  $refFile = "{$this->basedir}/{$head}";
114  if ( !is_readable( $refFile ) ) {
115  return false;
116  }
117 
118  $sha1 = rtrim( file_get_contents( $refFile ) );
119 
120  return $sha1;
121  }
122 
129  public function getHeadCommitDate() {
130  global $wgGitBin;
131 
132  if ( !is_file( $wgGitBin ) || !is_executable( $wgGitBin ) ) {
133  return false;
134  }
135 
136  $environment = array( "GIT_DIR" => $this->basedir );
137  $cmd = wfEscapeShellArg( $wgGitBin ) . " show -s --format=format:%ct HEAD";
138  $retc = false;
139  $commitDate = wfShellExec( $cmd, $retc, $environment );
140 
141  if ( $retc !== 0 ) {
142  return false;
143  } else {
144  return (int)$commitDate;
145  }
146  }
147 
152  public function getCurrentBranch() {
153  $head = $this->getHead();
154  if ( $head && preg_match( "#^refs/heads/(.*)$#", $head, $m ) ) {
155  return $m[1];
156  } else {
157  return $head;
158  }
159  }
160 
166  public function getHeadViewUrl() {
167  $config = "{$this->basedir}/config";
168  if ( !is_readable( $config ) ) {
169  return false;
170  }
171 
173  $configArray = parse_ini_file( $config, true );
175  $remote = false;
176 
177  // Use the "origin" remote repo if available or any other repo if not.
178  if ( isset( $configArray['remote origin'] ) ) {
179  $remote = $configArray['remote origin'];
180  } elseif ( is_array( $configArray ) ) {
181  foreach ( $configArray as $sectionName => $sectionConf ) {
182  if ( substr( $sectionName, 0, 6 ) == 'remote' ) {
183  $remote = $sectionConf;
184  }
185  }
186  }
187 
188  if ( $remote === false || !isset( $remote['url'] ) ) {
189  return false;
190  }
191 
192  $url = $remote['url'];
193  if ( substr( $url, -4 ) !== '.git' ) {
194  $url .= '.git';
195  }
196  foreach ( self::getViewers() as $repo => $viewer ) {
197  $pattern = '#^' . $repo . '$#';
198  if ( preg_match( $pattern, $url, $matches ) ) {
199  $viewerUrl = preg_replace( $pattern, $viewer, $url );
200  $headSHA1 = $this->getHeadSHA1();
201  $replacements = array(
202  '%h' => substr( $headSHA1, 0, 7 ),
203  '%H' => $headSHA1,
204  '%r' => urlencode( $matches[1] ),
205  );
206  return strtr( $viewerUrl, $replacements );
207  }
208  }
209  return false;
210  }
211 
216  public static function headSHA1() {
217  return self::repo()->getHeadSHA1();
218  }
219 
224  public static function currentBranch() {
225  return self::repo()->getCurrentBranch();
226  }
227 
232  public static function headViewUrl() {
233  return self::repo()->getHeadViewUrl();
234  }
235 
240  protected static function getViewers() {
241  global $wgGitRepositoryViewers;
242 
243  if ( self::$viewers === false ) {
244  self::$viewers = $wgGitRepositoryViewers;
245  wfRunHooks( 'GitViewers', array( &self::$viewers ) );
246  }
247 
248  return self::$viewers;
249  }
250 }
GitInfo\getHead
getHead()
Return the HEAD of the repo (without any opening "ref: ")
Definition: GitInfo.php:84
wfShellExec
wfShellExec( $cmd, &$retval=null, $environ=array(), $limits=array(), $options=array())
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Definition: GlobalFunctions.php:2804
GitInfo\getHeadCommitDate
getHeadCommitDate()
Return the commit date of HEAD entry of the git code repository.
Definition: GitInfo.php:129
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
GitInfo\isSHA1
static isSHA1( $str)
Check if a string looks like a hex encoded SHA1 hash.
Definition: GitInfo.php:76
GitInfo\$viewers
static $viewers
Map of repo URLs to viewer URLs.
Definition: GitInfo.php:41
GitInfo\headSHA1
static headSHA1()
Definition: GitInfo.php:216
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
GitInfo\headViewUrl
static headViewUrl()
Definition: GitInfo.php:232
GitInfo\getHeadSHA1
getHeadSHA1()
Return the SHA1 for the current HEAD of the repo.
Definition: GitInfo.php:104
GitInfo\repo
static repo()
Return a singleton for the repo at $IP.
Definition: GitInfo.php:62
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
GitInfo
Definition: GitInfo.php:26
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
GitInfo\getViewers
static getViewers()
Gets the list of repository viewers.
Definition: GitInfo.php:240
wfEscapeShellArg
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell,...
Definition: GlobalFunctions.php:2705
GitInfo\currentBranch
static currentBranch()
Definition: GitInfo.php:224
GitInfo\getHeadViewUrl
getHeadViewUrl()
Get an URL to a web viewer link to the HEAD revision.
Definition: GitInfo.php:166
GitInfo\$basedir
$basedir
Location of the .git directory.
Definition: GitInfo.php:36
$dir
if(count( $args)==0) $dir
Definition: importImages.php:49
GitInfo\$repo
static $repo
Singleton for the repo at $IP.
Definition: GitInfo.php:31
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
GitInfo\getCurrentBranch
getCurrentBranch()
Return the name of the current branch, or HEAD if not found.
Definition: GitInfo.php:152
$IP
$IP
Definition: WebStart.php:88
GitInfo\__construct
__construct( $dir)
Definition: GitInfo.php:46