Use local context to get message instead of relying on global variables
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
index e41e732..0581867 100644 (file)
@@ -1,4 +1,26 @@
 <?php
+/**
+ * Implements Special:DeletedContributions
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup SpecialPage
+ */
+
 /**
  * Implements Special:DeletedContributions to display archived revisions
  * @ingroup SpecialPage
@@ -9,11 +31,11 @@ class DeletedContribsPager extends IndexPager {
        var $messages, $target;
        var $namespace = '', $mDb;
 
-       function __construct( $target, $namespace = false ) {
-               parent::__construct();
+       function __construct( IContextSource $context, $target, $namespace = false ) {
+               parent::__construct( $context );
                $msgs = array( 'deletionlog', 'undeleteviewlink', 'diff' );
                foreach( $msgs as $msg ) {
-                       $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
+                       $this->messages[$msg] = $this->msg( $msg )->escaped();
                }
                $this->target = $target;
                $this->namespace = $namespace;
@@ -27,13 +49,13 @@ class DeletedContribsPager extends IndexPager {
        }
 
        function getQueryInfo() {
-               global $wgUser;
                list( $index, $userCond ) = $this->getUserCond();
                $conds = array_merge( $userCond, $this->getNamespaceCond() );
+               $user = $this->getUser();
                // Paranoia: avoid brute force searches (bug 17792)
-               if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
+               if( !$user->isAllowed( 'deletedhistory' ) ) {
                        $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::DELETED_USER) . ' = 0';
-               } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
+               } elseif( !$user->isAllowed( 'suppressrevision' ) ) {
                        $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::SUPPRESSED_USER) .
                                ' != ' . Revision::SUPPRESSED_USER;
                }
@@ -70,25 +92,24 @@ class DeletedContribsPager extends IndexPager {
        }
 
        function getNavigationBar() {
-               global $wgLang;
-
                if ( isset( $this->mNavigationBar ) ) {
                        return $this->mNavigationBar;
                }
-               $fmtLimit = $wgLang->formatNum( $this->mLimit );
+               $lang = $this->getLanguage();
+               $fmtLimit = $lang->formatNum( $this->mLimit );
                $linkTexts = array(
-                       'prev' => wfMsgExt( 'pager-newer-n', array( 'escape', 'parsemag' ), $fmtLimit ),
-                       'next' => wfMsgExt( 'pager-older-n', array( 'escape', 'parsemag' ), $fmtLimit ),
-                       'first' => wfMsgHtml( 'histlast' ),
-                       'last' => wfMsgHtml( 'histfirst' )
+                       'prev' => $this->msg( 'pager-newer-n', $fmtLimit )->escaped(),
+                       'next' => $this->msg( 'pager-older-n', $fmtLimit )->escaped(),
+                       'first' => $this->msg( 'histlast' )->escaped(),
+                       'last' => $this->msg( 'histfirst' )->escaped()
                );
 
                $pagingLinks = $this->getPagingLinks( $linkTexts );
                $limitLinks = $this->getLimitLinks();
-               $limits = $wgLang->pipeList( $limitLinks );
+               $limits = $lang->pipeList( $limitLinks );
 
-               $this->mNavigationBar = "(" . $wgLang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
-                       wfMsgExt( 'viewprevnext', array( 'parsemag', 'escape', 'replaceafter' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
+               $this->mNavigationBar = "(" . $lang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
+                       $this->msg( 'viewprevnext' )->rawParams( $pagingLinks['prev'], $pagingLinks['next'], $limits )->escaped();
                return $this->mNavigationBar;
        }
 
@@ -111,11 +132,8 @@ class DeletedContribsPager extends IndexPager {
         * @todo This would probably look a lot nicer in a table.
         */
        function formatRow( $row ) {
-               global $wgUser, $wgLang;
                wfProfileIn( __METHOD__ );
 
-               $sk = $this->getSkin();
-
                $rev = new Revision( array(
                                'id'         => $row->ar_rev_id,
                                'comment'    => $row->ar_comment,
@@ -123,7 +141,7 @@ class DeletedContribsPager extends IndexPager {
                                'user_text'  => $row->ar_user_text,
                                'timestamp'  => $row->ar_timestamp,
                                'minor_edit' => $row->ar_minor_edit,
-                               'deleted' => $row->ar_deleted,
+                               'deleted'    => $row->ar_deleted,
                                ) );
 
                $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
@@ -131,7 +149,7 @@ class DeletedContribsPager extends IndexPager {
                $undelete = SpecialPage::getTitleFor( 'Undelete' );
 
                $logs = SpecialPage::getTitleFor( 'Log' );
-               $dellog = $sk->linkKnown(
+               $dellog = Linker::linkKnown(
                        $logs,
                        $this->messages['deletionlog'],
                        array(),
@@ -141,13 +159,15 @@ class DeletedContribsPager extends IndexPager {
                        )
                );
 
-               $reviewlink = $sk->linkKnown(
+               $reviewlink = Linker::linkKnown(
                        SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
                        $this->messages['undeleteviewlink']
                );
 
-               if( $wgUser->isAllowed('deletedtext') ) {
-                       $last = $sk->linkKnown(
+               $user = $this->getUser();
+
+               if( $user->isAllowed('deletedtext') ) {
+                       $last = Linker::linkKnown(
                                $undelete,
                                $this->messages['diff'],
                                array(),
@@ -161,13 +181,13 @@ class DeletedContribsPager extends IndexPager {
                        $last = $this->messages['diff'];
                }
 
-               $comment = $sk->revComment( $rev );
-               $date = htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) );
+               $comment = Linker::revComment( $rev );
+               $date = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user ) );
 
-               if( !$wgUser->isAllowed('undelete') || !$rev->userCan(Revision::DELETED_TEXT) ) {
+               if( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
                        $link = $date; // unusable link
                } else {
-                       $link = $sk->linkKnown(
+                       $link = Linker::linkKnown(
                                $undelete,
                                $date,
                                array(),
@@ -182,42 +202,33 @@ class DeletedContribsPager extends IndexPager {
                        $link = '<span class="history-deleted">' . $link . '</span>';
                }
 
-               $pagelink = $sk->link( $page );
+               $pagelink = Linker::link( $page );
 
                if( $rev->isMinor() ) {
                        $mflag = ChangesList::flag( 'minor' );
                } else {
                        $mflag = '';
                }
-               
+
                // Revision delete link
-               $canHide = $wgUser->isAllowed( 'deleterevision' );
-               if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
-                       if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
-                               $del = $this->mSkin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
-                       } else {
-                               $query = array(
-                                       'type' => 'archive',
-                                       'target' => $page->getPrefixedDbkey(),
-                                       'ids' => $rev->getTimestamp() );
-                               $del = $this->mSkin->revDeleteLink( $query,
-                                       $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide ) . ' ';
-                       }
-               } else {
-                       $del = '';
-               }
+               $del = Linker::getRevDeleteLink( $user, $rev, $page );
+               if ( $del ) $del .= ' ';
 
                $tools = Html::rawElement(
                        'span',
                        array( 'class' => 'mw-deletedcontribs-tools' ),
-                       wfMsg( 'parentheses', $wgLang->pipeList( array( $last, $dellog, $reviewlink ) ) )
+                       $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
+                               array( $last, $dellog, $reviewlink ) ) )->escaped()
                );
 
-               $ret = Html::rawElement(
-                       'li',
-                       array(),
-                       "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}"
-               ) . "\n";
+               $ret = "{$del}{$link} {$tools} . . {$mflag} {$pagelink} {$comment}";
+
+               # Denote if username is redacted for this edit
+               if( $rev->isDeleted( Revision::DELETED_USER ) ) {
+                       $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
+               }
+
+               $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
 
                wfProfileOut( __METHOD__ );
                return $ret;
@@ -247,63 +258,65 @@ class DeletedContributionsPage extends SpecialPage {
         * @param       $par    String: (optional) user name of the user for which to show the contributions
         */
        function execute( $par ) {
-               global $wgUser;
+               global $wgQueryPageDefaultLimit;
                $this->setHeaders();
 
-               if ( !$this->userCanExecute( $wgUser ) ) {
+               $user = $this->getUser();
+
+               if ( !$this->userCanExecute( $user ) ) {
                        $this->displayRestrictionError();
                        return;
                }
 
-               global $wgUser, $wgOut, $wgLang, $wgRequest;
-
-               $wgOut->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
+               $request = $this->getRequest();
+               $out = $this->getOutput();
+               $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
 
                $options = array();
 
-               if ( isset( $par ) ) {
+               if ( $par !== null ) {
                        $target = $par;
                } else {
-                       $target = $wgRequest->getVal( 'target' );
+                       $target = $request->getVal( 'target' );
                }
 
                if ( !strlen( $target ) ) {
-                       $wgOut->addHTML( $this->getForm( '' ) );
+                       $out->addHTML( $this->getForm( '' ) );
                        return;
                }
 
-               $options['limit'] = $wgRequest->getInt( 'limit', 50 );
+               $options['limit'] = $request->getInt( 'limit', $wgQueryPageDefaultLimit );
                $options['target'] = $target;
 
-               $nt = Title::makeTitleSafe( NS_USER, $target );
-               if ( !$nt ) {
-                       $wgOut->addHTML( $this->getForm( '' ) );
+               $userObj = User::newFromName( $target );
+               if ( !$userObj ) {
+                       $out->addHTML( $this->getForm( '' ) );
                        return;
                }
-               $id = User::idFromName( $nt->getText() );
 
-               $target = $nt->getText();
-               $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
+               $target = $userObj->getName();
+               $out->addSubtitle( $this->getSubTitle( $userObj ) );
 
-               if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
+               if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
                        $options['namespace'] = intval( $ns );
                } else {
                        $options['namespace'] = '';
                }
 
-               $wgOut->addHTML( $this->getForm( $options ) );
+               $out->addHTML( $this->getForm( $options ) );
 
-               $pager = new DeletedContribsPager( $target, $options['namespace'] );
+               $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
                if ( !$pager->getNumRows() ) {
-                       $wgOut->addWikiMsg( 'nocontribs' );
+                       $out->addWikiMsg( 'nocontribs' );
                        return;
                }
 
                # Show a message about slave lag, if applicable
-               if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
-                       $wgOut->showLagWarning( $lag );
+               $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
+               if( $lag > 0 )
+                       $out->showLagWarning( $lag );
 
-               $wgOut->addHTML(
+               $out->addHTML(
                        '<p>' . $pager->getNavigationBar() . '</p>' .
                        $pager->getBody() .
                        '<p>' . $pager->getNavigationBar() . '</p>' );
@@ -315,43 +328,58 @@ class DeletedContributionsPage extends SpecialPage {
                                ? 'sp-contributions-footer-anon'
                                : 'sp-contributions-footer';
 
-
-                       $text = wfMsgNoTrans( $message, $target );
-                       if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
-                               $wgOut->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
+                       if( !$this->msg( $message )->isDisabled() ) {
+                               $out->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
                        }
                }
        }
 
        /**
         * Generates the subheading with links
-        * @param $nt @see Title object for the target
+        * @param $userObj User object for the target
+        * @return String: appropriately-escaped HTML to be output literally
+        * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
         */
-       function getSubTitle( $nt, $id ) {
-               global $wgSysopUserBans, $wgLang, $wgUser;
-
-               $sk = $wgUser->getSkin();
-
-               if ( 0 == $id ) {
-                       $user = htmlspecialchars( $nt->getText() );
+       function getSubTitle( $userObj ) {
+               if ( $userObj->isAnon() ) {
+                       $user = htmlspecialchars( $userObj->getName() );
                } else {
-                       $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
+                       $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
                }
+               $nt = $userObj->getUserPage();
+               $id = $userObj->getID();
                $talk = $nt->getTalkPage();
                if( $talk ) {
                        # Talk page link
-                       $tools[] = $sk->link( $talk, wfMsgHtml( 'talkpagelinktext' ) );
-                       if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
-                               # Block link
-                               if( $wgUser->isAllowed( 'block' ) )
-                                       $tools[] = $sk->linkKnown(
-                                               SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
-                                               wfMsgHtml( 'blocklink' )
-                                       );
+                       $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
+                       if( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
+                               if( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
+                                       if ( $userObj->isBlocked() ) {
+                                               $tools[] = Linker::linkKnown( # Change block link
+                                                       SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
+                                                       $this->msg( 'change-blocklink' )->escaped()
+                                               );
+                                               $tools[] = Linker::linkKnown( # Unblock link
+                                                       SpecialPage::getTitleFor( 'BlockList' ),
+                                                       $this->msg( 'unblocklink' )->escaped(),
+                                                       array(),
+                                                       array(
+                                                               'action' => 'unblock',
+                                                               'ip' => $nt->getDBkey()
+                                                       )
+                                               );
+                                       }
+                                       else { # User is not blocked
+                                               $tools[] = Linker::linkKnown( # Block link
+                                                       SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
+                                                       $this->msg( 'blocklink' )->escaped()
+                                               );
+                                       }
+                               }
                                # Block log link
-                               $tools[] = $sk->linkKnown(
+                               $tools[] = Linker::linkKnown(
                                        SpecialPage::getTitleFor( 'Log' ),
-                                       wfMsgHtml( 'sp-contributions-blocklog' ),
+                                       $this->msg( 'sp-contributions-blocklog' )->escaped(),
                                        array(),
                                        array(
                                                'type' => 'block',
@@ -360,40 +388,62 @@ class DeletedContributionsPage extends SpecialPage {
                                );
                        }
                        # Other logs link
-                       $tools[] = $sk->linkKnown(
+                       $tools[] = Linker::linkKnown(
                                SpecialPage::getTitleFor( 'Log' ),
-                               wfMsgHtml( 'sp-contributions-logs' ),
+                               $this->msg( 'sp-contributions-logs' )->escaped(),
                                array(),
                                array( 'user' => $nt->getText() )
                        );
-                       # Link to undeleted contributions
-                       $tools[] = $sk->linkKnown(
+                       # Link to contributions
+                       $tools[] = Linker::linkKnown(
                                SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
-                               wfMsgHtml( 'sp-deletedcontributions-contribs' )
+                               $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
                        );
 
                        # Add a link to change user rights for privileged users
                        $userrightsPage = new UserrightsPage();
-                       if( 0 !== $id && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
-                               $tools[] = $sk->linkKnown(
+                       $userrightsPage->setContext( $this->getContext() );
+                       if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
+                               $tools[] = Linker::linkKnown(
                                        SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
-                                       wfMsgHtml( 'sp-contributions-userrights' )
+                                       $this->msg( 'sp-contributions-userrights' )->escaped()
                                );
                        }
 
                        wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
 
-                       $links = $wgLang->pipeList( $tools );
+                       $links = $this->getLanguage()->pipeList( $tools );
+
+                       // Show a note if the user is blocked and display the last block log entry.
+                       if ( $userObj->isBlocked() ) {
+                               $out = $this->getOutput(); // LogEventsList::showLogExtract() wants the first parameter by ref
+                               LogEventsList::showLogExtract(
+                                       $out,
+                                       'block',
+                                       $nt,
+                                       '',
+                                       array(
+                                               'lim' => 1,
+                                               'showIfEmpty' => false,
+                                               'msgKey' => array(
+                                                       'sp-contributions-blocked-notice',
+                                                       $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
+                                               ),
+                                               'offset' => '' # don't use $this->getRequest() parameter offset
+                                       )
+                               );
+                       }
                }
 
                // Old message 'contribsub' had one parameter, but that doesn't work for
                // languages that want to put the "for" bit right after $user but before
                // $links.  If 'contribsub' is around, use it for reverse compatibility,
                // otherwise use 'contribsub2'.
-               if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
-                       return wfMsgHtml( 'contribsub2', $user, $links );
+               $oldMsg = $this->msg( 'contribsub' );
+               if ( $oldMsg->exists() ) {
+                       return $oldMsg->rawParams( "$user ($links)" );
                } else {
-                       return wfMsgHtml( 'contribsub', "$user ($links)" );
+                       return $this->msg( 'contribsub2' )->rawParams( $user, $links );
                }
        }
 
@@ -402,9 +452,9 @@ class DeletedContributionsPage extends SpecialPage {
         * @param $options Array: the options to be included.
         */
        function getForm( $options ) {
-               global $wgScript, $wgRequest;
+               global $wgScript;
 
-               $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
+               $options['title'] = $this->getTitle()->getPrefixedText();
                if ( !isset( $options['target'] ) ) {
                        $options['target'] = '';
                } else {
@@ -429,19 +479,19 @@ class DeletedContributionsPage extends SpecialPage {
                        if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
                                continue;
                        }
-                       $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
+                       $f .= "\t" . Html::hidden( $name, $value ) . "\n";
                }
 
                $f .=  Xml::openElement( 'fieldset' ) .
-                       Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
-                       Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
+                       Xml::element( 'legend', array(), $this->msg( 'sp-contributions-search' )->text() ) .
+                       Xml::tags( 'label', array( 'for' => 'target' ), $this->msg( 'sp-contributions-username' )->parse() ) . ' ' .
                        Html::input( 'target', $options['target'], 'text', array(
                                'size' => '20',
                                'required' => ''
                        ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
-                       Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
+                       Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) . ' ' .
                        Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
-                       Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
+                       Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() ) .
                        Xml::closeElement( 'fieldset' ) .
                        Xml::closeElement( 'form' );
                return $f;