}
/**
+ * An error page which can definitely be safely rendered using the OutputPage
* @ingroup Exception
*/
class ErrorPageError extends MWException {
- public $title, $msg;
+ public $title, $msg, $params;
/**
* Note: these arguments are keys into wfMsg(), not text!
*/
- function __construct( $title, $msg ) {
+ function __construct( $title, $msg, $params = null ) {
$this->title = $title;
$this->msg = $msg;
+ $this->params = $params;
parent::__construct( wfMsg( $msg ) );
}
function report() {
global $wgOut;
- $wgOut->showErrorPage( $this->title, $this->msg );
+ $wgOut->showErrorPage( $this->title, $this->msg, $this->params );
$wgOut->output();
}
}
+/**
+ * Show an error when a user tries to do something they do not have the necessary
+ * permissions for.
+ */
+class PermissionsError extends ErrorPageError {
+ public $permission;
+
+ function __construct( $permission ) {
+ global $wgLang;
+
+ $this->permission = $permission;
+
+ $groups = array_map(
+ array( 'User', 'makeGroupLinkWiki' ),
+ User::getGroupsWithPermission( $this->permission )
+ );
+
+ if( $groups ) {
+ parent::__construct(
+ 'badaccess',
+ 'badaccess-groups',
+ array(
+ $wgLang->commaList( $groups ),
+ count( $groups )
+ )
+ );
+ } else {
+ parent::__construct(
+ 'badaccess',
+ 'badaccess-group0'
+ );
+ }
+ }
+}
+
/**
* Install an exception handler for MediaWiki exception types.
*/
/**
* Display an error page noting that a given permission bit is required.
- *
+ * @deprecated since 1.18, just throw the exception directly
* @param $permission String: key required
*/
public function permissionRequired( $permission ) {
- $this->setPageTitle( wfMsg( 'badaccess' ) );
- $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
- $this->setRobotPolicy( 'noindex,nofollow' );
- $this->setArticleRelated( false );
- $this->mBodytext = '';
-
- $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
- User::getGroupsWithPermission( $permission ) );
- if( $groups ) {
- $this->addWikiMsg(
- 'badaccess-groups',
- $this->getContext()->getLang()->commaList( $groups ),
- count( $groups )
- );
- } else {
- $this->addWikiMsg( 'badaccess-group0' );
- }
- $this->returnToMain();
+ throw new PermissionsError( $permission );
}
/**
*/
public function loginToUse() {
if( $this->getUser()->isLoggedIn() ) {
- $this->permissionRequired( 'read' );
- return;
+ throw new PermissionsError( 'read' );
}
$this->setPageTitle( wfMsg( 'loginreqtitle' ) );