Index: trunk/extensions/EditSubpages/EditSubpages.php |
— | — | @@ -16,19 +16,25 @@ |
17 | 17 | 'descriptionmsg' => 'editsubpages-desc', |
18 | 18 | 'author' => array( '<span class="plainlinks\>[http://strategywiki.org/wiki/User:Ryan_Schmidt Ryan Schmidt]</span>', '<span class="plainlinks">[http://strategywiki.org/wiki/User:Prod Prod]</span>' ), |
19 | 19 | 'url' => 'https://www.mediawiki.org/wiki/Extension:EditSubpages', |
20 | | - 'version' => '3.0', |
| 20 | + 'version' => '3.1', |
21 | 21 | ); |
22 | 22 | |
23 | | -$wgHooks['userCan'][] = 'EditSubpages'; |
| 23 | +$wgHooks['userCan'][] = 'ExtEditSubpages::EditSubpages'; |
24 | 24 | $wgGroupPermissions['*']['edit'] = true; |
25 | 25 | $wgGroupPermissions['*']['createpage'] = true; |
26 | 26 | $wgGroupPermissions['*']['createtalk'] = true; |
| 27 | +$wgEditSubpagesDefaultFlags = '+scte-buinrw'; |
27 | 28 | $dir = dirname(__FILE__) . '/'; |
28 | 29 | $wgExtensionMessagesFiles['EditSubpages'] = $dir .'EditSubpages.i18n.php'; |
29 | 30 | $evEditSubpagesCache = array(); |
30 | 31 | |
31 | | -function EditSubpages( $title, $user, $action, $result ) { |
| 32 | +class ExtEditSubpages { |
| 33 | + |
| 34 | +public static function EditSubpages( $title, $user, $action, $result ) { |
32 | 35 | global $evEditSubpagesCache; |
| 36 | + if( $title->getNamespace() < 0 ) { |
| 37 | + return true; //don't operate on "special" namespaces |
| 38 | + } |
33 | 39 | $pagename = $title->getText(); //name of page w/ spaces, not underscores |
34 | 40 | if( !array_key_exists( 'pagename', $evEditSubpagesCache ) || $pagename != $evEditSubpagesCache['pagename'] ) { |
35 | 41 | $ns = $title->getNsText(); //namespace |
— | — | @@ -38,7 +44,7 @@ |
39 | 45 | } else { |
40 | 46 | $nstalk = $title->getTalkNsText(); |
41 | 47 | } |
42 | | - if($ns == '') { |
| 48 | + if( $ns == '' ) { |
43 | 49 | $text = $pagename; |
44 | 50 | } else { |
45 | 51 | $text = $ns . ":" . $pagename; |
— | — | @@ -49,9 +55,9 @@ |
50 | 56 | $talktext = $pagename; |
51 | 57 | } |
52 | 58 | //underscores -> spaces |
53 | | - $ns = str_replace('_', ' ', $ns); |
54 | | - $nstalk = str_replace('_', ' ', $nstalk); |
55 | | - $pages = explode ("\n", wfMsg ('unlockedpages')); //grabs MediaWiki:Unlockedpages |
| 59 | + $ns = str_replace( '_', ' ', $ns ); |
| 60 | + $nstalk = str_replace( '_', ' ', $nstalk ); |
| 61 | + $pages = explode( "\n", wfMsg( 'unlockedpages' ) ); //grabs MediaWiki:Unlockedpages |
56 | 62 | //cache the values so future checks on the same page take less time |
57 | 63 | $evEditSubpagesCache = array( |
58 | 64 | 'pagename' => $pagename, |
— | — | @@ -68,7 +74,15 @@ |
69 | 75 | if( strpos( $value, '*' ) === false || strpos( $value, '*' ) !== 0 ) { |
70 | 76 | continue; // "*" doesn't start the line, so treat it as a comment (aka skip over it) |
71 | 77 | } |
72 | | - $flags = array( 's' => 1, 'c' => 1, 't' => 1, 'e' => 1, 'b' => 0, 'u' => 0, 'i' => 0, 'n' => 0, 'r' => 0, 'w' => 0 ); //default flags |
| 78 | + global $wgEditSubpagesDefaultFlags; |
| 79 | + if( !is_array( $wgEditSubpagesDefaultFlags ) ) { |
| 80 | + $config_flags = self::parseFlags( $wgEditSubpagesDefaultFlags ); |
| 81 | + } else { |
| 82 | + $config_flags = $wgEditSubpagesDefaultFlags; |
| 83 | + } |
| 84 | + //also hardcode the default flags just in case they are not set in $config_flags |
| 85 | + $default_flags = array( 's' => 1, 'c' => 1, 't' => 1, 'e' => 1, 'b' => 0, 'u' => 0, 'i' => 0, 'n' => 0, 'r' => 0, 'w' => 0 ); |
| 86 | + $flags = array_merge( $default_flags, $config_flags ); |
73 | 87 | $value = trim( trim( trim( trim( $value ), "*[]" ) ), "*[]" ); |
74 | 88 | /* flags |
75 | 89 | * s = unlock subpages |
— | — | @@ -83,40 +97,22 @@ |
84 | 98 | * w = wildcard matching |
85 | 99 | */ |
86 | 100 | $pieces = explode( '|', $value, 3 ); |
87 | | - if( isset( $pieces[1] ) && strpos( $pieces[1], '+' ) === 0 ) { |
88 | | - //flag parsing |
89 | | - $flaglist1 = explode( '+', $pieces[1], 2 ); |
90 | | - if( isset( $flaglist1[1] ) ) { |
91 | | - $flaglist2 = explode( '-', $flaglist1[1], 2 ); |
92 | | - } else { |
93 | | - $flaglist2 = explode( '-', $pieces[1], 2 ); |
94 | | - } |
95 | | - $flagpos = str_split( $flaglist2[0] ); |
96 | | - if( isset( $flaglist2[1] ) ) { |
97 | | - $flagneg = str_split( $flaglist2[1] ); |
98 | | - } else { |
99 | | - $flagneg = array( '' ); |
100 | | - } |
101 | | - foreach( $flagpos as $flag ) { |
102 | | - $flags[$flag] = 1; |
103 | | - } |
104 | | - foreach( $flagneg as $flag ) { |
105 | | - $flags[$flag] = 0; |
106 | | - } |
| 101 | + if( isset( $pieces[1] ) ) { |
| 102 | + $flags = array_merge( $flags, self::parseFlags( $pieces[1] ) ); |
107 | 103 | } |
108 | | - $found = checkPage( $pieces[0], $evEditSubpagesCache['text'], $flags ); |
109 | | - if(!$found && $flags['n']) { |
110 | | - $found = checkPage( $pieces[0], $evEditSubpagesCache['pagename'], $flags ); |
| 104 | + $found = self::checkPage( $pieces[0], $evEditSubpagesCache['text'], $flags ); |
| 105 | + if( !$found && $flags['n'] ) { |
| 106 | + $found = self::checkPage( $pieces[0], $evEditSubpagesCache['pagename'], $flags ); |
111 | 107 | } |
112 | | - if(!$found && $flags['t']) { |
| 108 | + if( !$found && $flags['t'] ) { |
113 | 109 | $newtitle = Title::newFromText( $pieces[0] ); |
114 | 110 | //make sure that it's a valid title |
115 | 111 | if( $newtitle instanceOf Title && !$newtitle->isTalkPage() ) { |
116 | 112 | $talk = $newtitle->getTalkPage(); |
117 | 113 | $talkpage = $talk->getPrefixedText(); |
118 | | - $found = checkPage( $talkpage, $evEditSubpagesCache['talktext'], $flags ); |
| 114 | + $found = self::checkPage( $talkpage, $evEditSubpagesCache['talktext'], $flags ); |
119 | 115 | if( !$found ) { |
120 | | - $found = checkPage( $talkpage, $evEditSubpagesCache['text'], $flags ); |
| 116 | + $found = self::checkPage( $talkpage, $evEditSubpagesCache['text'], $flags ); |
121 | 117 | } |
122 | 118 | } |
123 | 119 | } |
— | — | @@ -147,7 +143,33 @@ |
148 | 144 | return true; |
149 | 145 | } |
150 | 146 | |
151 | | -function checkPage( $page, $check, $flags ) { |
| 147 | +/** |
| 148 | + * Parses a string of flags in the form +blah-blah (or -blah+blah, or +b+l+a+h-b-l-a-h, etc.) into an array |
| 149 | + * If a flag is encountered multiple times, the - will override the +, regardless of what position it was in originally |
| 150 | + * If no + or - prefixes a flag, it assumes that it is following the last seen + or -, if it is at the beginning, + is implied |
| 151 | + * @param $flags String of flags in +- format |
| 152 | + * @return array of flags with the flag letter as the key and boolean true or false as the value |
| 153 | + */ |
| 154 | +protected static function parseFlags( $flags_string = '' ) { |
| 155 | + $flags = array( |
| 156 | + '+' => array(), |
| 157 | + '-' => array() |
| 158 | + ); |
| 159 | + $type = '+'; |
| 160 | + $strflags = str_split( $flags_string ); |
| 161 | + foreach( $strflags as $c ) { |
| 162 | + if( $c == '+' ) { |
| 163 | + $type = '+'; |
| 164 | + } elseif( $c == '-' ) { |
| 165 | + $type = '-'; |
| 166 | + } else { |
| 167 | + $flags[$type][$c] = ( $type == '+' ) ? true : false; |
| 168 | + } |
| 169 | + } |
| 170 | + return array_merge( $flags['+'], $flags['-'] ); |
| 171 | +} |
| 172 | + |
| 173 | +protected static function checkPage( $page, $check, $flags ) { |
152 | 174 | if( $flags['w'] && !$flags['r'] ) { |
153 | 175 | $flags['r'] = 1; |
154 | 176 | $page = preg_quote( $page, '/' ); |
— | — | @@ -183,3 +205,5 @@ |
184 | 206 | } |
185 | 207 | return false; |
186 | 208 | } |
| 209 | + |
| 210 | +} |
\ No newline at end of file |