手册:Shell.php

From mediawiki.org
This page is a translated version of the page Manual:Shell.php and the translation is 14% complete.
Outdated translations are marked like this.

shell.php是一个位于maintenance目录中的维护脚本,该目录提供您可以使用的REPL shell与MediaWiki对象和功能一起现场播放。 它基于PsySH;它代替了eval.php ,后者不那么健壮,功能也不多。

Beyond the ability to evaluate PHP statements, functionality includes handling errors and fatals in a way that does not break the shell, examining object internals, source code and documentation via reflection, external editor integration and more.

Usage

Interactively: php maintenance/shell.php
Executing PHP commands from a file: cat commands.txt | maintenance/shell.php

Options

Option Description
--log-channels Print logs sent to the given log channels. Comma-separated: --log-channels authentication,cookie,session Log levels can be filtered with --log-channels authentication,cookie:info,session:error
--log-all Print logs sent to all channels
--dbo-debug Log more database-related information (roughly the same as setting $wgDebugDumpSql )
--no-session Pretend MW_NO_SESSION has been set (useful for debugging code that's invoked from no-session endpoints such as load.php)

Examples

Command execution and error handling

$ php maintenance/shell.php
Psy Shell v0.8.5 (PHP 5.6.30-0+deb8u1 — cli) by Justin Hileman

>>> User::newFromName('Foo')
=> User {#1075
     +mId: null,
     +mName: "Foo",
     +mRealName: null,
     +mEmail: null,
     +mTouched: null,
     +mEmailAuthenticated: null,
     +mOptionsLoaded: false,
     +mFrom: "name",
     +mBlockedby: -1,
     +mRights: null,
     +mHideName: null,
     +mOptions: null,
     +mBlock: null,
   }

>>> $_->mQuickTouched
PHP Error:  Cannot access protected property User::$mQuickTouched in Psy Shell code on line 2

>>> sudo $_->mQuickTouched
=> null

>>> User::newFromName( true )
InvalidArgumentException with message '$text must be a string.'

>>> wtf
InvalidArgumentException with message '$text must be a string.'
--
0:  () at includes/Title.php:283
1:  Title::newFromText() at includes/user/User.php:1255
2:  User::getCanonicalName() at includes/user/User.php:588

Use wtf -a to see 10 more lines

Reflection

>>> ls Message
Class Constants: FORMAT_BLOCK_PARSE, FORMAT_ESCAPED, FORMAT_PARSE, FORMAT_PLAIN, FORMAT_TEXT
Class Methods: __construct, __toString, bitrateParam, bitrateParams, content, durationParam, durationParams, escaped, exists, expiryParam, expiryParams, getFormat, getKey, getKeysToTry, getLanguage, getParams, getTitle, inContentLanguage, inLanguage, isBlank, isDisabled, isMultiKey, listParam, newFallbackSequence, newFromKey, newFromSpecifier, numParam, numParams, params, parse, parseAsBlock, plain, plaintextParam, plaintextParams, rawParam, rawParams, serialize, setContext, setInterfaceMessageFlag, sizeParam, sizeParams, text, timeperiodParam, timeperiodParams, title, toString, unserialize, useDatabase

>>> doc Message::setContext
public function setContext(IContextSource $context)

Description:
  Set the language and the title from a context object

Param:
  IContextSource  $context 

Return:
  Message  $this

Since: 1.19

>>> show Message::setContext
  > 724|        public function setContext( IContextSource $context ) {
    725|                $this->inLanguage( $context->getLanguage() );
    726|                $this->title( $context->getTitle() );
    727|                $this->interface = true;
    728| 
    729|                return $this;
    730|        }

Debug output

$ php maintenance/shell.php --log-all --dbo-debug
[debug] [DBReplication] Wikimedia\Rdbms\LBFactory::getChronologyProtector: using request info {
    "IPAddress": "",
    "UserAgent": "",
    "ChronologyProtection": "true"
}
[debug] [DBConnection] Wikimedia\Rdbms\LoadBalancer::openConnection: calling initLB() before first connection.
[debug] [DBConnection] Connected to database 0 at '127.0.0.1'.
[debug] [DBQuery] wiki SHOW /* Wikimedia\Rdbms\DatabaseMysqlBase::serverIsReadOnly  */ GLOBAL VARIABLES LIKE 'read_only'
[debug] [DBQuery] SHOW GLOBAL VARIABLES LIKE 'read_only'
Psy Shell v0.8.5 (PHP 5.6.30-0+deb8u1 — cli) by Justin Hileman

>>> wfMessage("Recentchanges")->text()
[debug] [CryptRand] mcrypt_create_iv generated 20 bytes of randomness.
[debug] [CryptRand] 0 bytes of randomness leftover in the buffer.
[info] [MessageCache] MessageCache::load: Loading en... local cache is empty, got from global cache
=> "Recent changes"

Editline integration

PsySH can integrate with readline / editline (PHP usually comes pre-built with the latter).

A sample .editrc for history search:

# make PgUp/PgDown work, sort of
# editline has no prefix search so use fulltext search
bind "\e[5~" ed-search-prev-history
bind "\e[6~" ed-search-next-history

See also