r21701 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r21700‎ | r21701 | r21702 >
Date:14:58, 29 April 2007
Author:thomasv
Status:old
Tags:
Comment:
javascript zoom and page navigation
Modified paths:
  • /trunk/extensions/ProofreadPage/ProofreadPage.i18n.php (modified) (history)
  • /trunk/extensions/ProofreadPage/ProofreadPage.php (modified) (history)
  • /trunk/extensions/ProofreadPage/proofread.js (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/ProofreadPage.i18n.php
@@ -3,6 +3,7 @@
44 $messages = array(
55 'en' => array(
66 'proofreadpage_namespace' => 'Page',
 7+ 'proofreadpage_index_namespace' => 'Index',
78 ),
89 );
910
Index: trunk/extensions/ProofreadPage/ProofreadPage.php
@@ -12,8 +12,94 @@
1313 'author' => 'ThomasV'
1414 );
1515
 16+
 17+
 18+/**
 19+ *
 20+ * Query the database to find if the current page is referred in an
 21+ * Index page. If yes, return the URLs of the index, previous and next pages.
 22+ *
 23+ */
 24+
 25+function wfProofreadPageNavigation( ) {
 26+ global $wgTitle, $wgExtraNamespaces;
 27+ $index_namespace = preg_quote( wfMsgForContent('proofreadpage_index_namespace' ) );
 28+ $err = array( '', '', '' );
 29+
 30+ $dbr =& wfGetDB( DB_SLAVE );
 31+ $pagelinks = $dbr->tableName( 'pagelinks' );
 32+ $sql = "SELECT pl_from FROM $pagelinks WHERE pl_title=? LIMIT 100" ;
 33+ $result = $dbr->safeQuery ( $sql, $wgTitle->getDBKey() ) ;
 34+ while( $x = $dbr->fetchObject ( $result )) {
 35+
 36+ $page_table = $dbr->tableName( 'page' );
 37+ $sql = "SELECT page_title, page_namespace, page_latest FROM $page_table WHERE page_id=? LIMIT 1" ;
 38+ $result2 = $dbr->safeQuery ( $sql, $x->pl_from ) ;
 39+ $y = $dbr->fetchObject ( $result2 );
 40+ $dbr->freeResult( $result2 ) ;
 41+ if(!$y) { continue; }
 42+
 43+ $ref_title = Title::makeTitle( $y->page_namespace, $y->page_title );
 44+ if(!$ref_title) { continue; }
 45+
 46+ if ( preg_match( "/^$index_namespace:(.*)$/", $ref_title->getPrefixedText() ) ) {
 47+ break;
 48+ }
 49+ else { continue;}
 50+
 51+ }
 52+ if( !$x ) { return $err;}
 53+ $dbr->freeResult( $result ) ;
 54+
 55+ $index_title = $ref_title;
 56+ $index_url = $index_title->getFullURL();
 57+
 58+ $revision_table = $dbr->tableName( 'revision' );
 59+ $sql = "SELECT rev_text_id FROM $revision_table WHERE rev_id=? LIMIT 1" ;
 60+ $result = $dbr->safeQuery ( $sql, $y->page_latest ) ;
 61+ $z = $dbr->fetchObject ( $result );
 62+ $dbr->freeResult ( $result ) ;
 63+ if( !$z ) return $err;
 64+
 65+ $text = $dbr->tableName( 'text' );
 66+ $sql = "SELECT old_text FROM $text WHERE old_id=? LIMIT 1" ;
 67+ $result = $dbr->safeQuery ( $sql, $z->rev_text_id ) ;
 68+ $zz = $dbr->fetchObject ( $result );
 69+ $dbr->freeResult( $result ) ;
 70+ if( !$zz ) return $err;
 71+
 72+ $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ) );
 73+ $tag_pattern = "/\[\[($page_namespace:[\s\S]*?)\]\]/m";
 74+ preg_match_all( $tag_pattern, $zz->old_text, $links, PREG_PATTERN_ORDER );
 75+
 76+ for( $i=0; $i<count( $links[1] ); $i++) {
 77+ if( $links[1][$i]==$wgTitle->getPrefixedText() ) break;
 78+ }
 79+ if( $i>0 ){
 80+ $prev_title = Title::newFromText( $links[1][$i-1] );
 81+ $prev_url = $prev_title->getFullURL();
 82+ } else $prev_url = '';
 83+
 84+ if( $i+1<count($links[1])){
 85+ $next_title = Title::newFromText( $links[1][$i+1] );
 86+ $next_url = $next_title->getFullURL();
 87+ } else $next_url = '';
 88+
 89+ return array( $index_url, $prev_url, $next_url );
 90+
 91+}
 92+
 93+
 94+/**
 95+ *
 96+ * Append javascript variables and code to the page.
 97+ *
 98+ */
 99+
