From 588539858c89024ee65636689817181a0c435fb7 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 9 Nov 2011 15:31:55 +0000 Subject: [PATCH] Some updates to EditPage and ExternalEdit: * Made ExternalEdit use a context * Updated DifferenceEngine to use ExternalEdit to send the diff stuff to reduce code duplication * Introduced ExternalEdit::useExternalEngine() to check whether to use the external edit or diff (except for the action, section and oldid which are still checked in Wiki.php) to remove code duplication; external diff can now also be controlled with externaledit or internaledit URL parameters * Use $wgContLang to get the name of the "Special" namespace instead of user's language * Modified the line breaks in the comment on the top of the control file so that the URL is on its own line * Updated extension to call EditPage::edit() instead of EditPage::submit(), the latter will just call the former * Updated extension to let core handle itself the ExternalEdit mode instead of doing this themself --- includes/ExternalEdit.php | 114 +++++++++++++++++++---------- includes/Wiki.php | 21 ++---- includes/diff/DifferenceEngine.php | 48 +++++------- 3 files changed, 101 insertions(+), 82 deletions(-) diff --git a/includes/ExternalEdit.php b/includes/ExternalEdit.php index bf97c1a5e1..b225001997 100644 --- a/includes/ExternalEdit.php +++ b/includes/ExternalEdit.php @@ -18,60 +18,101 @@ * and save the modified data back to the server. * */ -class ExternalEdit { - /** - * Title to perform the edit on - * @var Title - */ - private $title; +class ExternalEdit extends ContextSource { /** - * Mode of editing - * @var String + * Array of URLs to link to + * @var Array */ - private $mode; + private $urls; /** * Constructor - * @param $title Title object we're performing the edit on + * @param $context IContextSource context to use * @param $mode String What mode we're using. Only 'file' has any effect */ - public function __construct( $title, $mode ) { - $this->title = $title; - $this->mode = $mode; + public function __construct( IContextSource $context, array $urls = array() ) { + $this->setContext( $context ); + $this->urls = $urls; + } + + /** + * Check whether external edit or diff should be used. + * + * @param $context IContextSource context to use + * @param $type String can be either 'edit' or 'diff' + * @return Bool + */ + public static function useExternalEngine( IContextSource $context, $type ) { + global $wgUseExternalEditor; + + if ( !$wgUseExternalEditor ) { + return false; + } + + $pref = $type == 'diff' ? 'externaldiff' : 'externaleditor'; + $request = $context->getRequest(); + + return !$request->getVal( 'internaledit' ) && + ( $context->getUser()->getOption( $pref ) || $request->getVal( 'externaledit' ) ); } /** * Output the information for the external editor */ - public function edit() { - global $wgOut, $wgScript, $wgScriptPath, $wgCanonicalServer, $wgLang; - $wgOut->disable(); - header( 'Content-type: application/x-external-editor; charset=utf-8' ); - header( 'Cache-control: no-cache' ); + public function execute() { + global $wgContLang, $wgScript, $wgScriptPath, $wgCanonicalServer; + + $this->getOutput()->disable(); + + $response = $this->getRequest()->response(); + $response->header( 'Content-type: application/x-external-editor; charset=utf-8' ); + $response->header( 'Cache-control: no-cache' ); + + $special = $wgContLang->getNsText( NS_SPECIAL ); # $type can be "Edit text", "Edit file" or "Diff text" at the moment # See the protocol specifications at [[m:Help:External editors/Tech]] for # details. - if( $this->mode == "file" ) { - $type = "Edit file"; - $image = wfLocalFile( $this->title ); - $url = $image->getCanonicalURL(); - $extension = $image->getExtension(); + if ( count( $this->urls ) ) { + $urls = $this->urls; + $type = "Diff text"; } else { - $type = "Edit text"; - $url = $this->title->getCanonicalURL( - array( 'action' => 'edit', 'internaledit' => 'true' ) ); - # *.wiki file extension is used by some editors for syntax - # highlighting, so we follow that convention - $extension = "wiki"; + if ( $this->getRequest()->getVal( 'mode' ) == 'file' ) { + $type = "Edit file"; + $image = wfLocalFile( $this->title ); + $urls = array( 'File' => array( + 'Extension' => $image->getExtension(), + 'URL' => $image->getCanonicalURL() + ) ); + } else { + $type = "Edit text"; + # *.wiki file extension is used by some editors for syntax + # highlighting, so we follow that convention + $urls = array( 'File' => array( + 'Extension' => 'wiki', + 'URL' => $this->getTitle()->getCanonicalURL( + array( 'action' => 'edit', 'internaledit' => 'true' ) ) + ) ); + } } - $special = $wgLang->getNsText( NS_SPECIAL ); + + $files = ''; + foreach( $urls as $key => $vars ) { + $files .= "\n[$key]\n"; + foreach( $vars as $varname => $varval ) { + $files .= "$varname=$varval\n"; + } + } + + $url = $this->getTitle()->getFullURL( + $this->getRequest()->appendQueryValue( 'internaledit', 1, true ) ); + $control = <<getVal( 'internaledit' ); - $external = $request->getVal( 'externaledit' ); - $section = $request->getVal( 'section' ); - $oldid = $request->getVal( 'oldid' ); - if ( !$wgUseExternalEditor || $act == 'submit' || $internal || - $section || $oldid || - ( !$user->getOption( 'externaleditor' ) && !$external ) ) + if ( ExternalEdit::useExternalEngine( $this->context, 'edit' ) + && $act == 'edit' && !$request->getVal( 'section' ) + && !$request->getVal( 'oldid' ) ) { + $extedit = new ExternalEdit( $this->context ); + $extedit->execute(); + } else { $editor = new EditPage( $article ); - $editor->submit(); - } elseif ( $wgUseExternalEditor - && ( $external || $user->getOption( 'externaleditor' ) ) ) - { - $mode = $request->getVal( 'mode' ); - $extedit = new ExternalEdit( $article->getTitle(), $mode ); - $extedit->edit(); + $editor->edit(); } } break; diff --git a/includes/diff/DifferenceEngine.php b/includes/diff/DifferenceEngine.php index c433f89975..baba6dc634 100644 --- a/includes/diff/DifferenceEngine.php +++ b/includes/diff/DifferenceEngine.php @@ -204,39 +204,27 @@ class DifferenceEngine { throw new PermissionsError( 'read', $permErrors ); } + /// @todo Make RequestContext::getMain() go away when we have a context available + $context = RequestContext::getMain(); + # If external diffs are enabled both globally and for the user, # we'll use the application/x-external-editor interface to call # an external diff tool like kompare, kdiff3, etc. - if ( $wgUseExternalEditor && $wgUser->getOption( 'externaldiff' ) ) { - global $wgCanonicalServer, $wgScript, $wgLang; - $wgOut->disable(); - header ( "Content-type: application/x-external-editor; charset=UTF-8" ); - # This should be mOldPage, but it may not be set, see below. - $url1 = $this->mNewPage->getCanonicalURL( array( - 'action' => 'raw', - 'oldid' => $this->mOldid - ) ); - $url2 = $this->mNewPage->getCanonicalURL( array( - 'action' => 'raw', - 'oldid' => $this->mNewid - ) ); - $special = $wgLang->getNsText( NS_SPECIAL ); - $control = << array( 'Extension' => 'wiki', 'URL' => + # This should be mOldPage, but it may not be set, see below. + $this->mNewPage->getCanonicalURL( array( + 'action' => 'raw', 'oldid' => $this->mOldid ) ) + ), + 'File2' => array( 'Extension' => 'wiki', 'URL' => + $this->mNewPage->getCanonicalURL( array( + 'action' => 'raw', 'oldid' => $this->mNewid ) ) + ), + ); + + $externalEditor = new ExternalEdit( $context, $urls ); + $externalEditor->execute(); wfProfileOut( __METHOD__ ); return; -- 2.20.1