http://www.mediawiki.org/wiki/Special:Code/MediaWiki/84558
Revision: 84558
Author: ashley
Date: 2011-03-22 21:18:44 +0000 (Tue, 22 Mar 2011)
Log Message:
-----------
ImportFreeImages: coding style, whitespace and doc tweaks
Modified Paths:
--------------
trunk/extensions/ImportFreeImages/ImportFreeImages.body.php
trunk/extensions/ImportFreeImages/ImportFreeImages.php
trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php
trunk/extensions/ImportFreeImages/UploadFreeImage.php
Modified: trunk/extensions/ImportFreeImages/ImportFreeImages.body.php
===================================================================
--- trunk/extensions/ImportFreeImages/ImportFreeImages.body.php 2011-03-22
21:06:20 UTC (rev 84557)
+++ trunk/extensions/ImportFreeImages/ImportFreeImages.body.php 2011-03-22
21:18:44 UTC (rev 84558)
@@ -8,95 +8,98 @@
public $resultsPerPage;
public $resultsPerRow;
public $thumbType;
-
+
public function __construct() {
# Load settings
global $wgIFI_FlickrAPIKey, $wgIFI_CreditsTemplate,
$wgIFI_GetOriginal,
$wgIFI_PromptForFilename, $wgIFI_phpFlickr,
$wgIFI_ResultsPerRow,
$wgIFI_ResultsPerPage, $wgIFI_FlickrLicense,
$wgIFI_FlickrSort,
$wgIFI_FlickrSearchBy, $wgIFI_AppendRandomNumber,
$wgIFI_ThumbType;
-
+
$this->apiKey = $wgIFI_FlickrAPIKey;
$this->creditsTemplate = $wgIFI_CreditsTemplate;
$this->shouldGetOriginal = $wgIFI_GetOriginal;
$this->promptForFilename = $wgIFI_PromptForFilename;
$this->phpFlickrFile = $wgIFI_phpFlickr;
-
+
$this->resultsPerRow = $wgIFI_ResultsPerRow;
$this->resultsPerPage = $wgIFI_ResultsPerPage;
- $this->licenses = is_array( $wgIFI_FlickrLicense ) ?
+ $this->licenses = is_array( $wgIFI_FlickrLicense ) ?
$wgIFI_FlickrLicense : explode( ',',
$wgIFI_FlickrLicense );
$this->sortBy = $wgIFI_FlickrSort;
$this->searchBy = $wgIFI_FlickrSearchBy;
$this->appendRandomNumber = $wgIFI_AppendRandomNumber;
$this->thumbType = $wgIFI_ThumbType;
-
+
$this->load();
}
-
+
/**
- * Save the old error level and disable E_STRICT warnings. phpFlickr is
+ * Save the old error level and disable E_STRICT warnings. phpFlickr is
* written for PHP4 and causes a lot of E_STRICT warnings.
*/
protected function suppressStrictWarnings() {
$this->oldLevel = error_reporting();
- error_reporting( $this->oldLevel ^ E_STRICT );
+ error_reporting( $this->oldLevel ^ E_STRICT );
}
/**
* Restore the error levels disabled with suppressStrictWarnings()
*/
protected function restoreStrictWarnings() {
- error_reporting( $this->oldLevel );
+ error_reporting( $this->oldLevel );
}
-
+
/**
- * Try to initiate a phpFlickr object. Will throw MWException if not
+ * Try to initiate a phpFlickr object. Will throw MWException if not
* properly configured.
*/
protected function load() {
- if ( !file_exists( $this->phpFlickrFile ) )
+ if ( !file_exists( $this->phpFlickrFile ) ) {
throw new MWException( 'phpFlickr can not be found at '
. $this->phpFlickrFile );
-
+ }
+
$this->suppressStrictWarnings();
-
+
require_once( $this->phpFlickrFile );
- if ( !$this->apiKey )
+ if ( !$this->apiKey ) {
throw new MWException( 'No Flickr API key found' );
+ }
$this->flickr = new phpFlickr( $this->apiKey );
-
+
$this->restoreStrictWarnings();
}
-
+
/**
* Search for Flickr photos
- *
- * @param $query string Search query
- * @param $page int Page number
+ *
+ * @param $query String: search query
+ * @param $page Integer: page number
* @return array TODO
*/
public function searchPhotos( $query, $page ) {
$this->suppressStrictWarnings();
$result = $this->flickr->photos_search(
array(
- $this->searchBy => $query,
- 'tag_mode' => 'any',
- 'page' => $page,
- 'per_page' => $this->resultsPerPage,
- 'license' => implode( ',',
$this->licenses ),
- 'sort' => $this->sortBy,
+ $this->searchBy => $query,
+ 'tag_mode' => 'any',
+ 'page' => $page,
+ 'per_page' => $this->resultsPerPage,
+ 'license' => implode( ',', $this->licenses ),
+ 'sort' => $this->sortBy,
)
);
$this->restoreStrictWarnings();
-
- if ( !$result || !is_array( $result ) || !isset(
$result['photo'] ) )
+
+ if ( !$result || !is_array( $result ) || !isset(
$result['photo'] ) ) {
return false;
+ }
return $result;
}
-
+
/**
- * Get photo information for an id
- *
- * @param $id int id
+ * Get photo information for an ID
+ *
+ * @param $id Integer: ID
* @return array
*/
public function getPhotoInfo( $id ) {
@@ -105,11 +108,11 @@
$this->restoreStrictWarnings();
return $result;
}
-
+
/**
* Get author information for an nsid
- *
- * @param $owner string NSID
+ *
+ * @param $owner String: NSID
* @return array TODO
*/
public function getOwnerInfo( $owner ) {
@@ -118,10 +121,10 @@
$this->restoreStrictWarnings();
return $result;
}
-
+
/**
* Get sizes and urls for a certain photo
- *
+ *
* @param $id int Flickr photo id
* @return array [{'label': 'Large/Original', 'source': 'url', ...},
...]
*/
@@ -132,5 +135,4 @@
return $result;
}
-}
-
+}
\ No newline at end of file
Modified: trunk/extensions/ImportFreeImages/ImportFreeImages.php
===================================================================
--- trunk/extensions/ImportFreeImages/ImportFreeImages.php 2011-03-22
21:06:20 UTC (rev 84557)
+++ trunk/extensions/ImportFreeImages/ImportFreeImages.php 2011-03-22
21:18:44 UTC (rev 84558)
@@ -1,6 +1,6 @@
<?php
/**
- * Provides a way of importing properly licensed photos from flickr
+ * Provides a way of importing properly licensed photos from flickr
*
* @file
* @ingroup Extensions
@@ -10,26 +10,27 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
* @link http://www.mediawiki.org/wiki/Extension:ImportFreeImages Documentation
*/
-if ( ! defined( 'MEDIAWIKI' ) )
+if ( !defined( 'MEDIAWIKI' ) ) {
die();
+}
// Configuration settings
$wgIFI_FlickrAPIKey = ''; // the flickr API key. This is required for the
extension to work.
$wgIFI_CreditsTemplate = 'flickr'; // use this to format the image content
with some key parameters
$wgIFI_GetOriginal = true; // import the original version of the photo
-$wgIFI_PromptForFilename = true; // prompt the user through javascript for
the destination filename
+$wgIFI_PromptForFilename = true; // prompt the user through JavaScript for the
destination filename
$wgIFI_phpFlickr = 'phpFlickr-2.2.0/phpFlickr.php'; // Path to your phpFlickr
file
$wgIFI_ResultsPerPage = 20;
$wgIFI_ResultsPerRow = 4;
-// see the flickr api page for more information on these params
+// see the flickr API page for more information on these params
// for license info
http://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html
// default 4 is CC Attribution License
$wgIFI_FlickrLicense = '4,5';
$wgIFI_FlickrSort = 'interestingness-desc';
$wgIFI_FlickrSearchBy = 'tags'; // Can be tags or text. See
http://www.flickr.com/services/api/flickr.photos.search.html
$wgIFI_AppendRandomNumber = true; // append random # to destination filename
-$wgIFI_ThumbType = 't'; // s for square t for thumbnail
+$wgIFI_ThumbType = 't'; // s for square, t for thumbnail
// Extension credits that will show up on Special:Version
$wgExtensionCredits['specialpage'][] = array(
Modified: trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php
===================================================================
--- trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php
2011-03-22 21:06:20 UTC (rev 84557)
+++ trunk/extensions/ImportFreeImages/SpecialImportFreeImages.php
2011-03-22 21:18:44 UTC (rev 84558)
@@ -52,14 +52,12 @@
$wgOut->readOnlyPage();
return;
}
-
+
# Do all magic
$this->showForm();
$this->showResult();
+ }
-
- }
-
/**
* Show the search form
*/
@@ -70,25 +68,25 @@
Html::element( 'legend', array(), wfMsg(
'importfreeimages' ) ) . "\n" .
wfMsgExt( 'importfreeimages_description',
'parse' ) . "\n" .
Html::rawElement( 'form', array( 'action' =>
$wgScript ),
- Html::element( 'input', array(
- 'type' => 'hidden',
+ Html::element( 'input', array(
+ 'type' => 'hidden',
'name' => 'title',
- 'value' =>
$this->getTitle()->getPrefixedText(),
+ 'value' =>
$this->getTitle()->getPrefixedText(),
) ) . "\n" .
- Html::element( 'input', array(
+ Html::element( 'input', array(
'type' => 'text',
'name' => 'q',
'size' => '40',
- 'value' => $wgRequest->getText(
'q' ),
+ 'value' => $wgRequest->getText(
'q' ),
) ) . "\n" .
Html::element( 'input', array(
'type' => 'submit',
'value' => wfMsg( 'search' )
- ) )
- )
+ ) )
+ )
) );
}
-
+
/**
* Show the search result if available
*/
@@ -99,7 +97,7 @@
$q = $wgRequest->getVal( 'q' );
if ( !$q )
return;
-
+
$ifi = new ImportFreeImages();
// TODO: get the right licenses
$photos = $ifi->searchPhotos( $q, $page );
@@ -107,10 +105,10 @@
$wgOut->addHTML( wfMsg(
'importfreeimages_nophotosfound', htmlspecialchars( $q ) ) );
return;
}
-
+
$sk = $wgUser->getSkin();
$wgOut->addHTML( '<table cellpadding="4" id="mw-ifi-result">' );
-
+
$specialUploadTitle = SpecialPage::getTitleFor( 'Upload' );
$ownermsg = wfMsg( 'importfreeimages_owner' );
@@ -124,11 +122,15 @@
$title_esc = htmlspecialchars( $photo['title'],
ENT_QUOTES );
$username_esc = htmlspecialchars( $owner['username'],
ENT_QUOTES );
$thumb_esc = htmlspecialchars(
"http://farm{$photo['farm']}.static.flickr.com/{$photo['server']}/{$photo['id']}_{$photo['secret']}_{$ifi->thumbType}.jpg",
ENT_QUOTES );
- $link = $specialUploadTitle->escapeLocalUrl( array(
'wpSourceType' => 'IFI', 'wpFlickrId' => $photo['id'] ) );
-
- if ( $i % $ifi->resultsPerRow == 0 )
+ $link = $specialUploadTitle->escapeLocalURL( array(
+ 'wpSourceType' => 'IFI',
+ 'wpFlickrId' => $photo['id']
+ ) );
+
+ if ( $i % $ifi->resultsPerRow == 0 ) {
$wgOut->addHTML( '<tr>' );
-
+ }
+
# TODO: Fix nasty HTML generation
$wgOut->addHTML( "
<td align='center' style='padding-top:
15px; border-bottom: 1px solid #ccc;'>
@@ -138,19 +140,23 @@
<br />(<a
href='$link'>$importmsg</a>)</font>
</td>
" );
-
- if ( $i % $ifi->resultsPerRow == ($ifi->resultsPerRow -
1) )
+
+ if ( $i % $ifi->resultsPerRow == ( $ifi->resultsPerRow
- 1 ) ) {
$wgOut->addHTML( '</tr>' );
-
+ }
+
$i++;
}
$wgOut->addHTML( '</table>' );
-
+
if ( $ifi->resultsPerPage * $page < $photos['total'] ) {
$page++;
- $wgOut->addHTML("<br />" . $sk->makeLinkObj(
$this->getTitle(),
- wfMsg('importfreeimages_next',
$ifi->resultsPerPage), "p=$page&q=" . urlencode($q) ) );
+ $wgOut->addHTML( '<br />' . $sk->makeLinkObj(
+ $this->getTitle(),
+ wfMsg( 'importfreeimages_next',
$ifi->resultsPerPage ),
+ "p=$page&q=" . urlencode( $q )
+ ) );
}
}
Modified: trunk/extensions/ImportFreeImages/UploadFreeImage.php
===================================================================
--- trunk/extensions/ImportFreeImages/UploadFreeImage.php 2011-03-22
21:06:20 UTC (rev 84557)
+++ trunk/extensions/ImportFreeImages/UploadFreeImage.php 2011-03-22
21:18:44 UTC (rev 84558)
@@ -5,7 +5,7 @@
class UploadFreeImage extends UploadFromUrl {
/**
* Hook to UploadCreateOnRequest.
- *
+ *
* This class processes wpSourceType=IFI
*/
public static function onUploadCreateFromRequest( $type, &$className ) {
@@ -16,19 +16,19 @@
}
return true;
}
-
+
/**
* By installing this extension it is unconditionally enabled
*/
public static function isEnabled() { return true; }
-
+
/**
* A valid request requires wpFlickrId to be set
*/
public static function isValidRequest( $request ) {
return (bool)$request->getVal( 'wpFlickrId' );
}
-
+
/**
* Extract wpDestFile and construct the url from wpFlickrId and wpSize
and
* pass it to the parent.
@@ -40,29 +40,30 @@
false
);
}
+
/**
- * Get the source url for an image
- *
- * @param $flickrId int Flickr photo id
- * @param $requestedSize string Label of the requested size
- * @return mixed Url or false
+ * Get the source URL for an image
+ *
+ * @param $flickrId Integer: Flickr photo ID
+ * @param $requestedSize String: label of the requested size
+ * @return mixed URL or false
*/
public static function getUrl( $flickrId, $requestedSize ) {
if ( !$requestedSize )
return false;
-
+
$ifi = new ImportFreeImages();
$sizes = $ifi->getSizes( $flickrId );
-
+
foreach ( $sizes as $size ) {
if ( $size['label'] === $requestedSize ) {
return $size['source'];
}
}
-
+
return false;
}
-
+
/**
* UI hook to remove all source input selections and replace them by a
set
* of radio buttons allowing the user to select the requested size
@@ -70,24 +71,27 @@
public static function onUploadFormSourceDescriptors( &$descriptor,
&$radio, $selectedSourceType ) {
global $wgRequest;
if ( $wgRequest->getVal( 'wpSourceType' ) != 'IFI' ||
!$wgRequest->getCheck( 'wpFlickrId' ) )
+ {
return true;
-
+ }
+
// We entered here from Special:ImportFreeImages, so kill all
other source selections
foreach ( $descriptor as $name => $value ) {
- if ( isset( $value['section'] ) && $value['section'] ==
'source' )
+ if ( isset( $value['section'] ) && $value['section'] ==
'source' ) {
unset( $descriptor[$name] );
+ }
}
-
+
$ifi = new ImportFreeImages();
$sizes = $ifi->getSizes( $wgRequest->getText( 'wpFlickrId' ) );
-
+
// Create radio buttons. TODO: Show resolution; Make largest
size default
- $options = array();
+ $options = array();
foreach ( $sizes as $size ) {
$label = wfMsgExt( 'importfreeimages_size_' .
strtolower( $size['label'] ), 'parseinline' );
$options[$label] = $size['label'];
}
-
+
$descriptor['Size'] = array(
'type' => 'radio',
'section' => 'source',
@@ -104,39 +108,40 @@
'name' => 'wpSourceType',
'default' => 'IFI',
);
-
+
// Stop running further hooks
return false;
}
-
+
public static function onUploadFormInitDescriptor( &$descriptor ) {
global $wgRequest;
if ( $wgRequest->getVal( 'wpSourceType' ) != 'IFI' ||
!$wgRequest->getCheck( 'wpFlickrId' ) )
+ {
return true;
-
+ }
+
$ifi = new ImportFreeImages();
$id = $wgRequest->getVal( 'wpFlickrId', 0 );
$info = $ifi->getPhotoInfo( $id );
-
+
$name_wiki = wfEscapeWikiText( $info['owner']['username'] );
if ( $ifi->creditsTemplate ) {
$owner_wiki = wfEscapeWikiText(
$info['owner']['realname'] );
$id_wiki = wfEscapeWikiText( $id );
- $caption = "{{" . $ifi->creditsTemplate . intval(
$info['license'] ) .
+ $caption = "{{" . $ifi->creditsTemplate . intval(
$info['license'] ) .
"|1=$id_wiki|2=$owner_wiki|3=$name_wiki}}";
} else {
// TODO: this is totally wrong: The whole message
should be configurable, we shouldn't include arbitrary templates
// additionally, the license information is not correct
(we are not guaranteed to get "CC by 2.0" images only)
- $caption = wfMsgForContent(
'importfreeimages_filefromflickr',
- $info['title'],
- "http://www.flickr.com/people/" . urlencode(
$info['owner']['username'] ) . ' ' . $name_wiki
- )
- . " {{CC by 2.0}} ";
+ $caption = wfMsgForContent(
'importfreeimages_filefromflickr',
+ $info['title'],
+ 'http://www.flickr.com/people/' . urlencode(
$info['owner']['username'] ) . ' ' . $name_wiki
+ ) . ' {{CC by 2.0}} ';
$caption = trim( $caption );
}
$descriptor['UploadDescription']['default'] = $caption;
-
+
return true;
}
-
+
}
\ No newline at end of file
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs