r104364 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104363‎ | r104364 | r104365 >
Date:21:09, 27 November 2011
Author:reedy
Status:deferred
Tags:
Comment:
Bring some of the DB code into this century

Invert if's to de-indent code

Leave FIXME comment about using global esk variables (which aren't globals) in loops

Still scared by the URL building, manual replacing spaces with _'s...
Modified paths:
  • /trunk/extensions/SharedHelpNamespace/SharedHelpNamespace.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SharedHelpNamespace/SharedHelpNamespace.php
@@ -46,41 +46,42 @@
4747 function wfSharedHelpNamespaceLoad( $article ) {
4848 global $wgTitle, $wgOut, $wgContLang, $wgSharedHelpNamespaceFetchingWikis, $wgLanguageCode, $wgDBname;
4949
50 - if ( $wgTitle->getNamespace() == NS_HELP ) {
 50+ if ( $wgTitle->getNamespace() != NS_HELP ) {
 51+ return false;
 52+ }
5153
52 - $replacewhitespace = str_replace( ' ', '_', $wgOut->getTitle() );
53 - $title = str_replace( $wgContLang->namespaceNames[NS_HELP].':', '', $replacewhitespace );
 54+ $replacewhitespace = str_replace( ' ', '_', $wgOut->getTitle() );
 55+ $title = str_replace( $wgContLang->namespaceNames[NS_HELP].':', '', $replacewhitespace );
5456
55 - foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
56 - foreach ( $urls as $wgSharedHelpNamespaceFetchingWiki ) {
57 - if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
58 - $dbr = wfGetDB( DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki );
59 - $page = $dbr->query( 'SELECT page_title, page_namespace, page_latest FROM page WHERE page_namespace = 12 AND page_title = '.$dbr->addQuotes($title) );
60 - $page = $dbr->fetchObject( $page );
61 - }
 57+ foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
 58+ foreach ( $urls as $wgSharedHelpNamespaceFetchingWiki ) {
 59+ if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
 60+ $dbr = wfGetDB( DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki );
 61+ $page = $dbr->query( 'SELECT page_title, page_namespace, page_latest FROM page WHERE page_namespace = 12 AND page_title = '.$dbr->addQuotes($title) );
 62+ $page = $dbr->fetchObject( $page );
6263 }
6364 }
64 - if ( !empty($page->page_title) ) {
65 - $rev = $dbr->select( 'revision',
66 - array( 'rev_id', 'rev_text_id' ),
67 - 'rev_id = '.$dbr->addQuotes($page->page_latest),
68 - __METHOD__ );
69 - $rev = $dbr->fetchObject( $rev );
70 - } else {
71 - return false;
72 - }
73 - $text = $dbr->select( 'text',
74 - array( 'old_id', 'old_text' ),
75 - 'old_id = '.$dbr->addQuotes($rev->rev_text_id),
76 - __METHOD__ );
77 - $text = $dbr->fetchObject( $text );
 65+ }
 66+ if ( !empty( $page->page_title ) ) {
 67+ $rev = $dbr->select( 'revision',
 68+ array( 'rev_id', 'rev_text_id' ),
 69+ array( 'rev_id' => $page->page_latest ),
 70+ __METHOD__
 71+ );
 72+ $rev = $dbr->fetchObject( $rev );
 73+ } else {
 74+ return false;
 75+ }
 76+ $text = $dbr->select( 'text',
 77+ array( 'old_id', 'old_text' ),
 78+ array( 'old_id' => $rev->rev_text_id ),
 79+ __METHOD__
 80+ );
 81+ $text = $dbr->fetchObject( $text );
7882
79 - if ( !empty($text->old_text) ) {
80 - echo $wgOut->addWikiText( $text->old_text );
81 - return true;
82 - } else {
83 - return false;
84 - }
 83+ if ( !empty( $text->old_text ) ) {
 84+ $wgOut->addWikiText( $text->old_text );
 85+ return true;
8586 } else {
8687 return false;
8788 }
@@ -94,34 +95,39 @@
9596 function wfSharedHelpNamespaceRedirectTalks( $article, $fields ) {
9697 global $wgTitle, $wgOut, $wgContLang, $wgSharedHelpNamespaceFetchingWikis, $wgLanguageCode, $wgDBname;
9798
98 - if ( $wgTitle->getNamespace() == NS_HELP_TALK ) {
 99+ if ( $wgTitle->getNamespace() != NS_HELP_TALK ) {
 100+ return false;
 101+ }
99102
100 - $replacewhitespace = str_replace( ' ', '_', $wgOut->getTitle() );
101 - $title = str_replace( $wgContLang->namespaceNames[NS_HELP_TALK].':', '', $replacewhitespace );
 103+ $replacewhitespace = str_replace( ' ', '_', $wgOut->getTitle() );
 104+ $title = str_replace( $wgContLang->namespaceNames[NS_HELP_TALK].':', '', $replacewhitespace );
102105
103 - foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
104 - foreach ( $urls as $url => $wgSharedHelpNamespaceFetchingWiki ) {
105 - if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
106 - $dbr = wfGetDB( DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki );
107 - $page = $dbr->query( 'SELECT page_title, page_namespace, page_latest FROM page WHERE page_namespace = 12 AND page_title = '.$dbr->addQuotes($title) );
108 - $page = $dbr->fetchObject( $page );
 106+ foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
 107+ // FIXME: don't use global esk variable names for non globals
 108+ foreach ( $urls as $url => $wgSharedHelpNamespaceFetchingWiki ) {
 109+ if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
 110+ $dbr = wfGetDB( DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki );
 111+ $page = $dbr->select(
 112+ 'page',
 113+ array( 'page_title', 'page_namespace', 'page_latest' ),
 114+ array( 'page_namespace' => 12, 'page_title' => $title ),
 115+ __METHOD__
 116+ );
 117+ $page = $dbr->fetchObject( $page );
 118+ }
 119+ if ( !empty( $page->page_title ) ) {
 120+ if ( $page->page_title == $title && !$wgTitle->exists() ) {
 121+ $sharedHelpRedirectTalk = Title::newFromText( $url . '/index.php?title=' . str_replace( ' ', '_', $wgOut->getTitle() ) );
 122+ $redirectTalkPage = $sharedHelpRedirectTalk->getFullText();
 123+ $wgOut->redirect( $redirectTalkPage );
 124+ return true;
 125+ } else {
 126+ return false;
109127 }
110 - if ( !empty($page->page_title) ) {
111 - if ( $page->page_title == $title && !$wgTitle->exists() ) {
112 - $sharedHelpRedirectTalk = Title::newFromText( $url . '/index.php?title=' . str_replace( ' ', '_', $wgOut->getTitle() ) );
113 - $redirectTalkPage = $sharedHelpRedirectTalk->getFullText();
114 - $wgOut->redirect( $redirectTalkPage );
115 - return true;
116 - } else {
117 - return false;
118 - }
119 - }
120128 }
121129 }
122 - return true;
123 - } else {
124 - return false;
125130 }
 131+ return true;
126132 }
127133
128134 /**
@@ -130,7 +136,7 @@
131137 * @param $text
132138 * @param $customAttribs
133139 * @param $query
134 - * @param $options
 140+ * @param $options array
135141 * @param $ret
136142 * @return bool
137143 */
@@ -169,19 +175,21 @@
170176 function wfSharedHelpNamespaceChangeEditSectionLink( $skin, $title, $section, $tooltip, $result, $lang = false ) {
171177 global $wgTitle, $wgSharedHelpNamespaceFetchingWikis, $wgLanguageCode, $wgDBname;
172178
173 - if ( $wgTitle->getNamespace() == NS_HELP ) {
174 - foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
175 - foreach ( $urls as $url => $wgSharedHelpNamespaceFetchingWiki ) {
176 - if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
177 - $result = '<span class="editsection">[<a href="'.$url.'/index.php?title='.str_replace( ' ', '_', $title ).'&amp;action=edit&amp;section='.$section.'" title="'.wfMsg( 'editsectionhint', $tooltip ).'">'.wfMsg( 'editsection' ).'</a>]</span>';
178 - }
 179+ if ( $wgTitle->getNamespace() != NS_HELP ) {
 180+ return false;
 181+ }
 182+ foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
 183+ // FIXME: don't use global esk variable names for non globals
 184+ foreach ( $urls as $url => $wgSharedHelpNamespaceFetchingWiki ) {
 185+ if ( $wgLanguageCode == "$language" && $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
 186+ // FIXME: $result is unused
 187+ $result = '<span class="editsection">[<a href="' . $url . '/index.php?title=' .
 188+ str_replace( ' ', '_', $title ) . '&amp;action=edit&amp;section=' . $section .
 189+ '" title="' . wfMsg( 'editsectionhint', $tooltip ) . '">' . wfMsg( 'editsection' ) . '</a>]</span>';
179190 }
180191 }
181 - return true;
182 - } else {
183 - return false;
184192 }
185 -
 193+ return true;
186194 }
187195
188196 /**
@@ -194,18 +202,23 @@
195203 function fnProtectSharedHelpNamespace( &$title, &$user, $action, &$result) {
196204 global $wgSharedHelpNamespaceFetchingWikis, $wgDBname;
197205
198 - foreach ( $wgSharedHelpNamespaceFetchingWikis as $language => $urls ) {
 206+ foreach ( $wgSharedHelpNamespaceFetchingWikis as $urls ) {
 207+ // FIXME: don't use global esk variable names for non globals
199208 foreach ( $urls as $url => $wgSharedHelpNamespaceFetchingWiki ) {
200 - // only protect Help pages on non-help-pages-fetching wikis
 209+ // only protect Help pages on non-help-pages-fetching wikis
201210 if( $wgDBname != $wgSharedHelpNamespaceFetchingWiki ) {
202211 // block actions 'edit' and 'create'
203212 if( $action != 'edit' && $action != 'create' ) {
204213 return true;
205214 }
206215
207 - $dbr = wfGetDB(DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki);
208 - $res = $dbr->query( 'SELECT page_title, page_namespace FROM page WHERE page_namespace = 12 AND page_title = '
209 - . $dbr->addQuotes(str_replace( ' ', '_', $title->getText())) );
 216+ $dbr = wfGetDB( DB_SLAVE, array(), $wgSharedHelpNamespaceFetchingWiki );
 217+ $res = $dbr->select(
 218+ 'page',
 219+ array( 'page_title', 'page_namespace', 'page_latest' ),
 220+ array( 'page_namespace' => 12, 'page_title' => str_replace( ' ', '_', $title->getText() ) ),
 221+ __METHOD__
 222+ );
210223
211224 if ( $dbr->numRows( $res ) < 1 ) {
212225 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r104444Merging with Reedy's several code changes. Thankscervidae15:37, 28 November 2011

Status & tagging log