r48484 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48483‎ | r48484 | r48485 >
Date:09:40, 17 March 2009
Author:thomasv
Status:resolved
Tags:
Comment:
refactoring; adding indexref parser hook; using invalidateCache for index pages
Modified paths:
  • /trunk/extensions/ProofreadPage/ProofreadPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/ProofreadPage.php
@@ -4,12 +4,14 @@
55 die( "ProofreadPage extension\n" );
66 }
77
 8+$dir = dirname(__FILE__) . '/';
 9+
810 $wgExtensionMessagesFiles['ProofreadPage'] = dirname(__FILE__) . '/ProofreadPage.i18n.php';
911
10 -$wgHooks['BeforePageDisplay'][] = 'wfPRParserOutput';
11 -$wgHooks['GetLinkColours'][] = 'wfPRLinkColours';
12 -$wgHooks['ImageOpenShowImageInlineBefore'][] = 'wfPRImageMessage';
13 -$wgHooks['ArticleSave'][] = 'wfPRSave';
 12+$wgHooks['BeforePageDisplay'][] = 'pr_beforePageDisplay';
 13+$wgHooks['GetLinkColours'][] = 'pr_getLinkColours';
 14+$wgHooks['ImageOpenShowImageInlineBefore'][] = 'pr_imageMessage';
 15+$wgHooks['ArticleSaveComplete'][] = 'pr_articleSave';
