public
Last active

Wiki video uploading bot. Development by AleksandrT.

  • Download Gist
wikiuploadingbot.php
PHP
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
<?php
require_once './mediawikibot.class.php';
require_once '(db connection)';
define('WIKI_DOMAIN', 'https://commons.wikimedia.org');
define('WIKI_WIKI', '/w');
define('WIKI_USERNAME', 'SpreadthesignBot');
define('WIKI_PASSWORD', '');
define('WIKI_COOKIES', '/tmp/wikibot_cookies.tmp');
define('WIKI_USERAGENT', 'SpreadthesignBot/0.1 (http://www.spreadthesign.com/; info@spreadthesign.com)');
$basePath = '/videos';
$bot = new MediaWikiBot();
// Wiki login
if (($res = $bot->login()) !== NULL) {
die('Login failed. ' . print_r($res, 1));
}
// Get edit token from wiki
$response = $bot->query(array(
'prop' => 'info',
'titles' => 'Foo',
'intoken' => 'edit',
));
$editToken = isset($response['query']['pages'][-1]['edittoken'])
? $response['query']['pages'][-1]['edittoken'] : NULL;
// Get video data
//English for categories and wordclass
$CLASS = Dictionary::getAllClasses(1);
$CATEGORY = Category::getAllCategories(MASTER_CID);
 
$r = mysql_query("SELECT v.ID, d.cID, d.Word, DATE( v.Timestamp ) AS Created, dm.Class, c.SLC,
dm.Category, dm.Definition AS Description, st.Translation AS WordLanguage,
(SELECT st.Translation FROM SiteTranslation st, SiteTranslationMagnet stm WHERE
stm.Reference = CONCAT( c.CC, '_LANG' ) AND mID = stm.ID AND cID = 1) AS CategoryLangiage
FROM Video v
INNER JOIN DictionaryVideo dv ON ( dv.vID = v.ID )
INNER JOIN Dictionary d ON ( d.ID = dv.dID )
INNER JOIN DictionaryMagnet dm ON ( dm.ID = d.mID )
INNER JOIN Country c ON ( c.ID = d.cID AND c.Enabled = 1)
INNER JOIN SiteTranslationMagnet stm ON ( stm.Reference = CONCAT( c.CC, '_LANG' ) )
INNER JOIN SiteTranslation st ON ( st.mID = stm.ID AND st.cID = d.cID )
WHERE v.uID =1
AND v.WikiCommons IS NULL
AND v.ID >99
LIMIT 0,10");
while ($row = mysql_fetch_assoc($r)) {
$vid = $row['ID'];
$cid = $row['cID'];
$word = $row['Word'];
$desc = $row['Description'];
$date = $row['Created'];
$class = $row['Class'];
$category = $row['Category'];
$language = $row['WordLanguage'];
$categoryLanguage = $row['CategoryLangiage'];
$slc = $row['SLC'];
if (isset($CLASS[$class])) {
$desc .= " ({$CLASS[$class]})";
}
$text = <<<"EOT"
=={{int:filedesc}}==
{{Information
|description={{$categoryLanguage|1=$desc}}
|date=$date
|source={{STS-link|$vid}}
|author={{Spread the sign}}
|permission={{STS-cooperation}}
|other_versions=
|other_fields=
}}
 
[[category:$categoryLanguage sign language]]
[[category:Videos of sign language in $categoryLanguage]]
EOT;
// Add category:wordclass (English)
if (isset($CLASS[$class])) {
$text .= "\n[[Category:{$CLASS[$class]}]]\n"
}
// Upload to wiki
$response = $bot->upload(array(
'filename' => "$slc-$word-spreadthesign-$vid.ogv",
'file' => "@{$basePath}/ogv/$cid/$vid.ogv",
'text' => $text,
'token' => $editToken,
));
// Check response status
if (!isset($response['upload']['result']) || $response['upload']['result'] != 'Success') {
die("Failed to upload video ID: $vid\n" . print_r($response, 1));
}
// Persist wiki filename
$safe = Utils::safe($response['upload']['filename']);
mysql_query("UPDATE Video SET WikiCommons = '$safe' WHERE ID = $vid LIMIT 1");
$url = $response['upload']['imageinfo']['descriptionurl'];
$name = $response['upload']['filename'];
echo "<a href=\"$url\">$name</a>\n";
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.