r82040 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82039‎ | r82040 | r82041 >
Date:23:21, 12 February 2011
Author:kwisatz
Status:deferred
Tags:
Comment:
Partially working maintenance release of SlimboxThumbs.
This is the last version of this extensions with this codebase.
Future versions will be redone on a higher level,
doing away with the horribly regex kludge.
Modified paths:
  • /trunk/extensions/SlimboxThumbs/SlimboxThumbs.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SlimboxThumbs/SlimboxThumbs.php
@@ -49,7 +49,6 @@
5050 * but they would be called for each image, making the wiki even slower
5151 */
5252 if ( $slimboxThumbsFilesDir ) {
53 - $slimboxThumbsFilesDir = rtrim( trim( $slimboxThumbsFilesDir ), '/' ); // strip whitespace, then any trailing /
5453 $wgHooks['BeforeParserrenderImageGallery'][] = 'efSBTTestForGallery'; // this seems to fail on some pages :(
5554 $hasGallery = true; // temporary fix
5655 $wgHooks['BeforePageDisplay'][] = 'efSBTAddScripts';
@@ -59,26 +58,67 @@
6059 function efSBTTestForGallery( $parser, $gallery ) {
6160 global $hasGallery;
6261 $hasGallery = $gallery instanceof ImageGallery;
63 - return $hasGallery;
 62+ return true;
6463 }
6564
 65+/**
 66+ * Add javascript files and stylesheets.
 67+ * Also add inline js that regulates the image width.
 68+ */
 69+function efSBTAddScripts( $out ) {
 70+ global $hasGallery, $wgVersion, $wgExtensionAssetsPath;
 71+ $eDir = $wgExtensionAssetsPath .'/SlimboxThumbs/slimbox';
 72+
 73+ // We don't want to load jQuery if there's no gallery here.
 74+ //if ( !$hasGallery ) return false;
 75+
 76+ // 1.17 will require another setup
 77+ // (http://www.mediawiki.org/wiki/JQuery & http://www.mediawiki.org/wiki/ResourceLoader/Default_modules)
 78+ if ( substr($wgVersion,0,-2) < 1.16 ) {
 79+ $jQ = '$';
 80+ $out->addScript( '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>' . "\n" );
 81+ } else {
 82+ $jQ = '$j';
 83+ $out->includeJQuery(); // includes jquery.min.js
 84+ }
 85+ $out->addScript( '<script type="text/javascript" src="'. $eDir .'/js/slimbox2.js"></script>' . "\n" );
 86+ $out->addExtensionStyle( $eDir . '/css/slimbox2.css', 'screen' );
 87+
 88+ // use thumb.php to resize pictures if browser window is smaller than the picture itself
 89+ $out->addInlineScript( $jQ.'(document).ready(function(){
 90+ if('.$jQ.'("table.gallery").val() != undefined){
 91+ var boxWidth = ('.$jQ.'(window).width() - 20);
 92+ var rxp = new RegExp(/([0-9]{2,})$/);
 93+ '.$jQ.'("a[rel=\'lightbox[gallery]\']").each(function(el){
 94+ if(boxWidth < Number(this.search.match(rxp)[0])){
 95+ this.href = this.pathname+this.search.replace(rxp,boxWidth);
 96+ }
 97+ });
 98+ }
 99+ })' );
 100+
 101+ return true;
 102+}
 103+
66104 function efSBTDebugVar( $varName, $var ) {
67105 return "\n\n<!--\n$varName: " . str_replace( '--', '__', print_r( $var, true ) ) . "\n-->\n\n";
68106 }
69107
70 -// This is a callback function that gets called by efBeforePageDisplay().
 108+/**
 109+ * This is a callback function that gets called by efBeforePageDisplay().
 110+ */
