*
* @file
*/
-abstract class Action {
+abstract class Action extends ContextSource {
// Page on which we're performing the action
// @var Article
protected $page;
- // RequestContext if specified; otherwise we'll use the Context from the Page
- // @var RequestContext
- protected $context;
-
// The fields used to create the HTMLForm
// @var Array
protected $fields;
return self::getClass( $name ) !== null;
}
- /**
- * Get the RequestContext in use here
- * @return RequestContext
- */
- protected final function getContext() {
- if ( $this->context instanceof RequestContext ) {
- return $this->context;
- }
- return $this->page->getContext();
- }
-
- /**
- * Get the WebRequest being used for this instance
- *
- * @return WebRequest
- */
- protected final function getRequest() {
- return $this->getContext()->request;
- }
-
- /**
- * Get the OutputPage being used for this instance
- *
- * @return OutputPage
- */
- protected final function getOutput() {
- return $this->getContext()->output;
- }
-
- /**
- * Shortcut to get the User being used for this instance
- *
- * @return User
- */
- protected final function getUser() {
- return $this->getContext()->user;
- }
-
- /**
- * Shortcut to get the Skin being used for this instance
- *
- * @return Skin
- */
- protected final function getSkin() {
- return $this->getContext()->skin;
- }
-
- /**
- * Shortcut to get the user Language being used for this instance
- *
- * @return Skin
- */
- protected final function getLang() {
- return $this->getContext()->lang;
- }
-
- /**
- * Shortcut to get the Title object from the page
- * @return Title
- */
- protected final function getTitle() {
- return $this->page->getTitle();
- }
-
/**
* Protected constructor: use Action::factory( $action, $page ) to actually build
* these things in the real world
*/
protected function __construct( Article $page ) {
$this->page = $page;
+ $this->setContext( $page->getContext() );
}
/**
public function execute( array $data = null, $captureErrors = true ) {
try {
// Set a new context so output doesn't leak.
- $this->context = clone $this->page->getContext();
+ $this->setContext( clone $this->page->getContext() );
// This will throw exceptions if there's a problem
$this->checkCanExecute( $this->getUser() );
public function execute( array $data = null, $captureErrors = true ) {
try {
// Set a new context so output doesn't leak.
- $this->context = clone $this->page->getContext();
+ $context = clone $this->page->getContext();
if ( is_array( $data ) ) {
- $this->context->setRequest( new FauxRequest( $data, false ) );
+ $context->setRequest( new FauxRequest( $data, false ) );
}
+ $this->setContext( $context );
// This will throw exceptions if there's a problem
$this->checkCanExecute( $this->getUser() );
'ConfEditorParseError' => 'includes/ConfEditor.php',
'ConfEditorToken' => 'includes/ConfEditor.php',
'ConstantDependency' => 'includes/CacheDependency.php',
+ 'ContextSource' => 'includes/RequestContext.php',
'Cookie' => 'includes/Cookie.php',
'CookieJar' => 'includes/Cookie.php',
'CreativeCommonsRdf' => 'includes/Metadata.php',
'HTMLTextField' => 'includes/HTMLForm.php',
'Http' => 'includes/HttpFunctions.php',
'HttpRequest' => 'includes/HttpFunctions.old.php',
+ 'IContextSource' => 'includes/RequestContext.php',
'IcuCollation' => 'includes/Collation.php',
'ImageGallery' => 'includes/ImageGallery.php',
'ImageHistoryList' => 'includes/ImagePage.php',
*
* TODO: Document 'section' / 'subsection' stuff
*/
-class HTMLForm {
+class HTMLForm extends ContextSource {
# A mapping of 'type' inputs onto standard HTMLFormField subclasses
static $typeMappings = array(
protected $mSubmitText;
protected $mSubmitTooltip;
- protected $mContext; // <! RequestContext
protected $mTitle;
protected $mMethod = 'post';
*/
public function __construct( $descriptor, /*RequestContext*/ $context = null, $messagePrefix = '' ) {
if( $context instanceof RequestContext ){
- $this->mContext = $context;
+ $this->setContext( $context );
$this->mTitle = false; // We don't need them to set a title
$this->mMessagePrefix = $messagePrefix;
} else {
+ $this->setContext( RequestContext::getMain() );
// B/C since 1.18
if( is_string( $context ) && $messagePrefix === '' ){
// it's actually $messagePrefix
*/
function getTitle() {
return $this->mTitle === false
- ? $this->getContext()->title
+ ? parent::getTitle()
: $this->mTitle;
}
- /**
- * @return RequestContext
- */
- public function getContext(){
- return $this->mContext instanceof RequestContext
- ? $this->mContext
- : RequestContext::getMain();
- }
-
- /**
- * @return OutputPage
- */
- public function getOutput(){
- return $this->getContext()->output;
- }
-
- /**
- * @return WebRequest
- */
- public function getRequest(){
- return $this->getContext()->request;
- }
-
- /**
- * @return User
- */
- public function getUser(){
- return $this->getContext()->user;
- }
-
/**
* Set the method used to submit the form
* @param $method String
*
* @todo document
*/
-class OutputPage {
+class OutputPage extends ContextSource {
/// Should be private. Used with addMeta() which adds <meta>
var $mMetatags = array();
* a OutputPage tied to that context.
*/
function __construct( RequestContext $context = null ) {
- if ( !isset($context) ) {
+ if ( $context === null ) {
# Extensions should use `new RequestContext` instead of `new OutputPage` now.
wfDeprecated( __METHOD__ );
+ $this->setContext( RequestContext::getMain() );
+ } else {
+ $this->setContext( $context );
}
- $this->mContext = $context;
}
/**
return $this->mPagetitle;
}
- /**
- * Get the RequestContext used in this instance
- *
- * @return RequestContext
- */
- private function getContext() {
- if ( !isset($this->mContext) ) {
- wfDebug( __METHOD__ . " called and \$mContext is null. Using RequestContext::getMain(); for sanity\n" );
- $this->mContext = RequestContext::getMain();
- }
- return $this->mContext;
- }
-
- /**
- * Get the WebRequest being used for this instance
- *
- * @return WebRequest
- * @since 1.18
- */
- public function getRequest() {
- return $this->getContext()->getRequest();
- }
-
/**
* Set the Title object to use
*
$this->getContext()->setTitle( $t );
}
- /**
- * Get the Title object used in this instance
- *
- * @return Title
- */
- public function getTitle() {
- return $this->getContext()->getTitle();
- }
-
- /**
- * Get the User object used in this instance
- *
- * @return User
- * @since 1.18
- */
- public function getUser() {
- return $this->getContext()->getUser();
- }
-
- /**
- * Get the Skin object used to render this instance
- *
- * @return Skin
- * @since 1.18
- */
- public function getSkin() {
- return $this->getContext()->getSkin();
- }
-
/**
* Replace the subtile with $str
*
? 'lag-warn-normal'
: 'lag-warn-high';
$wrap = Html::rawElement( 'div', array( 'class' => "mw-{$message}" ), "\n$1\n" );
- $this->wrapWikiMsg( "$wrap\n", array( $message, $this->getContext()->getLang()->formatNum( $lag ) ) );
+ $this->wrapWikiMsg( "$wrap\n", array( $message, $this->getLang()->formatNum( $lag ) ) );
}
}
$dir = wfUILang()->getDir();
$bodyAttrs['class'] = "mediawiki $dir";
- if ( $this->getContext()->getLang()->capitalizeAllNouns() ) {
+ if ( $this->getLang()->capitalizeAllNouns() ) {
# A <body> class is probably not the best way to do this . . .
$bodyAttrs['class'] .= ' capitalize-all-nouns';
}
* @file
*/
-class RequestContext {
+class RequestContext implements IContextSource {
private $mRequest; // / WebRequest object
private $mTitle; // / Title object
private $mOutput; // / OutputPage object
}
}
+/**
+ * Interface for objects which can provide a context on request.
+ */
+interface IContextSource {
+
+ /**
+ * Get the WebRequest object
+ *
+ * @return WebRequest
+ */
+ public function getRequest();
+
+ /**
+ * Get the Title object
+ *
+ * @return Title
+ */
+ public function getTitle();
+
+ /**
+ * Get the OutputPage object
+ *
+ * @return OutputPage object
+ */
+ public function getOutput();
+
+ /**
+ * Get the User object
+ *
+ * @return User
+ */
+ public function getUser();
+
+ /**
+ * Get the Language object
+ *
+ * @return Language
+ */
+ public function getLang();
+
+ /**
+ * Get the Skin object
+ *
+ * @return Skin
+ */
+ public function getSkin();
+}
+
+/**
+ * The simplest way of implementing IContextSource is to hold a RequestContext as a
+ * member variable and provide accessors to it.
+ */
+abstract class ContextSource implements IContextSource {
+
+ /**
+ * @var RequestContext
+ */
+ private $context;
+
+ /**
+ * Get the RequestContext object
+ *
+ * @return RequestContext
+ */
+ public function getContext() {
+ return $this->context;
+ }
+
+ /**
+ * Set the RequestContext object
+ *
+ * @param $context RequestContext
+ */
+ public function setContext( RequestContext $context ) {
+ $this->context = $context;
+ }
+
+ /**
+ * Get the WebRequest object
+ *
+ * @return WebRequest
+ */
+ public function getRequest() {
+ return $this->context->getRequest();
+ }
+
+ /**
+ * Get the Title object
+ *
+ * @return Title
+ */
+ public function getTitle() {
+ return $this->context->getTitle();
+ }
+
+ /**
+ * Get the OutputPage object
+ *
+ * @return OutputPage object
+ */
+ public function getOutput() {
+ return $this->context->getOutput();
+ }
+
+ /**
+ * Get the User object
+ *
+ * @return User
+ */
+ public function getUser() {
+ return $this->context->getUser();
+ }
+
+ /**
+ * Get the Language object
+ *
+ * @return Language
+ */
+ public function getLang() {
+ return $this->context->getLang();
+ }
+
+ /**
+ * Get the Skin object
+ *
+ * @return Skin
+ */
+ public function getSkin() {
+ return $this->context->getSkin();
+ }
+}
\ No newline at end of file
*
* @ingroup Skins
*/
-abstract class Skin {
+abstract class Skin extends ContextSource {
/**#@+
* @private
*/
* Preload the existence of three commonly-requested pages in a single query
*/
function preloadExistence() {
- $user = $this->getContext()->getUser();
-
// User/talk link
- $titles = array( $user->getUserPage(), $user->getTalkPage() );
+ $titles = array( $this->getUser()->getUserPage(), $this->getUser()->getTalkPage() );
// Other tab link
if ( $this->getTitle()->getNamespace() == NS_SPECIAL ) {
* Set some local variables
*/
protected function setMembers() {
- $this->userpage = $this->getContext()->getUser()->getUserPage()->getPrefixedText();
+ $this->userpage = $this->getUser()->getUserPage()->getPrefixedText();
$this->usercss = false;
}
return $this->mRevisionId == 0 || $this->mRevisionId == $this->getTitle()->getLatestRevID();
}
- /**
- * Set the RequestContext used in this instance
- *
- * @param RequestContext $context
- */
- public function setContext( RequestContext $context ) {
- $this->mContext = $context;
- }
-
/**
* Get the RequestContext used in this instance
*
* @return RequestContext
*/
public function getContext() {
- if ( !isset($this->mContext) ) {
+ if ( !parent::getContext() instanceof RequestContext ) {
wfDebug( __METHOD__ . " called and \$mContext is null. Using RequestContext::getMain(); for sanity\n" );
- $this->mContext = RequestContext::getMain();
+ $this->setContext( RequestContext::getMain() );
}
- return $this->mContext;
- }
-
- /** Get the title
- *
- * @return Title
- */
- public function getTitle() {
- return $this->getContext()->getTitle();
- }
-
- /** Get the user
- *
- * @return User
- */
- public function getUser() {
- return $this->getContext()->getUser();
+ return parent::getContext();
}
/**
if ( $action != 'submit' ) {
return false;
}
- if ( !$this->getContext()->getRequest()->wasPosted() ) {
+ if ( !$this->getRequest()->wasPosted() ) {
return false;
}
if ( !$this->getTitle()->userCanEditCssSubpage() ) {
return false;
}
- return $this->getContext()->getUser()->matchEditToken(
- $this->getContext()->getRequest()->getVal( 'wpEditToken' ) );
+ return $this->getUser()->matchEditToken(
+ $this->getRequest()->getVal( 'wpEditToken' ) );
}
/**
// Per-site custom styles
if ( $wgUseSiteCss ) {
$out->addModuleStyles( array( 'site', 'noscript' ) );
- if( $this->getContext()->getUser()->isLoggedIn() ){
+ if( $this->getUser()->isLoggedIn() ){
$out->addModuleStyles( 'user.groups' );
}
}
// Per-user custom styles
if ( $wgAllowUserCss ) {
- if ( $this->getTitle()->isCssSubpage() && $this->userCanPreview( $this->getContext()->getRequest()->getVal( 'action' ) ) ) {
+ if ( $this->getTitle()->isCssSubpage() && $this->userCanPreview( $this->getRequest()->getVal( 'action' ) ) ) {
// @FIXME: properly escape the cdata!
- $out->addInlineStyle( $this->getContext()->getRequest()->getText( 'wpTextbox1' ) );
+ $out->addInlineStyle( $this->getRequest()->getText( 'wpTextbox1' ) );
} else {
$out->addModuleStyles( 'user' );
}
global $wgUseCategoryBrowser, $wgContLang;
if( $out === null ){
- $out = $this->getContext()->output;
+ $out = $this->getOutput();
}
if ( count( $out->mCategoryLinks ) == 0 ) {
# Hidden categories
if ( isset( $allCats['hidden'] ) ) {
- if ( $this->getContext()->getUser()->getBoolOption( 'showhiddencats' ) ) {
+ if ( $this->getUser()->getBoolOption( 'showhiddencats' ) ) {
$class = 'mw-hidden-cats-user-shown';
} elseif ( $this->getTitle()->getNamespace() == NS_CATEGORY ) {
$class = 'mw-hidden-cats-ns-shown';
// Check what we're showing
$allCats = $out->getCategoryLinks();
- $showHidden = $this->getContext()->getUser()->getBoolOption( 'showhiddencats' ) ||
+ $showHidden = $this->getUser()->getBoolOption( 'showhiddencats' ) ||
$this->getTitle()->getNamespace() == NS_CATEGORY;
if ( empty( $allCats['normal'] ) && !( !empty( $allCats['hidden'] ) && $showHidden ) ) {
}
function getUndeleteLink() {
- $action = $this->getContext()->getRequest()->getVal( 'action', 'view' );
+ $action = $this->getRequest()->getVal( 'action', 'view' );
- if ( $this->getContext()->getUser()->isAllowed( 'deletedhistory' ) &&
+ if ( $this->getUser()->isAllowed( 'deletedhistory' ) &&
( $this->getTitle()->getArticleId() == 0 || $action == 'history' ) ) {
$n = $this->getTitle()->isDeleted();
if ( $n ) {
- if ( $this->getContext()->getUser()->isAllowed( 'undelete' ) ) {
+ if ( $this->getUser()->isAllowed( 'undelete' ) ) {
$msg = 'thisisdeleted';
} else {
$msg = 'viewdeleted';
$msg,
Linker::link(
SpecialPage::getTitleFor( 'Undelete', $this->getTitle()->getPrefixedDBkey() ),
- wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $this->getContext()->getLang()->formatNum( $n ) ),
+ wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $n ) ),
array(),
array(),
array( 'known', 'noclasses' )
}
/**
- * The format without an explicit $out argument is deprecated
+ * The format with an explicit $out argument is deprecated
*/
function subPageSubtitle( OutputPage $out=null ) {
- $out = $this->getContext()->getOutput();
+ $out = $this->getOutput();
$subpages = '';
if ( !wfRunHooks( 'SkinSubPageSubtitle', array( &$subpages, $this, $out ) ) ) {
$linkObj = Title::newFromText( $growinglink );
if ( is_object( $linkObj ) && $linkObj->exists() ) {
- $getlink = $this->link(
+ $getlink = Linker::link(
$linkObj,
htmlspecialchars( $display ),
array(),
global $wgRightsPage, $wgRightsUrl, $wgRightsText;
if ( $type == 'detect' ) {
- $diff = $this->getContext()->getRequest()->getVal( 'diff' );
+ $diff = $this->getRequest()->getVal( 'diff' );
if ( is_null( $diff ) && !$this->isRevisionCurrent() && wfMsgForContent( 'history_copyright' ) !== '-' ) {
$type = 'history';
}
if ( $timestamp ) {
- $d = $this->getContext()->getLang()->date( $timestamp, true );
- $t = $this->getContext()->getLang()->time( $timestamp, true );
+ $d = $this->getLang()->date( $timestamp, true );
+ $t = $this->getLang()->time( $timestamp, true );
$s = ' ' . wfMsg( 'lastmodifiedat', $d, $t );
} else {
$s = '';
function showEmailUser( $id ) {
$targetUser = User::newFromId( $id );
- return $this->getContext()->getUser()->canSendEmail() && # the sending user must have a confirmed email address
+ return $this->getUser()->canSendEmail() && # the sending user must have a confirmed email address
$targetUser->canReceiveEmail(); # the target user must have a confirmed email address and allow emails from users
}
global $parserMemc, $wgEnableSidebarCache, $wgSidebarCacheExpiry;
wfProfileIn( __METHOD__ );
- $key = wfMemcKey( 'sidebar', $this->getContext()->getLang()->getCode() );
+ $key = wfMemcKey( 'sidebar', $this->getLang()->getCode() );
if ( $wgEnableSidebarCache ) {
$cachedsidebar = $parserMemc->get( $key );
* @return MediaWiki message or if no new talk page messages, nothing
*/
function getNewtalks() {
- $out = $this->getContext()->getOutput();
+ $out = $this->getOutput();
- $newtalks = $this->getContext()->getUser()->getNewMessageLinks();
+ $newtalks = $this->getUser()->getNewMessageLinks();
$ntl = '';
if ( count( $newtalks ) == 1 && $newtalks[0]['wiki'] === wfWikiID() ) {
$userTalkTitle = $userTitle->getTalkPage();
if ( !$userTalkTitle->equals( $out->getTitle() ) ) {
- $newMessagesLink = $this->link(
+ $newMessagesLink = Linker::link(
$userTalkTitle,
wfMsgHtml( 'newmessageslink' ),
array(),
array( 'known', 'noclasses' )
);
- $newMessagesDiffLink = $this->link(
+ $newMessagesDiffLink = Linker::link(
$userTalkTitle,
wfMsgHtml( 'newmessagesdifflink' ),
array(),
}
if ( $needParse ) {
- $parsed = $this->getContext()->getOutput()->parse( $notice );
+ $parsed = $this->getOutput()->parse( $notice );
$parserMemc->set( $key, array( 'html' => $parsed, 'hash' => md5( $notice ) ), 600 );
$notice = $parsed;
}
$siteNotice = '';
if ( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice, $this ) ) ) {
- if ( is_object( $this->getContext()->getUser() ) && $this->getContext()->getUser()->isLoggedIn() ) {
+ if ( $this->getUser() instanceof User && $this->getUser()->isLoggedIn() ) {
$siteNotice = $this->getCachedNotice( 'sitenotice' );
} else {
$anonNotice = $this->getCachedNotice( 'anonnotice' );
* page list.
* @ingroup SpecialPage
*/
-class SpecialPage {
+class SpecialPage extends ContextSource {
// The canonical name of this special page
// Also used for the default <h1> heading, @see getDescription()
function getTitle( $subpage = false ) {
return self::getTitleFor( $this->mName, $subpage );
}
-
- /**
- * Sets the context this SpecialPage is executed in
- *
- * @param $context RequestContext
- * @since 1.18
- */
- public function setContext( $context ) {
- $this->mContext = $context;
- }
/**
* Gets the context this SpecialPage is executed in
* @since 1.18
*/
public function getContext() {
- if ( $this->mContext instanceof RequestContext ) {
- return $this->mContext;
+ if ( parent::getContext() instanceof RequestContext ) {
+ return parent::getContext();
} else {
wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
return RequestContext::getMain();
}
}
- /**
- * Get the WebRequest being used for this instance
- *
- * @return WebRequest
- * @since 1.18
- */
- public function getRequest() {
- return $this->getContext()->getRequest();
- }
-
- /**
- * Get the OutputPage being used for this instance
- *
- * @return OutputPage
- * @since 1.18
- */
- public function getOutput() {
- return $this->getContext()->getOutput();
- }
-
- /**
- * Shortcut to get the skin being used for this instance
- *
- * @return User
- * @since 1.18
- */
- public function getUser() {
- return $this->getContext()->getUser();
- }
-
- /**
- * Shortcut to get the skin being used for this instance
- *
- * @return Skin
- * @since 1.18
- */
- public function getSkin() {
- return $this->getContext()->getSkin();
- }
-
/**
* Return the full title, including $par
*