Index: branches/RL2/extensions/Gadgets/GadgetHooks.php |
— | — | @@ -13,6 +13,20 @@ |
14 | 14 | */ |
15 | 15 | |
16 | 16 | 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 | + } |
17 | 31 | |
18 | 32 | /** |
19 | 33 | * ArticleDeleteComplete hook handler. |
— | — | @@ -23,15 +37,10 @@ |
24 | 38 | * @param $id Int: Page ID |
25 | 39 | */ |
26 | 40 | 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 ) { |
32 | 43 | return true; |
33 | 44 | } |
34 | | - // Trim .js from the page name to obtain the gadget id |
35 | | - $id = substr( $name, 0, -3 ); |
36 | 45 | |
37 | 46 | $repo = new LocalGadgetRepo( array() ); |
38 | 47 | $repo->deleteGadget( $id ); |
— | — | @@ -55,15 +64,10 @@ |
56 | 65 | public static function gadgetDefinitionSave( $article, $user, $text, $summary, $isMinor, |
57 | 66 | $isWatch, $section, $flags, $revision ) |
58 | 67 | { |
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 ) { |
64 | 70 | return true; |
65 | 71 | } |
66 | | - // Trim .js from the page name to obtain the gadget id |
67 | | - $id = substr( $name, 0, -3 ); |
68 | 72 | |
69 | 73 | $previousRev = $revision->getPrevious(); |
70 | 74 | $prevTs = $previousRev instanceof Revision ? $previousRev->getTimestamp() : wfTimestampNow(); |
— | — | @@ -88,14 +92,10 @@ |
89 | 93 | * @param $comment String: Undeletion summary |
90 | 94 | */ |
91 | 95 | 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 ) { |
96 | 98 | return true; |
97 | 99 | } |
98 | | - // Trim .js from the page name to obtain the gadget id |
99 | | - $id = substr( $name, 0, -3 ); |
100 | 100 | |
101 | 101 | // Check whether this undeletion changed the latest revision of the page, by comparing |
102 | 102 | // the timestamp of the latest revision with the timestamp in the DB |