Subversion Repositories vnag

Rev

Rev 77 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * VNag - Nagios Framework for PHP
  5.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  6.  * Licensed under the terms of the Apache 2.0 license
  7.  *
  8.  * Revision 2023-10-13
  9.  */
  10.  
  11. declare(ticks=1);
  12.  
  13. class MediaWikiVersionCheck extends VNag {
  14.         protected $argSystemDir = null;
  15.  
  16.         public function __construct() {
  17.                 parent::__construct();
  18.  
  19.                 $this->registerExpectedStandardArguments('Vht');
  20.  
  21.                 $this->getHelpManager()->setPluginName('check_mediawiki_version');
  22.                 $this->getHelpManager()->setVersion('2023-10-13');
  23.                 $this->getHelpManager()->setShortDescription('This plugin checks if a local MediaWiki system has the latest version installed.');
  24.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  25.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d <directory>]');
  26.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  27.  
  28.                 // Individual (non-standard) arguments:
  29.                 $this->addExpectedArgument($this->argSystemDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'mediawikiPath', 'The local directory where MediaWiki installation is located.'));
  30.         }
  31.  
  32.         protected function get_local_version($path) {
  33.                 $path = realpath($path) === false ? $path : realpath($path);
  34.  
  35.                 $cont = @file_get_contents("$path/includes/Defines.php");
  36.                 if ($cont !== false) {
  37.                         if (preg_match('@define\\( \'MW_VERSION\', \'([0-9\\.]+)\' \\);@is', $cont, $m)) return $m[1];
  38.                 }
  39.  
  40.                 $cont = @file_get_contents("$path/includes/DefaultSettings.php");
  41.                 if ($cont !== false) {
  42.                         if (preg_match('@\\$wgVersion = \'([0-9\\.]+)\';@is', $cont, $m)) return $m[1];
  43.                 }
  44.  
  45.                 throw new VNagException("Cannot find version information at $path");
  46.         }
  47.  
  48.         protected function get_latest_version() {
  49.                 $cont = $this->url_get_contents('https://www.mediawiki.org/wiki/Download/en');
  50.                 if ($cont === false) {
  51.  
  52.                         // The server replies to some older versions of PHP: 426 Upgrade Required
  53.                         $ch = curl_init();
  54.                         curl_setopt($ch, CURLOPT_URL, "https://www.mediawiki.org/wiki/Download/en");
  55.                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
  56.                         $cont = @curl_exec( $ch );
  57.                         curl_close($ch);
  58.  
  59.                         if (!$cont) {
  60.                                 throw new VNagException("Cannot access website with latest version");
  61.                         }
  62.                 }
  63.  
  64.                 if (!preg_match('@//releases\.wikimedia\.org/mediawiki/([^"]+)/mediawiki\-([^"]+)\.tar\.gz"@ismU', $cont, $m)) {
  65.                         throw new VNagException("Cannot find version information on the website");
  66.                 }
  67.  
  68.                 return $m[2];
  69.         }
  70.  
  71.         protected function cbRun($optional_args=array()) {
  72.                 $system_dir = $this->argSystemDir->getValue();
  73.                 if (empty($system_dir)) {
  74.                         throw new VNagException("Please specify the directory of the MediaWiki installation.");
  75.                 }
  76.                 $system_dir = realpath($system_dir) === false ? $system_dir : realpath($system_dir);
  77.  
  78.                 if (!is_dir($system_dir)) {
  79.                         throw new VNagException('Directory "'.$system_dir.'" not found.');
  80.                 }
  81.  
  82.                 $version = $this->get_local_version($system_dir);
  83.  
  84.                 $latest_version = $this->get_latest_version();
  85.  
  86.                 if (version_compare($version,$latest_version) >= 0) {
  87.                         $this->setStatus(VNag::STATUS_OK);
  88.                         $this->setHeadline("Version $version at $system_dir", true);
  89.                 } else {
  90.                         $this->setStatus(VNag::STATUS_WARNING);
  91.                         $this->setHeadline("Version $version is outdated (Latest version is $latest_version) at $system_dir", true);
  92.                 }
  93.         }
  94. }
  95.