r114412 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114411‎ | r114412 | r114413 >
Date:21:42, 21 March 2012
Author:skizzerz
Status:new
Tags:
Comment:
added ability to configure default flags, improved flags parsing, fixed error when viewing special pages, encapsulated code into a class so it doesn't pollute the global scope with functions, bump version to 3.1
Modified paths:
  • /trunk/extensions/EditSubpages/EditSubpages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EditSubpages/EditSubpages.php
@@ -16,19 +16,25 @@
1717 'descriptionmsg' => 'editsubpages-desc',
1818 '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>' ),
1919 'url' => 'https://www.mediawiki.org/wiki/Extension:EditSubpages',
20 - 'version' => '3.0',
 20+ 'version' => '3.1',
2121 );
2222
23 -$wgHooks['userCan'][] = 'EditSubpages';
 23+$wgHooks['userCan'][] = 'ExtEditSubpages::EditSubpages';
2424 $wgGroupPermissions['*']['edit'] = true;
2525 $wgGroupPermissions['*']['createpage'] = true;
2626 $wgGroupPermissions['*']['createtalk'] = true;
 27+$wgEditSubpagesDefaultFlags = '+scte-buinrw';
2728 $dir = dirname(__FILE__) . '/';
2829 $wgExtensionMessagesFiles['EditSubpages'] = $dir .'EditSubpages.i18n.php';
2930 $evEditSubpagesCache = array();
3031
31 -function EditSubpages( $title, $user, $action, $result ) {
 32+class ExtEditSubpages {
 33+
 34+public static function EditSubpages( $title, $user, $action, $result ) {
3235 global $evEditSubpagesCache;
 36+ if( $title->getNamespace() < 0 ) {
 37+ return true; //don't operate on "special" namespaces
 38+ }
3339 $pagename = $title->getText(); //name of page w/ spaces, not underscores
3440 if( !array_key_exists( 'pagename', $evEditSubpagesCache ) || $pagename != $evEditSubpagesCache['pagename'] ) {
3541 $ns = $title->getNsText(); //namespace
@@ -38,7 +44,7 @@
3945 } else {
4046 $nstalk = $title->getTalkNsText();
4147 }
42 - if($ns == '') {
 48+ if( $ns == '' ) {
4349 $text = $pagename;
4450 } else {
4551 $text = $ns . ":" . $pagename;
@@ -49,9 +55,9 @@
5056 $talktext = $pagename;
5157 }
5258 //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
5662 //cache the values so future checks on the same page take less time
5763 $evEditSubpagesCache = array(
5864 'pagename' => $pagename,
@@ -68,7 +74,15 @@
6975 if( strpos( $value, '*' ) === false || strpos( $value, '*' ) !== 0 ) {
7076 continue; // "*" doesn't start the line, so treat it as a comment (aka skip over it)
7177 }
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 );
7387 $value = trim( trim( trim( trim( $value ), "*[]" ) ), "*[]" );
7488 /* flags
7589 * s = unlock subpages
@@ -83,40 +97,22 @@
8498 * w = wildcard matching
8599 */
86100 $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] ) );
107103 }
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 );
111107 }
112 - if(!$found && $flags['t']) {
 108+ if( !$found && $flags['t'] ) {
113109 $newtitle = Title::newFromText( $pieces[0] );
114110 //make sure that it's a valid title
115111 if( $newtitle instanceOf Title && !$newtitle->isTalkPage() ) {
116112 $talk = $newtitle->getTalkPage();
117113 $talkpage = $talk->getPrefixedText();
118 - $found = checkPage( $talkpage, $evEditSubpagesCache['talktext'], $flags );
 114+ $found = self::checkPage( $talkpage, $evEditSubpagesCache['talktext'], $flags );
119115 if( !$found ) {
120 - $found = checkPage( $talkpage, $evEditSubpagesCache['text'], $flags );
 116+ $found = self::checkPage( $talkpage, $evEditSubpagesCache['text'], $flags );
121117 }
122118 }
123119 }
@@ -147,7 +143,33 @@
148144 return true;
149145 }
150146
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 ) {
152174 if( $flags['w'] && !$flags['r'] ) {
153175 $flags['r'] = 1;
154176 $page = preg_quote( $page, '/' );
@@ -183,3 +205,5 @@
184206 }
185207 return false;
186208 }
 209+
 210+}
\ No newline at end of file