X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FEditPage.php;h=8df876a2f0794f885d19828fb3a87f53f49f2a46;hb=40589a98d7743ae0726de1321f0933f4aae8720f;hp=c2ced80cfe1217043475b6cc139f235ecf0e1741;hpb=a67787f7487fb05199158d55c574f063227d364f;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/EditPage.php b/includes/EditPage.php index c2ced80cfe..8df876a2f0 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1,6 +1,7 @@ mTitle (as well as $mArticle) is the + * page in the database that is actually being edited. These are + * usually the same, but they are now allowed to be different. */ class EditPage { + const AS_SUCCESS_UPDATE = 200; + const AS_SUCCESS_NEW_ARTICLE = 201; + const AS_HOOK_ERROR = 210; + const AS_FILTERING = 211; + const AS_HOOK_ERROR_EXPECTED = 212; + const AS_BLOCKED_PAGE_FOR_USER = 215; + const AS_CONTENT_TOO_BIG = 216; + const AS_USER_CANNOT_EDIT = 217; + const AS_READ_ONLY_PAGE_ANON = 218; + const AS_READ_ONLY_PAGE_LOGGED = 219; + const AS_READ_ONLY_PAGE = 220; + const AS_RATE_LIMITED = 221; + const AS_ARTICLE_WAS_DELETED = 222; + const AS_NO_CREATE_PERMISSION = 223; + const AS_BLANK_ARTICLE = 224; + const AS_CONFLICT_DETECTED = 225; + const AS_SUMMARY_NEEDED = 226; + const AS_TEXTBOX_EMPTY = 228; + const AS_MAX_ARTICLE_SIZE_EXCEEDED = 229; + const AS_OK = 230; + const AS_END = 231; + const AS_SPAM_ERROR = 232; + const AS_IMAGE_REDIRECT_ANON = 233; + const AS_IMAGE_REDIRECT_LOGGED = 234; + var $mArticle; var $mTitle; var $mMetaData = ''; @@ -42,32 +74,39 @@ class EditPage { # extensions should take care to _append_ to the present value public $editFormPageTop; // Before even the preview public $editFormTextTop; + public $editFormTextBeforeContent; public $editFormTextAfterWarn; public $editFormTextAfterTools; public $editFormTextBottom; + /* $didSave should be set to true whenever an article was succesfully altered. */ + public $didSave = false; + + public $suppressIntro = false; + /** * @todo document * @param $article */ function EditPage( $article ) { $this->mArticle =& $article; - global $wgTitle; - $this->mTitle =& $wgTitle; + $this->mTitle = $article->getTitle(); # Placeholders for text injection by hooks (empty per default) $this->editFormPageTop = $this->editFormTextTop = + $this->editFormTextBeforeContent = $this->editFormTextAfterWarn = $this->editFormTextAfterTools = $this->editFormTextBottom = ""; } - + /** * Fetch initial editing page content. + * @private */ - private function getContent( $def_text = '' ) { - global $wgOut, $wgRequest, $wgParser; + function getContent( $def_text = '' ) { + global $wgOut, $wgRequest, $wgParser, $wgMessageCache; # Get variables from query string :P $section = $wgRequest->getVal( 'section' ); @@ -80,7 +119,8 @@ class EditPage { $text = ''; if( !$this->mTitle->exists() ) { if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) { - # If this is a system message, get the default text. + $wgMessageCache->loadAllMessages(); + # If this is a system message, get the default text. $text = wfMsgWeirdKey ( $this->mTitle->getText() ) ; } else { # If requested, preload some text. @@ -114,39 +154,43 @@ class EditPage { $oldrev = $undorev ? $undorev->getPrevious() : null; } - #Sanity check, make sure it's the right page. + # Sanity check, make sure it's the right page, + # the revisions exist and they were not deleted. # Otherwise, $text will be left as-is. - if ( !is_null($undorev) && !is_null($oldrev) && $undorev->getPage()==$oldrev->getPage() && $undorev->getPage()==$this->mArticle->getID() ) { + if( !is_null( $undorev ) && !is_null( $oldrev ) && + $undorev->getPage() == $oldrev->getPage() && + $undorev->getPage() == $this->mArticle->getID() && + !$undorev->isDeleted( Revision::DELETED_TEXT ) && + !$oldrev->isDeleted( Revision::DELETED_TEXT ) ) { $undorev_text = $undorev->getText(); $oldrev_text = $oldrev->getText(); $currev_text = $text; - #No use doing a merge if it's just a straight revert. if ( $currev_text != $undorev_text ) { - $result = wfMerge($undorev_text, $oldrev_text, $currev_text, $text); + $result = wfMerge( $undorev_text, $oldrev_text, $currev_text, $text ); } else { + # No use doing a merge if it's just a straight revert. $text = $oldrev_text; $result = true; } + if( $result ) { + # Inform the user of our success and set an automatic edit summary + $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-success' ) ); + $firstrev = $oldrev->getNext(); + # If we just undid one rev, use an autosummary + if( $firstrev->mId == $undo ) { + $this->summary = wfMsgForContent('undo-summary', $undo, $undorev->getUserText()); + } + $this->formtype = 'diff'; + } else { + # Warn the user that something went wrong + $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-failure' ) ); + } } else { // Failed basic sanity checks. // Older revisions may have been removed since the link // was created, or we may simply have got bogus input. - $result = false; - } - - if( $result ) { - # Inform the user of our success and set an automatic edit summary - $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-success' ) ); - $firstrev = $oldrev->getNext(); - # If we just undid one rev, use an autosummary - if ( $firstrev->mId == $undo ) { - $this->summary = wfMsgForContent('undo-summary', $undo, $undorev->getUserText()); - } - $this->formtype = 'diff'; - } else { - # Warn the user that something went wrong - $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-failure' ) ); + $this->editFormPageTop .= $wgOut->parse( wfMsgNoTrans( 'undo-norev' ) ); } } else if( $section != '' ) { if( $section == 'new' ) { @@ -281,6 +325,25 @@ class EditPage { $this->mMetaData = $s ; } + protected function wasDeletedSinceLastEdit() { + /* Note that we rely on the logging table, which hasn't been always there, + * but that doesn't matter, because this only applies to brand new + * deletes. + */ + if ( $this->deletedSinceEdit ) + return true; + if ( $this->mTitle->isDeleted() ) { + $this->lastDelete = $this->getLastDelete(); + if ( !is_null($this->lastDelete) ) { + $deletetime = $this->lastDelete->log_timestamp; + if ( ($deletetime - $this->starttime) > 0 ) { + $this->deletedSinceEdit = true; + } + } + } + return $this->deletedSinceEdit; + } + function submit() { $this->edit(); } @@ -297,14 +360,13 @@ class EditPage { * the newly-edited page. */ function edit() { - global $wgOut, $wgUser, $wgRequest, $wgTitle; + global $wgOut, $wgUser, $wgRequest; - if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) ) + if ( !wfRunHooks( 'AlternateEdit', array( &$this ) ) ) return; - $fname = 'EditPage::edit'; - wfProfileIn( $fname ); - wfDebug( "$fname: enter\n" ); + wfProfileIn( __METHOD__ ); + wfDebug( __METHOD__.": enter\n" ); // this is not an article $wgOut->setArticleFlag(false); @@ -314,18 +376,28 @@ class EditPage { if( $this->live ) { $this->livePreview(); - wfProfileOut( $fname ); + wfProfileOut( __METHOD__ ); + return; + } + + $wgOut->addScriptFile( 'edit.js' ); + + if( wfReadOnly() ) { + $this->readOnlyPage( $this->getContent() ); + wfProfileOut( __METHOD__ ); return; } $permErrors = $this->mTitle->getUserPermissionsErrors('edit', $wgUser); - if( !$this->mTitle->exists() ) - $permErrors += array_diff( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors ); + if( !$this->mTitle->exists() ) { + $permErrors = array_merge( $permErrors, + wfArrayDiff2( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors ) ); + } # Ignore some permissions errors. $remove = array(); foreach( $permErrors as $error ) { - if ($this->preview || $this->diff && + if ( ( $this->preview || $this->diff ) && ($error[0] == 'blockedtext' || $error[0] == 'autoblockedtext')) { // Don't worry about blocks when previewing/diffing @@ -341,14 +413,12 @@ class EditPage { } } } - # array_diff returns elements in $permErrors that are not in $remove. - $permErrors = array_diff( $permErrors, $remove ); + $permErrors = wfArrayDiff2( $permErrors, $remove ); - if ( !empty($permErrors) ) - { - wfDebug( "$fname: User can't edit\n" ); - $wgOut->readOnlyPage( $this->getContent(), true, $permErrors ); - wfProfileOut( $fname ); + if ( $permErrors ) { + wfDebug( __METHOD__.": User can't edit\n" ); + $this->readOnlyPage( $this->getContent(), true, $permErrors ); + wfProfileOut( __METHOD__ ); return; } else { if ( $this->save ) { @@ -368,40 +438,19 @@ class EditPage { } } - wfProfileIn( "$fname-business-end" ); + wfProfileIn( __METHOD__."-business-end" ); $this->isConflict = false; // css / js subpages of user pages get a special treatment - $this->isCssJsSubpage = $wgTitle->isCssJsSubpage(); - $this->isValidCssJsSubpage = $wgTitle->isValidCssJsSubpage(); - - /* Notice that we can't use isDeleted, because it returns true if article is ever deleted - * no matter it's current state - */ - $this->deletedSinceEdit = false; - if ( $this->edittime != '' ) { - /* Note that we rely on logging table, which hasn't been always there, - * but that doesn't matter, because this only applies to brand new - * deletes. This is done on every preview and save request. Move it further down - * to only perform it on saves - */ - if ( $this->mTitle->isDeleted() ) { - $this->lastDelete = $this->getLastDelete(); - if ( !is_null($this->lastDelete) ) { - $deletetime = $this->lastDelete->log_timestamp; - if ( ($deletetime - $this->starttime) > 0 ) { - $this->deletedSinceEdit = true; - } - } - } - } + $this->isCssJsSubpage = $this->mTitle->isCssJsSubpage(); + $this->isValidCssJsSubpage = $this->mTitle->isValidCssJsSubpage(); # Show applicable editing introductions if( $this->formtype == 'initial' || $this->firsttime ) $this->showIntro(); - + if( $this->mTitle->isTalkPage() ) { - $wgOut->addWikiText( wfMsg( 'talkpagetext' ) ); + $wgOut->addWikiMsg( 'talkpagetext' ); } # Attempt submission here. This will check for edit conflicts, @@ -411,8 +460,8 @@ class EditPage { if ( 'save' == $this->formtype ) { if ( !$this->attemptSave() ) { - wfProfileOut( "$fname-business-end" ); - wfProfileOut( $fname ); + wfProfileOut( __METHOD__."-business-end" ); + wfProfileOut( __METHOD__ ); return; } } @@ -422,17 +471,34 @@ class EditPage { if ( 'initial' == $this->formtype || $this->firsttime ) { if ($this->initialiseForm() === false) { $this->noSuchSectionPage(); - wfProfileOut( "$fname-business-end" ); - wfProfileOut( $fname ); + wfProfileOut( __METHOD__."-business-end" ); + wfProfileOut( __METHOD__ ); return; } - if( !$this->mTitle->getArticleId() ) + if( !$this->mTitle->getArticleId() ) wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) ); } $this->showEditForm(); - wfProfileOut( "$fname-business-end" ); - wfProfileOut( $fname ); + wfProfileOut( __METHOD__."-business-end" ); + wfProfileOut( __METHOD__ ); + } + + /** + * Show a read-only error + * Parameters are the same as OutputPage:readOnlyPage() + * Redirect to the article page if redlink=1 + */ + function readOnlyPage( $source = null, $protected = false, $reasons = array() ) { + global $wgRequest, $wgOut; + if ( $wgRequest->getBool( 'redlink' ) ) { + // The edit page was reached via a red link. + // Redirect to the article page and let them click the edit tab if + // they really want a permission error. + $wgOut->redirect( $this->mTitle->getFullUrl() ); + } else { + $wgOut->readOnlyPage( $source, $protected, $reasons ); + } } /** @@ -471,15 +537,21 @@ class EditPage { $fname = 'EditPage::importFormData'; wfProfileIn( $fname ); + # Section edit can come from either the form or a link + $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) ); + if( $request->wasPosted() ) { # These fields need to be checked for encoding. # Also remove trailing whitespace, but don't remove _initial_ # whitespace from the text boxes. This may be significant formatting. $this->textbox1 = $this->safeUnicodeInput( $request, 'wpTextbox1' ); $this->textbox2 = $this->safeUnicodeInput( $request, 'wpTextbox2' ); - $this->mMetaData = rtrim( $request->getText( 'metadata' ) ); + $this->mMetaData = rtrim( $request->getText( 'metadata' ) ); # Truncate for whole multibyte characters. +5 bytes for ellipsis - $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 250 ); + $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 250 ); + + # Remove extra headings from summaries and new sections. + $this->summary = preg_replace('/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->summary); $this->edittime = $request->getVal( 'wpEdittime' ); $this->starttime = $request->getVal( 'wpStarttime' ); @@ -516,7 +588,7 @@ class EditPage { $this->preview = true; } } - $this->save = ! ( $this->preview OR $this->diff ); + $this->save = !$this->preview && !$this->diff; if( !preg_match( '/^\d{14}$/', $this->edittime )) { $this->edittime = null; } @@ -537,7 +609,7 @@ class EditPage { $this->allowBlankSummary = $request->getBool( 'wpIgnoreBlankSummary' ); } - $this->autoSumm = $request->getText( 'wpAutoSummary' ); + $this->autoSumm = $request->getText( 'wpAutoSummary' ); } else { # Not a posted form? Start with nothing. wfDebug( "$fname: Not a posted form.\n" ); @@ -554,13 +626,14 @@ class EditPage { $this->minoredit = false; $this->watchthis = false; $this->recreate = false; + + if ( $this->section == 'new' && $request->getVal( 'preloadtitle' ) ) { + $this->summary = $request->getVal( 'preloadtitle' ); + } } $this->oldid = $request->getInt( 'oldid' ); - # Section edit can come from either the form or a link - $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) ); - $this->live = $request->getCheck( 'live' ); $this->editintro = $request->getText( 'editintro' ); @@ -587,16 +660,32 @@ class EditPage { */ private function showIntro() { global $wgOut, $wgUser; + if( $this->suppressIntro ) + return; + + # Show a warning message when someone creates/edits a user (talk) page but the user does not exists + if( $this->mTitle->getNamespace() == NS_USER || $this->mTitle->getNamespace() == NS_USER_TALK ) { + $parts = explode( '/', $this->mTitle->getText(), 2 ); + $username = $parts[0]; + $id = User::idFromName( $username ); + $ip = User::isIP( $username ); + + if ( $id == 0 && !$ip ) { + $wgOut->wrapWikiMsg( '