r86813 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86812‎ | r86813 | r86814 >
Date:11:41, 24 April 2011
Author:happy-melon
Status:resolved (Comments)
Tags:
Comment:
Introduce a CaptchaStore abstract class that CaptchaSessionStore and CaptchaCacheStore can extend. Some tidying, documentation and type hinting.
Modified paths:
  • /trunk/extensions/ConfirmEdit/Captcha.php (modified) (history)
  • /trunk/extensions/ConfirmEdit/CaptchaStore.php (modified) (history)
  • /trunk/extensions/ConfirmEdit/ConfirmEdit.php (modified) (history)
  • /trunk/extensions/ConfirmEdit/ConfirmEditHooks.php (modified) (history)
  • /trunk/extensions/ConfirmEdit/FancyCaptcha.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ConfirmEdit/Captcha.php
@@ -1,9 +1,19 @@
22 <?php
33
44 class SimpleCaptcha {
 5+
 6+ /**
 7+ * @var CaptchaStore
 8+ */
 9+ protected $storage;
 10+
511 function __construct() {
612 global $wgCaptchaStorageClass;
7 - $this->storage = new $wgCaptchaStorageClass;
 13+ if( in_array( 'CaptchaStore', class_implements( $wgCaptchaStorageClass ) ) ) {
 14+ $this->storage = new $wgCaptchaStorageClass;
 15+ } else {
 16+ throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
 17+ }
818 }
919
1020 function getCaptcha() {
@@ -81,7 +91,7 @@
8292 /**
8393 * Inject whazawhoo
8494 * @fixme if multiple thingies insert a header, could break
85 - * @param HTMLForm
 95+ * @param $form HTMLForm
8696 * @return bool true to keep running callbacks
8797 */
8898 function injectEmailUser( &$form ) {
@@ -103,7 +113,7 @@
104114 /**
105115 * Inject whazawhoo
106116 * @fixme if multiple thingies insert a header, could break
107 - * @param SimpleTemplate $template
 117+ * @param QuickTemplate $template
108118 * @return bool true to keep running callbacks
109119 */
110120 function injectUserCreate( &$template ) {
@@ -126,7 +136,7 @@
127137 * Inject a captcha into the user login form after a failed
128138 * password attempt as a speedbump for mass attacks.
129139 * @fixme if multiple thingies insert a header, could break
130 - * @param SimpleTemplate $template
 140+ * @param $template QuickTemplate
131141 * @return bool true to keep running callbacks
132142 */
133143 function injectUserLogin( &$template ) {
@@ -410,6 +420,8 @@
411421
412422 /**
413423 * Load external links from the externallinks table
 424+ * @param $title Title
 425+ * @return Array
414426 */
415427 function getLinksFromTracker( $title ) {
416428 $dbr = wfGetDB( DB_SLAVE );
Index: trunk/extensions/ConfirmEdit/ConfirmEdit.php
@@ -202,6 +202,7 @@
203203
204204 $wgAutoloadClasses['ConfirmEditHooks'] = "$wgConfirmEditIP/ConfirmEditHooks.php";
205205 $wgAutoloadClasses['SimpleCaptcha']= "$wgConfirmEditIP/Captcha.php";
 206+$wgAutoloadClasses['CaptchaStore']= "$wgConfirmEditIP/CaptchaStore.php";
206207 $wgAutoloadClasses['CaptchaSessionStore']= "$wgConfirmEditIP/CaptchaStore.php";
207208 $wgAutoloadClasses['CaptchaCacheStore']= "$wgConfirmEditIP/CaptchaStore.php";
208209 $wgAutoloadClasses['CaptchaSpecialPage'] = "$wgConfirmEditIP/ConfirmEditHooks.php";
Index: trunk/extensions/ConfirmEdit/ConfirmEditHooks.php
@@ -1,6 +1,12 @@
22 <?php
33
44 class ConfirmEditHooks {
 5+
 6+ /**
 7+ * Get the global Captcha instance
 8+ *
 9+ * @return Captcha
 10+ */
511 static function getInstance() {
612 global $wgCaptcha, $wgCaptchaClass;
713 static $done = false;
Index: trunk/extensions/ConfirmEdit/CaptchaStore.php
@@ -1,6 +1,64 @@
22 <?php
3 -class CaptchaSessionStore {
43
 4+abstract class CaptchaStore {
 5+ /**
 6+ * Store the correct answer for a given captcha
 7+ * @param $index String
 8+ * @param $info String the captcha result
 9+ */
 10+ public abstract function store( $index, $info );
 11+
 12+ /**
 13+ * Retrieve the answer for a given captcha
 14+ * @param $index String
 15+ * @return String
 16+ */
 17+ public abstract function retrieve( $index );
 18+
 19+ /**
 20+ * Delete a result once the captcha has been used, so it cannot be reused
 21+ * @param $index
 22+ */
 23+ public abstract function clear( $index );
 24+
 25+ /**
 26+ * Whether this type of CaptchaStore needs cookies
 27+ * @return Bool
 28+ */
 29+ public abstract function cookiesNeeded();
 30+
 31+ /**
 32+ * The singleton instance
 33+ * @var CaptchaStore
 34+ */
 35+ private static $instance;
 36+
 37+ /**
 38+ * Get somewhere to store captcha data that will persist between requests
 39+ *
 40+ * @throws MWException
 41+ * @return CaptchaStore
 42+ */
 43+ public final static function get() {
 44+ if( !self::$instance instanceof self ){
 45+ global $wgCaptchaStorageClass;
 46+ if( in_array( 'CaptchaStore', class_implements( $wgCaptchaStorageClass ) ) ) {
 47+ self::$instance = new $wgCaptchaStorageClass;
 48+ } else {
 49+ throw new MWException( "Invalid CaptchaStore class $wgCaptchaStorageClass" );
 50+ }
 51+ }
 52+ return self::$instance;
 53+ }
 54+
 55+ /**
 56+ * Protected constructor: no creating instances except through the factory method above
 57+ */
 58+ protected function __construct(){}
 59+}
 60+
 61+class CaptchaSessionStore extends CaptchaStore {
 62+
563 function store( $index, $info ) {
664 $_SESSION['captcha' . $info['index']] = $info;
765 }
@@ -22,7 +80,7 @@
2381 }
2482 }
2583
26 -class CaptchaCacheStore {
 84+class CaptchaCacheStore extends CaptchaStore {
2785
2886 function store( $index, $info ) {
2987 global $wgMemc, $wgCaptchaSessionExpiration;
Index: trunk/extensions/ConfirmEdit/FancyCaptcha.class.php
@@ -44,7 +44,7 @@
4545 function getForm() {
4646 $info = $this->pickImage();
4747 if ( !$info ) {
48 - die( "out of captcha images; this shouldn't happen" );
 48+ throw new MWException( "Ran out of captcha images" );
4949 }
5050
5151 // Generate a random key for use of this captcha image in this session.

Follow-up revisions

RevisionCommit summaryAuthorDate
r86820Follow-up r86813: fix exceptions seen on TWN. Ironically, I changed the Capt...happy-melon16:49, 24 April 2011
r86825File missing from r86820. Actually these changes should have been committed ...happy-melon17:33, 24 April 2011

Comments

#Comment by Raymond (talk | contribs)   15:37, 24 April 2011

Seen on translatewiki:

[24-Apr-2011 15:34:37] /w/i.php?title=MediaWiki_talk:Mwe-upwiz-feedback-submit/ml&action=submit: Exception: Invalid CaptchaStore class CaptchaSessionStore
#0 /www/w/extensions/ConfirmEdit/ConfirmEditHooks.php(15): SimpleCaptcha->__construct()
#1 /www/w/extensions/ConfirmEdit/ConfirmEditHooks.php(25): ConfirmEditHooks::getInstance()
#2 [internal function]: ConfirmEditHooks::confirmEditMerged(Object(EditPage), 'While saving tr...', '', 'New thread: Err...')
#3 /www/w/includes/Hooks.php(235): call_user_func_array('ConfirmEditHook...', Array)
#4 /www/w/includes/Hooks.php(38): Hooks::run('EditFilterMerge...', Array)
#5 /www/w/includes/EditPage.php(961): wfRunHooks('EditFilterMerge...', Array)
#6 /www/w/includes/EditPage.php(2746): EditPage->internalAttemptSave(false, false)
#7 /www/w/includes/EditPage.php(457): EditPage->attemptSave()
#8 /www/w/extensions/LiquidThreads/classes/View.php(477): EditPage->edit()
#9 /www/w/extensions/LiquidThreads/pages/TalkpageView.php(315): LqtView->showNewThreadForm(Object(Article))
#10 /www/w/extensions/LiquidThreads/classes/Dispatch.php(54): TalkpageView->show()
#11 /www/w/extensions/LiquidThreads/classes/Dispatch.php(179): LqtDispatch::talkpageMain(Object(OutputPage), Object(Article), Object(Title), Object(User), Object(WebRequest))
#12 [internal function]: LqtDispatch::tryPage(Object(OutputPage), Object(Article), Object(Title), Object(User), Object(WebRequest), Object(MediaWiki))
#13 /www/w/includes/Hooks.php(235): call_user_func_array('LqtDispatch::tr...', Array)
#14 /www/w/includes/Hooks.php(38): Hooks::run('MediaWikiPerfor...', Array)
#15 /www/w/includes/Wiki.php(468): wfRunHooks('MediaWikiPerfor...', Array)
#16 /www/w/includes/Wiki.php(104): MediaWiki->performAction(Object(Article))
#17 /www/w/index.php(145): MediaWiki->performRequestForTitle(Object(Article))

Status & tagging log