Back to index

moin  1.9.0~rc2
AnyWikiDraw.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @(#)AnyWikiDraw.php  
00004  *
00005  * Copyright (c) 2007-2009 by the original authors of AnyWikiDraw
00006  * and all its contributors.
00007  *
00008  * The copyright of this software is owned by the authors and  
00009  * contributors of the AnyWikiDraw project ("the copyright holders").
00010  * You may not use, copy or modify this software, except in  
00011  * accordance with the license agreement you entered into with  
00012  * the copyright holders. For details see accompanying license terms. 
00013  */
00014 
00047 # Add a hook for the parser function setup function
00048 $wgExtensionFunctions[] = 'efAnyWikiDrawParserFunction_Setup';
00049 $wgHooks['LanguageGetMagic'][] = 'efAnyWikiDrawParserFunction_Magic';
00050 
00051 # Add a hook for the special page
00052 $wgAutoloadClasses['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.body.php';
00053 $wgExtensionMessagesFiles['AnyWikiDraw'] = dirname(__FILE__) . '/AnyWikiDraw.i18n.php';
00054 $wgSpecialPages['AnyWikiDraw'] = 'AnyWikiDraw';
00055 
00056 # Setup the AnyWikDraw parser function
00057 function efAnyWikiDrawParserFunction_Setup() {
00058     # Setup AnyWikiDraw Version
00059     global $wgAnywikidrawVersion;
00060     $wgAnyWikiDrawVersion = '0.13.2';
00061 
00062     # Setup messages
00063     global $wgMessageCache;
00064     require( dirname( __FILE__ ) . '/AnyWikiDraw.i18n.php' );
00065     foreach ( $messages as $lang => $langMessages ) {
00066         $wgMessageCache->addMessages( $langMessages, $lang );
00067     }
00068 
00069     # Setup extension credits
00070     $wgExtensionCredits['parserhook'][] = array(
00071        'name' => 'AnyWikiDraw',
00072        'version' => $wgAnyWikiDrawVersion,
00073        'author' =>'Werner Randelshofer', 
00074        'url' => 'http://sourceforge.net/projects/anywikidraw', 
00075        '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.'
00076        );
00077 
00078        # Setup function hook associating the "drawing" magic word with our function
00079        global $wgParser;
00080        $wgParser->setFunctionHook( 'drawing', 'efAnyWikiDrawParserFunction_Render' );
00081 
00082 }
00083 
00084 function efAnyWikiDrawParserFunction_Magic( &$magicWords, $langCode ) {
00085        # Add the magic word
00086        # The first array element is case sensitive, in this case it is not case sensitive
00087        # All remaining elements are synonyms for our parser function
00088        $magicWords['drawing'] = array( 0, 'drawing' );
00089        # unless we return true, other parser functions extensions won't get loaded.
00090        return true;
00091 }
00092 
00093 function efAnyWikiDrawParserFunction_Render( &$parser, $name = null, $width = null, $height = null ) {
00094        global $wgUser, $wgLang, $wgTitle, $wgRightsText, $wgOut, $wgArticlePath, $wgScriptPath, $wgEnableUploads;
00095        $skin = $wgUser->getSkin();
00096 
00097        // Don't cache pages with drawings on it
00098        $parser->disableCache();
00099        
00100        # Validate parameters
00101        $error = '';
00102        if ($name == null || strlen($name) == 0) {
00103               $error .= '<br>Please specify a name for your drawing.';
00104        }
00105        if ($width != null && 
00106                      (! is_numeric($width) || $width < 1 || $width > 2000)) {
00107               $error .= '<br>Please specify the width as a number between 1 and 2000 or leave it away.';
00108        }
00109        if ($height != null && 
00110                      (! is_numeric($height) || $height < 1 || $height > 2000)) {
00111               $error .= '<br>Please specify the height as a number between 1 and 2000 or leave it away.';
00112        }
00113        if (strlen($error) > 0) {
00114               $error = '<b>Sorry.</b>'.$error.'<br>'.
00115                             'Usage: <code>{{#drawing:<i>image.svg</i>|<i>width</i>||<i>height</i>}}</code><br>'.
00116                             'Example: <code>{{#drawing:HappyDrawing.svg|400|300}}</code><br>';
00117               return array($error, 'isHTML'=>true, 'noparse'=>true);
00118        }
00119 
00120        # The parser function itself
00121        # The input parameters are wikitext with templates expanded
00122        # The output should be wikitext too, but in this case, it is HTML
00123        #return array("param1 is $param1 and param2 is $param2", 'isHTML');
00124        
00125        # Generate the image HTML as if viewed by a web request
00126        $image = wfFindFile($name);
00127        
00128        
00129        $isProtected = $parser->getTitle()->isProtected();
00130        
00131        if ($width == null && $image != null && $image->getWidth() != -1) {
00132                      $width = $image->getWidth();
00133        }
00134        if ($height == null && $image != null && $image->getHeight() != -1) {
00135               $height = $image->getHeight();
00136        }
00137               
00138        // render a header
00139        $output = '<table><tr><td>';
00140        if ($wgEnableUploads && ! $isProtected && 
00141                      key_exists('drawingtitle', $_POST) && 
00142                      $_POST['drawingtitle'] == $name) {
00143               
00144               // edit the drawing using the applet
00145         $uploadURL = str_replace('$1', 'Special:AnyWikiDraw', $wgArticlePath);
00146               $output .= 
00147                             '<a name="anywikidraw" id="anywikidraw">'.
00148                             '<applet codebase="/"'. 
00149                             ' archive="'.$wgScriptPath.'/extensions/AnyWikiDraw/AnyWikiDrawForMediaWiki.jar"'.
00150                             ' code="org.anywikidraw.mediawiki.MediaWikiDrawingApplet.class"'.
00151                             ' width="'.htmlspecialchars(min(max($width+4, 600), 800)).'"'.
00152                 ' height="'.htmlspecialchars(min(max($height+240, 480), 600)).'"'.
00153                 ' mayscript'.
00154                             '>'.
00155 
00156            // The following parameters are used to tell AnyWikiDraw how to communicate with MediaWiki:
00157 
00158                             //'<script type="text/javascript">document.write(\'<param name="Cookies" value="\'+document.cookie+\'"/>\');</script>'.
00159                             '<param name="DrawingName" value="'.htmlspecialchars($name).'"/>'.
00160                             '<param name="DrawingWidth" value="'.htmlspecialchars($width).'"/>'.
00161                             '<param name="DrawingHeight" value="'.htmlspecialchars($height).'"/>'.
00162                             (($image !== false) ? '<param name="DrawingURL" value="'.$image->getFullUrl().'">' : '').
00163                             '<param name="PageURL" value="'.htmlspecialchars($wgTitle->getLocalURL()).'">'.
00164                             '<param name="UploadURL" value="'.$uploadURL.'">'.
00165 
00166            // The following parameters are used to configure the drawing applet: 
00167 
00168                 '<param name="Locale" value="'.$wgUser->getOption('language','en').'"/>'.
00169            
00170            // The following parameters are used to configure Sun's Java Plug-In: 
00171 
00172                 '<param name="codebase_lookup" value="false"/>'.
00173                 '<param name="classloader_cache" value="false"/>'.
00174                 '<param name="java_arguments" value="-Djnlp.packEnabled=true"/>'.
00175                 '<param name="image" value="lib/Splash.gif"/>'.
00176                 '<param name="boxborder" value="false"/>'.
00177                 '<param name="centerimage" value="true"/>'.
00178 
00179                             '</applet>'.
00180                             '</a>'
00181                 ;
00182                      
00183               $output .= '<div style="'.
00184                                    'background-color: #C9CDD6; border: 1px solid #ccc; padding: 5px 10px 5px 10px; text-align: left; '.
00185                                    'font-size: 12px; line-height: 16px; '.
00186                                    '">'.
00187                                    wfMsg(($image === false) ? 'anywikidraw_license_terms_new_work' : 'anywikidraw_license_terms_derived_work',
00188                                    '<a href='.wfMsgForContent('copyrightpage').'>'.wfMsg('copyrightpage').'</a>', '<a href="./Image:'.$name.'">Image:'.$name.'</a>').
00189                                    '</div>';
00190        } else {
00191               // Retrieve the page object of the image to determine, whether the user may edit it
00192               $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
00193               $nt = Title::newFromText( $filtered );
00194               if(! is_null( $nt ) ) {
00195                      $nt =& Title::makeTitle( NS_IMAGE, $nt->getDBkey() );
00196               }
00197 
00198         // Determine if the user has permission to edit the image
00199               $userCanEdit = $wgEnableUploads && 
00200                     !$isProtected && 
00201                     (is_null($nt) || $nt->userCanEdit()) &&
00202                     (is_null($image) || $wgUser->isAllowed( 'reupload' ));
00203         /*$output .= 'enableUploads='.$wgEnableUploads.','.
00204                 'isProtected='.$isProtected.','.
00205                 'page.userCanEdit='.(is_null($nt) ? 'null' : $nt->userCanEdit()).','.
00206                 'image.exists='.$image->exists().','.
00207                 'user.reupload allowed='.$wgUser->isAllowed( 'reupload' );
00208         */
00209 
00210         // Determine if the user may edit images using the specified 
00211         // filename extension.
00212         if ($userCanEdit) {
00213             $extension = array_pop(explode( '.', $filtered ));
00214             global $wgFileExtensions;
00215             $userCanEdit = in_array($extension, $wgFileExtensions);
00216         }
00217               
00218               
00219               // If the user can edit the image, display an edit link.
00220         // We do not display the edit link, if the user is already
00221         // editing a drawing.
00222               if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
00223                      $formId = 'Form'.rand();
00224             global $wgUsePathInfo;
00225             if ($wgUsePathInfo) {
00226                 $action = $wgTitle->getLocalURL().'#anywikidraw';
00227             } else {
00228                 //$action = str_replace('?','#anywikidraw?',$wgTitle->getLocalURL());
00229                 $action = $wgTitle->getLocalURL();
00230             }
00231                      $output .= '<form name="'.$formId.'" method="post" action="'.$action.'">'.
00232                                    '<input type="hidden" name="drawingtitle" value="'.htmlspecialchars($name).'">'.
00233                                    '<p align="right">'.
00234                                    '[<a href="javascript:document.'.$formId.'.submit();">'.wfMsg('edit').'</a>]'.
00235                                    '<noscript><input type="submit" name="submit" value="'.wfMsg('edit').'"></input></noscript>'.
00236                                    '</p>'
00237                                    ;
00238               }
00239               // render the drawing
00240               if ($image === false) {
00241                      // the drawing does not exist yet, render an empty rectangle
00242                      $output .= '<div style="border:1px solid #000;text-align:center;'.
00243                             (($width != null) ? 'width:'.$width.'px;' : '').
00244                             (($height != null) ? 'height:'.$height.'px;' : '').'"'.
00245                             '>'.htmlspecialchars($name).'</div>';
00246               } else {
00247                      // Render an img tag
00248                      // Render the image map, if it exists
00249                      $thumbnail = $image->getThumbnail($width);
00250                      $isImageMap = $thumbnail != null && file_exists($thumbnail->path.'.map');
00251                      $mapId = 'Map_'.$name.'_'.rand();
00252                      if ($isImageMap) {
00253                             $output .= '<map name="'.$mapId.'">'.
00254                                    file_get_contents($thumbnail->path.'.map').
00255                                    '</map>';
00256                      }
00257                      // Render the image
00258                      if (! $isImageMap) {
00259                             $output .= '<a href="./Image:'.$name.'">';
00260                      }
00261             // Note: We append the timestamp of the image to the
00262             //       view URL as a query string. This way, we ensure,
00263             //       that the browser always displays the last edited version
00264             //       of the image
00265                      $output .= '<img '.
00266                             'src="'.$image->getViewUrl().
00267                     '?version='.$image->nextHistoryLine()->img_timestamp.'" '.
00268                             (($width != null) ? 'width="'.$width.'" ' : '').
00269                             (($height != null) ? 'height="'.$height.'" ' : '').
00270                             'alt="Image:'.$name.'" '.
00271                             'title="Image:'.$name.'" '.
00272                             (($isImageMap) ? 'usemap="#'.$mapId.'" ' : '').
00273                             '></img>';
00274                      if (! $isImageMap) {
00275                             $output .= '</a>';
00276                      }
00277               }
00278               // If the user can edit the image, display an edit link.
00279         // We do not display the edit link, if the user is already
00280         // editing a drawing.
00281               if ($userCanEdit && ! key_exists('drawingtitle', $_POST)) {
00282                      $output .= '</form>';
00283               }
00284        }
00285        
00286        // render a footer
00287        $output .= '</tr></td></table>';
00288        
00289        return array($output, 'isHTML'=>true, 'noparse'=>true);
00290 }
00291 ?>