Page MenuHomePhabricator

PHP Notice: Undefined index: SERVER_NAME when running maintenance scripts
Closed, ResolvedPublic

Description

When running a maintenance script, this is printed in console if E_NOTICE is enabled for PHP error reporting:

PHP Notice: Undefined index: SERVER_NAME in /home/jesus/git/mediawiki/core/includes/GlobalFunctions.php on line 1505

Apparently there's no check/sane default when running scripts from cli. $_SERVER is obviously not available in this scenario:

function wfHostname() {
	static $host;
	if ( is_null( $host ) ) {
		# Hostname overriding
		global $wgOverrideHostname;
		if ( $wgOverrideHostname !== false ) {
			# Set static and skip any detection
			$host = $wgOverrideHostname;
			return $host;
		}

		if ( function_exists( 'posix_uname' ) ) {
			// This function not present on Windows
			$uname = posix_uname();
		} else {
			$uname = false;
		}
		if ( is_array( $uname ) && isset( $uname['nodename'] ) ) {
			$host = $uname['nodename'];
		} elseif ( getenv( 'COMPUTERNAME' ) ) {
			# Windows computer name
			$host = getenv( 'COMPUTERNAME' );
		} else {
			# This may be a virtual server.
			$host = $_SERVER['SERVER_NAME'];
		}
	}
	return $host;
}

It can be resolved by defining $wgOverrideHostname in LocalSettings.php as a workaround.

Event Timeline

Hi, I recently came back to MediaWiki development, since a long time. I discovered PHP Notice: Undefined index: SERVER_NAME in /includes/GlobalFunctions.php on line 1432.

In my view this could be fixed by additional checking in the last condition path

} elseif ( isset ( $_SERVER['SERVER_NAME'] ) ) {
        # This may be a virtual server.
        $host = $_SERVER['SERVER_NAME'];
} else {
        global $wgServerName;
        $host = $wgServerName;
}

whether $wgServerName has been set in LocalSettings.php and then use this $wgServerName value here.

See https://www.mediawiki.org/wiki/Manual:$wgServerName/

Hello. I've got same error with mediawiki 1.23 1.32 when I've run maintinance scripts rebuildImages.php or rebuildFileCache.php

PHP Notice:  Undefined index: SERVER_NAME in /var/www/example.wiki/htdocs/includes/GlobalFunctions.php on line 1416

I've tried set $wgServerName and $wgCanonicalServer in LocalSettings.php, but it doesn't work.

@ivanov17: Note that 1.23 is an ancient unsupported insecure version and hence out of scope in this issue tracker. Thanks!

@Aklapper Sorry for the mistake. Version that I use is 1.32, of course.

I solved the problem by installing package with modules for PHP script using system process interfaces and now maintinance scripts work fine. Thanks!

For anyone else who ends up here via Google, you can also solve this error by passing the --server parameter to most of the maintenance scripts:

For example:

php maintenance/dumpBackup.php --server="https://your_wiki/url/" --output=gzip:wikibackup.gz --current

Hi, I recently came back to MediaWiki development, since a long time. I discovered PHP Notice: Undefined index: SERVER_NAME in /includes/GlobalFunctions.php on line 1432.

In my view this could be fixed by additional checking in the last condition path

} elseif ( isset ( $_SERVER['SERVER_NAME'] ) ) {
        # This may be a virtual server.
        $host = $_SERVER['SERVER_NAME'];
} else {
        global $wgServerName;
        $host = $wgServerName;
}

whether $wgServerName has been set in LocalSettings.php and then use this $wgServerName value here.

See https://www.mediawiki.org/wiki/Manual:$wgServerName/

Please allow me again to apply this patch.
(Note to myself: I ran again into this issue when setting up the 36C3-congress-wiki. Like last year.)

Please allow me again to apply this patch.

Thanks for taking a look at the code. You (or anybody else) are very welcome to use developer access to submit the proposed code changes as a Git branch directly into Gerrit which makes it easier to review and provide feedback. If you don't want to set up Git/Gerrit, you can also use the Gerrit Patch Uploader.

This comment was removed by Wikinaut.

Change 546370 had a related patch set uploaded (by Wikinaut; owner: Wikinaut):
[mediawiki/core@master] Undefined index: SERVER_NAME when running maintenance scripts

https://gerrit.wikimedia.org/r/546370

This happens when a unix-like OS has no posix extension installed.

Running maintenance scripts on a virtual server Mediawiki 1.31 with Semantic mediawiki gets me the same errors.
setting
COMPUTERNAME=mycompuname
will make the errrors get away.
The suggestion by wikinaut looks fine to me.

Same error with 1.34.1 with

/usr/bin/php /var/www/mediawiki/maintenance/runJobs.php

With a notice for each job:

Notice: Undefined index: SERVER_NAME in /var/www/mediawiki/includes/GlobalFunctions.php on line 1350

Resolved in the LocalSettings with :

