Merge "[JobQueue] Added support for approximate FIFO job queues."
[lhc/web/wiklou.git] / includes / ExternalEdit.php
1 <?php
2 /**
3 * External editors support
4 *
5 * License: Public domain
6 *
7 * @file
8 * @author Erik Moeller <moeller@scireview.de>
9 */
10
11 /**
12 * Support for external editors to modify both text and files
13 * in external applications. It works as follows: MediaWiki
14 * sends a meta-file with the MIME type 'application/x-external-editor'
15 * to the client. The user has to associate that MIME type with
16 * a helper application (a reference implementation in Perl
17 * can be found in extensions/ee), which will launch the editor,
18 * and save the modified data back to the server.
19 *
20 */
21 class ExternalEdit extends ContextSource {
22
23 /**
24 * Array of URLs to link to
25 * @var Array
26 */
27 private $urls;
28
29 /**
30 * Constructor
31 * @param $context IContextSource context to use
32 * @param $urls array
33 */
34 public function __construct( IContextSource $context, array $urls = array() ) {
35 $this->setContext( $context );
36 $this->urls = $urls;
37 }
38
39 /**
40 * Check whether external edit or diff should be used.
41 *
42 * @param $context IContextSource context to use
43 * @param $type String can be either 'edit' or 'diff'
44 * @return Bool
45 */
46 public static function useExternalEngine( IContextSource $context, $type ) {
47 global $wgUseExternalEditor;
48
49 if ( !$wgUseExternalEditor ) {
50 return false;
51 }
52
53 $pref = $type == 'diff' ? 'externaldiff' : 'externaleditor';
54 $request = $context->getRequest();
55
56 return !$request->getVal( 'internaledit' ) &&
57 ( $context->getUser()->getOption( $pref ) || $request->getVal( 'externaledit' ) );
58 }
59
60 /**
61 * Output the information for the external editor
62 */
63 public function execute() {
64 global $wgContLang, $wgScript, $wgScriptPath, $wgCanonicalServer;
65
66 $this->getOutput()->disable();
67
68 $response = $this->getRequest()->response();
69 $response->header( 'Content-type: application/x-external-editor; charset=utf-8' );
70 $response->header( 'Cache-control: no-cache' );
71
72 $special = $wgContLang->getNsText( NS_SPECIAL );
73
74 # $type can be "Edit text", "Edit file" or "Diff text" at the moment
75 # See the protocol specifications at [[m:Help:External editors/Tech]] for
76 # details.
77 if ( count( $this->urls ) ) {
78 $urls = $this->urls;
79 $type = "Diff text";
80 } elseif ( $this->getRequest()->getVal( 'mode' ) == 'file' ) {
81 $type = "Edit file";
82 $image = wfLocalFile( $this->getTitle() );
83 if ( $image ) {
84 $urls = array(
85 'File' => array(
86 'Extension' => $image->getExtension(),
87 'URL' => $image->getCanonicalURL()
88 )
89 );
90 } else{
91 $urls = array();
92 }
93 } else {
94 $type = "Edit text";
95 # *.wiki file extension is used by some editors for syntax
96 # highlighting, so we follow that convention
97 $urls = array( 'File' => array(
98 'Extension' => 'wiki',
99 'URL' => $this->getTitle()->getCanonicalURL(
100 array( 'action' => 'edit', 'internaledit' => 'true' ) )
101 ) );
102 }
103
104 $files = '';
105 foreach( $urls as $key => $vars ) {
106 $files .= "\n[$key]\n";
107 foreach( $vars as $varname => $varval ) {
108 $files .= "$varname=$varval\n";
109 }
110 }
111
112 $url = $this->getTitle()->getFullURL(
113 $this->getRequest()->appendQueryValue( 'internaledit', 1, true ) );
114
115 $control = <<<CONTROL
116 ; You're seeing this file because you're using Mediawiki's External Editor feature.
117 ; This is probably because you selected use external editor in your preferences.
118 ; To edit normally, either disable that preference or go to the URL:
119 ; $url
120 ; See http://www.mediawiki.org/wiki/Manual:External_editors for details.
121 [Process]
122 Type=$type
123 Engine=MediaWiki
124 Script={$wgCanonicalServer}{$wgScript}
125 Server={$wgCanonicalServer}
126 Path={$wgScriptPath}
127 Special namespace=$special
128 $files
129 CONTROL;
130 echo $control;
131 }
132 }