Index: trunk/extensions/SlimboxThumbs/SlimboxThumbs.php |
— | — | @@ -49,7 +49,6 @@ |
50 | 50 | * but they would be called for each image, making the wiki even slower |
51 | 51 | */ |
52 | 52 | if ( $slimboxThumbsFilesDir ) { |
53 | | - $slimboxThumbsFilesDir = rtrim( trim( $slimboxThumbsFilesDir ), '/' ); // strip whitespace, then any trailing / |
54 | 53 | $wgHooks['BeforeParserrenderImageGallery'][] = 'efSBTTestForGallery'; // this seems to fail on some pages :( |
55 | 54 | $hasGallery = true; // temporary fix |
56 | 55 | $wgHooks['BeforePageDisplay'][] = 'efSBTAddScripts'; |
— | — | @@ -59,26 +58,67 @@ |
60 | 59 | function efSBTTestForGallery( $parser, $gallery ) { |
61 | 60 | global $hasGallery; |
62 | 61 | $hasGallery = $gallery instanceof ImageGallery; |
63 | | - return $hasGallery; |
| 62 | + return true; |
64 | 63 | } |
65 | 64 | |
| 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 | + |
66 | 104 | function efSBTDebugVar( $varName, $var ) { |
67 | 105 | return "\n\n<!--\n$varName: " . str_replace( '--', '__', print_r( $var, true ) ) . "\n-->\n\n"; |
68 | 106 | } |
69 | 107 | |
70 | | -// This is a callback function that gets called by efBeforePageDisplay(). |
| 108 | +/** |
| 109 | + * This is a callback function that gets called by efBeforePageDisplay(). |
| 110 | + */ |
71 | 111 | function efRewriteThumbImage( $matches ) { |
72 | | - global $wgOut, $slimboxThumbsDebug; |
| 112 | + global $wgOut, $slimboxThumbsDebug; |
73 | 113 | |
74 | | - if ( $slimboxThumbsDebug ) { global $wgContLang; } |
| 114 | + if ( $slimboxThumbsDebug ) { global $wgContLang; } |
75 | 115 | |
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&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 ) |
83 | 123 | . efSBTDebugVar( '$titleObj', $titleObj ) |
84 | 124 | . efSBTDebugVar( '$image', $image ) |
85 | 125 | . efSBTDebugVar( '$wgContLang->namespaceNames', $wgContLang->namespaceNames ):'' ); |
— | — | @@ -88,53 +128,27 @@ |
89 | 129 | |
90 | 130 | // Rewrite the gallery code. |
91 | 131 | function efRewriteGalleryImage( $matches ) { |
92 | | - global $wgOut, $slimboxThumbsDebug, $slimboxDefaultWidth; |
| 132 | + global $wgOut, $slimboxThumbsDebug, $slimboxDefaultWidth, $wgScriptPath; |
| 133 | + |
93 | 134 | $titleObj = Title::newFromText( rawurldecode( $matches[2] ) ); |
94 | 135 | $image = wfFindFile( $titleObj, false, false, true ); |
95 | 136 | $realwidth = (Integer) $image->getWidth(); |
96 | 137 | $width = ( $realwidth > $slimboxDefaultWidth ) ? $slimboxDefaultWidth : $realwidth -1; |
97 | 138 | $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() . '&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() . "]]:''' " ) |
101 | 141 | . $matches[4] ) |
102 | 142 | . '" ' . $matches[3] . $matches[4] . "</div>" |
103 | | - . ( $slimboxThumbsDebug ? efDebugVar( '$matches', $matches ) |
| 143 | + . ( $slimboxThumbsDebug ? efSBTDebugVar( '$matches', $matches ) |
104 | 144 | . efSBTDebugVar( '$titleObj', $titleObj ) |
105 | 145 | . efSBTDebugVar( '$image', $image ):'' ); |
106 | 146 | |
107 | 147 | return $output; |
108 | 148 | } |
109 | 149 | |
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; |
116 | 150 | |
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 | | - |
137 | 151 | function efSBTAddSlimboxCode( $out, $skin ) { |
138 | | - global $slimboxThumbsFilesDir, $wgContLang, $hasGallery; |
| 152 | + global $wgContLang, $hasGallery; |
139 | 153 | |
140 | 154 | // We don't want to run regular expressions if there's no gallery here. |
141 | 155 | if ( !$hasGallery ) { |
— | — | @@ -144,34 +158,44 @@ |
145 | 159 | # # ideally we'd do this with XPath, but we'd need valid XML for that, so we'll do it with some ugly regexes |
146 | 160 | # # (could use a regex to pull out all div.thumb, maybe they're valid XML? ...probably not) |
147 | 161 | # # An other alternative would be to use javascript and the DOM |
| 162 | + # # The jquery lightbox plugin does exactly that. |
148 | 163 | |
149 | 164 | // 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'; |
161 | 173 | |
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 | + */ |
173 | 182 | |
174 | | - $out->clearHTML(); |
175 | | - $out->addHTML( $allDone ); |
| 183 | + $thumbnailsDone = preg_replace_callback( $pattern, 'efRewriteThumbImage', $out->getHTML() ); |
176 | 184 | |
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 | +} |