$wgOverrideHostname = "http://myservername";
  • Which MW and PHP version is the problem seen on?
  • Which operating system (Debian Linux, Ubuntu, Windows, Mac.. )
  • Is the problem happening on web pages as well, or only via maintenance script on the command-line?
  • What does var_dump(posix_uname(), php_uname('n')); show?
  • What does var_dump($_SERVER); show? (specifically, is there a HOST_NAME or COMPUTERNAME set?

For me

  • Which MW and PHP version is the problem seen on?

Product Version
MediaWiki 1.34.1 (748c5ea)
17:51, 26 March 2020
PHP 7.3.19 (apache2handler)
MariaDB 10.4.13-MariaDB
Lua 5.1.5

  • Which operating system (Debian Linux, Ubuntu, Windows, Mac.. )

Centos7 (docker)
Linux serverdev-mediawiki-v2 5.6.15-200.fc31.x86_64 #1 SMP Fri May 29 14:59:49 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

  • Is the problem happening on web pages as well, or only via maintenance script on the command-line?

Only maintenance

  • What does var_dump(posix_uname(), php_uname('n')); show?

Error : Call to undefined function posix_uname()

  • What does var_dump($_SERVER); show? (specifically, is there a HOST_NAME or COMPUTERNAME set?

var_dump($_SERVER); ==> null

/etc/sysconfig/network is empty (it's a little container docker in order to test my extensions...)

  • What does var_dump(posix_uname(), php_uname('n')); show?

Error : Call to undefined function posix_uname()

OK. I should have separated these. What about var_dump( php_uname('n') );?

  • What does var_dump($_SERVER); show? (specifically, is there a HOST_NAME or COMPUTERNAME set?

var_dump($_SERVER); ==> null

This doesn't make sense. It should at least be an array. Where did you place these code statements, and how did you execute them?

  • What does var_dump(posix_uname(), php_uname('n')); show?

Error : Call to undefined function posix_uname()

OK. I should have separated these. What about var_dump( php_uname('n') );?

string 'serverdev-mediawiki-v2' (length=22)

It's the name of the docker image.

  • What does var_dump($_SERVER); show? (specifically, is there a HOST_NAME or COMPUTERNAME set?

var_dump($_SERVER); ==> null

This doesn't make sense. It should at least be an array. Where did you place these code statements, and how did you execute them?

It was in the code of a special page. I move the code in the localsettings. Here the result :

array (size=35)
  'UNIQUE_ID' => string 'XuXlj6-pqOfiAnhgDHqRdwAAAAc' (length=27)
  'HTTP_HOST' => string 'wiki.serverdev-mediawiki' (length=24)
  'HTTP_CONNECTION' => string 'keep-alive' (length=10)
  'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
  'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' (length=1)
  'HTTP_USER_AGENT' => string 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36' (length=104)
  'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' (length=124)
  'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
  'HTTP_ACCEPT_LANGUAGE' => string 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7' (length=35)
  'HTTP_COOKIE' => string 'testUserName=Admin; XDEBUG_SESSION=XDEBUG_ECLIPSE' (length=49)
  'PATH' => string '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin' (length=49)
  'SERVER_SIGNATURE' => string '' (length=0)
  'SERVER_SOFTWARE' => string 'Apache/2.4.6 (CentOS) PHP/7.3.19' (length=32)
  'SERVER_NAME' => string 'wiki.serverdev-mediawiki' (length=24)
  'SERVER_ADDR' => string '172.17.0.3' (length=10)
  'SERVER_PORT' => string '80' (length=2)
  'REMOTE_ADDR' => string '172.17.0.1' (length=10)
  'DOCUMENT_ROOT' => string '/var/www/mediawiki/' (length=19)
  'REQUEST_SCHEME' => string 'http' (length=4)
  'CONTEXT_PREFIX' => string '' (length=0)
  'CONTEXT_DOCUMENT_ROOT' => string '/var/www/mediawiki/' (length=19)
  'SERVER_ADMIN' => string 'root@localhost' (length=14)
  'SCRIPT_FILENAME' => string '/var/www/mediawiki/index.php' (length=28)
  'REMOTE_PORT' => string '44176' (length=5)
  'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
  'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
  'REQUEST_METHOD' => string 'GET' (length=3)
  'QUERY_STRING' => string '' (length=0)
  'REQUEST_URI' => string '/index.php/Main_Page' (length=20)
  'SCRIPT_NAME' => string '/index.php' (length=10)
  'PATH_INFO' => string '/Main_Page' (length=10)
  'PATH_TRANSLATED' => string '/var/www/mediawiki/Main_Page' (length=28)
  'PHP_SELF' => string '/index.php/Main_Page' (length=20)
  'REQUEST_TIME_FLOAT' => float 1592124815.083
  'REQUEST_TIME' => int 1592124815

Okay, so that confirms a few things:

  • There is a reliable way to get the hostname even without ext-posix, namely via php_uname('n').
  • Even in this unusual environment for PHP, $_SERVER['SERVER_NAME'] is still set just fine. The reason it is absent isn't due to ['SERVER_NAME'] being absent, but rather due to some code specific to your MediaWiki installation that is breaking the $_SERVER variable in its entirety, sometime after loading LocalSettings.php. This is unsupported and will break many things, not just wfHostname.

@Krinkle I just wish to drop in. In more than one decade, I had quite a lot of installations of MediaWiki in hosting environments, i.e. virtual servers, where my above patch fully solved the issue (shown in the very first posting). I cannot go into technical details, I just wanted to say this again.

Change 605424 had a related patch set uploaded (by Krinkle; owner: Krinkle):
[mediawiki/core@master] GlobalFunctions: Use php_uname instead of posix_uname

https://gerrit.wikimedia.org/r/605424

Change 546370 abandoned by Krinkle:
Hostname fallback to $wgServerName if present

Reason:
Closing in favour of https://gerrit.wikimedia.org/r/605424

https://gerrit.wikimedia.org/r/546370

Change 605424 merged by jenkins-bot:
[mediawiki/core@master] GlobalFunctions: Use php_uname instead of posix_uname

https://gerrit.wikimedia.org/r/605424

Change 619604 had a related patch set uploaded (by Krinkle; owner: Krinkle):
[mediawiki/core@REL1_34] GlobalFunctions: Use php_uname instead of posix_uname

https://gerrit.wikimedia.org/r/619604

Change 619604 merged by jenkins-bot:
[mediawiki/core@REL1_34] GlobalFunctions: Use php_uname instead of posix_uname

https://gerrit.wikimedia.org/r/619604