r30267 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r30266‎ | r30267 | r30268 >
Date:16:47, 29 January 2008
Author:thomasv
Status:old
Tags:
Comment:
fix for pages with no index
Modified paths:
  • /trunk/extensions/ProofreadPage/ProofreadPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/ProofreadPage.php
@@ -43,7 +43,7 @@
4444 global $wgTitle;
4545 $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
4646 $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
47 - $err = array( '', '', '', '', '' );
 47+ $err = array( '', '', '', '', array() );
4848
4949 $dbr = wfGetDB( DB_SLAVE );
5050 $result = $dbr->select(
@@ -55,77 +55,86 @@
5656 'pl_from=page_id'
5757 ),
5858 __METHOD__);
 59+
 60+ $index_title = '';
5961 while( $x = $dbr->fetchObject( $result ) ) {
6062 $ref_title = Title::makeTitle( $x->page_namespace, $x->page_title );
6163 if( preg_match( "/^$index_namespace:(.*)$/", $ref_title->getPrefixedText() ) ) {
 64+ $index_title = $ref_title;
6265 break;
6366 }
6467 }
6568 $dbr->freeResult( $result ) ;
6669
67 - if( !$x ) { // there is no index page; but maybe we can create one (for multipage documents like .pdf and .djvu)
68 - if( $image->exists() && $image->isMultiPage() ) {
69 - $pagenr = 1;
70 - $parts = explode( '/', $wgTitle->getText() );
71 - if( count( $parts ) > 1 ) {
72 - $pagenr = intval( array_pop( $parts ) );
73 - }
74 - $count = $image->pageCount();
75 - if( $pagenr < 1 || $pagenr > $count || $count == 1 )
76 - return $err;
 70+ if( !$index_title ) { // there is no index, or no page list in the index
7771
78 - $name = $image->getTitle()->getText();
79 - $prev_name = "$page_namespace:$name/" . ( $pagenr - 1 );
80 - $next_name = "$page_namespace:$name/" . ( $pagenr + 1 );
81 - $index_name = "$index_namespace:$name";
82 - $prev_url = ( $pagenr == 1 ) ? '' : Title::newFromText( $prev_name )->getFullURL();
83 - $next_url = ( $pagenr == $count ) ? '' : Title::newFromText( $next_name )->getFullURL();
84 - $index_url = Title::newFromText( $index_name )->getFullURL();
85 - return array( $index_url, $prev_url, $next_url, '', '' );
 72+ if( ! ($image->exists() && $image->isMultiPage() ) ) return $err;
 73+
 74+ $pagenr = 1;
 75+ $parts = explode( '/', $wgTitle->getText() );
 76+ if( count( $parts ) > 1 ) {
 77+ $pagenr = intval( array_pop( $parts ) );
8678 }
87 - return $err;
 79+ $count = $image->pageCount();
 80+ if( $pagenr < 1 || $pagenr > $count || $count == 1 )
 81+ return $err;
 82+ $name = $image->getTitle()->getText();
 83+ $index_name = "$index_namespace:$name";
 84+ $prev_name = "$page_namespace:$name/" . ( $pagenr - 1 );
 85+ $next_name = "$page_namespace:$name/" . ( $pagenr + 1 );
 86+ $index_title = Title::newFromText( $index_name );
 87+ $prev_url = ( $pagenr == 1 ) ? '' : Title::newFromText( $prev_name )->getFullURL();
 88+ $next_url = ( $pagenr == $count ) ? '' : Title::newFromText( $next_name )->getFullURL();
 89+ $page_num = $page_enr;
 90+ }
 91+ else {
 92+ $page_num='';
 93+ $prev_url = '';
 94+ $next_url = '';
8895 }
8996
90 - $index_title = $ref_title;
9197 $index_url = $index_title->getFullURL();
92 - $rev = Revision::newFromTitle( $index_title );
93 - $text = $rev->getText();
9498
95 - $tag_pattern = "/\[\[($page_namespace:.*?)(\|(.*?)|)\]\]/i";
96 - preg_match_all( $tag_pattern, $text, $links, PREG_PATTERN_ORDER );
 99+ //if the index page exists, read metadata
 100+ if( $index_title->exists()) {
97101
 102+ $rev = Revision::newFromTitle( $index_title );
 103+ $text = $rev->getText();
98104
99 - $var_names = explode(" ", wfMsgForContent('proofreadpage_js_attributes') );
100 - $attributes = array();
101 - for($i=0; $i< count($var_names);$i++){
102 - $tag_pattern = "/\n\|".$var_names[$i]."=(.*?)\n/i";
103 - $var = 'proofreadPage'.$var_names[$i];
104 - if( preg_match( $tag_pattern, $text, $matches ) ) $attributes[$var] = $matches[1];
105 - else $attributes[$var] = '';
106 - }
 105+ $tag_pattern = "/\[\[($page_namespace:.*?)(\|(.*?)|)\]\]/i";
 106+ preg_match_all( $tag_pattern, $text, $links, PREG_PATTERN_ORDER );
107107
 108+ for( $i=0; $i<count( $links[1] ); $i++) {
 109+ $a_title = Title::newFromText( $links[1][$i] );
 110+ if(!$a_title) continue;
 111+ if( $a_title->getPrefixedText() == $wgTitle->getPrefixedText() ) {
 112+ $page_num = $links[3][$i];
 113+ break;
 114+ }
 115+ }
 116+ if( ($i>0) && ($i<count($links[1])) ){
 117+ $prev_title = Title::newFromText( $links[1][$i-1] );
 118+ if(!$prev_title) return $err;
 119+ $prev_url = $prev_title->getFullURL();
 120+ }
 121+ if( ($i>=0) && ($i+1<count($links[1])) ){
 122+ $next_title = Title::newFromText( $links[1][$i+1] );
 123+ if(!$next_title) return $err;
 124+ $next_url = $next_title->getFullURL();
 125+ }
108126
109 - $page_num='';
110 - for( $i=0; $i<count( $links[1] ); $i++) {
111 - $a_title = Title::newFromText( $links[1][$i] );
112 - if(!$a_title) continue;
113 - if( $a_title->getPrefixedText() == $wgTitle->getPrefixedText() ) {
114 - $page_num = $links[3][$i];
115 - break;
 127+ $var_names = explode(" ", wfMsgForContent('proofreadpage_js_attributes') );
 128+ $attributes = array();
 129+ for($i=0; $i< count($var_names);$i++){
 130+ $tag_pattern = "/\n\|".$var_names[$i]."=(.*?)\n/i";
 131+ $var = 'proofreadPage'.$var_names[$i];
 132+ if( preg_match( $tag_pattern, $text, $matches ) ) $attributes[$var] = $matches[1];
 133+ else $attributes[$var] = '';
116134 }
117135 }
118 - if( ($i>0) && ($i<count($links[1])) ){
119 - $prev_title = Title::newFromText( $links[1][$i-1] );
120 - if(!$prev_title) return $err;
121 - $prev_url = $prev_title->getFullURL();
 136+ else {
 137+ $attributes=array();
122138 }
123 - else $prev_url = '';
124 - if( ($i>=0) && ($i+1<count($links[1])) ){
125 - $next_title = Title::newFromText( $links[1][$i+1] );
126 - if(!$next_title) return $err;
127 - $next_url = $next_title->getFullURL();
128 - }
129 - else $next_url = '';
130139
131140 return array( $index_url, $prev_url, $next_url, $page_num, $attributes );
132141

Status & tagging log