Split includes/HTMLForm
[lhc/web/wiklou.git] / includes / SpecialPage.php
index 20571d7..f70a6dc 100644 (file)
@@ -595,6 +595,49 @@ class SpecialPage {
                }
        }
 
+       /**
+        * If the user is not logged in, throws UserNotLoggedIn error.
+        *
+        * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
+        * parameter.
+        *
+        * @since 1.23
+        * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
+        *     will be used as message keys. If a string is given, the message will also receive a
+        *     formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
+        *     Message is given, it will be passed on verbatim.
+        * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
+        *     will be used as message keys.
+        * @throws UserNotLoggedIn
+        */
+       public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
+               if ( $this->getUser()->isAnon() ) {
+                       // Use default messages if not given or explicit null passed
+                       if ( !$reasonMsg ) {
+                               $reasonMsg = 'exception-nologin-text-manual';
+                       }
+                       if ( !$titleMsg ) {
+                               $titleMsg = 'exception-nologin';
+                       }
+
+                       // Convert to Messages with current context
+                       if ( is_string( $reasonMsg ) ) {
+                               $loginreqlink = Linker::linkKnown(
+                                       SpecialPage::getTitleFor( 'Userlogin' ),
+                                       $this->msg( 'loginreqlink' )->escaped(),
+                                       array(),
+                                       array( 'returnto' => $this->getTitle()->getPrefixedText() )
+                               );
+                               $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
+                       }
+                       if ( is_string( $titleMsg ) ) {
+                               $titleMsg = $this->msg( $titleMsg );
+                       }
+
+                       throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
+               }
+       }
+
        /**
         * Sets headers - this should be called from the execute() method of all derived classes!
         */
@@ -833,12 +876,10 @@ class SpecialPage {
         * @see wfMessage
         */
        public function msg( /* $args */ ) {
-               // Note: can't use func_get_args() directly as second or later item in
-               // a parameter list until PHP 5.3 or you get a fatal error.
-               // Works fine as the first parameter, which appears elsewhere in the
-               // code base. Sighhhh.
-               $args = func_get_args();
-               $message = call_user_func_array( array( $this->getContext(), 'msg' ), $args );
+               $message = call_user_func_array(
+                       array( $this->getContext(), 'msg' ),
+                       func_get_args()
+               );
                // RequestContext passes context to wfMessage, and the language is set from
                // the context, but setting the language for Message class removes the
                // interface message status, which breaks for example usernameless gender
@@ -877,7 +918,6 @@ class SpecialPage {
        public function getFinalGroupName() {
                global $wgSpecialPageGroups;
                $name = $this->getName();
-               $group = '-';
 
                // Allow overbidding the group from the wiki side
                $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
@@ -1420,7 +1460,6 @@ class SpecialAllMyUploads extends RedirectSpecialPage {
        }
 }
 
-
 /**
  * Redirect from Special:PermanentLink/### to index.php?oldid=###
  */