* Support for a license selection box on Special:Upload, configurable from MediaWiki...
authorÆvar Arnfjörð Bjarmason <avar@users.mediawiki.org>
Mon, 29 Aug 2005 16:43:48 +0000 (16:43 +0000)
committerÆvar Arnfjörð Bjarmason <avar@users.mediawiki.org>
Mon, 29 Aug 2005 16:43:48 +0000 (16:43 +0000)
RELEASE-NOTES
includes/Image.php
includes/Licenses.php [new file with mode: 0644]
includes/SpecialUpload.php
languages/Language.php

index bc3a62f..b966582 100644 (file)
@@ -71,7 +71,7 @@ fully support the editing toolbar, but was found to be too confusing.
 * (bug 2885) More PHP 5.1 fixes: skin, search, log, undelete
 * Fix interlanguage links on special pages when extra namespaces configured
 * IP privacy fix for blocklist search on autoblocks
-
+* Support for a license selection box on Special:Upload, configurable from MediaWiki:Licenses
 
 === Caveats ===
 
index ecae80a..bbf1d42 100644 (file)
@@ -1253,7 +1253,7 @@ class Image
        /**
         * Record an image upload in the upload log and the image table
         */
-       function recordUpload( $oldver, $desc, $copyStatus = '', $source = '' ) {
+       function recordUpload( $oldver, $desc, $license, $copyStatus = '', $source = '' ) {
                global $wgUser, $wgLang, $wgTitle, $wgDeferredUpdateList;
                global $wgUseCopyrightUpload, $wgUseSquid, $wgPostCommitUpdateList;
 
@@ -1272,11 +1272,21 @@ class Image
                }
 
                if ( $wgUseCopyrightUpload ) {
+                       if ( $license != '' ) {
+                               $licensetxt = '== ' . wfMsg( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
+                       }
                        $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
                          '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
+                         "$licensetxt" .
                          '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
                } else {
-                       $textdesc = $desc;
+                       if ( $license != '' ) {
+                               $filedesc = $desc == '' ? '' : '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n";
+                                $textdesc = $filedesc . 
+                                        '== ' . wfMsg ( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n"; 
+                       } else {
+                               $textdesc = $desc;
+                       }
                }
 
                $now = $dbw->timestamp();
diff --git a/includes/Licenses.php b/includes/Licenses.php
new file mode 100644 (file)
index 0000000..1925e86
--- /dev/null
@@ -0,0 +1,168 @@
+<?php
+/**
+ * A License class for use on Special:Upload
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ *
+ * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
+ */
+
+class Licenses {
+       /**#@+
+        * @access private
+        */
+       /**
+        * @var string
+        */
+       var $msg;
+       
+       /**
+        * @var array
+        */
+       var $licenses = array();
+
+       /**
+        * @var string
+        */
+       var $html;
+
+       /**
+        * Constrictor
+        *
+        * @param string $str The string to build the licenses member from, will use
+        *                    wfMsgForContent( 'licenses' ) if null (default: null)
+        */
+       function Licenses( $str = null ) {
+               // PHP sucks, this should be possible in the constructor
+               $this->msg = is_null( $str ) ? wfMsgForContent( 'licenses' ) : $str;
+               $this->html = '';
+
+               $this->makeLicenses();
+               $tmp = $this->getLicenses();
+               $this->makeHtml( $tmp );
+       }
+       
+       /**#@+
+        * @access private
+        */
+       function makeLicenses() {
+               $levels = array();
+               $lines = explode( "\n", $this->msg );
+               
+               foreach ( $lines as $line ) {
+                       if ( strpos( $line, '*' ) !== 0 )
+                               continue;
+                       else {
+                               list( $level, $line ) = $this->trimStars( $line );
+                               
+                               if ( strpos( $line, '|' ) !== false ) {
+                                       $obj = new License( $line );
+                                       // TODO: Do this without using eval()
+                                       eval( '$this->licenses' . $this->makeIndexes( $levels ) . '[] = $obj;' );
+                               } else {
+                                       if ( $level < count( $levels ) )
+                                               $levels = array_slice( $levels, count( $levels ) - $level );
+                                       if ( $level == count( $levels ) )
+                                               $levels[$level - 1] = $line;
+                                       else if ( $level > count( $levels ) )
+                                               $levels[] = $line;
+       
+                               }
+                       }
+               }
+       }
+       
+       function trimStars( $str ) {
+               $i = $count = 0;
+               
+               while ($str[$i++] == '*')
+                       ++$count;
+       
+               return array( $count, ltrim( $str, '* ' ) );
+       }
+       
+       function makeIndexes( $arr ) {
+               $str = '';
+       
+               foreach ( $arr as $item )
+                       $str .= '["' . addslashes( $item ) . '"]';
+               
+               return $str;
+       }
+
+       function makeHtml( &$tagset, $depth = 0 ) {
+               foreach ( $tagset as $key => $val )
+                       if ( is_array( $val ) ) {
+                               
+                               $this->html .= $this->outputOption(
+                                       $this->msg( $key ),
+                                       array(
+                                               'value' => ''
+                                       ),
+                                       $depth
+                               );
+                               $this->makeHtml( $val, $depth + 1 );
+                       } else {
+                               $this->html .= $this->outputOption(
+                                       $this->msg( $val->text ),
+                                       array(
+                                               'value' => $val->template
+                                       ),
+                                       $depth
+                               );
+                       }
+       }
+
+       function outputOption( $val, $attribs = null, $depth ) {
+               $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth ) . $val;
+               return str_repeat( "\t", $depth ) . wfElement( 'option', $attribs, $val ) . "\n";
+       }
+       
+       function msg( $str ) {
+               $out = wfMsg( $str );
+               return wfNoMsg( $str, $out ) ? $str : $out;
+       }
+       
+       /**#@-*/
+       
+       /**
+        *  Accessor for $this->licenses
+        *
+        * @return array
+        */
+       function getLicenses() { return $this->licenses; }
+
+       /**
+        * Accessor for $this->html
+        *
+        * @return string
+        */
+       function getHtml() { return $this->html; }
+}
+
+class License {
+       /**
+        * @var string
+        */
+       var $template;
+       
+       /**
+        * @var string
+        */
+       var $text;
+
+       /**
+        * Constructor
+        *
+        * @param string $str
+        */
+       function License( $str ) {
+               list( $template, $text ) = explode( '|', $str, 2 );
+               
+               $this->template = $template;
+               $this->text = $text;
+       }
+}
+?>
index c17fea2..6919fd2 100644 (file)
@@ -8,9 +8,9 @@
 /**
  *
  */
-require_once( 'Image.php' );
-require_once( 'MacBinary.php' );
-
+require_once 'Image.php';
+require_once 'MacBinary.php';
+require_once 'Licenses.php';
 /**
  * Entry point
  */
@@ -29,7 +29,7 @@ class UploadForm {
        /**#@+
         * @access private
         */
-       var $mUploadFile, $mUploadDescription, $mIgnoreWarning, $mUploadError;
+       var $mUploadFile, $mUploadDescription, $mLicense ,$mIgnoreWarning, $mUploadError;
        var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
        var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
        var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile;
@@ -53,6 +53,7 @@ class UploadForm {
                $this->mUpload            = $request->getCheck( 'wpUpload' );
 
                $this->mUploadDescription = $request->getText( 'wpUploadDescription' );
+               $this->mLicense           = $request->getText( 'wpLicense' );
                $this->mUploadCopyStatus  = $request->getText( 'wpUploadCopyStatus' );
                $this->mUploadSource      = $request->getText( 'wpUploadSource');
 
@@ -281,6 +282,7 @@ class UploadForm {
                        $img = Image::newFromName( $this->mUploadSaveName );
                        $success = $img->recordUpload( $this->mUploadOldVersion,
                                                        $this->mUploadDescription,
+                                                       $this->mLicense,
                                                        $this->mUploadCopyStatus,
                                                        $this->mUploadSource );
 
@@ -490,6 +492,7 @@ class UploadForm {
                <input type='hidden' name='wpIgnoreWarning' value='1' />
                <input type='hidden' name='wpSessionKey' value=\"" . htmlspecialchars( $this->mSessionKey ) . "\" />
                <input type='hidden' name='wpUploadDescription' value=\"" . htmlspecialchars( $this->mUploadDescription ) . "\" />
+               <input type='hidden' name='wpLicense' value=\"" . htmlspecialchars( $this->mLicense ) . "\" />
                <input type='hidden' name='wpDestFile' value=\"" . htmlspecialchars( $this->mDestFile ) . "\" />
        {$copyright}
        <table border='0'>
@@ -538,6 +541,12 @@ class UploadForm {
                $sourcefilename = wfMsgHtml( 'sourcefilename' );
                $destfilename = wfMsgHtml( 'destfilename' );
                $summary = wfMsgWikiHtml( 'fileuploadsummary' );
+
+               $licenses = new Licenses();
+               $license = wfMsgHtml( 'license' );
+               $nolicense = wfMsgHtml( 'nolicense' );
+               $licenseshtml = $licenses->getHtml();
+               
                $ulb = wfMsgHtml( 'uploadbtn' );
 
 
@@ -576,8 +585,20 @@ class UploadForm {
        <textarea tabindex='2' name='wpUploadDescription' rows='6' cols='{$cols}'{$ew}>"        
          . htmlspecialchars( $this->mUploadDescription ) .
        "</textarea>
+       </td></tr><tr>" );
+       
+       if ( $licenseshtml != '' ) {
+               $wgOut->addHTML( "
+       <td align='right'>$license:</td>
+       <td align='left'>
+               <select name='wpLicense'>
+                       <option value=''>$nolicense</option>
+                       $licenseshtml
+               </select>
        </td></tr><tr>
-       {$source}
+               ");
+       }
+       $wgOut->addHtml( "{$source}
        </tr>
        <tr><td></td><td align='left'>
        <input tabindex='5' type='submit' name='wpUpload' value=\"{$ulb}\" />
index 1da7271..7515de7 100644 (file)
@@ -1032,6 +1032,9 @@ created and by whom, and anything else you may know about it. If this is an imag
 'sourcefilename' => 'Source filename',
 'destfilename' => 'Destination filename',
 
+'license' => 'License',        
+'nolicense' => 'None',
+
 # Image list
 #
 'imagelist'            => 'File list',
@@ -2734,7 +2737,7 @@ class Language {
                        if ($i == $m) {
                                $s = $l[$i];
                        } else if ($i == $m - 1) {
-                               $s = $l[$i] . ' ' . $this->getMessage('and') . ' ' . $s;
+                               $s = $l[$i] . ' ' . wfMsg('and') . ' ' . $s;
                        } else {
                                $s = $l[$i] . ', ' . $s;
                        }