This file is indexed.

/usr/share/perl5/WWW/Mediawiki/Client.pm is in libwww-mediawiki-client-perl 0.31-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
package WWW::Mediawiki::Client;

use warnings;
use strict;
use File::Spec;
use File::Find;
use LWP::UserAgent;
use HTML::TokeParser;
use HTTP::Request;
use HTTP::Request::Common;
use HTTP::Cookies;
use URI::Escape;
use VCS::Lite;
use Data::Dumper;
use WWW::Mediawiki::Client::Exceptions;
use XML::LibXML ();
use HTML::Entities qw(encode_entities);
use File::Temp ();
use Encode;
use Encode::Guess;
use utf8;

use base 'Exporter';
our %EXPORT_TAGS = (
	options => [qw(OPT_YES OPT_NO OPT_DEFAULT OPT_KEEP)],
);
our @EXPORT_OK = map { @{$EXPORT_TAGS{$_}} } keys %EXPORT_TAGS;

=head1 NAME

WWW::Mediawiki::Client - module providing a VCS like to MediaWiki

=cut

=head1 SYNOPSIS
  
  use WWW::Mediawiki::Client;

  my $filename = 'Subject.wiki';
  my $mvs = WWW::Mediawiki::Client->new(
      host => 'www.wikitravel.org'
  );

  # like cvs update
  $mvs->do_update($filename);

  # like cvs commit
  $mvs->do_commit($filename, $message);

  #aliases
  $mvs->do_up($filename);
  $mvs->do_com($filename, $message);

=cut

=head1 DESCRIPTION

WWW::Mediawiki::Client provides a very simple cvs-like interface for
Mediawiki driven WikiWiki websites, such as
L<http://www.wikitravel.org|Wikitravel> or
L<http://www.wikipedia.org|Wikipedia.>  
The interface mimics the two most basic cvs commands: update and commit
with similarly named methods.  Each of these has a shorter alias, as in
cvs.  

=cut

=head1 CONSTANTS

=cut

use constant ACTION => 'action';

use constant TITLE => 'title';

use constant SUBMIT => 'submit';

use constant LOGIN => 'submitlogin';

use constant LOGIN_TITLE => 'Special:Userlogin';

use constant EDIT => 'edit';

# defaults for various known Mediawiki installations
my %DEFAULTS;

$DEFAULTS{'www.wikitravel.org'} =
    {
        'host'              => 'wikitravel.org',
        'protocol'          => 'http',
        'space_substitute'  => '_',
        'wiki_path'         => 'wiki/__LANG__/index.php',
    };
$DEFAULTS{'wikitravel.org'} = $DEFAULTS{'www.wikitravel.org'};

$DEFAULTS{'www.wikipedia.org'} =
    {
        'host'              => '__LANG__.wikipedia.org',
        'protocol'          => 'http',
        'space_substitute'  => '+',
        'wiki_path'         => 'w/wiki.phtml',
    };
$DEFAULTS{'wikipedia.org'} = $DEFAULTS{'www.wikipedia.org'};

$DEFAULTS{'www.wiktionary.org'} = 
    {
        'host'              => '__LANG__.wiktionary.org',
        'protocol'          => 'http',
        'space_substitute'  => '_',
        'wiki_path'         => 'w/wiki.phtml',
    };
$DEFAULTS{'wiktionary.org'} = $DEFAULTS{'www.wiktionary.org'};

$DEFAULTS{'www.wikibooks.org'} = 
    {
        'host'              => '__LANG__.wikibooks.org',
        'protocol'          => 'http',
        'space_substitute'  => '_',
        'wiki_path'         => 'w/wiki.phtml',
    };
$DEFAULTS{'wikibooks.org'} = $DEFAULTS{'www.wikibooks.org'};

sub DEFAULTS { \%DEFAULTS };

use constant SPACE_SUBSTITUTE => '_';
use constant WIKI_PATH => 'wiki/index.php';
use constant LANGUAGE_CODE => 'en';
use constant PROTOCOL => 'http';

use constant SPECIAL_EXPORT => 'Special:Export';
use constant SPECIAL_VERSION => 'Special:Version';


=head3 $VERSION 

=cut 

our $VERSION = 0.31;

=head2 Update Status

=head3 STATUS_UNKNOWN

Indicates that C<WWW::Mediawiki::Client> has no information about the file.

=head3 STATUS_UNCHANGED

Indicates that niether the file nor the server page have changed.

=head3 STATUS_LOCAL_ADDED

Indicates that the file is new locally, and does not exist on the server.

=head3 STATUS_LOCAL_MODIFIED

Indicates that the file has been modified locally.

=head3 STATUS_SERVER_MODIFIED

Indicates that the server page was modified, and that the modifications
have been successfully merged into the local file.

=head3 STATUS_CONFLICT

Indicates that there are conflicts in the local file resulting from a
failed merge between the server page and the local file.

=cut

use constant STATUS_UNKNOWN         => '?';
use constant STATUS_UNCHANGED       => '=';
use constant STATUS_LOCAL_ADDED     => 'A';
use constant STATUS_LOCAL_MODIFIED  => 'M';
use constant STATUS_SERVER_MODIFIED => 'U';
use constant STATUS_CONFLICT        => 'C';

=head2 Option Settings

=head3 OPT_YES

Indicates that the setting should always be applied.

=head3 OPT_NO

Indicates that the setting should never be applied.

=head3 OPT_DEFAULT

Indicates that the setting should be applied based on the user profile
default on the Wikimedia server.

=head3 OPT_KEEP

Four-state options only.  Indicates that the setting should not be
changed from its current value on the server.

=cut

# Option values:
use constant OPT_YES     =>  1;
use constant OPT_NO      =>  0;
use constant OPT_DEFAULT => -1;
use constant OPT_KEEP    => -2; # Only for watch.

# Reverse lookup:
use constant OPTION_SETTINGS => (
	OPT_YES,     'OPT_YES', 
	OPT_NO,      'OPT_NO', 
	OPT_DEFAULT, 'OPT_DEFAULT',
	OPT_KEEP,    'OPT_KEEP',
);

# Option defaults:
use constant MINOR_DEFAULT => OPT_DEFAULT;
use constant WATCH_DEFAULT => OPT_DEFAULT;

=head2 Mediawiki form widgets

=head3 TEXTAREA_NAME

=head3 COMMENT_NAME

=head3 EDIT_SUBMIT_NAME

=head3 EDIT_SUBMIT_VALUE

=head3 EDIT_PREVIEW_NAME

=head3 EDIT_PREVIEW_VALUE

=head3 EDIT_TIME_NAME

=head3 EDIT_TOKEN_NAME

