Index: trunk/extensions/QrCode/QrCode.i18n.php |
— | — | @@ -0,0 +1,18 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Internationalization file for the QrCode extension. |
| 6 | + * |
| 7 | + * @file QrCode.i18n.php |
| 8 | + * |
| 9 | + * @author David Raison |
| 10 | + */ |
| 11 | + |
| 12 | +$messages = array(); |
| 13 | + |
| 14 | +/** English |
| 15 | + * @author David Raison |
| 16 | + */ |
| 17 | +$messages['en'] = array( |
| 18 | + 'qrcode-desc' => 'Generates and adds qrcode png to a wiki page' |
| 19 | +); |
Index: trunk/extensions/QrCode/QrCode.php |
— | — | @@ -0,0 +1,169 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * QrCode.php |
| 5 | + * Written by David Raison |
| 6 | + * @license: LGPL (GNU Lesser General Public License) |
| 7 | + * |
| 8 | + * @file QrCode.php |
| 9 | + * |
| 10 | + * @author David Raison |
| 11 | + * |
| 12 | + * Uses the PHPQrcode library written by Dominik Dzienia (C) 2010, |
| 13 | + * which is, in turn, based on C libqrencode library |
| 14 | + * Copyright (C) 2006-2010 by Kentaro Fukuchi |
| 15 | + * http://megaui.net/fukuchi/works/qrencode/index.en.html |
| 16 | + * |
| 17 | + * Todo: |
| 18 | + * * Update (i.e. replace) the image file when the QrCode arguments change |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +if ( !defined( 'MEDIAWIKI' ) ) |
| 23 | + die( 'This is a MediaWiki extension, and must be run from within MediaWiki.' ); |
| 24 | + |
| 25 | +$wgExtensionCredits['parserhook'][] = array( |
| 26 | + 'path' => __FILE__, |
| 27 | + 'name' => 'QrCode', |
| 28 | + 'version' => '0.01', |
| 29 | + 'author' =>'David Raison', |
| 30 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:QrCode', |
| 31 | + 'descriptionmsg' => 'qrcode-desc' |
| 32 | +); |
| 33 | + |
| 34 | +$wgAutoloadClasses['QRcode'] = dirname(__FILE__) . '/phpqrcode/qrlib.php'; |
| 35 | +$wgExtensionFunctions[] = "efQrcodeSetup"; |
| 36 | +$wgExtensionMessagesFiles['QrCode'] = dirname(__FILE__) .'/QrCode.i18n.php'; |
| 37 | +$wgHooks['LanguageGetMagic'][] = 'wfQrCodeLanguageGetMagic'; |
| 38 | + |
| 39 | +function efQrcodeSetup(){ |
| 40 | + global $wgParser; |
| 41 | + $wgParser->setFunctionHook( 'qrcode', array(new mwQrCode, 'showCode' )); |
| 42 | +} |
| 43 | + |
| 44 | +function wfQrCodeLanguageGetMagic( &$magicWords, $langCode='en' ) { |
| 45 | + $magicWords['qrcode'] = array( 0, 'qrcode' ); |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +// Defaults (overwritten by LocalSettings.php and possibly also by the function's arguments) |
| 50 | +$wgQrCodeECC = 'L'; // L,M,Q,H |
| 51 | +$wgQrCodeSize = 4; // pixel size of black squares |
| 52 | +$wgQrCodeBoundary = 2; // boundary around square |
| 53 | +$wgQrCodeBot = 'QrCodeBot'; |
| 54 | + |
| 55 | +class mwQrCode { |
| 56 | + |
| 57 | + private $_dstFileName; // what the file will be named? |
| 58 | + private $_label; // What will the qrcode contain? |
| 59 | + private $_ecc; // error correction |
| 60 | + private $_size; // qrcode size |
| 61 | + private $_boundary; // qrcode margin |
| 62 | + |
| 63 | + public function __construct(){ |
| 64 | + global $wgQrCodeECC, $wgQrCodeSize, $wgQrCodeBoundary; |
| 65 | + $this->_ecc = $wgQrCodeECC; |
| 66 | + $this->_size = $wgQrCodeSize; |
| 67 | + $this->_boundary = $wgQrCodeBoundary; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * If we don't have the code on file, generate, then publish it |
| 72 | + * @return wikitext for image display |
| 73 | + */ |
| 74 | + public function showCode(){ |
| 75 | + global $wgTitle; |
| 76 | + $this->_label = $wgTitle->getFullURL(); |
| 77 | + |
| 78 | + $params = func_get_args(); |
| 79 | + $parser = array_shift($params); |
| 80 | + |
| 81 | + foreach( $params as $pair ) { |
| 82 | + $rpms = explode( '=', $pair ); |
| 83 | + if( $rpms[0] == 'ecc' ) $this->_ecc = $rpms[1]; |
| 84 | + if( $rpms[0] == 'size' ) $this->_size = $rpms[1]; |
| 85 | + if( $rpms[0] == 'boundary' ) $this->_boundary = $rpms[1]; |
| 86 | + if( $rpms[0] == 'label' ) $this->_label = $rpms[1]; |
| 87 | + } |
| 88 | + |
| 89 | + // Do we have a label? |
| 90 | + $append = ( $this->_label != $wgTitle->getFullURL() ) ? '-'.$this->_label : ''; |
| 91 | + |
| 92 | + // Use this page's title as part of the filename |
| 93 | + $this->_dstFileName = 'QR-'.$wgTitle->getDBKey().$append.'.png'; |
| 94 | + |
| 95 | + $file = wfFindFile( $this->_dstFileName ); // Shortcut for RepoGroup::singleton()->findFile() |
| 96 | + if( $file && $file->isVisible() ){ |
| 97 | + $ft = $file->getTitle(); |
| 98 | + return $this->_displayImage( $ft ); |
| 99 | + } else { |
| 100 | + $this->_generate(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * This only creates the wikitext to display an image. |
| 106 | + * @return wikitext for image display |
| 107 | + */ |
| 108 | + private function _displayImage( $fileTitle ){ |
| 109 | + // a tag hook would use $parser->makeImage($ft,$options); |
| 110 | + return '[['.$fileTitle->getFullText().']]'; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Generate the qrcode using the phpqrcode library |
| 115 | + * @return output of the _publish method |
| 116 | + */ |
| 117 | + private function _generate(){ |
| 118 | + global $wgTmpDirectory; |
| 119 | + $tmpName = tempnam( $wgTmpDirectory, 'qrcode' ); |
| 120 | + |
| 121 | + QRcode::png( $this->_label, $tmpName, $this->_ecc, $this->_size, $this->_boundary ); |
| 122 | + wfDebug( "Generated qrcode file $tmpName with ecc $wgQrCodeECC, size $wgQrCodeSize and boundary $wgQrCodeBoundary.\n" ); |
| 123 | + |
| 124 | + return $this->_publish( $tmpName ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Create or select a bot user to attribute the code generation to |
| 129 | + * @return user object |
| 130 | + * */ |
| 131 | + private function _getBot(){ |
| 132 | + global $wgQrCodeBot; |
| 133 | + |
| 134 | + // there doesn't seem to be a decent method for checking if a user already exists... |
| 135 | + $bot = User::createNew( $wgQrCodeBot ); |
| 136 | + if( $bot != null ){ |
| 137 | + //$bot->setPassword( '' ); // doc says empty password disables, but it triggers an exception |
| 138 | + } else { |
| 139 | + $bot = User::newFromName( $wgQrCodeBot ); |
| 140 | + } |
| 141 | + if( !$bot->isBot() ) |
| 142 | + $bot->addGroup( 'bot' ); |
| 143 | + |
| 144 | + return $bot; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Handle mediawiki file repositories |
| 149 | + * @param $tmpName, the file's temporary name |
| 150 | + * @return boolean value for success or failure of file "upload" |
| 151 | + */ |
| 152 | + private function _publish( $tmpName ){ |
| 153 | + global $wgOut; |
| 154 | + |
| 155 | + $ft = Title::makeTitleSafe( NS_FILE, $this->_dstFileName ); |
| 156 | + $localfile = wfLocalFile( $ft ); // Get an object referring to a locally registered file. |
| 157 | + $saveName = $localfile->getName(); |
| 158 | + $pageText = 'QrCode '.$saveName.', generated on '.date( "r" ).' by the QrCode Extension.'; |
| 159 | + $status = $localfile->upload( $tmpName, $this->_label, $pageText, File::DELETE_SOURCE, false, false, $this->_getBot() ); |
| 160 | + |
| 161 | + if( !$status->isGood() ){ |
| 162 | + $wgOut->addWikiText( $status->getWikiText() ); |
| 163 | + return false; |
| 164 | + } else { |
| 165 | + // now that we generated the file, let's display it |
| 166 | + $this->_displayImage( $ft ); |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | +} |