Part of bug 26280: added license headers to PHP files in maintenance
[lhc/web/wiklou.git] / maintenance / purgeOldText.inc
1 <?php
2
3 /**
4 * Support functions for cleaning up redundant text records
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
24 */
25
26 function PurgeRedundantText( $delete = false ) {
27
28 # Data should come off the master, wrapped in a transaction
29 $dbw = wfGetDB( DB_MASTER );
30 $dbw->begin();
31
32 $tbl_arc = $dbw->tableName( 'archive' );
33 $tbl_rev = $dbw->tableName( 'revision' );
34 $tbl_txt = $dbw->tableName( 'text' );
35
36 # Get "active" text records from the revisions table
37 echo( "Searching for active text records in revisions table..." );
38 $res = $dbw->query( "SELECT DISTINCT rev_text_id FROM $tbl_rev" );
39 foreach ( $res as $row ) {
40 $cur[] = $row->rev_text_id;
41 }
42 echo( "done.\n" );
43
44 # Get "active" text records from the archive table
45 echo( "Searching for active text records in archive table..." );
46 $res = $dbw->query( "SELECT DISTINCT ar_text_id FROM $tbl_arc" );
47 foreach ( $res as $row ) {
48 $cur[] = $row->ar_text_id;
49 }
50 echo( "done.\n" );
51
52 # Get the IDs of all text records not in these sets
53 echo( "Searching for inactive text records..." );
54 $set = implode( ', ', $cur );
55 $res = $dbw->query( "SELECT old_id FROM $tbl_txt WHERE old_id NOT IN ( $set )" );
56 $old = array();
57 foreach ( $res as $row ) {
58 $old[] = $row->old_id;
59 }
60 echo( "done.\n" );
61
62 # Inform the user of what we're going to do
63 $count = count( $old );
64 echo( "$count inactive items found.\n" );
65
66 # Delete as appropriate
67 if ( $delete && $count ) {
68 echo( "Deleting..." );
69 $set = implode( ', ', $old );
70 $dbw->query( "DELETE FROM $tbl_txt WHERE old_id IN ( $set )" );
71 echo( "done.\n" );
72 }
73
74 # Done
75 $dbw->commit();
76
77 }