Start adding support for leaving user's messages. Begin using it in UploadFromUrl.php
authorMark A. Hershberger <mah@users.mediawiki.org>
Fri, 14 May 2010 20:36:23 +0000 (20:36 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Fri, 14 May 2010 20:36:23 +0000 (20:36 +0000)
In the end, we should probably use a template if its around and do something sane with the subject line if not.

includes/Article.php
includes/User.php
includes/upload/UploadFromUrl.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index 2ec76b1..5d7912e 100644 (file)
@@ -1870,6 +1870,22 @@ class Article {
                return true;
        }
 
+       /**
+        * Check flags and add EDIT_NEW or EDIT_UPDATE to them as needed.
+        * @param $flags Int
+        * @return Int updated $flags
+        */
+       function checkFlags( $flags ) {
+               if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
+                       if ( $this->mTitle->getArticleID() ) {
+                               $flags |= EDIT_UPDATE;
+                       } else {
+                               $flags |= EDIT_NEW;
+                       }
+               }
+
+               return $flags;
+       }
        /**
         * Article::doEdit()
         *
@@ -1936,14 +1952,7 @@ class Article {
                # Load $this->mTitle->getArticleID() and $this->mLatest if it's not already
                $this->loadPageData();
 
-               if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
-                       $aid = $this->mTitle->getArticleID();
-                       if ( $aid ) {
-                               $flags |= EDIT_UPDATE;
-                       } else {
-                               $flags |= EDIT_NEW;
-                       }
-               }
+               $flags = $this->checkFlags( $flags );
 
                if ( !wfRunHooks( 'ArticleSave', array( &$this, &$user, &$text, &$summary,
                        $flags & EDIT_MINOR, null, null, &$flags, &$status ) ) )
index 275800f..cd1ae37 100644 (file)
@@ -3748,4 +3748,64 @@ class User {
 
                return $ret;
        }
+
+       /**
+        * Leave a user a message
+        * @param $text String the message to leave
+        * @param $summary String the summary for this change, defaults to
+        *                        "Leave system message."
+        * @param $article Article The article to update, defaults to the
+        *                        user's talk page.
+        * @param $editor User The user leaving the message, defaults to
+        *                        "SystemMessage"
+        * @param $flags Int default edit flags
+        *
+        * @return boolean true if it was successful
+        */
+       public function leaveUserMessage( $subject, $text, $signature = "",
+                       $summary = null, $editor = null, $flags = 0 ) {
+               if ( !isset( $summary ) ) {
+                       $summary = wfMsgForContent( 'usermessage-summary' );
+               }
+
+               if ( !isset( $editor ) ) {
+                       $editor = User::newFromName( wfMsgForContent( 'usermessage-editor' ) );
+                       if ( !$editor->isLoggedIn() ) {
+                               $editor->addToDatabase();
+                       }
+               }
+
+               $article = new Article( $this->getTalkPage() );
+               wfRunHooks( 'SetupUserMessageArticle',
+                       array( &$article, $subject, $this, $editor ) );
+
+               $flags = $article->checkFlags( $flags );
+
+               if ( $flags & EDIT_UPDATE ) {
+                       $text .= $article->getContent();
+               }
+
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
+
+               try {
+                       $status = $article->doEdit( $text, $summary, $flags, false, $editor );
+               } catch ( DBQueryError $e ) {
+                       $status = Status::newFatal("DB Error");
+               }
+
+               if ( $status->isGood() ) {
+                       // Set newtalk with the right user ID
+                       $this->setNewtalk( true );
+                       wfRunHooks( 'AfterUserMessage',
+                               array( $this, $article, $summary, $signature, $editor, $text ) );
+                       $dbw->commit();
+               } else {
+                       // The article was concurrently created
+                       wfDebug( __METHOD__ . ": Error ".$status->getWikiText() );
+                       $dbw->rollback();
+               }
+
+               return $status->isGood();
+       }
 }
index dce9ae3..91a630b 100644 (file)
@@ -143,12 +143,12 @@ class UploadFromUrl extends UploadBase {
                }
 
                $status = $this->saveTempFile( $req );
-               $this->mRemoveTempFile = true;
-
-               if( !$status->isOk() ) {
+               if ( !$status->isGood() ) {
                        return $status;
                }
 
+               $this->mRemoveTempFile = true;
+
                $v = $this->verifyUpload();
                if( $v['status'] !== UploadBase::OK ) {
                        return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
@@ -162,13 +162,18 @@ class UploadFromUrl extends UploadBase {
                // This comes from ApiBase
                /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
 
-               if ( !$status->isGood() ) {
-                       return $status;
-               }
-
                $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
                        $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
 
+               if ( $status->isGood() ) {
+                       $url = $this->getLocalFile()->getDescriptionUrl();
+                       $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
+                               wfMsg( 'upload-success-msg', $url ) );
+               } else {
+                       $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
+                               wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
+               }
+
                return $status;
        }
 }
index 93c030c..36a2742 100644 (file)
@@ -2149,6 +2149,9 @@ JD # Jenoptik
 MGP # Pentax
 PICT # misc.
  #</pre> <!-- leave this line exactly as it is -->', # only translate this message to other languages if you have to change it
+'upload-successful-msg'     => 'Your upload is available here: $1',
+'upload-failure-subj'       => 'Upload Problem',
+'upload-failure-msg'        => 'There was a problem with your upload:\n $1',
 
 'upload-proto-error'        => 'Incorrect protocol',
 'upload-proto-error-text'   => 'Remote upload requires URLs beginning with <code>http://</code> or <code>ftp://</code>.',
@@ -2602,6 +2605,10 @@ The e-mail address you entered in [[Special:Preferences|your user preferences]]
 'emailsenttext'        => 'Your e-mail message has been sent.',
 'emailuserfooter'      => 'This e-mail was sent by $1 to $2 by the "E-mail user" function at {{SITENAME}}.',
 
+# User Message
+'usermessage-summary'  => 'Leave system message.',
+'usermessage-editor'   => 'System messenger',
+
 # Watchlist
 'watchlist'            => 'My watchlist',
 'mywatchlist'          => 'My watchlist',
index 0f818e5..b9a1d94 100644 (file)
@@ -1265,6 +1265,9 @@ $wgMessageStructure = array(
                'upload-wasdeleted',
                'filename-bad-prefix',
                'filename-prefix-blacklist',
+               'upload-successful-msg',
+               'upload-failure-subj',
+               'upload-failure-msg',
        ),
        'upload-errors' => array(
                'upload-proto-error',
@@ -1696,6 +1699,10 @@ $wgMessageStructure = array(
                'emailsenttext',
                'emailuserfooter',
        ),
+       'usermessage' => array(
+               'usermessage-summary',
+               'usermessage-editor',
+       ),
        'watchlist' => array(
                'watchlist',
                'mywatchlist',
@@ -3258,6 +3265,7 @@ XHTML id names.",
        'newuserlog'          => 'Special:Log/newusers',
        'listgrouprights'     => 'Special:ListGroupRights',
        'emailuser'           => 'E-mail user',
+       'usermessage'         => 'User Messenger',
        'watchlist'           => 'Watchlist',
        'watching'            => 'Displayed when you click the "watch" button and it is in the process of watching',
        'enotif'              => '',