=head3 EDIT_WATCH_NAME

=head3 EDIT_MINOR_NAME

=head3 CHECKED

=head3 UNCHECKED

=head3 USERNAME_NAME

=head3 PASSWORD_NAME

=head3 REMEMBER_NAME

=head3 LOGIN_SUBMIT_NAME

=head3 LOGIN_SUBMIT_VALUE

=cut

use constant TEXTAREA_NAME      => 'wpTextbox1';
use constant COMMENT_NAME       => 'wpSummary';
use constant EDIT_SUBMIT_NAME   => 'wpSave';
use constant EDIT_SUBMIT_VALUE  => 'Save Page';
use constant EDIT_PREVIEW_NAME  => 'wpPreview';
use constant EDIT_PREVIEW_VALUE => 'Show preview';
use constant EDIT_TIME_NAME     => 'wpEdittime';
use constant EDIT_TOKEN_NAME    => 'wpEditToken';
use constant EDIT_WATCH_NAME    => 'wpWatchthis';
use constant EDIT_MINOR_NAME    => 'wpMinoredit';
use constant CHECKED            => 1;
use constant UNCHECKED          => 0;
use constant USERNAME_NAME      => 'wpName';
use constant PASSWORD_NAME      => 'wpPassword';
use constant REMEMBER_NAME      => 'wpRemember';
use constant LOGIN_SUBMIT_NAME  => 'wpLoginattempt';
use constant LOGIN_SUBMIT_VALUE => 'Log In';
use constant LOGIN_TOKEN        => 'wpLoginToken';

=head2 Files

=head3 CONFIG_FILE

  .mediawiki

=head3 COOKIE_FILE

  .mediawiki.cookies

=head3 SAVED_ATTRIBUTES

Controls which attributes get saved out to the config file.

=cut

use constant CONFIG_FILE => '.mediawiki';
use constant COOKIE_FILE => '.mediawiki_cookies.dat';
use constant SAVED_ATTRIBUTES => (
    qw(site_url host protocol language_code space_substitute username
       password wiki_path watch encoding minor_edit escape_filenames)
);  # It's important that host goes first since it has side effects


=head1 CONSTRUCTORS

=cut

=head2 new

  my $mvs = WWW::Mediawiki::Client->new(host = 'www.wikitravel.org');

Accepts name-value pairs which will be used as initial values for any of
the fields which have accessors below.  Throws the same execptions as the
accessor for any field named.

=cut

sub new {
    my $pkg = shift;
    my %init = @_;
    my $self = bless {};
    $self->load_state;
    foreach my $attr (SAVED_ATTRIBUTES) {
        next unless $init{$attr};
        $self->$attr($init{$attr});
    }
    $self->{ua} = LWP::UserAgent->new();
    push @{ $self->{ua}->requests_redirectable }, 'POST';
    my $agent = 'WWW::Mediawiki::Client/' . $VERSION;
    $self->{ua}->agent($agent);
    $self->{ua}->env_proxy;
    my $cookie_jar = HTTP::Cookies->new(
        file => COOKIE_FILE,
        autosave => 1,
    );
    $self->{ua}->cookie_jar($cookie_jar);
    return $self;
}

=head1 ACCESSORS

=cut

=head2 host

  my $url = $mvs->host('www.wikipediea.org');

  my $url = $mvs->host('www.wikitravel.org');

The C<host> is the name of the Mediawiki server from which you want to
obtain content, and to which your submissions will be made.  There is no
default.  This has to be set before attempting to use any of the methods
which attempt to access the server.

B<Side Effects:>

=over 4

=item Server defaults

If WWW::Mediawiki::Client knows about the path settings for the Mediawiki
installation you are trying to use then the various path fields will also
be set as a side-effect.

=item Trailing slashes

Any trailing slashes are deleted I<before> the value of C<host> is set.

=back

=cut

