r96870 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96869‎ | r96870 | r96871 >
Date:17:19, 12 September 2011
Author:catrope
Status:ok
Tags:
Comment:
RL2: Clean up duplicated code in GadgetHooks. More deduplication to come
Modified paths:
  • /branches/RL2/extensions/Gadgets/GadgetHooks.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/GadgetHooks.php
@@ -13,6 +13,20 @@
1414 */
1515
1616 class GadgetHooks {
 17+ /**
 18+ * Get the gadget ID from a title
 19+ * @param $title Title object
 20+ * @return string Gadget name or null if not a gadget definition page
 21+ */
 22+ public static function getIDFromTitle( Title $title ) {
 23+ $name = $title->getText();
 24+ if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) ) {
 25+ // Not a gadget definition page
 26+ return null;
 27+ }
 28+ // Trim .js from the page name to obtain the gadget ID
 29+ return substr( $name, 0, -3 );
 30+ }
1731
1832 /**
1933 * ArticleDeleteComplete hook handler.
@@ -23,15 +37,10 @@
2438 * @param $id Int: Page ID
2539 */
2640 public static function gadgetDefinitionDelete( $article, $user, $reason, $id ) {
27 - // FIXME: AARGH, duplication, refactor this
28 - $title = $article->getTitle();
29 - $name = $title->getText();
30 - // Check that the deletion is in the Gadget definition: namespace and that the name ends in .js
31 - if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) ) {
 41+ $id = self::getIDFromTitle( $article->getTitle() );
 42+ if ( !$id ) {
3243 return true;
3344 }
34 - // Trim .js from the page name to obtain the gadget id
35 - $id = substr( $name, 0, -3 );
3645
3746 $repo = new LocalGadgetRepo( array() );
3847 $repo->deleteGadget( $id );
@@ -55,15 +64,10 @@
5665 public static function gadgetDefinitionSave( $article, $user, $text, $summary, $isMinor,
5766 $isWatch, $section, $flags, $revision )
5867 {
59 - $title = $article->getTitle();
60 - $name = $title->getText();
61 - // Check that the edit is in the Gadget definition: namespace, that the name ends in .js
62 - // and that $revision isn't null (this happens for a no-op edit)
63 - if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) || !$revision ) {
 68+ $id = self::getIDFromTitle( $article->getTitle() );
 69+ if ( !$id ) {
6470 return true;
6571 }
66 - // Trim .js from the page name to obtain the gadget id
67 - $id = substr( $name, 0, -3 );
6872
6973 $previousRev = $revision->getPrevious();
7074 $prevTs = $previousRev instanceof Revision ? $previousRev->getTimestamp() : wfTimestampNow();
@@ -88,14 +92,10 @@
8993 * @param $comment String: Undeletion summary
9094 */
9195 public static function gadgetDefinitionUndelete( $title, $created, $comment ) {
92 - // FIXME: AARGH, duplication, refactor this
93 - $name = $title->getText();
94 - // Check that the deletion is in the Gadget definition: namespace and that the name ends in .js
95 - if ( $title->getNamespace() !== NS_GADGET_DEFINITION || !preg_match( '!\.js$!u', $name ) ) {
 96+ $id = self::getIDFromTitle( $title );
 97+ if ( !$id ) {
9698 return true;
9799 }
98 - // Trim .js from the page name to obtain the gadget id
99 - $id = substr( $name, 0, -3 );
100100
101101 // Check whether this undeletion changed the latest revision of the page, by comparing
102102 // the timestamp of the latest revision with the timestamp in the DB

Status & tagging log