Index: trunk/extensions/Drafts/Drafts.classes.php |
— | — | @@ -10,6 +10,11 @@ |
11 | 11 | |
12 | 12 | /* Static Functions */ |
13 | 13 | |
| 14 | + private static function getDraftAgeCutoff() { |
| 15 | + global $egDraftsLifeSpan; |
| 16 | + return wfTimestamp( TS_UNIX ) - ( $egDraftsLifeSpan * 60 * 60 * 24 ); |
| 17 | + } |
| 18 | + |
14 | 19 | /** |
15 | 20 | * Counts the number of existing drafts for a specific user |
16 | 21 | * @return Number of drafts which match condition parameters |
— | — | @@ -17,16 +22,18 @@ |
18 | 23 | * @param integer $userID[optional] ID of user, defaults to current user |
19 | 24 | */ |
20 | 25 | public static function num( |
21 | | - &$title = null, |
| 26 | + $title = null, |
22 | 27 | $userID = null |
23 | 28 | ) { |
24 | 29 | global $wgUser; |
25 | | - // Removes expired drafts for a more accurate count |
26 | | - self::clean(); |
27 | 30 | // Get database connection |
28 | 31 | $dbr = wfGetDB( DB_SLAVE ); |
29 | 32 | // Builds where clause |
30 | | - $where = array(); |
| 33 | + $where = array( |
| 34 | + 'draft_savetime > ' . $dbr->addQuotes( |
| 35 | + $dbr->timestamp( self::getDraftAgeCutoff() ) |
| 36 | + ) |
| 37 | + ); |
31 | 38 | // Checks if a specific title was given |
32 | 39 | if ( $title !== null ) { |
33 | 40 | // Adds specific title to conditions |
— | — | @@ -50,19 +57,23 @@ |
51 | 58 | * by $wgDraftsLifeSpan |
52 | 59 | */ |
53 | 60 | public static function clean() { |
54 | | - global $egDraftsLifeSpan; |
55 | | - // Get database connection |
56 | | - $dbw = wfGetDB( DB_MASTER ); |
57 | | - // Sets cuttoff as age longer than $wgDraftsLifeSpan days old |
58 | | - $cutoff = wfTimestamp( TS_UNIX ) - ( $egDraftsLifeSpan * 60 * 60 * 24 ); |
59 | | - // Removes expired drafts from database |
60 | | - $dbw->delete( 'drafts', |
61 | | - array( |
62 | | - 'draft_savetime < ' . |
63 | | - $dbw->addQuotes( $dbw->timestamp( $cutoff ) ) |
64 | | - ), |
65 | | - __METHOD__ |
66 | | - ); |
| 61 | + global $egDraftsCleanRatio; |
| 62 | + |
| 63 | + // Only perform this action a fraction of the time |
| 64 | + if ( rand( 0, $egDraftsCleanRatio ) == 0 ) { |
| 65 | + // Get database connection |
| 66 | + $dbw = wfGetDB( DB_MASTER ); |
| 67 | + // Removes expired drafts from database |
| 68 | + $dbw->delete( 'drafts', |
| 69 | + array( |
| 70 | + 'draft_savetime < ' . |
| 71 | + $dbw->addQuotes( |
| 72 | + $dbw->timestamp( self::getDraftAgeCutoff() ) |
| 73 | + ) |
| 74 | + ), |
| 75 | + __METHOD__ |
| 76 | + ); |
| 77 | + } |
67 | 78 | } |
68 | 79 | |
69 | 80 | /** |
— | — | @@ -104,7 +115,11 @@ |
105 | 116 | // Gets database connection |
106 | 117 | $dbw = wfGetDB( DB_MASTER ); |
107 | 118 | // Builds where clause |
108 | | - $where = array(); |
| 119 | + $where = array( |
| 120 | + 'draft_savetime > ' . $dbw->addQuotes( |
| 121 | + $dbw->timestamp( self::getDraftAgeCutoff() ) |
| 122 | + ) |
| 123 | + ); |
109 | 124 | // Checks if specific title was given |
110 | 125 | if ( $title !== null ) { |
111 | 126 | // Gets page id from title |
— | — | @@ -148,7 +163,7 @@ |
149 | 164 | * @param integer $userID[optional] ID of user, defaults to current user |
150 | 165 | */ |
151 | 166 | public static function display( |
152 | | - &$title = null, |
| 167 | + $title = null, |
153 | 168 | $userID = null |
154 | 169 | ) { |
155 | 170 | global $wgOut, $wgRequest, $wgUser, $wgLang; |
Index: trunk/extensions/Drafts/Drafts.php |
— | — | @@ -50,6 +50,12 @@ |
51 | 51 | // Days to keep drafts around before automatic deletion |
52 | 52 | $egDraftsLifeSpan = 30; |
53 | 53 | |
| 54 | +// Ratio of times which a list of drafts requested and the list should be pruned |
| 55 | +// for expired drafts - expired drafts will not apear in the list even if they |
| 56 | +// are not yet pruned, this is just a way to keep the database from filling up |
| 57 | +// with old drafts |
| 58 | +$egDraftsCleanRatio = 1000; |
| 59 | + |
54 | 60 | // Save and View components |
55 | 61 | $wgAutoloadClasses['Drafts'] = $dir . 'Drafts.classes.php'; |
56 | 62 | $wgAutoloadClasses['Draft'] = $dir . 'Drafts.classes.php'; |