16100 function wfProofreadPageParserOutput( &$out, &$pout ) {
 101+
17102 global $wgTitle, $wgJsMimeType, $wgScriptPath, $wgRequest;
 103+
18104 wfProofreadPageLoadMessages();
19105 $action = $wgRequest->getVal('action');
20106 $isEdit = ( $action == 'submit' || $action == 'edit' ) ? 1 : 0;
@@ -21,23 +107,34 @@
22108 return true;
23109 }
24110 $out->proofreadPageDone = true;
25 - $namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ) );
26 - if ( !preg_match( "/^$namespace:(.*)$/", $wgTitle->getPrefixedText(), $m ) ) {
 111+
 112+ $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ) );
 113+ if ( !preg_match( "/^$page_namespace:(.*)$/", $wgTitle->getPrefixedText(), $m ) ) {
27114 return true;
28115 }
 116+
 117+ list($index_url,$prev_url,$next_url ) = wfProofreadPageNavigation();
 118+
29119 $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
30120 if ( !$imageTitle ) {
31121 return true;
32122 }
 123+
33124 $image = new Image( $imageTitle );
34 - if ( !$image->exists() ) {
35 - return true;
 125+ if ( $image->exists() ) {
 126+ $width = intval( $image->getWidth() );
 127+ $height = intval( $image->getHeight() );
 128+ $viewURL = Xml::escapeJsString( $image->getViewURL() );
 129+ list( $isScript, $thumbURL ) = $image->thumbUrl( '##WIDTH##' );
 130+ $thumbURL = Xml::escapeJsString( str_replace( '%23', '#', $thumbURL ) );
 131+ }
 132+ else {
 133+ $width = 0;
 134+ $height = 0;
 135+ $viewURL = '';
 136+ $thumbURL = '';
36137 }
37 - $width = intval( $image->getWidth() );
38 - $height = intval( $image->getHeight() );
39 - $viewURL = Xml::escapeJsString( $image->getViewURL() );
40 - list( $isScript, $thumbURL ) = $image->thumbUrl( '##WIDTH##' );
41 - $thumbURL = Xml::escapeJsString( str_replace( '%23', '#', $thumbURL ) );
 138+
42139 $jsFile = htmlspecialchars( "$wgScriptPath/extensions/ProofreadPage/proofread.js" );
43140
44141 $out->addScript( <<<EOT
@@ -47,6 +144,9 @@
48145 var proofreadPageViewURL = "$viewURL";
49146 var proofreadPageThumbURL = "$thumbURL";
50147 var proofreadPageIsEdit = $isEdit;
 148+var proofreadPageIndexURL = "$index_url";
 149+var proofreadPagePrevURL = "$prev_url";
 150+var proofreadPageNextURL = "$next_url";
51151 </script>
52152 <script type="$wgJsMimeType" src="$jsFile"></script>
53153
Index: trunk/extensions/ProofreadPage/proofread.js
@@ -1,10 +1,52 @@
22 // Author : ThomasV - License : GPL
33
4 -function proofreadPageSetup() {
5 - if(!self.proofreadPageViewURL) {
6 - return;
 4+
 5+function proofreadPageDoTabs(){
 6+
 7+ a = document.getElementById("p-cactions");
 8+ if (!a) return;
 9+ b = a.getElementsByTagName("ul");
 10+ if (!b) return;
 11+
 12+ if(self.proofreadPageViewURL) {
 13+ b[0].innerHTML = b[0].innerHTML
 14+ + '<li id="ca-image">'
 15+ + '<a href='+proofreadPageViewURL+'>'
 16+ + 'Image</a></li>';
717 }
818
 19+ if(self.proofreadPageIndexURL){
 20+ b[0].innerHTML = b[0].innerHTML
 21+ + '<li id="ca-index">'
 22+ + '<a href='+proofreadPageIndexURL+' title="Index">'
 23+ + "<img src='http://upload.wikimedia.org/wikipedia/commons/a/af/1uparrow.png' alt='Index' width='15' height='15' longdesc='Next Page'/></a></li>";
 24+ }
 25+
 26+ if(self.proofreadPageNextURL){
 27+ b[0].innerHTML =
 28+ '<li id="ca-next">'
 29+ + '<a href='+self.proofreadPageNextURL+' title="Next Page">'
 30+ + "<img src='http://upload.wikimedia.org/wikipedia/commons/3/3c/1rightarrow.png' alt='Next Page' width='15' height='15' longdesc='Next Page'/></a></li>"
 31+ + b[0].innerHTML ;
 32+ }
 33+
 34+ if(self.proofreadPagePrevURL){
 35+ b[0].innerHTML =
 36+ '<li id="ca-prev">'
 37+ + '<a href='+self.proofreadPagePrevURL+' title="Previous Page">'
 38+ + "<img src='http://upload.wikimedia.org/wikipedia/commons/8/8e/1leftarrow.png' alt='Previous Page' width='15' height='15' longdesc='Previous Page'/></a></li>"
 39+ + b[0].innerHTML ;
 40+ }
 41+}
 42+
 43+
 44+
 45+
 46+
 47+function proofreadPageSetup() {
 48+
 49+ if(!self.proofreadPageViewURL) return;
 50+
951 if(document.URL.indexOf("action=protect") > 0 || document.URL.indexOf("action=unprotect") > 0) return;
1052 if(document.URL.indexOf("action=delete") > 0 || document.URL.indexOf("action=undelete") > 0) return;
1153 if(document.URL.indexOf("action=watch") > 0 || document.URL.indexOf("action=unwatch") > 0) return;
@@ -137,7 +179,162 @@
138180 pf.removeChild(footer);
139181 form.elements["wpTextbox1"].value = h+form.elements["wpTextbox1"].value+f;
140182 }
 183+
141184 }
142185
143186
 187+
 188+
 189+
 190+
 191+/*
 192+ * Mouse Zoom. Credits: http://valid.tjp.hu/zoom/
 193+ */
 194+
 195+
 196+//size of the zoom window
 197+var zoomw=160;
 198+var zoomh=120;
 199+
 200+var zp_pic;
 201+var zp_clip;
 202+var zp_container;
 203+
 204+var zoomamount=2;
 205+//size of the lowresself.pr image
 206+var objw;
 207+var objh;
 208+var zoom_status='';
 209+var zoomratio=zoomw/zoomh;
 210+var ieox=0; var ieoy=0;
 211+var ffox=0; var ffoy=0;
 212+
 213+
 214+function zoom_move(evt) {
 215+
 216+ if(zoom_status == 0) { return false;}
 217+
 218+ if(typeof(evt) == 'object') {
 219+ var evt = evt?evt:window.event?window.event:null; if(!evt){ return;}
 220+ if(evt.pageX) {
 221+ xx=evt.pageX - ffox;
 222+ yy=evt.pageY - ffoy;
 223+ }
 224+ else {
 225+ if(typeof(document.getElementById("zp")+1) == 'number') {return true;}
 226+ xx=evt.clientX - ieox;
 227+ yy=evt.clientY - ieoy;
 228+ }
 229+ }
 230+ else {
 231+ xx=lastxx;
 232+ yy=lastyy;
 233+ }
 234+ lastxx=xx; lastyy=yy;
 235+
 236+ zp_clip.style.margin=((yy-zoomh/2 > 0)?(zoomh/2-yy*zoomamount):(yy*(1-zoomamount)))+'px 0px 0px '+((xx-zoomw/2 > 0)?(zoomw/2-xx*zoomamount):(xx*(1-zoomamount)))+'px';
 237+
 238+ zp_container.style.margin=((yy-zoomh/2 > 0)?(yy-zoomh/2):(0))+'px 0px 0px '+((xx-zoomw/2 > 0)?(xx-zoomw/2):(0))+'px';
 239+ //width of the zoom window
 240+ w2=((xx+zoomw/2<objw)?((zoomw/2<xx)?(zoomw):(zoomw/2+xx)):(zoomw/2+objw-xx)); if(w2<0) {w2=0;}
 241+ h2=((yy+zoomh/2<objh)?((zoomh/2<yy)?(zoomh):(zoomh/2+yy)):(zoomh/2+objh-yy)); if(h2<0) {h2=0;}
 242+ zp_container.style.width=w2+'px';
 243+ zp_container.style.height=h2+'px';
 244+
 245+ return false;
 246+}
 247+
 248+function zoom_off() {
 249+ if(zoom_status == 1) {
 250+ zp_container.style.width='0px';
 251+ zp_container.style.height='0px';
 252+ }
 253+ zoom_status=0;
 254+}
 255+
 256+function countoffset() {
 257+ zme=document.getElementById("zp");
 258+ ieox=0; ieoy=0;
 259+ for(zmi=0;zmi<50;zmi++) {
 260+ if(zme+1 == 1) {
 261+ break;
 262+ }
 263+ else {
 264+ ieox+=zme.offsetLeft;
 265+ ieoy+=zme.offsetTop;
 266+ }
 267+ zme=zme.offsetParent;
 268+ }
 269+ ffox=ieox;
 270+ ffoy=ieoy;
 271+ ieox-=document.body.scrollLeft;
 272+ ieoy-=document.body.scrollTop;
 273+}
 274+
 275+
 276+function zoom_on(evt) {
 277+
 278+ if(zoom_status==1) {
 279+ zoom_off();
 280+ return false;
 281+ }
 282+
 283+ var evt = evt?evt:window.event?window.event:null; if(!evt){ return;}
 284+ zoom_status=1;
 285+
 286+ if(evt.pageX) {
 287+ countoffset();
 288+ lastxx=evt.pageX - ffox;
 289+ lastyy=evt.pageY - ffoy;
 290+ }
 291+ else {
 292+ countoffset();
 293+ lastxx=evt.clientX - ieox;
 294+ lastyy=evt.clientY - ieoy;
 295+ }
 296+
 297+ objw=zp_pic.width;
 298+ objh=zp_pic.height;
 299+ zoomamount = zp_clip.height/objh;
 300+
 301+ if(zoomw>objw) {zoomw=objw; zoomh=objw/zoomratio;}
 302+ else if(zoomh>objh) {zoomh=objh; zoomw=objh*zoomratio}
 303+ zoom_move('');
 304+ return false;
 305+}
 306+
 307+
 308+function proofreadPageZoom(){
 309+
 310+ if(!self.proofreadPageViewURL) return;
 311+ if(!self.proofreadPageWidth) return;
 312+
 313+ zp = document.getElementById("proofreadImage");
 314+ if(zp){
 315+ if(proofreadPageWidth>800) {
 316+ hires_url = proofreadPageThumbURL.replace('##WIDTH##',""+800);
 317+ }
 318+ else {
 319+ hires_url = proofreadPageViewURL;
 320+ }
 321+
 322+ zp.setAttribute("onmouseup","zoom_on(event);" );
 323+ zp.setAttribute("onmousemove","zoom_move(event);" );
 324+ zp.setAttribute("id","zp" );
 325+ zp_pic=zp.firstChild;
 326+
 327+ zp_container = document.createElement("div");
 328+ zp_container.setAttribute("style","position:absolute; width:0; height:0; overflow:hidden;");
 329+ zp_clip = document.createElement("img");
 330+ zp_clip.setAttribute("src", hires_url);
 331+ zp_clip.setAttribute("style", "padding:0;margin:0;border:0;");
 332+ zp_container.appendChild(zp_clip);
 333+ zp.insertBefore(zp_container,zp_pic);
 334+ }
 335+}
 336+
 337+
 338+
144339 addOnloadHook(proofreadPageSetup);
 340+addOnloadHook(proofreadPageDoTabs);
 341+hookEvent("load", proofreadPageZoom);