public List < Integer > getRevisionsWithFirstTemplateAppearance ( String templateName ) throws WikiApiException { /* * Note: This method does not use any revision-template-index. Each revision has to be parsed until the first revision is found that does not contain a certain template. * TODO also create version using revision-template index */ System . err . println ( "Note: This function call demands parsing several revision for each page. A method using the revision-template index is currently under construction." ) ; templateName = templateName . trim ( ) . replaceAll ( " " , "_" ) ; List < Integer > revisionIds = new LinkedList < Integer > ( ) ; List < Integer > pageIds = getPageIdsContainingTemplateNames ( Arrays . asList ( new String [ ] { templateName } ) ) ; if ( pageIds . size ( ) == 0 ) { return revisionIds ; } if ( revApi == null ) { revApi = new RevisionApi ( wiki . getDatabaseConfiguration ( ) ) ; } if ( parser == null ) { //TODO switch to SWEBLE MediaWikiParserFactory pf = new MediaWikiParserFactory ( wiki . getDatabaseConfiguration ( ) . getLanguage ( ) ) ; pf . setTemplateParserClass ( ShowTemplateNamesAndParameters . class ) ; parser = pf . createParser ( ) ; } for ( int id : pageIds ) { //get timestamps of all revisions List < Timestamp > tsList = revApi . getRevisionTimestamps ( id ) ; // sort in reverse order - newest first Collections . sort ( tsList , new Comparator < Timestamp > ( ) { public int compare ( Timestamp ts1 , Timestamp ts2 ) { return ts2 . compareTo ( ts1 ) ; } } ) ; Revision prevRev = null ; tsloop : for ( Timestamp ts : tsList ) { Revision rev = revApi . getRevision ( id , ts ) ; //initialize previous revision if ( prevRev == null ) { prevRev = rev ; } //Parse templates and check if the revision contains the template ParsedPage pp = parser . parse ( rev . getRevisionText ( ) ) ; boolean containsTpl = false ; tplLoop : for ( Template tpl : pp . getTemplates ( ) ) { if ( tpl . getName ( ) . equalsIgnoreCase ( templateName ) ) { containsTpl = true ; break tplLoop ; } } //if the revision does not contain the template, we have found //what we were looking for. add id of previous revision if ( ! containsTpl ) { revisionIds . add ( prevRev . getRevisionID ( ) ) ; break tsloop ; } prevRev = rev ; } } return revisionIds ; }
This method first creates a list of pages containing templates that equal any of the provided Strings . It then returns a list of revision ids of the revisions in which the respective templates first appeared .