sub host {
    my ($self, $host) = @_;
    if ($host) {
        $host =~ s{/*$}{}; # remove any trailing /s
        $self->{host} = $host;
        my $defaults = $DEFAULTS{$host};
        foreach my $k (keys %$defaults) {
            $self->{$k} = $defaults->{$k};
        }
    }
    return $self->{host};
}

=head2 protocol

  my $url = $mvs->protocol('www.wikipediea.org');

  my $url = $mvs->protocol('www.wikitravel.org');

The C<protocol> is the protocol used by the Mediawiki server from which you
want to obtain content, and to which your submissions will be made.  It can
be one of C<http> or C<https> with the default value being http.

B<Side Effects:>

=over 4

=item Server defaults

If WWW::Mediawiki::Client knows about the settings for the Mediawiki
installation you are trying to use then the various path fields will also
be set as a side-effect.

=back

=cut

sub protocol {
    my ($self, $protocol) = @_;
    if ($protocol) {
        WWW::Mediawiki::Client::URLConstructionException->throw(
                "The protocol must be either 'http' or 'https'."
                . "You specified $protocol." )
            unless $protocol =~ m/^http(s){0,1}$/;
        $self->{protocol} = $protocol;
    } 
    return $self->{protocol} || PROTOCOL;
}

=head2 language_code

  my $lang = $mvs->language_code($lang);

Most Mediawiki projects have multiple language versions.  This field can be
set to target a particular language version of the project the client is
set up to address.  When the C<filename_to_url> and C<pagename_to_url> methods
encounter the text '__LANG__' in any part of their constructed URL the
C<language_code> will be substituted.

C<language_code> defaults to 'en'.

=cut

sub language_code {
    my ($self, $char) = @_;
    $self->{language_code} = $char if $char;
    $self->{language_code} = LANGUAGE_CODE 
            unless $self->{language_code};
    return $self->{language_code};
}

=head2 space_substitute

  my $char = $mvs->space_substitute($char);

Mediawiki allows article names to have spaces, for instance the default
Meidawiki main page is called "Main Page".  The spaces need to be converted
for the URL, and to avoid the normal but somewhat difficult to read URL
escape the Mediawiki software substitutes some other character.  Wikipedia
uses a '+', as in "Main+Page" and Wikitravel uses a '_' as in "Main_page".
WWW::Mediawiki::Client always writes wiki files using the '_', but converts
them to whatever the C<space_substitute> is set to for the URL.

B<Throws:>

=over

=item WWW::Mediawiki::Client::URLConstructionException

=back

=cut

sub space_substitute {
    my ($self, $char) = @_;
    if ($char) {
        WWW::Mediawiki::Client::URLConstructionException->throw(
                "Illegal Character in space_substitute $char" )
            if $char =~ /[\&\?\=\\\/]/;
        $self->{space_substitute} = $char;
    }
    $self->{space_substitute} = SPACE_SUBSTITUTE 
            unless $self->{space_substitute};
    return $self->{space_substitute};
}

=head2 escape_filenames

  my $char = $mvs->escape_filenames($do_escape);

Mediawiki allows article names to be in UTF-8 and most international
Wikipedias use this feature. That leads us to UTF-8 encoded file names
and not all filesystems can handle them. So you can set this option to
some true value to make all your local file names with wiki articles
URL-escaped.

=cut

sub escape_filenames {
    my ($self, $do_escape) = @_;
    if ($do_escape) {
        $self->{escape_filenames} = $do_escape;
    } elsif (!defined $self->{escape_filenames}) {
        $self->{escape_filenames} = 0;
    }

    return $self->{escape_filenames};
}

=head2 wiki_path

  my $path = $mvs->wiki_path($path);

C<wiki_path> is the path to the php page which handles all request to
edit or submit a page, or to login.  If you are using a Mediawiki site
which WWW::Mediawiki::Client knows about this will be set for you when you
set the C<host>.  Otherwise it defaults to the 'wiki/wiki.phtml' which is
what you'll get if you follow the installation instructions that some with
Mediawiki.

B<Side effects>

=over

=item Leading slashes

Leading slashes in any incoming value will be stripped.

=back

=cut

sub wiki_path {
    my ($self, $wiki_path) = @_;
    if ($wiki_path) {
        $wiki_path =~ s{^/*}{}; # strip leading slashes
        $self->{wiki_path} = $wiki_path;
    }
    $self->{wiki_path} = WIKI_PATH 
            unless $self->{wiki_path};
    return $self->{wiki_path};
}

=head2 encoding

  my $encoding = $mvs->encoding($encoding);

C<encoding> is the charset in which the Mediawiki server expects uploaded
content to be encoded.  This should be set the first time you use do_login.

=cut

sub encoding {
    my ($self, $encoding) = @_;
    $self->{encoding} = $encoding if $encoding;
    return $self->{encoding};
}

=head2 username

  my $url = $mvs->username($url);

The username to use if WWW::Mediawiki::Client is to log in to the Mediawiki server as a given
user.

=cut

sub username {
    my ($self, $username) = @_;
    $self->{username} = $username if $username;
    return $self->{username};
}

=head2 password

  my $url = $mvs->password($url);

The password to use if WWW::Mediawiki::Client is to log in to the Mediawiki server as a given
user.  Note that this password is sent I<en clair>, so it's probably not a
good idea to use an important one.

=cut

sub password {
    my ($self, $password) = @_;
    $self->{password} = $password if $password;
    return $self->{password};
}

=head2 commit_message

  my $msg = $mvs->commit_message($msg);

A C<commit_message> must be specified before C<do_commit> can be run.  This
will be used as the comment when submitting pages to the Mediawiki server.

=cut

sub commit_message {
    my ($self, $msg) = @_;
    $self->{commit_message} = $msg if $msg;
    return $self->{commit_message};
}

=head2 watch

  my $watch = $mvs->watch($watch);

Mediawiki allows users to add a page to thier watchlist at submit time
using using the "Watch this page" checkbox.  The field C<watch> allows
commits from this library to add or remove the page in question to/from
your watchlist.

This is a four-state option:

=over

=item C<OPT_YES>

Always add pages to the watchlist.

=item C<OPT_NO>

Remove pages from the watchlist.

=item C<OPT_KEEP>

Maintain current watched state.

=item C<OPT_DEFAULT> (default)

Adhere to user profile default on the server.  Watched pages will
always remain watched, and all other pages will be watched if the
"watch all pages by default" option is enabled in the user profile.

=back

B<Throws:>

=over

=item WWW::Mediawiki::Client::InvalidOptionException

=back

=cut

sub watch {
    my ($self, $watch) = @_;
    if (defined($watch)) {
	$self->_option_verify('watch', $watch, 
		[OPT_YES, OPT_NO, OPT_KEEP, OPT_DEFAULT]);
	$self->{watch} = $watch;
    }
    $self->{watch} = WATCH_DEFAULT unless defined $self->{watch};
    return $self->{watch};
}

=head2 minor_edit

  my $minor = $mvs->minor_edit($minor);

Mediawiki allows users to mark some of their edits as minor using the "This
is a minor edit" checkbox.  The field C<minor_edit> allows a commit from
the mediawiki client to be marked as a minor edit.

This is a three-state option:

=over

=item C<OPT_YES>

Always declare change as minor.

=item C<OPT_NO>

Never declare change as minor.

=item C<OPT_DEFAULT> (default)

Adhere to user profile default on the server.  Edits will be marked
as minor if the "minor changes by default" option is enabled in the
user profile.

=back

B<Throws:>

=over

=item WWW::Mediawiki::Client::InvalidOptionException

=back

=cut

sub minor_edit {
    my ($self, $minor) = @_;
    if (defined($minor)) {
	$self->_option_verify('minor_edit', $minor, 
		[OPT_YES, OPT_NO, OPT_DEFAULT]);
	$self->{minor_edit} = $minor;
    }
    $self->{minor_edit} = MINOR_DEFAULT unless defined $self->{minor_edit};
    return $self->{minor_edit};
}

=head2 status

  my %status = $mvs->status;

This field will be empty until do_update has been called, after which it
will be set to a hash of C<filename> => C<status> pairs.  Each C<status> 
will be one of the following (see CONSTANTS for discriptions):

=over

=item WWW::Mediawiki::Client::STATUS_UNKNOWN;

=item WWW::Mediawiki::Client::STATUS_UNCHANGED;

=item WWW::Mediawiki::Client::STATUS_LOCAL_ADDED;

=item WWW::Mediawiki::Client::STATUS_LOCAL_MODIFIED;

=item WWW::Mediawiki::Client::STATUS_SERVER_MODIFIED;

=item WWW::Mediawiki::Client::STATUS_CONFLICT;

=back

=cut

sub status {
    my ($self, $arg) = @_;
    WWW::Mediawiki::Client::ReadOnlyFieldException->throw(
            "Tried to set read-only field 'status' to $arg.") if $arg;
    return unless defined($self->{status});
    return $self->{status};
}

=head2 site_url DEPRICATED

  my $url = $mvs->site_url($url);

The site URL is the base url for reaching the Mediawiki server who's
content you wish to edit.  This field is now depricated in favor of the
C<host> field which is basically the same thing without the protocol
string.


B<Side Effects:>

=over 4

=item Server defaults

If WWW::Mediawiki::Client knows about the path settings for the Mediawiki
installation you are trying to use then the various path fields will also
be set as a side-effect.

=item Trailing slashes

Any trailing slashes are deleted I<before> the value of C<site_url> is set.

=back

=cut

sub site_url {
    my ($self, $host) = @_;
    my ($pkg, $caller, $line) = caller;
    warn "Using depricated method 'site_url' at $caller line $line."
            unless $pkg =~ "WWW::Mediawiki::Client";
    my $protocol = $self->protocol;
    $host =~ s{^$protocol://}{} if $host;
    $host = $self->host($host);
    return "$protocol://" . $host if $host;
}

=head1 Instance Methods

=cut

=head2 do_login

  $mvs->do_login;

The C<do_login> method operates like the cvs login command.  The
C<host>, C<username>, and C<password> attributes must be set before
attempting to login.  Once C<do_login> has been called successfully any
successful commit from the same directory will be logged in the Mediawiki
server as having been done by C<username>.

B<Throws:>

=over

=item WWW::Mediawiki::Client::AuthException

=item WWW::Mediawiki::Client::CookieJarException

=item WWW::Mediawiki::Client::LoginException

=item WWW::Mediawiki::Client::URLConstructionException

=back

=cut

sub do_login {
    my $self = shift;
    WWW::Mediawiki::Client::URLConstructionException->throw(
            "No Mediawiki host specified.")
            unless $self->host;
    WWW::Mediawiki::Client::URLConstructionException->throw(
            "No wiki_path specified.")
            unless $self->wiki_path;
    WWW::Mediawiki::Client::AuthException->throw(
        "Must have username and password to login.")
            unless $self->username && $self->password;
    my $host = $self->host;
    my $path = $self->wiki_path;
    my $lang = $self->language_code;
    $host =~ s/__LANG__/$lang/;
    $path =~ s/__LANG__/$lang/;

    my $protocol = $self->protocol;
    my $url = "$protocol://$host/$path"
            . "?" . ACTION . "=" . LOGIN
            . "&" . TITLE  . "=" . LOGIN_TITLE;
    $self->{ua}->cookie_jar->clear;
    $self->{ua}->cookie_jar->save
            or WWW::Mediawiki::Client::CookieJarException->throw(
            "Could not save cookie jar.");

   # get login token
    my $res = $self->{ua}->request(GET $url);
    unless ($res->is_success) {
        WWW::Mediawiki::Client::LoginException->throw(
	    error => "Could not connect to MediaWiki to get login token.\n",
	    res => $res);
    }

    my $token;
    $token = $1 if $res->content() =~ /input type=['"]hidden['"] name=['"]wpLoginToken['"] value=['"]([a-fA-F0-9]+)['"]/i;

    $res = $self->{ua}->request(POST $url, 
        [ (
            &USERNAME_NAME      => $self->username,
            &PASSWORD_NAME      => $self->password,
            &REMEMBER_NAME      => 1,
            &LOGIN_SUBMIT_NAME  => &LOGIN_SUBMIT_VALUE
	   ), defined $token ? (&LOGIN_TOKEN => $token) : () ]
    );

    # success == Mediawiki gave us a Password cookie
    if ($self->{ua}->cookie_jar->as_string =~ /UserID=/) {
        $self->encoding($self->_get_server_encoding);
        $self->save_state;
        $self->{ua}->cookie_jar->save
                or WWW::Mediawiki::Client::CookieJarException->throw(
                "Could not save cookie jar.");
        return $self;
    } elsif ($res->is_success) {  # got a page, but not what we wanted
        WWW::Mediawiki::Client::LoginException->throw(
                error => "Login did not work, please check username and password.\n",
                res => $res,
                cookie_jar => $self->{ua}->cookie_jar,
            );
    } else { # something else went wrong, send all the data in exception
        my $err = "Login to $url failed.";
        WWW::Mediawiki::Client::LoginException->throw(
                error => $err, 
                res => $res,
                cookie_jar => $self->{ua}->cookie_jar,
            );
    }
}

=head2 do_li
  
  $mvs->do_li;

An alias for C<do_login>.

=cut

sub do_li {
    do_login(@_);
}

=head2 do_update
  
  $self->do_update($filename, ...);

The C<do_update> method operates like a much-simplified version of the cvs
update command.  The argument is a list of filenames, whose contents will
be compared to the version on the WikiMedia server and to a locally stored
reference copy.  Lines which have changed only in the server version will
be merged into the local version, while lines which have changed in both
the server and local version will be flagged as possible conflicts, and
marked as such, somewhate in the manner of cvs (actually this syntax comes
from the default conflict behavior of VCS::Lite):

  ********************Start of conflict 1  Insert to Primary, Insert to Secondary ************************************************************

  The line as it appears on the server

  ****************************************************************************************************

  The line as it appears locally
  ********************End of conflict 1********************************************************************************

After the merging, and conflict marking is complete the server version will
be copied into the reference version.

If either the reference version or the local version are empty, or if
either file does not exist they will both be created as a copy of the
current server version.

B<Throws:>

=over

=item WWW::Mediawiki::Client::URLConstructionException

=item WWW::Mediawiki::Client::FileAccessException

=item WWW::Mediawiki::Client::FileTypeException

=item WWW::Mediawiki::Client::ServerPageException

=item WWW::Mediawiki::Client::AbsoluteFileNameException

=back

=cut

sub do_update {
	my ($self, @files) = @_;
        @files = $self->list_wiki_files unless @files;
	WWW::Mediawiki::Client::URLConstructionException->throw(
		"No server URL specified.") unless $self->{host};
	my %pages;
	my %dirs;
	foreach my $filename (@files) {
		my ($vol, $dirs, $fn) = $self->_check_path($filename);
		my $pagename = $self->filename_to_pagename($filename);
		$pages{$filename} = $pagename;
		$dirs{$filename}  = $dirs;
	}
	$self->_get_exported_pages(values %pages);
	foreach my $filename (@files) {
		my $pagename = $pages{$filename};
		my $dirs     = $dirs{$filename};
		my $status = $self->_update_core($filename, $pagename, $dirs);
		$self->{status}->{$filename} = $status;
	}
        return $self->status;
}

sub _update_core {
    my ($self, $filename, $pagename, $dirs) = @_;
    my $sv = $self->get_server_page($pagename);
    my $lv = $self->get_local_page($filename);
    my $rv = $self->_get_reference_page($filename);
    my $nv = $self->_merge($filename, $rv, $sv, $lv);
    my $status = $self->_get_update_status($rv, $sv, $lv, $nv);
    return unless $status;  # nothing changes, nothing to do
    return $status
            if $status eq STATUS_LOCAL_ADDED
                or $status eq STATUS_UNKNOWN
                or $status eq STATUS_UNCHANGED;
    # save the newly retrieved and/or merged version as our local copy
    my @dirs = split '/', $dirs;
    for my $d (@dirs) {
        mkdir $d;
        chdir $d;
    }
    for (@dirs) {
        chdir '..';
    }
    open OUT, ">:utf8", $filename or WWW::Mediawiki::Client::FileAccessException->throw(
            "Cannot open $filename for writing.");
    print OUT $nv;
    # save the server version out as the reference file
    $filename = $self->_get_ref_filename($filename);
    open OUT, ">:utf8", $filename or WWW::Mediawiki::Client::FileAccessException->throw(
            "Cannot open $filename for writing.");
    print OUT $sv;
    close OUT;
    return $status;
}

=head2 do_up

An alias for C<do_update>.

=cut

sub do_up {
    do_update(@_);
}

=head2 do_commit
  
  $self->do_commit($filename);

As with C<do_update> the C<do_commit> method operates like a much
simplified version of the cvs commit command.  Again, the argument is a
filename.  In keeping with the operation of cvs, C<do_commit> does not
automatically do an update, but does check the server version against the
local reference copy, throwing an error if the server version has changed,
thus forcing the user to do an update.  A different error is thrown if the
conflict pattern sometimes created by C<do_update> is found.

After the error checking is done the local copy is submitted to the server,
and, if all goes well, copied to the local reference version.

B<Throws:>

=over

=item WWW::Mediawiki::Client::CommitMessageException

=item WWW::Mediawiki::Client::ConflictsPresentException

=item WWW::Mediawiki::Client::FileAccessException

=item WWW::Mediawiki::Client::FileTypeException

=item WWW::Mediawiki::Client::URLConstructionException

=item WWW::Mediawiki::Client::UpdateNeededException

=item WWW::Mediawiki::Client::InvalidOptionException

=back

=cut

sub do_commit {
    my ($self, $filename) = @_;
    WWW::Mediawiki::Client::CommitMessageException->throw(
            "No commit message specified")
        unless $self->{commit_message};
    # Perform the actual upload:
    my ($res, $text) = $self->_upload_file($filename, 1);
    # save the local version as the reference version
    my $refname = $self->_get_ref_filename($filename);
    open OUT, ">:utf8", $refname 
            or WWW::Mediawiki::Client::FileAccessException->throw(
            "Cannot open $refname for writing.");
    print OUT $text;
    close OUT;
}

=head2 do_com

This is an alias for C<do_commit>.

=cut

sub do_com {
    do_commit(@_);
}

=head2 do_preview
  
  $self->do_preview($filename);

The C<do_preview> method is a non-writing version of the C<do_commit>
method.  It uploads the given filename to test its formatting.  Its
behaviour and arguments are identical to C<do_commit>.

The behaviour of C<do_preview> is currently based on the environment.
If C<MVS_BROWSER> is set, this program (typically a web browser) will
be launched on a temporary file.  Otherwise, the preview will be saved
to the file specified by the C<MVS_PREVIEW> variable, or preview.html
if this is unset.  This behaviour is considered a prototype for future
functionality, and is C<subject to change> in the near future.

Returns the name of the preview file, or undef if the file was sent to
a web browser.

B<Throws:>

=over

=item WWW::Mediawiki::Client::ConflictsPresentException

=item WWW::Mediawiki::Client::FileAccessException

=item WWW::Mediawiki::Client::FileTypeException

=item WWW::Mediawiki::Client::URLConstructionException

=item WWW::Mediawiki::Client::UpdateNeededException

=back

=cut

sub do_preview {
	my ($self, $filename) = @_;
	my ($response) = $self->_upload_file($filename, 0);
	my $url = encode_entities($response->request->uri);
	my $content = $response->decoded_content;
	$content =~ s#<head>#$&<base href="$url"/>#;
	my $browser = $ENV{MVS_BROWSER};
	if (defined($browser)) {
            my $fh = new File::Temp(UNLINK => 1, SUFFIX => '.html');
            print $fh Encode::encode_utf8($content);
            $fh->close;
            system($browser, $fh->filename);
            return undef;
	}
	my $preview = $ENV{MVS_PREVIEW};
	$preview = 'preview.html' unless defined($preview);
	open(PREVIEW, '>', $preview) 
		or WWW::Mediawiki::Client::FileAccessException->throw(
			"Cannot open $preview for writing.");
	print PREVIEW $content;
	close(PREVIEW) or WWW::Mediawiki::Client::FileAccessException->throw(
			"Cannot close $preview.");
	print STDERR "Saved preview: $preview\n";
	return $preview;
}

=head2 do_clean

  $self->do_clean;

Removes all reference files under the current directory that have no
corresponding Wiki files.

B<Throws:>

=over

=item WWW::Mediawiki::Client::FileAccessException

=back

=cut

sub do_clean {
	my ($self) = @_;

	my $dir = File::Spec->curdir();
	find(sub { 
		return unless m/^\..*\.ref\.wiki\z/s;

		my $name = $File::Find::name;
		$name = File::Spec->abs2rel($name);

		my $wiki = $self->_ref_to_filename($name);
		return if -e $wiki;

		warn "Deleting: $name\n";
		unlink($name) 
		    or WWW::Mediawiki::Client::FileAccessException->throw(
			"Cannot delete reference file $name");
	}, $dir);
}

=head2 save_state
  
  $mvs->save_state;

Saves the current state of the wmc object in the current working directory.

B<Throws:>

=over

=item WWW::Mediawiki::Client::FileAccessException

=back

=cut

sub save_state {
    my $self = shift;
    my $conf = CONFIG_FILE;
    my %init;
    foreach my $attr (SAVED_ATTRIBUTES) {
        $init{$attr} = $self->$attr;
    }
    open OUT, ">:utf8", $conf or WWW::Mediawiki::Client::FileAccessException->throw(
            "Cannot write to config file, $conf.");
    print OUT Dumper(\%init);
    close OUT;
}

=head2 load_state

  $mvs = $mvs->load_state;

Loads the state of the wmc object from that saved in the current working
directory.

B<Throws:>

=over

=item WWW::Mediawiki::Client::CorruptedConfigFileException

=back

=cut

sub load_state {
    my $self = shift;
    my $config = CONFIG_FILE;
    return $self unless -e $config;
    our $VAR1;
    do $config or 
            WWW::Mediawiki::Client::CorruptedConfigFileException->throw(
            "Could not read config file: $config.");
    my %init = %$VAR1;
    foreach my $attr (SAVED_ATTRIBUTES) {
        $self->$attr($init{$attr});
    }
    return $self;
}

=head2 get_server_page

  my $wikitext = $mvs->get_server_page($pagename);

Returns the wikitext of the given Mediawiki page name.

B<Throws:>

=over

=item WWW::Mediawiki::Client::ServerPageException

=back

=cut

sub get_server_page {
    my ($self, $pagename) = @_;

    my $export = delete $self->{export}->{$pagename};
    return $export if defined($export);

    my $url = $self->pagename_to_url($pagename, EDIT);
    my $res = $self->{ua}->get($url);
    WWW::Mediawiki::Client::ServerPageException->throw(
            error => "Couldn't fetch \"$pagename\" from the server.",
            res => $res,
        ) unless $res->is_success;
    my $doc = $res->decoded_content;
    my $text = $self->_get_wiki_text($doc);
    $self->{edit}->{date} = $self->_get_edit_date($doc);
    $self->{edit}->{token} = $self->_get_edit_token($doc);
    $self->{edit}->{watch_now} = $self->_get_edit_is_watching($doc);
    $self->{edit}->{def_watch} = $self->_get_edit_watch_default($doc);
    $self->{edit}->{def_minor} = $self->_get_edit_minor_default($doc);
    my $headline = Encode::encode("utf8", $self->_get_page_headline($doc));
    my $expected = lc $pagename;
    unless (lc($headline) =~ /\Q$expected\E$/) {
        WWW::Mediawiki::Client::ServerPageException->throw(
	        error => "The server could not resolve the page name
                        '$pagename', but responded that it was '$headline'.",
                res   => $res,
            ) if ($headline && $headline =~ /^Editing /);
        WWW::Mediawiki::Client::ServerPageException->throw(
	        error => "Error message from the server: '$headline'.",
                res   => $res,
            ) if ($headline);
        WWW::Mediawiki::Client::ServerPageException->throw(
                error => "Could not identify the error in this context.",
                res   => $res,
            );
    }
    chomp $text;
    return $text;
}

=head2 get_local_page

  my $wikitext = $mvs->get_local_page($filename);

Returns the wikitext from the given local file;

B<Throws:>

=over

=item WWW::Mediawiki::Client::FileAccessException

=item WWW::Mediawiki::Client::FileTypeException

=item WWW::Mediawiki::Client::AbsoluteFileNameException 

=back

=cut

sub get_local_page {
    my ($self, $filename) = @_;
    $self->_check_path($filename);
    return '' unless -e $filename;
    open IN, "<:utf8", $filename or 
            WWW::Mediawiki::Client::FileAccessException->throw(
            "Cannot open $filename.");
    local $/;
    my $text = <IN>;
    close IN;
    return $text;
}

=head2 pagename_to_url

  my $url = $mvs->pagename_to_url($pagename);

Returns the url at which a given pagename will be found on the Mediawiki
server to which this instance of points.

B<Throws:>

=over

=item WWW::Mediawiki::Client::URLConstructionException;

=back

=cut

sub pagename_to_url {
    my ($self, $name, $action) = @_;
    WWW::Mediawiki::Client::URLConstructionException->throw(
            error => 'No action supplied.',
        ) unless $action;
    WWW::Mediawiki::Client::URLConstructionException->throw(
            error => "Page name $name ends with '.wiki'.",
        ) if $name =~ /.wiki$/;
    my $char = $self->space_substitute;
    $name =~ s/ /$char/;
    my $lang = $self->language_code;
    my $host = $self->host;
    $host =~ s/__LANG__/$lang/g;
    my $wiki_path = $self->wiki_path;
    $wiki_path =~ s/__LANG__/$lang/g;
    my $protocol = $self->protocol;
    return "$protocol://$host/$wiki_path?" . ACTION . "=$action&" . TITLE . "=$name";
}

=head2 filename_to_pagename

  my $pagename = $mvs->filname_to_pagename($filename);

Returns the cooresponding server page name given a filename.

B<Throws:>

=over

=item WWW::Mediawiki::Client::AbsoluteFileNameException

=item WWW::Mediawiki::Client::FileTypeException 

=back

=cut

sub filename_to_pagename {
    my ($self, $name) = @_;
    $self->_check_path($name);
    $name =~ s/.wiki$//;

    $self->{escape_filenames} and $name = decode('UTF-8', URI::Escape::uri_unescape($name));

    $name =~ s/_/ /g;
    return ucfirst $name;
}

=head2 filename_to_url

  my $pagename = $mvs->filname_to_url($filename);

Returns the cooresponding server URL given a filename.

B<Throws:>

=over

=item WWW::Mediawiki::Client::AbsoluteFileNameException

=item WWW::Mediawiki::Client::FileTypeException 

=back

=cut

sub filename_to_url {
    my ($self, $name, $action) = @_;
    $name = $self->filename_to_pagename($name);
    return $self->pagename_to_url($name, $action);
}

=head2 pagename_to_filename

  my $filename = $mvs->pagename_to_filename($pagename);

Returns a local filename which cooresponds to the given Mediawiki page
name.

=cut

sub pagename_to_filename {
    my ($self, $name) = @_;
    $name =~ s/ /_/;

    $self->{escape_filenames} and $name = URI::Escape::uri_escape_utf8($name);
    
    $name .= '.wiki';
    return $name;
}

=head2 url_to_filename
  
  my $filename = $mvs->url_to_filename($url);

Returns the local filename which cooresponds to a given URL.

=cut

sub url_to_filename {
    my ($self, $url) = @_;
    my $char = '\\' . $self->space_substitute;
    $url =~ s/$char/_/g;
    my $title = TITLE;
    $url =~ m/&$title=([^&]*)/;
    return "$1.wiki";
}

=head2 list_wiki_files

  @filenames = $mvs->list_wiki_files;

Returns a recursive list of all wikitext files in the local repository.

=cut

sub list_wiki_files {
    my $self = shift;
    my @files;
    my $dir = File::Spec->curdir();
    find(sub { 
        return unless /^[^.].*\.wiki\z/s;
        my $name = $File::Find::name;
        $name = File::Spec->abs2rel($name);
        push @files, $name;
    }, $dir);
    return @files;
}

=begin comment

=head1 Private Methods

=cut

sub _merge {
    my ($self, $filename, $ref, $server, $local) = @_;
    my $control = {
            in => $\,
            out => $/,
            chomp => 1
        };
    $ref = VCS::Lite->new('ref', "\n", "$ref\n");
    $server = VCS::Lite->new('server', "\n", "$server\n");
    $local = VCS::Lite->new('local', "\n", "$local\n");
    my $merge = $ref->merge($server, $local);
    return scalar $merge->text();
}

sub _get_wiki_text {
    my ($self, $doc) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    $p->get_tag("textarea");
    my $text = $p->get_text;
    $text =~ s/
//gs;                      # convert endlines
    return $text;
}

sub _get_server_encoding {
    my ($self) = @_;
    my $url = $self->_get_version_url;
    my $res = $self->{ua}->get($url);
    my $doc = $res->decoded_content;
    my $p = HTML::TokeParser->new(\$doc);
    while ( my $t = $p->get_tag("meta") ) {
        next unless defined $t->[1]->{'http-equiv'}
     and ($t->[1]->{'http-equiv'} eq 'Content-Type'
     or $t->[1]->{'http-equiv'} eq 'Content-type');
        my $cont = $t->[1]->{'content'};
        $cont =~ m/charset=(.*)/;
        return $1;
    }
}

sub _get_page_headline {
    my ($self, $doc) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    $p->get_tag("h1");
    my $text = $p->get_text;
    $text =~ s/
//gs;                      # convert endlines
    return $text;
}

sub _get_edit_date {
    my ($self, $doc) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    my $date;
    while (my $tag = $p->get_tag('input')) {
        next unless exists $tag->[1]->{type} and $tag->[1]->{type} eq 'hidden';
        next unless $tag->[1]->{name} eq EDIT_TIME_NAME;
        $date = $tag->[1]->{value};
    }
    return $date;
}

sub _get_edit_token {
    my ($self, $doc) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    my $token;
    while (my $tag = $p->get_tag('input')) {
        next unless exists $tag->[1]->{type} and $tag->[1]->{type} eq 'hidden';
        next unless $tag->[1]->{name} eq 'wpEditToken';
        $token = $tag->[1]->{value};
    }
    return $token;
}

sub _get_edit_is_watching {
    my ($self, $doc, $name) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    my $status;
    while (my $tag = $p->get_tag('a')) {
        next unless $tag->[1]->{href} 
                && $tag->[1]->{href} =~ m/&action=((?:un)?watch)/;
	# If 'un'watch, then it's watched; otherwise, it's not.
        $status = ($1 eq 'unwatch' ? 1 : 0);
    }
    return $status;
}

sub _get_edit_checkbox {
    my ($self, $doc, $name) = @_;
    my $p = HTML::TokeParser->new(\$doc);
    my $status;
    while (my $tag = $p->get_tag('input')) {
        next unless exists $tag->[1]->{type} and $tag->[1]->{type} eq 'checkbox';
        next unless $tag->[1]->{name} eq $name;
        $status = ($tag->[1]->{checked} ? 1 : 0);
    }
    return $status;
}

sub _get_edit_watch_default {
	my ($self, $doc) = @_;
	return $self->_get_edit_checkbox($doc, EDIT_WATCH_NAME);
}

sub _get_edit_minor_default {
	my ($self, $doc) = @_;
	return $self->_get_edit_checkbox($doc, EDIT_MINOR_NAME);
}

sub _check_path {
    my ($self, $filename) = @_;
    WWW::Mediawiki::Client::FileTypeException->throw(
            "'$filename' doesn't appear to be a wiki file.")
            unless $filename =~ /\.wiki$/;
    WWW::Mediawiki::Client::AbsoluteFileNameException->throw(
            "No absolute filenames allowed!")
            if File::Spec->file_name_is_absolute($filename);
    return File::Spec->splitpath($filename);
}

sub _get_reference_page {
    my ($self, $filename) = @_;
    return '' unless -e $filename;
    $filename = $self->_get_ref_filename($filename);
    my $ref = $self->get_local_page($filename);
    return $ref;
}

sub _get_ref_filename {
    my ($self, $filename) = @_;
    WWW::Mediawiki::Client::FileTypeException->throw(
            "Not a .wiki file.") unless $filename =~ /\.wiki$/;
    my ($vol, $dirs, $fn) = File::Spec->splitpath($filename);
    $fn =~ s/(.*)\.wiki/.$1.ref.wiki/;
    return File::Spec->catfile('.', $dirs, $fn);
}

sub _ref_to_filename {
    my ($self, $ref) = @_;
    my ($vol, $dirs, $fn) = File::Spec->splitpath($ref);
    $fn =~ s/^\.(.*)\.ref\.wiki$/$1.wiki/
        or WWW::Mediawiki::Client::FileTypeException->throw(
            "Not a .ref.wiki file.");
    return File::Spec->catpath($vol, $dirs, $fn);
}

sub _conflicts_found_in {
    my ($self, $text) = @_;
    return 1 if $text =~ /Start of conflict 1/m;
    return 0;
}

sub _get_update_status {
    my ($self, $rv, $sv, $lv, $nv) = @_;
    chomp ($rv, $sv, $lv, $nv); # double chomp
    chomp ($rv, $sv, $lv, $nv); # it's a nasty hack, but necessary until we re-write
    return STATUS_CONFLICT if $self->_conflicts_found_in($nv);
    return STATUS_UNKNOWN unless $sv or $lv;
    return STATUS_LOCAL_ADDED unless $sv;
    return STATUS_UNCHANGED if $sv eq $lv;
    return STATUS_LOCAL_MODIFIED if $lv ne $rv;
    return STATUS_SERVER_MODIFIED if $rv ne $sv;
    return STATUS_UNKNOWN;
}

sub _get_host_url {
	my ($self) = @_;
	my $lang = $self->language_code;
	my $host = $self->host;
	$host =~ s/__LANG__/$lang/g;
        my $protocol = $self->protocol;
	return "$protocol://$host/";
}

sub _get_version_url {
    my ($self) = @_;
    my $lang = $self->language_code;
    my $path = $self->wiki_path;
    $path =~ s/__LANG__/$lang/g;
    return $self->_get_host_url 
         . $path . '?' . TITLE . '=' . SPECIAL_VERSION;
}

sub _get_export_url {
    my ($self) = @_;
    my $lang = $self->language_code;
    my $path = $self->wiki_path;
    $path =~ s/__LANG__/$lang/g;
    return $self->_get_host_url 
         . $path . '?' . TITLE . '=' . SPECIAL_EXPORT;
}

sub _get_exported_pages {
    my ($self, @pages) = @_;
    my $count = scalar @pages;
    my $url = $self->_get_export_url;
    my $response = $self->{ua}->request(POST $url, [ 
            pages => join("\n", @pages),
            action => 'submit',
            curonly => 'true',
    ]);
    WWW::Mediawiki::Client::ServerPageException->throw(
            error => "Couldn't fetch $count pages from the server.",
            res => $response,
        ) unless $response->is_success;
    my $parser = XML::LibXML->new;
    my $doc = $parser->parse_string($response->decoded_content);
    my %expecting = map {$_ => 1} @pages;
    my %export = ();
    my %timestamp = ();
    foreach my $node ($doc->findnodes('/mediawiki/page')) {
        my $page = $node->findvalue(TITLE);
        my $text = $node->findvalue('revision/text');
        my $time = $node->findvalue('revision/timestamp');
        WWW::Mediawiki::Client::ServerPageException->throw(
            error => "Server returned unexpected page '$page'.",
            res => $response) unless $expecting{$page};
        $export{$page} = $text;
        $timestamp{$page} = $time;
    }
    $self->{export} = \%export;
    $self->{timestamp} = \%timestamp;
}

sub _upload_file {
    my ($self, $filename, $commit) = @_;
    WWW::Mediawiki::Client::URLConstructionException->throw(
            "No server URL specified.") unless $self->{host};
    WWW::Mediawiki::Client::FileAccessException->throw("No such file!") 
        unless -e $filename;
    WWW::Mediawiki::Client::CommitException->throw(
            'Could not determine charset for uploading to this server.'
        ) unless $self->encoding;
    my $text = $self->get_local_page($filename);
    my $pagename = $self->filename_to_pagename($filename);
    my $sp = $self->get_server_page($pagename);
    if ($self->{edit}->{date}) {
        my $ref = $self->_get_reference_page($filename);
        chomp ($sp, $ref);
        WWW::Mediawiki::Client::UpdateNeededException->throw(
                error => $self->filename_to_pagename($filename) 
                       . " has changed on the server.",
            ) unless $sp eq $ref;
    }
    chomp ($text);
    WWW::Mediawiki::Client::ConflictsPresentException->throw(
            "$filename appears to have unresolved conflicts")
        if $self->_conflicts_found_in($text);
    my @params;
    push(@params, EDIT_MINOR_NAME, CHECKED) if $self->_option_check(
        'minor_edit', $self->{minor_edit}, 
	$self->{edit}->{def_minor});
    push(@params, EDIT_WATCH_NAME, CHECKED) if $self->_option_check(
	'watch', $self->{watch}, 
	$self->{edit}->{def_watch}, $self->{edit}->{watch_now});
    my $act_name  = ($commit ? EDIT_SUBMIT_NAME  : EDIT_PREVIEW_NAME );
    my $act_value = ($commit ? EDIT_SUBMIT_VALUE : EDIT_PREVIEW_VALUE);
    my $url = $self->filename_to_url($filename, SUBMIT);
    my $content_type = 'application/x-www-form-urlencoded;charset=' . 
	$self->encoding;

    my $res = $self->{ua}->post($url,
	'Content-type' => $content_type,
	Content =>
	[
            $act_name           => $act_value,
            &TEXTAREA_NAME      => $text,
            &COMMENT_NAME       => $self->{commit_message},
            &EDIT_TIME_NAME     => $self->{edit}->{date},
            &EDIT_TOKEN_NAME    => $self->{edit}->{token},
	    @params,
        ]
    );

    my $doc = $res->decoded_content;

    my $headline = $self->_get_page_headline($doc);
    my $expect = ($commit ? $pagename : "Editing $pagename");
    unless (lc($headline) eq lc($expect)) {
        WWW::Mediawiki::Client::CommitException->throw(
	        error => "The page you are trying to commit appears to contain a link which is associated with Wikispam.",
                res   => $res,
            ) if ($headline eq 'Spam protection filter');
        WWW::Mediawiki::Client::CommitException->throw(
	        error => "When we tried to commit '$pagename' the server responded with '$headline'.",
                res   => $res,
            ) if ($headline);
    }
    return ($res, $text);
}

sub _option_verify {
	my ($self, $name, $value, $r_accept) = @_;

	foreach my $acc (@{$r_accept}) {
		return 1 if $acc == $value;
	}

	my %opts = OPTION_SETTINGS;
	my $valstr = $opts{$value};
	$valstr = "value '$value'" unless defined($valstr);

        WWW::Mediawiki::Client::InvalidOptionException->throw(
	        error  => "Cannot set field $name to $valstr.",
		field  => $name,
                option => $opts{$value},
		value  => $value,
	);
}

sub _option_check {
	my ($self, $name, $value, $default, $current) = @_;

	return 1 if $value == OPT_YES;
	return 0 if $value == OPT_NO;

	if ($value == OPT_DEFAULT) {
		return $default if defined($default);
		WWW::Mediawiki::Client::InvalidOptionException->throw(
			error  => "Field '$name' cannot use OPT_DEFAULT:"
			    . " Default information could not be determined.",
			field  => $name,
			option => 'OPT_DEFAULT',
			value  => $value,
		);
	}

	if ($value == OPT_KEEP) {
		return $current if defined($current);
		WWW::Mediawiki::Client::InvalidOptionException->throw(
			error  => "Field '$name' cannot use OPT_KEEP:"
			    . " Current information could not be determined.",
			field  => $name,
			option => 'OPT_DEFAULT',
			value  => $value,
		);
	}

	# Should never happen; these are verified at assignment time.
	my %opts = OPTION_SETTINGS;
        WWW::Mediawiki::Client::InvalidOptionException->throw(
	        error  => "Field '$name' is in unknown state '$value'.",
		field  => $name,
                option => $opts{$value},
		value  => $value
	);
}

1;

__END__

=end comment

=head1 BUGS

Please submit bug reports to the CPAN bug tracker at
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Mediawiki-Client>.

=head1 DISCUSSION

There is a discussion list.  You can subscribe or read the archives at:
L<http://www.geekhive.net/cgi-bin/mailman/listinfo/www-mediawiki-client-l>

=head1 AUTHORS

=over

=item Mark Jaroski <mark@geekhive.net> 

Original author, maintainer

=item Mike Wesemann <mike@fhi-berlin.mpg.de>

Added support for Mediawiki 1.3.10+ edit tokens

=item Bernhard Kaindl <bkaindl@ffii.org>

Improved error messages.

=item Oleg Alexandrov <aoleg@math.ucla.edu>, Thomas Widmann <twid@bibulus.org>

Bug reports and feedback.

=item Adrian Irving-Beer <wisq@wisq.net>

Preview support, export support for multi-page update, more 'minor'
and 'watch' settings, and bug reports.

=item Nicolas Brouard <nicolas.brouard@libertysurf.fr>

Fixed content-type bug.

=item Alex Kapranoff <alex@kapranoff.ru>

Added C<escape_filename> in order to support UTF-8 filenames on filesystems
lacking UTF-8 support.

=back

=head1 LICENSE

Copyright (c) 2004-2006 Mark Jaroski. 

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.