1416
1517 $wgExtensionCredits['other'][] = array(
1618 'name' => 'ProofreadPage',
@@ -21,49 +23,113 @@
2224 'descriptionmsg' => 'proofreadpage_desc',
2325 );
2426
25 -$wgExtensionFunctions[] = "wfPRPageList";
26 -function wfPRPageList() {
27 - global $wgParser;
28 - $wgParser->setHook( "pagelist", "wfPRRenderPageList" );
 27+
 28+$wgExtensionFunctions[] = "pr_main";
 29+function pr_main() {
 30+ global $wgParser;
 31+
 32+ $wgParser->setHook( "pagelist", "pr_renderPageList" );
 33+ $wgParser->setHook( "indexref", "pr_renderIndexTag" );
 34+ wfLoadExtensionMessages( 'ProofreadPage' );
 35+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
 36+
2937 }
3038
 39+
 40+
 41+
3142 # Bump the version number every time you change proofread.js
3243 $wgProofreadPageVersion = 17;
3344
3445 /**
3546 *
36 - * Query the database to find if the current page is referred in an
37 - * Index page. If yes, return the URLs of the index, previous and next pages.
 47+ * Query the database to find if the current page is referred in an Index page.
3848 *
3949 */
 50+function pr_load_index($title){
4051
41 -function wfPRNavigation( $image ) {
42 - global $wgTitle;
4352 $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
4453 $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
45 - $err = array( '', '', '', '', array() );
4654
 55+ $title->pr_index_title=NULL;
 56+
4757 $dbr = wfGetDB( DB_SLAVE );
4858 $result = $dbr->select(
4959 array( 'page', 'pagelinks' ),
5060 array( 'page_namespace', 'page_title' ),
5161 array(
52 - 'pl_namespace' => $wgTitle->getNamespace(),
53 - 'pl_title' => $wgTitle->getDBkey(),
 62+ 'pl_namespace' => $title->getNamespace(),
 63+ 'pl_title' => $title->getDBkey(),
5464 'pl_from=page_id'
5565 ),
5666 __METHOD__);
5767
58 - $index_title = '';
5968 while( $x = $dbr->fetchObject( $result ) ) {
6069 $ref_title = Title::makeTitle( $x->page_namespace, $x->page_title );
6170 if( preg_match( "/^$index_namespace:(.*)$/", $ref_title->getPrefixedText() ) ) {
62 - $index_title = $ref_title;
 71+ $title->pr_index_title = $ref_title->getPrefixedText();
6372 break;
6473 }
6574 }
6675 $dbr->freeResult( $result ) ;
6776
 77+ if($title->pr_index_title) return;
 78+
 79+ /*check if we are a page of a multipage file*/
 80+
 81+ if ( preg_match( "/^$page_namespace:(.*?)(\/([0-9]*)|)$/", $title->getPrefixedText(), $m ) ) {
 82+ $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
 83+ }
 84+ if ( !$imageTitle ) return;
 85+
 86+ $image = Image::newFromTitle( $imageTitle );
 87+
 88+ //if it is multipage, we use the page order of the file
 89+ if( $image->exists() && $image->isMultiPage() ) {
 90+
 91+ $pagenr = 1;
 92+ $parts = explode( '/', $title->getText() );
 93+ if( count( $parts ) > 1 ) {
 94+ $pagenr = intval( array_pop( $parts ) );
 95+ }
 96+ $count = $image->pageCount();
 97+ if( $pagenr < 1 || $pagenr > $count || $count == 1 )
 98+ return $err;
 99+ $name = $image->getTitle()->getText();
 100+ $index_name = "$index_namespace:$name";
 101+ $prev_name = "$page_namespace:$name/" . ( $pagenr - 1 );
 102+ $next_name = "$page_namespace:$name/" . ( $pagenr + 1 );
 103+ $prev_url = ( $pagenr == 1 ) ? '' : Title::newFromText( $prev_name )->getFullURL();
 104+ $next_url = ( $pagenr == $count ) ? '' : Title::newFromText( $next_name )->getFullURL();
 105+
 106+ //todo : we should read pagenum from the index if it is provided
 107+ $title->pr_page_num = "$pagenr";
 108+
 109+ if( !$title->pr_index_title ) {
 110+ //there is no index, or the page is not listed in the index : use canonical index
 111+ $title->pr_index_title = $index_name;
 112+ }
 113+ }
 114+
 115+
 116+}
 117+
 118+
 119+
 120+/**
 121+ *
 122+ * return the URLs of the index, previous and next pages.
 123+ *
 124+ */
 125+
 126+
 127+function pr_navigation( $image ) {
 128+ global $wgTitle;
 129+ $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
 130+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
 131+ $err = array( '', '', '', '', array() );
 132+
 133+
68134 //if multipage, we use the page order, but we should read pagenum from the index
69135 if( $image->exists() && $image->isMultiPage() ) {
70136
@@ -96,50 +162,74 @@
97163
98164
99165 if( !$index_title ) return $err;
100 - $index_url = $index_title->getFullURL();
 166+ if( !$index_title->exists()) return $err;
101167
102168 //if the index page exists, read metadata
103 - if( $index_title->exists()) {
104169
105 - $rev = Revision::newFromTitle( $index_title );
106 - $text = $rev->getText();
 170+ list( $prev_title, $next_title, $attributes ) = pr_parse_index($index_title,$wgTitle);
107171
 172+ $index_url = $index_title->getFullURL();
 173+ if($prev_title) $prev_url = $prev_title->getFullURL();
 174+ if($next_title) $next_url = $next_title->getFullURL();
 175+
 176+ return array( $index_url, $prev_url, $next_url, $attributes );
 177+
 178+}
 179+
 180+
 181+/*
 182+ read metadata from the index page
 183+ read also pagenum if page_title is provided (not for djvu with pagelist)
 184+*/
 185+
 186+function pr_parse_index($index_title, $page_title){
 187+
 188+ $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
 189+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
 190+
 191+ if( !$index_title ) return ;
 192+ if( !$index_title->exists() ) return;
 193+
 194+ $rev = Revision::newFromTitle( $index_title );
 195+ $text = $rev->getText();
 196+
 197+ $attributes = array();
 198+
 199+ if($page_title){
 200+
 201+ //default pagenum was set during load()
 202+ if($page_title->pr_page_num) $attributes["pagenum"] = $page_title->pr_page_num;
 203+
108204 $tag_pattern = "/\[\[($page_namespace:.*?)(\|(.*?)|)\]\]/i";
109205 preg_match_all( $tag_pattern, $text, $links, PREG_PATTERN_ORDER );
110206
111207 for( $i=0; $i<count( $links[1] ); $i++) {
112208 $a_title = Title::newFromText( $links[1][$i] );
113209 if(!$a_title) continue;
114 - if( $a_title->getPrefixedText() == $wgTitle->getPrefixedText() ) {
115 - $page_num = $links[3][$i];
 210+ if( $a_title->getPrefixedText() == $page_title->getPrefixedText() ) {
 211+ $attributes["pagenum"] = $links[3][$i];
116212 break;
117213 }
118214 }
119215 if( ($i>0) && ($i<count($links[1])) ){
120216 $prev_title = Title::newFromText( $links[1][$i-1] );
121 - if(!$prev_title) return $err;
122 - $prev_url = $prev_title->getFullURL();
123217 }
124218 if( ($i>=0) && ($i+1<count($links[1])) ){
125219 $next_title = Title::newFromText( $links[1][$i+1] );
126 - if(!$next_title) return $err;
127 - $next_url = $next_title->getFullURL();
128220 }
 221+ }
129222
130 - $var_names = explode(" ", wfMsgForContent('proofreadpage_js_attributes') );
131 - $attributes = array();
132 - for($i=0; $i< count($var_names);$i++){
133 - $tag_pattern = "/\n\|".$var_names[$i]."=(.*?)\n/i";
134 - $var = 'proofreadPage'.$var_names[$i];
135 - if( preg_match( $tag_pattern, $text, $matches ) ) $attributes[$var] = $matches[1];
136 - else $attributes[$var] = '';
137 - }
 223+ $var_names = explode(" ", wfMsgForContent('proofreadpage_js_attributes') );
 224+ for($i=0; $i< count($var_names);$i++){
 225+ $tag_pattern = "/\n\|".$var_names[$i]."=(.*?)\n/i";
 226+ //$var = 'proofreadPage'.$var_names[$i];
 227+ $var = strtolower($var_names[$i]);
 228+ if( preg_match( $tag_pattern, $text, $matches ) ) $attributes[$var] = $matches[1];
 229+ else $attributes[$var] = '';
138230 }
139 - else {
140 - $attributes=array();
141 - }
 231+
142232
143 - return array( $index_url, $prev_url, $next_url, $page_num, $attributes );
 233+ return array( $prev_title, $next_title, $attributes );
144234
145235 }
146236
@@ -150,7 +240,7 @@
151241 *
152242 */
153243
154 -function wfPRParserOutput( &$out ) {
 244+function pr_beforePageDisplay( &$out ) {
155245 global $wgTitle, $wgJsMimeType, $wgScriptPath, $wgRequest, $wgProofreadPageVersion;
156246
157247 wfLoadExtensionMessages( 'ProofreadPage' );
@@ -163,13 +253,14 @@
164254
165255 $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
166256 if ( preg_match( "/^$page_namespace:(.*?)(\/([0-9]*)|)$/", $wgTitle->getPrefixedText(), $m ) ) {
167 - wfPRPreparePage( $out, $m, $isEdit );
 257+ if( !isset($wgTitle->pr_index_title) ) pr_load_index($wgTitle);
 258+ pr_preparePage( $out, $m, $isEdit );
168259 return true;
169260 }
170261
171262 $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
172263 if ( $isEdit && (preg_match( "/^$index_namespace:(.*?)(\/([0-9]*)|)$/", $wgTitle->getPrefixedText(), $m ) ) ) {
173 - wfPRPrepareIndex( $out );
 264+ pr_prepareIndex( $out );
174265 return true;
175266 }
176267
@@ -177,7 +268,7 @@
178269 }
179270
180271
181 -function wfPRPrepareIndex( $out ) {
 272+function pr_prepareIndex( $out ) {
182273 global $wgTitle, $wgJsMimeType, $wgScriptPath, $wgRequest, $wgProofreadPageVersion;
183274 $jsFile = htmlspecialchars( "$wgScriptPath/extensions/ProofreadPage/proofread_index.js?$wgProofreadPageVersion" );
184275
@@ -194,7 +285,11 @@
195286 }
196287
197288
198 -function wfPRPreparePage( $out, $m, $isEdit ) {
 289+
 290+
 291+
 292+
 293+function pr_preparePage( $out, $m, $isEdit ) {
199294 global $wgTitle, $wgJsMimeType, $wgScriptPath, $wgRequest, $wgProofreadPageVersion;
200295
201296 $imageTitle = Title::makeTitleSafe( NS_IMAGE, $m[1] );
@@ -202,7 +297,6 @@
203298 return true;
204299 }
205300
206 -
207301 $image = Image::newFromTitle( $imageTitle );
208302 if ( $image->exists() ) {
209303 $width = $image->getWidth();
@@ -228,7 +322,7 @@
229323 $thumbURL = '';
230324 }
231325
232 - list( $index_url, $prev_url, $next_url, $page_num, $attributes ) = wfPRNavigation( $image );
 326+ list( $index_url, $prev_url, $next_url, $attributes ) = pr_navigation( $image );
233327
234328 $jsFile = htmlspecialchars( "$wgScriptPath/extensions/ProofreadPage/proofread.js?$wgProofreadPageVersion" );
235329 $jsVars = array(
@@ -240,7 +334,6 @@
241335 'proofreadPageIndexURL' => $index_url,
242336 'proofreadPagePrevURL' => $prev_url,
243337 'proofreadPageNextURL' => $next_url,
244 - 'proofreadPageNum' => $page_num,
245338 ) + $attributes;
246339 $varScript = Skin::makeVariablesScript( $jsVars );
247340
@@ -273,9 +366,10 @@
274367
275368
276369 /**
277 - * Give quality colour codes to pages linked from an index page
 370+ * Return the quality colour codes to pages linked from an index page
 371+ * Update page counts in pr_index table
278372 */
279 -function wfPRLinkColours( $page_ids, &$colours ) {
 373+function pr_getLinkColours( $page_ids, &$colours ) {
280374 global $wgTitle;
281375
282376 if ( !isset( $wgTitle ) ) {
@@ -289,6 +383,9 @@
290384 return true;
291385 }
292386
 387+ //counters
 388+ $n = $n1 = $n2 = $n3 = $n4 = 0;
 389+
293390 $dbr = wfGetDB( DB_SLAVE );
294391 $catlinks = $dbr->tableName( 'categorylinks' );
295392 foreach ( $page_ids as $id => $pdbk ) {
@@ -298,6 +395,7 @@
299396 if ( preg_match( "/^$page_namespace:(.*?)$/", $pdbk ) ) {
300397
301398 $colours[$pdbk] = 'quality1';
 399+ $n++;
302400
303401 if ( !isset( $query ) ) {
304402 $query = "SELECT cl_from, cl_to FROM $catlinks WHERE cl_from IN(";
@@ -307,6 +405,7 @@
308406 $query .= $id;
309407 }
310408 }
 409+
311410 if ( isset( $query ) ) {
312411 $query .= ')';
313412 $res = $dbr->query( $query, __METHOD__ );
@@ -316,10 +415,22 @@
317416 $pdbk = $page_ids[$x->cl_from];
318417
319418 switch($x->cl_to){
320 - case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality1_category')): $colours[$pdbk] = 'quality1';break;
321 - case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality2_category')): $colours[$pdbk] = 'quality2';break;
322 - case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality3_category')): $colours[$pdbk] = 'quality3';break;
323 - case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality4_category')): $colours[$pdbk] = 'quality4';break;
 419+ case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality1_category')):
 420+ $colours[$pdbk] = 'quality1';
 421+ $n1++;
 422+ break;
 423+ case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality2_category')):
 424+ $colours[$pdbk] = 'quality2';
 425+ $n2++;
 426+ break;
 427+ case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality3_category')):
 428+ $colours[$pdbk] = 'quality3';
 429+ $n3++;
 430+ break;
 431+ case str_replace( ' ' , '_' , wfMsgForContent('proofreadpage_quality4_category')):
 432+ $colours[$pdbk] = 'quality4';
 433+ $n4++;
 434+ break;
324435 }
325436 }
326437 }
@@ -327,7 +438,7 @@
328439 return true;
329440 }
330441
331 -function wfPRImageMessage( &$imgpage , &$wgOut ) {
 442+function pr_imageMessage( &$imgpage , &$wgOut ) {
332443
333444 global $wgUser;
334445 $sk = $wgUser->getSkin();
@@ -377,8 +488,45 @@
378489
379490
380491
381 -function wfPRRenderPageList( $input, $args ) {
 492+function pr_renderIndexTag( $input, $args ) {
 493+ global $wgParser, $wgTitle;
382494
 495+ if( !isset($wgTitle->pr_index_title) ) pr_load_index($wgTitle);
 496+
 497+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
 498+
 499+ $name = $args['src'];
 500+ if( $name )
 501+ $index_title = Title::newFromText( "$index_namespace:$name" );
 502+ else
 503+ $index_title = Title::newFromText( $wgTitle->pr_index_title );
 504+
 505+ if( ! $index_title || ! $index_title->exists() ) return "error: no such index: $index_namespace:$name";
 506+
 507+ if($wgTitle->pr_index_title) $page_index = $wgTitle; else $page_index=NULL;
 508+
 509+ //here we must parse the index everytime we render the tag:
 510+ //it would be better to store the attributes in a table
 511+ //especially in the case of a 'special' page
 512+ list( $prev_title, $next_title, $attributes ) = pr_parse_index( $index_title, $page_index );
 513+
 514+ //first parse
 515+ $input = $wgParser->recursiveTagParse($input);
 516+
 517+ foreach($attributes as $key=>$val){
 518+ $input = str_replace( "{{{{$key}}}}", $val, $input );
 519+ }
 520+
 521+
 522+ $out = $wgParser->recursiveTagParse($input);
 523+
 524+ return $out;
 525+
 526+}
 527+
 528+
 529+function pr_renderPageList( $input, $args ) {
 530+
383531 global $wgUser, $wgTitle;
384532
385533 wfLoadExtensionMessages( 'ProofreadPage' );
@@ -429,7 +577,7 @@
430578 $colours[$pdbk] = 'known';
431579 $linkcolour_ids[$s->page_id] = $pdbk;
432580 }
433 - wfPRLinkColours( $linkcolour_ids, $colours );
 581+ pr_getLinkColours( $linkcolour_ids, $colours );
434582
435583 $sk = $wgUser->getSkin();
436584
@@ -479,15 +627,24 @@
480628
481629
482630 /* update coloured links in index pages */
483 -function wfPRSave( $article ) {
 631+function pr_articleSave( $article ) {
484632
485633 wfLoadExtensionMessages( 'ProofreadPage' );
486634 $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
 635+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
487636
488 - if( preg_match( "/^$page_namespace:(.*)$/", $article->mTitle->getPrefixedText() ) ) {
489 - $article->mTitle->touchLinks();
490 - $article->mTitle->purgeSquid();
 637+ $title = $article->mTitle;
 638+
 639+ if( preg_match( "/^$page_namespace:(.*)$/", $title->getPrefixedText() ) ) {
 640+
 641+ if( !isset($title->pr_index_title) ) pr_load_index($title);
 642+ if( $title->pr_index_title) {
 643+ $index_title = Title::makeTitleSafe( $index_namespace, $title->pr_index_title );
 644+ $index_title->invalidateCache();
 645+ }
491646 }
 647+
492648 return true;
493649
494650 }
 651+

Status & tagging log