Guest User

COLLECTION

a guest
Nov 8th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.59 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Collection Extension for MediaWiki
  5. *
  6. * Copyright (C) PediaPress GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23.  
  24. # Not a valid entry point, skip unless MEDIAWIKI is defined
  25. if ( !defined( 'MEDIAWIKI' ) ) {
  26. echo <<<EOT
  27. To install the Collection extension, put the following line in LocalSettings.php:
  28. require_once( "\$IP/extensions/Collection/Collection.php" );
  29. EOT;
  30. exit( 1 );
  31. }
  32.  
  33. $dir = dirname( __FILE__ ) . '/';
  34.  
  35. # Extension version
  36. $wgCollectionVersion = "1.5";
  37.  
  38. # ==============================================================================
  39.  
  40. # Configuration:
  41.  
  42. /** Bump the version number every time you change any of the JavaScript files */
  43. $wgCollectionStyleVersion = 8;
  44.  
  45. /** URL of mw-serve render server */
  46. $wgCollectionMWServeURL = 'http://tools.pediapress.com/mw-serve/';
  47.  
  48. /** Login credentials to this MediaWiki as 'USERNAME:PASSWORD' string */
  49. $wgCollectionMWServeCredentials = null;
  50.  
  51. /** PEM-encoded SSL certificate for the mw-serve render server to pass to CURL */
  52. $wgCollectionMWServeCert = null;
  53.  
  54. /** if not null, treat this string as hierarchy delimiter in page titles,
  55. * i.e. support subpages */
  56. $wgCollectionHierarchyDelimiter = null;
  57.  
  58. /** Array of namespaces that can be added to a collection */
  59. $wgCollectionArticleNamespaces = array(
  60. NS_MAIN,
  61. NS_TALK,
  62. NS_USER,
  63. NS_USER_TALK,
  64. NS_PROJECT,
  65. NS_PROJECT_TALK,
  66. NS_MEDIAWIKI,
  67. NS_MEDIAWIKI_TALK,
  68. 100,
  69. 101,
  70. 102,
  71. 103,
  72. 104,
  73. 105,
  74. 106,
  75. 107,
  76. 108,
  77. 109,
  78. 110,
  79. 111,
  80. );
  81.  
  82. /** Namespace for "community books" */
  83. $wgCommunityCollectionNamespace = NS_PROJECT;
  84.  
  85. /** Maximum no. of articles in a book */
  86. $wgCollectionMaxArticles = 500;
  87.  
  88. /** Name of license */
  89. $wgCollectionLicenseName = null;
  90.  
  91. /** HTTP(s) URL pointing to license in wikitext format: */
  92. $wgCollectionLicenseURL = null;
  93.  
  94. /** List of available download formats,
  95. as mapping of mwlib writer to format name */
  96. $wgCollectionFormats = array(
  97. 'rl' => 'PDF',
  98. #'zeno' => 'ZENO',
  99. #'okawix_zeno' => 'Okawix (ZENO + search engine)',
  100. );
  101.  
  102. /** For formats which rendering depends on an external server
  103. */
  104. $wgCollectionFormatToServeURL = array(
  105. 'zeno'=>'http://www.okawix.com/collections/render.php',
  106. 'okawix_zeno'=>'http://www.okawix.com/collections/render.php',
  107. );
  108.  
  109. $wgCollectionContentTypeToFilename = array(
  110. 'application/pdf' => 'collection.pdf',
  111. 'application/vnd.oasis.opendocument.text' => 'collection.odt',
  112. );
  113.  
  114. $wgCollectionPortletFormats = array( 'rl' );
  115.  
  116. $wgCollectionPortletForLoggedInUsersOnly = false;
  117.  
  118. $wgCollectionMaxSuggestions = 10;
  119.  
  120. $wgCollectionSuggestCheapWeightThreshhold = 50;
  121.  
  122. $wgCollectionSuggestThreshhold = 100;
  123.  
  124. # ==============================================================================
  125.  
  126. $wgExtensionCredits['specialpage'][] = array(
  127. 'path' => __FILE__,
  128. 'name' => 'Collection',
  129. 'version' => $wgCollectionVersion,
  130. 'author' => array( 'PediaPress GmbH', 'Siebrand Mazeland' ),
  131. 'url' => 'https://www.mediawiki.org/wiki/Extension:Collection',
  132. 'descriptionmsg' => 'coll-desc',
  133. );
  134.  
  135. # register Special:Book:
  136. $wgAutoloadClasses['SpecialCollection'] = $dir . 'Collection.body.php';
  137. $wgAutoloadClasses['CollectionSession'] = $dir . 'Collection.session.php';
  138. $wgAutoloadClasses['CollectionHooks'] = $dir . 'Collection.hooks.php';
  139. $wgAutoloadClasses['CollectionSuggest'] = $dir . 'Collection.suggest.php';
  140. $wgAutoloadClasses['CollectionPageTemplate'] = $dir . 'Collection.templates.php';
  141. $wgAutoloadClasses['CollectionListTemplate'] = $dir . 'Collection.templates.php';
  142. $wgAutoloadClasses['CollectionLoadOverwriteTemplate'] = $dir . 'Collection.templates.php';
  143. $wgAutoloadClasses['CollectionSaveOverwriteTemplate'] = $dir . 'Collection.templates.php';
  144. $wgAutoloadClasses['CollectionRenderingTemplate'] = $dir . 'Collection.templates.php';
  145. $wgAutoloadClasses['CollectionFinishedTemplate'] = $dir . 'Collection.templates.php';
  146. $wgAutoloadClasses['CollectionSuggestTemplate'] = $dir . 'Collection.templates.php';
  147. $wgExtensionMessagesFiles['CollectionCore'] = $dir . 'CollectionCore.i18n.php'; // Only contains essential messages outside the special page
  148. $wgExtensionMessagesFiles['Collection'] = $dir . 'Collection.i18n.php'; // Contains all messages used on special page
  149. $wgExtensionMessagesFiles['CollectionAlias'] = $dir . 'Collection.alias.php';
  150. $wgSpecialPages['Book'] = 'SpecialCollection';
  151. $wgSpecialPageGroups['Book'] = 'pagetools';
  152.  
  153. $wgHooks['SkinTemplateBuildNavUrlsNav_urlsAfterPermalink'][] = 'CollectionHooks::buildNavUrls';
  154. $wgHooks['SkinBuildSidebar'][] = 'CollectionHooks::buildSidebar';
  155. $wgHooks['SiteNoticeAfter'][] = 'CollectionHooks::siteNoticeAfter';
  156. $wgHooks['OutputPageCheckLastModified'][] = 'CollectionHooks::checkLastModified';
  157. $wgHooks['ResourceLoaderGetConfigVars'][] = 'CollectionHooks::resourceLoaderGetConfigVars';
  158.  
  159. $wgAvailableRights[] = 'collectionsaveasuserpage';
  160. $wgAvailableRights[] = 'collectionsaveascommunitypage';
  161.  
  162. $collResourceTemplate = array(
  163. 'localBasePath' => "$dir/js",
  164. 'remoteExtPath' => 'Collection/js'
  165. );
  166.  
  167. $wgResourceModules += array(
  168. 'ext.collection.jquery.jstorage' => $collResourceTemplate + array(
  169. 'scripts' => 'jstorage.js',
  170. 'dependencies' => 'jquery.json'
  171. ),
  172. 'ext.collection.suggest' => $collResourceTemplate + array(
  173. 'scripts' => 'suggest.js',
  174. 'dependencies' => 'ext.collection.bookcreator'
  175. ),
  176. 'ext.collection' => $collResourceTemplate + array(
  177. 'scripts' => 'collection.js',
  178. 'dependencies' => array( 'ext.collection.bookcreator', 'jquery.ui.sortable' ),
  179. ),
  180. 'ext.collection.bookcreator' => $collResourceTemplate + array(
  181. 'scripts' => 'bookcreator.js',
  182. 'styles' => 'bookcreator.css',
  183. 'dependencies' => 'ext.collection.jquery.jstorage'
  184. ),
  185. 'ext.collection.checkLoadFromLocalStorage' => $collResourceTemplate + array(
  186. 'scripts' => 'check_load_from_localstorage.js',
  187. 'styles' => 'bookcreator.css',
  188. 'dependencies' => 'ext.collection.jquery.jstorage'
  189. )
  190. );
  191.  
  192. # register global Ajax functions:
  193.  
  194. function wfAjaxGetCollection() {
  195. $json = new Services_JSON();
  196. if ( isset( $_SESSION['wsCollection'] ) ) {
  197. $collection = $_SESSION['wsCollection'];
  198. } else {
  199. $collection = array();
  200. }
  201. $r = new AjaxResponse( $json->encode( array( 'collection' => $collection ) ) );
  202. $r->setContentType( 'application/json' );
  203. return $r;
  204. }
  205.  
  206. $wgAjaxExportList[] = 'wfAjaxGetCollection';
  207.  
  208. function wfAjaxPostCollection( $collection = '', $redirect = '' ) {
  209. $json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE );
  210. if ( session_id() == '' ) {
  211. wfSetupSession();
  212. }
  213. $collection = $json->decode( $collection );
  214. $collection['enabled'] = true;
  215. $_SESSION['wsCollection'] = $collection;
  216. $r = new AjaxResponse();
  217. if ( $redirect ) {
  218. $title = Title::newFromText( $redirect );
  219. $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  220. $r->setResponseCode( 302 );
  221. header( 'Location: ' . $redirecturl );
  222. } else {
  223. $title = SpecialPage::getTitleFor( 'Book' );
  224. $redirecturl = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
  225. $r->setContentType( 'application/json' );
  226. $r->addText( $json->encode( array( 'redirect_url' => $redirecturl ) ) );
  227. }
  228. return $r;
  229. }
  230.  
  231. $wgAjaxExportList[] = 'wfAjaxPostCollection';
  232.  
  233. function wfAjaxGetMWServeStatus( $collection_id = '', $writer = 'rl' ) {
  234. $json = new Services_JSON();
  235. $result = SpecialCollection::mwServeCommand( 'render_status', array(
  236. 'collection_id' => $collection_id,
  237. 'writer' => $writer
  238. ) );
  239. if ( isset( $result['status']['progress'] ) ) {
  240. $result['status']['progress'] = number_format( $result['status']['progress'], 2, '.', '' );
  241. }
  242. $r = new AjaxResponse( $json->encode( $result ) );
  243. $r->setContentType( 'application/json' );
  244. return $r;
  245. }
  246.  
  247. $wgAjaxExportList[] = 'wfAjaxGetMWServeStatus';
  248.  
  249. function wfAjaxCollectionAddArticle( $namespace = 0, $title = '', $oldid = '' ) {
  250. SpecialCollection::addArticleFromName( $namespace, $title, $oldid );
  251. return wfAjaxCollectionGetItemList();
  252. }
  253.  
  254. $wgAjaxExportList[] = 'wfAjaxCollectionAddArticle';
  255.  
  256. function wfAjaxCollectionRemoveArticle( $namespace = 0, $title = '', $oldid = '' ) {
  257. SpecialCollection::removeArticleFromName( $namespace, $title, $oldid );
  258. return wfAjaxCollectionGetItemList();
  259. }
  260.  
  261. $wgAjaxExportList[] = 'wfAjaxCollectionRemoveArticle';
  262.  
  263. function wfAjaxCollectionAddCategory( $title = '' ) {
  264. SpecialCollection::addCategoryFromName( $title );
  265. return wfAjaxCollectionGetItemList();
  266. }
  267.  
  268. $wgAjaxExportList[] = 'wfAjaxCollectionAddCategory';
  269.  
  270. function wfAjaxCollectionGetBookCreatorBoxContent( $ajaxHint = '', $oldid = null, $pageName = null ) {
  271. if ( !is_null( $oldid ) ) {
  272. $oldid = intval( $oldid );
  273. }
  274.  
  275. $title = null;
  276. if ( !is_null( $pageName ) ) {
  277. $title = Title::newFromText( $pageName );
  278. }
  279. if ( is_null( $title ) ) {
  280. $title = Title::newMainPage();
  281. }
  282.  
  283. $html = CollectionHooks::getBookCreatorBoxContent( $title, $ajaxHint, $oldid );
  284.  
  285. $json = new Services_JSON();
  286. $result = array();
  287. $result['html'] = $html;
  288. $r = new AjaxResponse( $json->encode( $result ) );
  289. $r->setContentType( 'application/json' );
  290. return $r;
  291. }
  292.  
  293. $wgAjaxExportList[] = 'wfAjaxCollectionGetBookCreatorBoxContent';
  294.  
  295. function wfAjaxCollectionGetItemList() {
  296. $collection = $_SESSION['wsCollection'];
  297.  
  298. $template = new CollectionListTemplate();
  299. $template->set( 'collection', $collection );
  300. $template->set( 'is_ajax', true );
  301. ob_start();
  302. $template->execute();
  303. $html = ob_get_contents();
  304. ob_end_clean();
  305.  
  306. $json = new Services_JSON();
  307. $result = array();
  308. $result['html'] = $html;
  309. $result['collection'] = $collection;
  310. $r = new AjaxResponse( $json->encode( $result ) );
  311. $r->setContentType( 'application/json' );
  312. return $r;
  313. }
  314.  
  315. $wgAjaxExportList[] = 'wfAjaxCollectionGetItemList';
  316.  
  317. function wfAjaxCollectionRemoveItem( $index ) {
  318. SpecialCollection::removeItem( (int)$index );
  319. return wfAjaxCollectionGetItemList();
  320. }
  321.  
  322. $wgAjaxExportList[] = 'wfAjaxCollectionRemoveItem';
  323.  
  324. function wfAjaxCollectionAddChapter( $name ) {
  325. SpecialCollection::addChapter( $name );
  326. return wfAjaxCollectionGetItemList();
  327. }
  328.  
  329. $wgAjaxExportList[] = 'wfAjaxCollectionAddChapter';
  330.  
  331. function wfAjaxCollectionRenameChapter( $index, $name ) {
  332. SpecialCollection::renameChapter( (int)$index, $name );
  333. return wfAjaxCollectionGetItemList();
  334. }
  335.  
  336. $wgAjaxExportList[] = 'wfAjaxCollectionRenameChapter';
  337.  
  338. function wfAjaxCollectionSetTitles( $title, $subtitle ) {
  339. SpecialCollection::setTitles( $title, $subtitle );
  340. return wfAjaxCollectionGetItemList();
  341. }
  342.  
  343. $wgAjaxExportList[] = 'wfAjaxCollectionSetTitles';
  344.  
  345. function wfAjaxCollectionSetSorting( $items_string ) {
  346. $parsed = array();
  347. parse_str( $items_string, $parsed );
  348. $items = array();
  349. foreach ( $parsed['item'] as $s ) {
  350. if ( is_numeric( $s ) ) {
  351. $items[] = intval( $s );
  352. }
  353. }
  354. SpecialCollection::setSorting( $items );
  355. return wfAjaxCollectionGetItemList();
  356. }
  357.  
  358. $wgAjaxExportList[] = 'wfAjaxCollectionSetSorting';
  359.  
  360. function wfAjaxCollectionClear() {
  361. CollectionSession::clearCollection();
  362. CollectionSuggest::clear();
  363. return wfAjaxCollectionGetItemList();
  364. }
  365.  
  366. $wgAjaxExportList[] = 'wfAjaxCollectionClear';
  367.  
  368. function wfAjaxCollectionGetPopupData( $title ) {
  369. global $wgScriptPath;
  370.  
  371. $json = new Services_JSON();
  372. $result = array();
  373. $imagePath = "$wgScriptPath/extensions/Collection/images";
  374. $t = Title::newFromText( $title );
  375. if ( $t && $t->isRedirect() ) {
  376. $a = new Article( $t, 0 );
  377. $t = $a->followRedirect();
  378. if ( $t instanceof Title ) {
  379. $title = $t->getPrefixedText();
  380. }
  381. }
  382. if ( CollectionSession::findArticle( $title ) == - 1 ) {
  383. $result['action'] = 'add';
  384. $result['text'] = wfMsg( 'coll-add_linked_article' );
  385. $result['img'] = "$imagePath/silk-add.png";
  386. } else {
  387. $result['action'] = 'remove';
  388. $result['text'] = wfMsg( 'coll-remove_linked_article' );
  389. $result['img'] = "$imagePath/silk-remove.png";
  390. }
  391. $result['title'] = $title;
  392. $r = new AjaxResponse( $json->encode( $result ) );
  393. $r->setContentType( 'application/json' );
  394. return $r;
  395. }
  396.  
  397. $wgAjaxExportList[] = 'wfAjaxCollectionGetPopupData';
  398.  
  399. /**
  400. * Backend of several following SAJAX function handlers...
  401. * @param String $action provided by the specific handlers internally
  402. * @param String $article title passed in from client
  403. * @return AjaxResponse with JSON-encoded array including HTML fragment.
  404. */
  405. function wfCollectionSuggestAction( $action, $article ) {
  406. $json = new Services_JSON();
  407. $result = CollectionSuggest::refresh( $action, $article );
  408. $undoLink = Xml::element( 'a',
  409. array(
  410. 'href' => SkinTemplate::makeSpecialUrl(
  411. 'Book',
  412. array( 'bookcmd' => 'suggest', 'undo' => $action, 'arttitle' => $article )
  413. ),
  414. 'onclick' => "collectionSuggestCall('UndoArticle'," .
  415. Xml::encodeJsVar( array( $action, $article ) ) . "); return false;",
  416. 'title' => wfMsg( 'coll-suggest_undo_tooltip' ),
  417. ),
  418. wfMsg( 'coll-suggest_undo' )
  419. );
  420. $result['last_action'] = wfMsg(
  421. "coll-suggest_article_$action",
  422. htmlspecialchars( $article ),
  423. $undoLink
  424. );
  425. $result['collection'] = CollectionSession::getCollection();
  426. $r = new AjaxResponse( $json->encode( $result ) );
  427. $r->setContentType( 'application/json' );
  428. return $r;
  429. }
  430.  
  431. function wfAjaxCollectionSuggestBanArticle( $article ) {
  432. return wfCollectionSuggestAction( 'ban', $article );
  433. }
  434.  
  435. $wgAjaxExportList[] = 'wfAjaxCollectionSuggestBanArticle';
  436.  
  437. function wfAjaxCollectionSuggestAddArticle( $article ) {
  438. return wfCollectionSuggestAction( 'add', $article );
  439. }
  440.  
  441. $wgAjaxExportList[] = 'wfAjaxCollectionSuggestAddArticle';
  442.  
  443. function wfAjaxCollectionSuggestRemoveArticle( $article ) {
  444. return wfCollectionSuggestAction( 'remove', $article );
  445. }
  446.  
  447. $wgAjaxExportList[] = 'wfAjaxCollectionSuggestRemoveArticle';
  448.  
  449. function wfAjaxCollectionSuggestUndoArticle( $lastAction, $article ) {
  450. $json = new Services_JSON();
  451. $result = CollectionSuggest::undo( $lastAction, $article );
  452. $r = new AjaxResponse( $json->encode( $result ) );
  453. $r->setContentType( 'application/json' );
  454. return $r;
  455. }
  456.  
  457. $wgAjaxExportList[] = 'wfAjaxCollectionSuggestUndoArticle';
Advertisement
Add Comment
Please, Sign In to add comment