~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to contrib/AnyWikiDraw/anywikidraw/mediawiki 1.12 to 1.15/extensions/AnyWikiDraw/AnyWikiDraw.php

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * @(#)AnyWikiDraw.php  
4
 
 *
5
 
 * Copyright (c) 2007-2009 by the original authors of AnyWikiDraw
6
 
 * and all its contributors.
7
 
 *
8
 
 * The copyright of this software is owned by the authors and  
9
 
 * contributors of the AnyWikiDraw project ("the copyright holders").
10
 
 * You may not use, copy or modify this software, except in  
11
 
 * accordance with the license agreement you entered into with  
12
 
 * the copyright holders. For details see accompanying license terms. 
13
 
 */
14
 
 
15
 
/**
16
 
 * --------
17
 
 * WARNING: This is an extension for MediaWiki 1.12 through 1.15 only.
18
 
 * Do not use it with other versions of MediaWiki without extensive testing!
19
 
 * --------
20
 
 *
21
 
 * With the AnyWikiDraw extension it is possible to define new tags of the form
22
 
 *
23
 
 * {{#drawing:image.svg|width|height}}
24
 
 *
25
 
 # - The tag needs to be put into two curly braces.
26
 
 * - The name of the tag is #drawing:
27
 
 * - The parameter image.svg specifies the name of the image file. 
28
 
 * - If you specify an image file that does not yet exist in your Wiki, the image 
29
 
 *   is created the first time you save a drawing.
30
 
 * - The parameter width specifies the width of the image.
31
 
 * - The parameter height specifies the height of the image.
32
 
 *
33
 
 * The enable drawing for the image HappyWiki.svg, which has a width of 400 and
34
 
 * a height of 300 pixels, insert the following tag into a page:
35
 
 *
36
 
 * {{#drawing:HappyDrawing.svg|300|200}}
37
 
 *
38
 
 * To activate the extension, include it from your LocalSettings.php
39
 
 * with: 
40
 
 *
41
 
 * require_once("$IP/extensions/AnyWikiDraw/AnyWikiDraw.php"); 
42
 
 * 
43
 
 * @author Werner Randelshofer
44
 
 * @version $Id: AnyWikiDraw.php 131 2009-07-08 20:24:04Z rawcoder $
45
 
 */
46
 
 
47
 
# Add a hook for the parser function setup function
48
 
$wgExtensionFunctions[] = 'efAnyWikiDrawParserFunction_Setup';
49
 
$wgHooks['LanguageGetMagic'][] = 'efAnyWikiDrawParserFunction_Magic';
50
 
 
51
 
# Add a hook for the special page
52
 
$wgAutoloadClasses['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.body.php';
53
 
$wgExtensionMessagesFiles['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.i18n.php';
54
 
$wgSpecialPages['AnyWikiDraw'] = 'AnyWikiDraw';
55
 
 
56
 
# Setup the AnyWikDraw parser function
57
 
function efAnyWikiDrawParserFunction_Setup() {
58
 
    # Setup AnyWikiDraw Version
59
 
    global $wgAnywikidrawVersion;
60
 
    $wgAnyWikiDrawVersion = '0.13.2';
61
 
 
62
 
    # Setup messages
63
 
    global $wgMessageCache;
64
 
    require( dirname( __FILE__ ) . '/AnyWikiDraw.i18n.php' );
65
 
    foreach ( $messages as $lang => $langMessages ) {
66
 
        $wgMessageCache->addMessages( $langMessages, $lang );
67
 
    }
68
 
 
69
 
    # Setup extension credits
70
 
    $wgExtensionCredits['parserhook'][] = array(
71
 
       'name' => 'AnyWikiDraw',
72
 
       'version' => $wgAnyWikiDrawVersion,
73
 
       'author' =>'Werner Randelshofer', 
74
 
       'url' => 'http://sourceforge.net/projects/anywikidraw', 
75
 
       'description' => 'The AnyWikiDraw extensions adds a <nowiki>{{#drawing:}}</nowiki> tag to the MediaWiki parser which allows to edit SVG, PNG and JPEG images directly in a page using a Java applet.'
76
 
       );
77
 
 
78
 
        # Setup function hook associating the "drawing" magic word with our function
79
 
        global $wgParser;
80
 
        $wgParser->setFunctionHook( 'drawing', 'efAnyWikiDrawParserFunction_Render' );
81
 
 
82
 
}
83
 
 
84
 
function efAnyWikiDrawParserFunction_Magic( &$magicWords, $langCode ) {
85
 
        # Add the magic word
86
 
        # The first array element is case sensitive, in this case it is not case sensitive
87
 
        # All remaining elements are synonyms for our parser function
88
 
        $magicWords['drawing'] = array( 0, 'drawing' );
89
 
        # unless we return true, other parser functions extensions won't get loaded.
90
 
        return true;
91
 
}
92
 
 
93
 
function efAnyWikiDrawParserFunction_Render( &$parser, $name = null, $width = null, $height = null ) {
94
 
        global $wgUser, $wgLang, $wgTitle, $wgRightsText, $wgOut, $wgArticlePath, $wgScriptPath, $wgEnableUploads;
95
 
        $skin = $wgUser->getSkin();
96
 
 
97
 
        // Don't cache pages with drawings on it
98
 
        $parser->disableCache();
99
 
        
100
 
        # Validate parameters
101
 
        $error = '';
102
 
        if ($name == null || strlen($name) == 0) {
103
 
                $error .= '<br>Please specify a name for your drawing.';
104
 
        }
105
 
        if ($width != null && 
106
 
                        (! is_numeric($width) || $width < 1 || $width > 2000)) {
107
 
                $error .= '<br>Please specify the width as a number between 1 and 2000 or leave it away.';
108
 
        }
109
 
        if ($height != null && 
110
 
                        (! is_numeric($height) || $height < 1 || $height > 2000)) {
111
 
                $error .= '<br>Please specify the height as a number between 1 and 2000 or leave it away.';
112
 
        }
113
 
        if (strlen($error) > 0) {
114
 
                $error = '<b>Sorry.</b>'.$error.'<br>'.
115
 
                                'Usage: <code>{{#drawing:<i>image.svg</i>|<i>width</i>||<i>height</i>}}</code><br>'.
116
 
                                'Example: <code>{{#drawing:HappyDrawing.svg|400|300}}</code><br>';
117
 
                return array($error, 'isHTML'=>true, 'noparse'=>true);
118
 
        }
119
 
 
120
 
        # The parser function itself
121
 
        # The input parameters are wikitext with templates expanded
122
 
        # The output should be wikitext too, but in this case, it is HTML
123
 
        #return array("param1 is $param1 and param2 is $param2", 'isHTML');
124
 
        
125
 
        # Generate the image HTML as if viewed by a web request
126
 
        $image = wfFindFile($name);
127
 
        
128
 
        
129
 
        $isProtected = $parser->getTitle()->isProtected();
130
 
        
131
 
        if ($width == null && $image != null && $image->getWidth() != -1) {
132
 
                        $width = $image->getWidth();
133
 
        }
134
 
        if ($height == null && $image != null && $image->getHeight() != -1) {
135
 
                $height = $image->getHeight();
136
 
        }
137
 
                
138
 
        // render a header
139
 
        $output = '<table><tr><td>';
140
 
        if ($wgEnableUploads && ! $isProtected && 
141
 
                        key_exists('drawingtitle', $_POST) && 
142
 
                        $_POST['drawingtitle'] == $name) {
143
 
                
144
 
                // edit the drawing using the applet
145
 
        $uploadURL = str_replace('$1', 'Special:AnyWikiDraw', $wgArticlePath);
146
 
                $output .= 
147
 
                                '<a name="anywikidraw" id="anywikidraw">'.
148
 
                                '<applet codebase="/"'. 
149
 
                                ' archive="'.$wgScriptPath.'/extensions/AnyWikiDraw/AnyWikiDrawForMediaWiki.jar"'.
150
 
                                ' code="org.anywikidraw.mediawiki.MediaWikiDrawingApplet.class"'.
151
 
                                ' width="'.htmlspecialchars(min(max($width+4, 600), 800)).'"'.
152
 
                ' height="'.htmlspecialchars(min(max($height+240, 480), 600)).'"'.
153
 
                ' mayscript'.
154
 
                                '>'.
155
 
 
156
 
           // The following parameters are used to tell AnyWikiDraw how to communicate with MediaWiki:
157
 
 
158
 
                                //'<script type="text/javascript">document.write(\'<param name="Cookies" value="\'+document.cookie+\'"/>\');</script>'.
159
 
                                '<param name="DrawingName" value="'.htmlspecialchars($name).'"/>'.
160
 
                                '<param name="DrawingWidth" value="'.htmlspecialchars($width).'"/>'.
161
 
                                '<param name="DrawingHeight" value="'.htmlspecialchars($height).'"/>'.
162
 
                                (($image !== false) ? '<param name="DrawingURL" value="'.$image->getFullUrl().'">' : '').
163
 
                                '<param name="PageURL" value="'.htmlspecialchars($wgTitle->getLocalURL()).'">'.
164
 
                                '<param name="UploadURL" value="'.$uploadURL.'">'.
165
 
 
166
 
           // The following parameters are used to configure the drawing applet: 
167
 
 
168
 
                '<param name="Locale" value="'.$wgUser->getOption('language','en').'"/>'.
169
 
           
170
 
           // The following parameters are used to configure Sun's Java Plug-In: 
171
 
 
172
 
                '<param name="codebase_lookup" value="false"/>'.
173
 
                '<param name="classloader_cache" value="false"/>'.
174
 
                '<param name="java_arguments" value="-Djnlp.packEnabled=true"/>'.
175
 
                '<param name="image" value="lib/Splash.gif"/>'.
176
 
                '<param name="boxborder" value="false"/>'.
177
 
                '<param name="centerimage" value="true"/>'.
178
 
 
179
 
                                '</applet>'.
180
 
                                '</a>'
181
 
                ;
182
 
                        
183
 
                $output .= '<div style="'.
184
 
                                        'background-color: #C9CDD6; border: 1px solid #ccc; padding: 5px 10px 5px 10px; text-align: left; '.
185
 
                                        'font-size: 12px; line-height: 16px; '.
186
 
                                        '">'.
187
 
                                        wfMsg(($image === false) ? 'anywikidraw_license_terms_new_work' : 'anywikidraw_license_terms_derived_work',
188
 
                                        '<a href='.wfMsgForContent('copyrightpage').'>'.wfMsg('copyrightpage').'</a>', '<a href="./Image:'.$name.'">Image:'.$name.'</a>').
189
 
                                        '</div>';
190
 
        } else {
191
 
                // Retrieve the page object of the image to determine, whether the user may edit it
192
 
                $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
193
 
                $nt = Title::newFromText( $filtered );
194
 
                if(! is_null( $nt ) ) {
195
 
                        $nt =& Title::makeTitle( NS_IMAGE, $nt->getDBkey() );
196
 
                }
197
 
 
198
 
        // Determine if the user has permission to edit the image
199
 
                $userCanEdit = $wgEnableUploads && 
200
 
                    !$isProtected && 
201
 
                    (is_null($nt) || $nt->userCanEdit()) &&
202
 
                    (is_null($image) || $wgUser->isAllowed( 'reupload' ));
203
 
        /*$output .= 'enableUploads='.$wgEnableUploads.','.
204
 
                'isProtected='.$isProtected.','.
205
 
                'page.userCanEdit='.(is_null($nt) ? 'null' : $nt->userCanEdit()).','.
206
 
                'image.exists='.$image->exists().','.
207
 
                'user.reupload allowed='.$wgUser->isAllowed( 'reupload' );
208
 
        */
209
 
 
210
 
        // Determine if the user may edit images using the specified 
211
 
        // filename extension.
212
 
        if ($userCanEdit) {
213
 
            $extension = array_pop(explode( '.', $filtered ));
214
 
            global $wgFileExtensions;
215
 
            $userCanEdit = in_array($extension, $wgFileExtensions);
216
 
        }
217
 
                
218
 
                
219
 
                // If the user can edit the image, display an edit link.
220
 
        // We do not display the edit link, if the user is already
221
 
        // editing a drawing.
222
 
                if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
223
 
                        $formId = 'Form'.rand();
224
 
            global $wgUsePathInfo;
225
 
            if ($wgUsePathInfo) {
226
 
                $action = $wgTitle->getLocalURL().'#anywikidraw';
227
 
            } else {
228
 
                //$action = str_replace('?','#anywikidraw?',$wgTitle->getLocalURL());
229
 
                $action = $wgTitle->getLocalURL();
230
 
            }
231
 
                        $output .= '<form name="'.$formId.'" method="post" action="'.$action.'">'.
232
 
                                        '<input type="hidden" name="drawingtitle" value="'.htmlspecialchars($name).'">'.
233
 
                                        '<p align="right">'.
234
 
                                        '[<a href="javascript:document.'.$formId.'.submit();">'.wfMsg('edit').'</a>]'.
235
 
                                        '<noscript><input type="submit" name="submit" value="'.wfMsg('edit').'"></input></noscript>'.
236
 
                                        '</p>'
237
 
                                        ;
238
 
                }
239
 
                // render the drawing
240
 
                if ($image === false) {
241
 
                        // the drawing does not exist yet, render an empty rectangle
242
 
                        $output .= '<div style="border:1px solid #000;text-align:center;'.
243
 
                                (($width != null) ? 'width:'.$width.'px;' : '').
244
 
                                (($height != null) ? 'height:'.$height.'px;' : '').'"'.
245
 
                                '>'.htmlspecialchars($name).'</div>';
246
 
                } else {
247
 
                        // Render an img tag
248
 
                        // Render the image map, if it exists
249
 
                        $thumbnail = $image->getThumbnail($width);
250
 
                        $isImageMap = $thumbnail != null && file_exists($thumbnail->path.'.map');
251
 
                        $mapId = 'Map_'.$name.'_'.rand();
252
 
                        if ($isImageMap) {
253
 
                                $output .= '<map name="'.$mapId.'">'.
254
 
                                        file_get_contents($thumbnail->path.'.map').
255
 
                                        '</map>';
256
 
                        }
257
 
                        // Render the image
258
 
                        if (! $isImageMap) {
259
 
                                $output .= '<a href="./Image:'.$name.'">';
260
 
                        }
261
 
            // Note: We append the timestamp of the image to the
262
 
            //       view URL as a query string. This way, we ensure,
263
 
            //       that the browser always displays the last edited version
264
 
            //       of the image
265
 
                        $output .= '<img '.
266
 
                                'src="'.$image->getViewUrl().
267
 
                    '?version='.$image->nextHistoryLine()->img_timestamp.'" '.
268
 
                                (($width != null) ? 'width="'.$width.'" ' : '').
269
 
                                (($height != null) ? 'height="'.$height.'" ' : '').
270
 
                                'alt="Image:'.$name.'" '.
271
 
                                'title="Image:'.$name.'" '.
272
 
                                (($isImageMap) ? 'usemap="#'.$mapId.'" ' : '').
273
 
                                '></img>';
274
 
                        if (! $isImageMap) {
275
 
                                $output .= '</a>';
276
 
                        }
277
 
                }
278
 
                // If the user can edit the image, display an edit link.
279
 
        // We do not display the edit link, if the user is already
280
 
        // editing a drawing.
281
 
                if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
282
 
                        $output .= '</form>';
283
 
                }
284
 
        }
285
 
        
286
 
        // render a footer
287
 
        $output .= '</tr></td></table>';
288
 
        
289
 
        return array($output, 'isHTML'=>true, 'noparse'=>true);
290
 
}
291
 
?>
 
 
b'\\ No newline at end of file'