71111 function efRewriteThumbImage( $matches ) {
72 - global $wgOut, $slimboxThumbsDebug;
 112+ global $wgOut, $slimboxThumbsDebug;
73113
74 - if ( $slimboxThumbsDebug ) { global $wgContLang; }
 114+ if ( $slimboxThumbsDebug ) { global $wgContLang; }
75115
76 - $titleObj = Title::newFromText( rawurldecode( $matches[2] ) );
77 - $image = wfFindFile( $titleObj, false, false, true ); # # wfFindFile($titleObj,false,false,true) to bypass cache
78 - $output = $matches[1]
79 - . ' href="' . $image->getURL() . '" class="image" rel="lightbox" title="'
80 - . htmlspecialchars( $wgOut->parse( "'''[[:" . $titleObj->getFullText() . "|" . $titleObj->getText() . "]]:''' " ) . $matches[3] )
81 - . '" ' . $matches[4] . $matches[5] // url, hashpath,/w/thumb.php?f=FoodHacker_01.jpg&amp;width=800
82 - . ( $slimboxThumbsDebug ? efDebugVar( '$matches', $matches )
 116+ $titleObj = Title::newFromText( rawurldecode( 'File:'.$matches[2] ) );
 117+ $image = wfFindFile( $titleObj, false, false, true ); # # wfFindFile($titleObj,false,false,true) to bypass cache
 118+ $output = $matches[1] // <a (not much else)
 119+ . 'href="' . $image->getURL() . '" class="image" rel="lightbox" title="'
 120+ . htmlspecialchars( $wgOut->parse( "'''[[:" . $titleObj->getFullText() . "|" . $titleObj->getText() . "]]:''' " ) )
 121+ . '">'. $matches[3] . '</a>' //$matches[4]
 122+ . ( $slimboxThumbsDebug ? efSBTDebugVar( '$matches', $matches )
83123 . efSBTDebugVar( '$titleObj', $titleObj )
84124 . efSBTDebugVar( '$image', $image )
85125 . efSBTDebugVar( '$wgContLang->namespaceNames', $wgContLang->namespaceNames ):'' );
@@ -88,53 +128,27 @@
89129
90130 // Rewrite the gallery code.
91131 function efRewriteGalleryImage( $matches ) {
92 - global $wgOut, $slimboxThumbsDebug, $slimboxDefaultWidth;
 132+ global $wgOut, $slimboxThumbsDebug, $slimboxDefaultWidth, $wgScriptPath;
 133+
93134 $titleObj = Title::newFromText( rawurldecode( $matches[2] ) );
94135 $image = wfFindFile( $titleObj, false, false, true );
95136 $realwidth = (Integer) $image->getWidth();
96137 $width = ( $realwidth > $slimboxDefaultWidth ) ? $slimboxDefaultWidth : $realwidth -1;
97138 $output = $matches[1]
98 - // .' href="'.$image->getURL().'" class="image" rel="lightbox[gallery]" title="'
99 - . ' href="https://www.mediawiki.org/w/thumb.php?f=' . $image->getName() . '&amp;width=' . $width . '" class="image" rel="lightbox[gallery]" title="'
100 - . htmlspecialchars( $wgOut->parse( "'''[[:File:" . $titleObj->getFullText() . "|" . $titleObj->getText() . "]]:''' " )
 139+ .' href="'.$image->createThumb($width).'" class="image" rel="lightbox[gallery]" title="'
 140+ . htmlspecialchars( $wgOut->parse( "'''[[:" . $titleObj->getFullText() . "|" . $titleObj->getText() . "]]:''' " )
101141 . $matches[4] )
102142 . '" ' . $matches[3] . $matches[4] . "</div>"
103 - . ( $slimboxThumbsDebug ? efDebugVar( '$matches', $matches )
 143+ . ( $slimboxThumbsDebug ? efSBTDebugVar( '$matches', $matches )
104144 . efSBTDebugVar( '$titleObj', $titleObj )
105145 . efSBTDebugVar( '$image', $image ):'' );
106146
107147 return $output;
108148 }
109149
110 -/* Add javacsripts and stylesheets */
111 -function efSBTAddScripts( $out ) {
112 - global $slimboxThumbsFilesDir, $hasGallery;
113 -
114 - // We don't want to load jQuery if there's no gallery here.
115 - //if ( !$hasGallery ) return false;
116150
117 - $out->addScript( '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>' . "\n" );
118 - $out->addScript( '<script type="text/javascript" src="' . $slimboxThumbsFilesDir . '/js/slimbox2.js"></script>' . "\n" );
119 - $out->addExtensionStyle( $slimboxThumbsFilesDir . '/css/slimbox2.css', 'screen' );
120 -
121 - // use thumb.php to resize pictures if browser window is smaller than the picture itself
122 - $out->addInlineScript( '$(document).ready(function(){
123 - if($("table.gallery").val() != undefined){
124 - var boxWidth = ($(window).width() - 20);
125 - var rxp = new RegExp(/([0-9]{2,})$/);
126 - $("a[rel=\'lightbox[gallery]\']").each(function(el){
127 - if(boxWidth < Number(this.search.match(rxp)[0])){
128 - this.href = this.pathname+this.search.replace(rxp,boxWidth);
129 - }
130 - });
131 - }
132 - })' );
133 -
134 - return true;
135 -}
136 -
137151 function efSBTAddSlimboxCode( $out, $skin ) {
138 - global $slimboxThumbsFilesDir, $wgContLang, $hasGallery;
 152+ global $wgContLang, $hasGallery;
139153
140154 // We don't want to run regular expressions if there's no gallery here.
141155 if ( !$hasGallery ) {
@@ -144,34 +158,44 @@
145159 # # ideally we'd do this with XPath, but we'd need valid XML for that, so we'll do it with some ugly regexes
146160 # # (could use a regex to pull out all div.thumb, maybe they're valid XML? ...probably not)
147161 # # An other alternative would be to use javascript and the DOM
 162+ # # The jquery lightbox plugin does exactly that.
148163
149164 // regex for thumbnails
150 - $pattern = '/(<a[^>]+?)' // $1: start of opening <a> tag through start of href attribute in <a> tag
151 - . '\s*href="[^"]*(?:' . $wgContLang->namespaceNames[6] . '):' // dont care about start of original link href...
152 - . '([^"\/]+)' // $2: ...but end is wiki name for the image
153 - . '"\s*class="image"\s*title="'
154 - . '([^"]+)' // $3: link title becomes image caption
155 - . '"\s*.'
156 - . '([^>]*>)' // $4: remainder of opening <a> tag
157 - . '\s*'
158 - . '(<img[^>]+?class="thumbimage"[^>]*>)' // $5: the img tag itself
159 - . '/x';
160 - $thumbnailsDone = preg_replace_callback( $pattern, 'efRewriteThumbImage', $out->getHTML() );
 165+ $pattern = '/(<a[^>]+?)' // $1: start of opening <a> tag through start of href attribute in <a> tag
 166+ . '\s*href="[^"]*(?:' . $wgContLang->namespaceNames[6] . '):' // dont care about start of original link href...
 167+ . '([^"\/]+)' // $2: ...but end is wiki name for the image
 168+ . '"\s*class="image"\s*' // (1.16 title= gone)
 169+ . '[^>]*>\s*'
 170+ . '(<img[^>]+?[^>]*>)' // $3: the img tag itself (1.16 thumbimage class gone)
 171+ //.'(.*<\/*a>)' // $4 should get until >, but gets more...
 172+ .'/xs';
161173
162 - // regex for galleries
163 - $pattern = '/(<div\s*class="gallerybox".+?div\s*class="thumb".+?) # $1: div.gallerybox opening tag through href attribute, so we can keep it intact
164 - \s*href="[^"]+"\s*class="image"\s* # this is getting replaced
165 - title="([^"]+)" # $2: link title attribute holds wiki name for the image
166 - ([^>]*>.+?<div\s*class="gallerytext">) # $3: end of open <a> through start of caption
167 - \s*(?:<p>\s*)? #
168 - (.+?) # $4: caption is raw HTML... (may choke if contains an ending div)
169 - (?:\s*(<\/p>|<br\s*\/?>))?\s*<\/div> #
170 - /sx';
171 -
172 - $allDone = preg_replace_callback( $pattern, 'efRewriteGalleryImage', $thumbnailsDone );
 174+ //by User:Cm
 175+ /*$pattern = '/(<a[^>]+?)' // $1 start of opening <a> tag through start of href attribute in <a> tag
 176+ .'\s*href="([^"]*)' // $2 link
 177+ .'"\s*class="image"\s*title="([^"]+)' // $3 title
 178+ .'"\s*([^>]*>)' // $4 end of <a>
 179+ .'\s*(<img[^>]+?class="thumbimage"[^>]*>)' // $5 img tag
 180+ .'(.*<\/*a>)/xs'; // $6 anything else
 181+ */
173182
174 - $out->clearHTML();
175 - $out->addHTML( $allDone );
 183+ $thumbnailsDone = preg_replace_callback( $pattern, 'efRewriteThumbImage', $out->getHTML() );
176184
177 - return true;
178 -}
\ No newline at end of file
 185+ /**
 186+ * Redone regex for galleries
 187+ * I don't get this, why am I regexxing code I'm creating in the first callback? WTF?
 188+ */
 189+ $pattern = '/(<div\s*class="gallerybox".+?<div\s*class="thumb".+?)' # $1
 190+ .'.+?href="\/wiki\/([^"]+)"\s*class="image"' # $2 (link)
 191+ .'([^>]*>.+?<div\s*class="gallerytext">)' # $3 rest
 192+ .'\s*(?:<p>\s*)?(.+?)'
 193+ .'(?:\s*(<\/p>|<br\s*\/?>))?\s*<\/div>'
 194+ .'/xs';
 195+
 196+ $allDone = preg_replace_callback( $pattern, 'efRewriteGalleryImage', $thumbnailsDone );
 197+
 198+ $out->clearHTML();
 199+ $out->addHTML( $thumbnailsDone );
 200+ //$out->addHTML( $allDone );
 201+ return true;
 202+}

Status & tagging log