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