From ec31dd602ea370497c69d86d12e41a310dc6d4eb Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Fri, 14 May 2010 20:36:23 +0000 Subject: [PATCH] Start adding support for leaving user's messages. Begin using it in UploadFromUrl.php 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 | 25 ++++++++----- includes/User.php | 60 +++++++++++++++++++++++++++++++ includes/upload/UploadFromUrl.php | 19 ++++++---- languages/messages/MessagesEn.php | 7 ++++ maintenance/language/messages.inc | 8 +++++ 5 files changed, 104 insertions(+), 15 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 2ec76b1f1f..5d7912ec01 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -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 ) ) ) diff --git a/includes/User.php b/includes/User.php index 275800f806..cd1ae37c94 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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(); + } } diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index dce9ae3927..91a630bd11 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -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; } } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 93c030c61b..36a274286a 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2149,6 +2149,9 @@ JD # Jenoptik MGP # Pentax PICT # misc. # ', # 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 http:// or ftp://.', @@ -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', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 0f818e5991..b9a1d94982 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -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' => '', -- 2.20.1