Index: trunk/extensions/ProofreadPage/ProofreadPage.php |
— | — | @@ -43,7 +43,7 @@ |
44 | 44 | global $wgTitle; |
45 | 45 | $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' ); |
46 | 46 | $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' ); |
47 | | - $err = array( '', '', '', '', '' ); |
| 47 | + $err = array( '', '', '', '', array() ); |
48 | 48 | |
49 | 49 | $dbr = wfGetDB( DB_SLAVE ); |
50 | 50 | $result = $dbr->select( |
— | — | @@ -55,77 +55,86 @@ |
56 | 56 | 'pl_from=page_id' |
57 | 57 | ), |
58 | 58 | __METHOD__); |
| 59 | + |
| 60 | + $index_title = ''; |
59 | 61 | while( $x = $dbr->fetchObject( $result ) ) { |
60 | 62 | $ref_title = Title::makeTitle( $x->page_namespace, $x->page_title ); |
61 | 63 | if( preg_match( "/^$index_namespace:(.*)$/", $ref_title->getPrefixedText() ) ) { |
| 64 | + $index_title = $ref_title; |
62 | 65 | break; |
63 | 66 | } |
64 | 67 | } |
65 | 68 | $dbr->freeResult( $result ) ; |
66 | 69 | |
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 |
77 | 71 | |
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 ) ); |
86 | 78 | } |
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 = ''; |
88 | 95 | } |
89 | 96 | |
90 | | - $index_title = $ref_title; |
91 | 97 | $index_url = $index_title->getFullURL(); |
92 | | - $rev = Revision::newFromTitle( $index_title ); |
93 | | - $text = $rev->getText(); |
94 | 98 | |
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()) { |
97 | 101 | |
| 102 | + $rev = Revision::newFromTitle( $index_title ); |
| 103 | + $text = $rev->getText(); |
98 | 104 | |
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 ); |
107 | 107 | |
| 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 | + } |
108 | 126 | |
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] = ''; |
116 | 134 | } |
117 | 135 | } |
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(); |
122 | 138 | } |
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 = ''; |
130 | 139 | |
131 | 140 | return array( $index_url, $prev_url, $next_url, $page_num, $attributes ); |
132 | 141 | |